From d9d72791a309a8ef68560f4472ac19717eadacf7 Mon Sep 17 00:00:00 2001 From: u4aew Date: Tue, 5 Nov 2024 14:33:44 +0300 Subject: [PATCH] fix(core): added escaping the underscore character via serializing (#452) --- src/core/markdown/Markdown.test.ts | 11 +++++++++++ src/core/markdown/MarkdownSerializer.js | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/core/markdown/Markdown.test.ts b/src/core/markdown/Markdown.test.ts index adc935be..42a084ec 100644 --- a/src/core/markdown/Markdown.test.ts +++ b/src/core/markdown/Markdown.test.ts @@ -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.', + ), + ), + ); + }); }); diff --git a/src/core/markdown/MarkdownSerializer.js b/src/core/markdown/MarkdownSerializer.js index 3af4f865..c4762b61 100644 --- a/src/core/markdown/MarkdownSerializer.js +++ b/src/core/markdown/MarkdownSerializer.js @@ -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, '\\$&');