Skip to content

Commit

Permalink
fix(core): added escaping the underscore character via serializing (#452
Browse files Browse the repository at this point in the history
)
  • Loading branch information
u4aew authored Nov 5, 2024
1 parent 6f752e1 commit d9d7279
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/core/markdown/Markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,15 @@ describe('markdown', () => {
same('* list item\n\n```\ncode\n```', doc(ul(li(p('list item'))), pre('code\n'))));

it("doesn't escape characters in code", () => same('foo`*`', doc(p('foo', code('*')))));

it('escapes special characters in a text', () => {
same(
'Markdown special characters: \\_underscore\\_, \\*asterisk\\*, \\`backtick\\`, \\$dollar\\$, \\{curly\\} brace, \\[square\\] bracket, and a \\|vertical\\| bar.',
doc(
p(
'Markdown special characters: _underscore_, *asterisk*, `backtick`, $dollar$, {curly} brace, [square] bracket, and a |vertical| bar.',
),
),
);
});
});
2 changes: 1 addition & 1 deletion src/core/markdown/MarkdownSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export class MarkdownSerializerState {
// content. If `startOfLine` is true, also escape characters that
// have special meaning only at the start of the line.
esc(str, startOfLine) {
const escRegexp = this.options?.commonEscape || /[`\^+*\\\|~\[\]\{\}<>\$]/g;
const escRegexp = this.options?.commonEscape || /[`\^+*\\\|~\[\]\{\}<>\$_]/g;
const startOfLineEscRegexp = this.options?.startOfLineEscape || /^[:#\-*+>]/;

str = str.replace(escRegexp, '\\$&');
Expand Down

0 comments on commit d9d7279

Please sign in to comment.