Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape backslash in the middle of sentence #2588

Merged
merged 2 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/editor/src/__test__/unit/convertor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1098,4 +1098,15 @@ describe('Convertor', () => {

expect(result).toBe(`<strong>"test"</strong>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);
});
});
18 changes: 12 additions & 6 deletions apps/editor/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down