From 3a73b0312f410630e51fc192e5d101ce31a0e81b Mon Sep 17 00:00:00 2001 From: QichenZhu <57348009+QichenZhu@users.noreply.github.com> Date: Mon, 30 Dec 2024 21:46:45 +1300 Subject: [PATCH] Fix double backticks --- __tests__/ExpensiMark-HTML-test.js | 38 +++++++++++++++++------------- lib/ExpensiMark.ts | 5 +++- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 24931b83..3eef3952 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -472,14 +472,14 @@ test('Test inline code blocks', () => { expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is JavaScript. How about you?'); }); -test('Test inline code blocks with double backticks', () => { - const inlineCodeStartString = 'My favorite language is ``JavaScript``. How about you?'; - expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is `JavaScript`. How about you?'); +test('Test double backticks', () => { + const testString = 'My favorite language is ``JavaScript``. How about you?'; + expect(parser.replace(testString)).toBe('My favorite language is ``JavaScript``. How about you?'); }); test('Test inline code blocks with triple backticks', () => { const inlineCodeStartString = 'My favorite language is ```JavaScript```. How about you?'; - expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is ``JavaScript``. How about you?'); + expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is ``JavaScript``. How about you?'); }); test('Test multiple inline codes in one line', () => { @@ -487,14 +487,14 @@ test('Test multiple inline codes in one line', () => { expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is JavaScript. How about you? I also like Python.'); }); -test('Test inline code with one backtick as content', () => { - const inlineCodeStartString = '```'; - expect(parser.replace(inlineCodeStartString)).toBe('```'); +test('Test triple backticks', () => { + const testString = '```'; + expect(parser.replace(testString)).toBe('```'); }); -test('Test inline code with multiple backtick symbols as content', () => { - const inlineCodeStartString = '``````'; - expect(parser.replace(inlineCodeStartString)).toBe('``````'); +test('Test multiple backtick symbols', () => { + const testString = '``````'; + expect(parser.replace(testString)).toBe('``````'); }); test('Test inline code blocks with ExpensiMark syntax inside', () => { @@ -508,9 +508,9 @@ test('Test inline code blocks inside ExpensiMark', () => { expect(parser.replace(testString)).toBe(resultString); }); -test('Test inline code blocks with two backticks', () => { +test('Test words enclosed in double backticks', () => { const testString = '``JavaScript``'; - expect(parser.replace(testString)).toBe('`JavaScript`'); + expect(parser.replace(testString)).toBe('``JavaScript``'); }); test('Test code fencing with ExpensiMark syntax inside', () => { @@ -1240,10 +1240,9 @@ test('Test for backticks with complete escaped backtick characters inside it', ( expect(parser.replace(testString)).toBe(resultString); }); -// Backticks with only tab characters inside it are not replaced with -test('Test for backticks only tab characters inside it', () => { +test('Test for inline code blocks with only tab characters as content', () => { const testString = '`\u0009`'; - const resultString = '`\u0009`'; + const resultString = ' '; expect(parser.replace(testString)).toBe(resultString); }); @@ -1254,10 +1253,15 @@ test('Test for backticks with only space characters as content', () => { expect(parser.replace(testString)).toBe(resultString); }); -// Code-fence with spaces as content test('Test for inline code block with triple backtick with spaces as content', () => { const testString = '``` ```'; - const resultString = '`` ``'; + const resultString = '``   ``'; + expect(parser.replace(testString)).toBe(resultString); +}); + +test('Test for a normal code block, an empty code block, followed by a word and a backtick', () => { + const testString = '`hello` `` hi`'; + const resultString = 'hello `` hi`'; expect(parser.replace(testString)).toBe(resultString); }); diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index e81d39a0..a2b696f8 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -227,9 +227,12 @@ export default class ExpensiMark { // but we should not replace backtick symbols if they include
 tags between them.
                 // At least one non-whitespace character or a specific whitespace character (" " and "\u00A0")
                 // must be present inside the backticks.
-                regex: /(\B|_|)`((?:`)*(?!`).*?[\S| |\u00A0]+?.*?(?)[^<]*(?:<(?!pre>)[^<]*)*<\/pre>|[^<]*<\/video>)/gm,
+                regex: /(\B|_|)`(.*?)`(\B|_|)(?!(?!
)[^<]*(?:<(?!pre>)[^<]*)*<\/pre>|[^<]*<\/video>)/gm,
                 replacement: (_extras, _match, g1, g2, g3) => {
                     const g2Value = g2.trim() === '' ? g2.replaceAll(' ', ' ') : g2;
+                    if (!g2Value) {
+                        return _match;
+                    }
                     return `${g1}${g2Value}${g3}`;
                 },
             },