From 3fc8ac5d8dc4f454b8aa13e7c3c246b9be8875e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=8C=80=EC=97=B0?= Date: Tue, 5 Jul 2022 14:23:25 +0900 Subject: [PATCH] fix: escape backslash in the middle of sentence (#2588) * test: add test for escape baskslash * fix: escape backslash in the middle of sentence --- .../editor/src/__test__/unit/convertor.spec.ts | 11 +++++++++++ apps/editor/src/utils/common.ts | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/apps/editor/src/__test__/unit/convertor.spec.ts b/apps/editor/src/__test__/unit/convertor.spec.ts index bef09ec536..6b98f6e0a7 100644 --- a/apps/editor/src/__test__/unit/convertor.spec.ts +++ b/apps/editor/src/__test__/unit/convertor.spec.ts @@ -1098,4 +1098,15 @@ describe('Convertor', () => { expect(result).toBe(`"test"a`); }); + + it('should escape the backslash, which is a plain chracter in the middle of a sentence', () => { + const markdown = source` + backslash \\in the middle of a sentence + `; + const expected = source` + backslash \\\\in the middle of a sentence + `; + + assertConverting(markdown, expected); + }); }); diff --git a/apps/editor/src/utils/common.ts b/apps/editor/src/utils/common.ts index cd38e93083..3754219237 100644 --- a/apps/editor/src/utils/common.ts +++ b/apps/editor/src/utils/common.ts @@ -12,7 +12,8 @@ const reEscapeHTML = /<([a-zA-Z_][a-zA-Z0-9\-._]*)(\s|[^\\>])*\/?>|<(\/)([a-zA-Z const reEscapeBackSlash = /\\[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~\\]/g; const reEscapePairedChars = /[*_~`]/g; const reMdImageSyntax = /!\[.*\]\(.*\)/g; -const reEscapedCharInLinkSyntax = /[[\]]/g; // +const reEscapedCharInLinkSyntax = /[[\]]/g; +const reEscapeBackSlashInSentence = /(?:^|[^\\])\\(?!\\)/g; const XMLSPECIAL = '[&<>"]'; const reXmlSpecial = new RegExp(XMLSPECIAL, 'g'); @@ -118,22 +119,27 @@ export function escapeTextForLink(text: string) { } export function escape(text: string) { - const replacer = (matched: string) => `\\${matched}`; + const aheadReplacer = (matched: string) => `\\${matched}`; + const behindReplacer = (matched: string) => `${matched}\\`; let escapedText = text.replace(reSpaceMoreThanOne, ' '); if (reEscapeBackSlash.test(escapedText)) { - escapedText = escapedText.replace(reEscapeBackSlash, replacer); + escapedText = escapedText.replace(reEscapeBackSlash, aheadReplacer); } - escapedText = escapedText.replace(reEscapePairedChars, replacer); + if (reEscapeBackSlashInSentence.test(escapedText)) { + escapedText = escapedText.replace(reEscapeBackSlashInSentence, behindReplacer); + } + + escapedText = escapedText.replace(reEscapePairedChars, aheadReplacer); if (reEscapeHTML.test(escapedText)) { - escapedText = escapedText.replace(reEscapeHTML, replacer); + escapedText = escapedText.replace(reEscapeHTML, aheadReplacer); } if (isNeedEscapeText(escapedText)) { - escapedText = escapedText.replace(reEscapeChars, replacer); + escapedText = escapedText.replace(reEscapeChars, aheadReplacer); } return escapedText;