Skip to content

Commit

Permalink
fix: escape whitespaces within superscript and subscript (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v authored Jun 15, 2023
1 parent 3415a3f commit db35e5b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/core/markdown/MarkdownSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export class MarkdownSerializerState {
this.noAutoBlank = false;
/** @type {Boolean|undefined} */
this.isAutolink = undefined;
/** @type {Boolean} */
this.escapeWhitespace = false;
// :: Object
// The options passed to the serializer.
// tightLists:: ?bool
Expand Down Expand Up @@ -162,7 +164,10 @@ export class MarkdownSerializerState {
for (let i = 0; i < lines.length; i++) {
const startOfLine = this.atBlank() || this.closed;
this.write();
this.out += escape !== false ? this.esc(lines[i], startOfLine) : lines[i];
let text = lines[i];
if (escape !== false) text = this.esc(text, startOfLine)
if (this.escapeWhitespace) text = this.escWhitespace(text);
this.out += text
if (i != lines.length - 1) this.out += '\n';
}
}
Expand Down Expand Up @@ -297,6 +302,10 @@ export class MarkdownSerializerState {
return str;
}

escWhitespace(str) {
return str.replace(/ /g, '\\ ');
}

quote(str) {
const wrap = str.indexOf('"') == -1 ? '""' : str.indexOf("'") == -1 ? "''" : '()';
return wrap[0] + str + wrap[1];
Expand Down
7 changes: 7 additions & 0 deletions src/extensions/markdown/Subscript/Subscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ describe('Subscript extension', () => {
it('should parse html - sub tag', () => {
parseDOM(schema, '<p><sub>subscript</sub></p>', doc(p(s('subscript'))));
});

it('should escape whitespaces', () => {
same(
'Ok, hello~w\\ o\\ r\\ l\\ d~! This world is beautiful!',
doc(p('Ok, hello', s('w o r l d'), '! This world is beautiful!')),
);
});
});
13 changes: 12 additions & 1 deletion src/extensions/markdown/Subscript/SubscriptSpecs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ export const SubscriptSpecs: ExtensionAuto = (builder) => {
return ['sub'];
},
},
toYfm: {open: '~', close: '~', mixable: false, expelEnclosingWhitespace: true},
toYfm: {
open: (state) => {
state.escapeWhitespace = true;
return '~';
},
close: (state) => {
state.escapeWhitespace = false;
return '~';
},
mixable: false,
expelEnclosingWhitespace: true,
},
fromYfm: {tokenSpec: {name: subscriptMarkName, type: 'mark'}},
}));
};
7 changes: 7 additions & 0 deletions src/extensions/markdown/Superscript/Superscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ describe('Superscript extension', () => {
it('should parse html - sup tag', () => {
parseDOM(schema, '<p><sup>superscript</sup></p>', doc(p(s('superscript'))));
});

it('should escape whitespaces', () => {
same(
'Ok, hello^w\\ o\\ r\\ l\\ d^! This world is beautiful!',
doc(p('Ok, hello', s('w o r l d'), '! This world is beautiful!')),
);
});
});
13 changes: 12 additions & 1 deletion src/extensions/markdown/Superscript/SuperscriptSpecs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ export const SuperscriptSpecs: ExtensionAuto = (builder) => {
return ['sup'];
},
},
toYfm: {open: '^', close: '^', mixable: true, expelEnclosingWhitespace: true},
toYfm: {
open: (state) => {
state.escapeWhitespace = true;
return '^';
},
close: (state) => {
state.escapeWhitespace = false;
return '^';
},
mixable: true,
expelEnclosingWhitespace: true,
},
fromYfm: {tokenSpec: {name: superscriptMarkName, type: 'mark'}},
}));
};

0 comments on commit db35e5b

Please sign in to comment.