From 6c7ca24f08aa771d0ee8e94aff9d33e803403070 Mon Sep 17 00:00:00 2001 From: Tsaqif Date: Tue, 20 Aug 2024 16:44:57 +0700 Subject: [PATCH] Fix inline code regex mismatch when there is an escaped backtick character Signed-off-by: Tsaqif --- __tests__/ExpensiMark-HTML-test.js | 18 ++++++++++++++++++ lib/ExpensiMark.ts | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 072359d5..5bd216c5 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -1219,6 +1219,24 @@ test('Test for backticks with suffix', () => { expect(parser.replace(testString)).toBe(resultString); }); +test('Test for backticks with an escaped backtick character inside it', () => { + const testString = 'a `;` abc `:` a'; + const resultString = 'a ; abc : a'; + expect(parser.replace(testString)).toBe(resultString); +}); + +test('Test for backticks with an escaped backtick character inside it without prefix and suffix', () => { + const testString = '`#` abc `:`'; + const resultString = '# abc :'; + expect(parser.replace(testString)).toBe(resultString); +}); + +test('Test for backticks with complete escaped backtick characters inside it', () => { + const testString = 'a `&` b `#` c `x` d `6` e `0` f `;` g `test` h'; + const resultString = 'a & b # c x d 6 e 0 f ; g test h'; + expect(parser.replace(testString)).toBe(resultString); +}); + // Backticks with no content are not replaced with test('Test for backticks with no content', () => { const testString = '` `'; diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index 4310f88e..4f12cb2b 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -190,8 +190,8 @@ export default class ExpensiMark { // Use the url escaped version of a backtick (`) symbol. Mobile platforms do not support lookbehinds, // so capture the first and third group and place them in the replacement. // but we should not replace backtick symbols if they include
 tags between them.
-                regex: /(\B|_|)`(.*?(?![`])\S.*?)`(\B|_|)(?!`|[^<]*<\/pre>|[^<]*<\/video>)/gm,
-                replacement: '$1$2$3',
+                regex: /(\B|_|)`((?:`)*)(?!`)(.*?\S+?.*?)(?|[^<]*<\/video>)/gm,
+                replacement: '$1$2$3$4$6',
             },
 
             /**