From cb45b5be1f0a6441a901cafe4385a30a4d5c9f54 Mon Sep 17 00:00:00 2001 From: Jingtai Piao Date: Fri, 12 Jul 2024 19:45:09 +1000 Subject: [PATCH 1/4] Fix bold when underscore ahead of asterisk --- __tests__/ExpensiMark-HTML-test.js | 6 ++++++ lib/ExpensiMark.ts | 10 ++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 0051dd4c..6c7f8e1b 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -26,6 +26,12 @@ test('Test bold within code blocks is skipped', () => { expect(parser.replace(testString)).toBe(replacedString); }); +test('Test _ ahead asterisk still result in bold', () => { + const testString = 'bold\n```\n*not a bold*\n```\nThis is _*bold*'; + const replacedString = 'bold
*not a bold*
This is _bold'; + expect(parser.replace(testString)).toBe(replacedString); +}); + test('Test heading markdown replacement', () => { let testString = '# Heading should not have new line after it.\n'; expect(parser.replace(testString)).toBe('

Heading should not have new line after it.

'); diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index fc2cf408..554265b2 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -461,8 +461,14 @@ export default class ExpensiMark { // \B will match everything that \b doesn't, so it works // for * and ~: https://www.rexegg.com/regex-boundaries.html#notb name: 'bold', - regex: /(?]*)\B\*(?![^<]*(?:<\/pre>|<\/code>|<\/a>))((?![\s*])[\s\S]*?[^\s*](?)(?![^<]*(<\/pre>|<\/code>|<\/a>))/g, - replacement: (_extras, match, g1) => (g1.includes('') || this.containsNonPairTag(g1) ? match : `${g1}`), + regex: /(?]*)(\b_|\B)\*(?![^<]*(?:<\/pre>|<\/code>|<\/a>))((?![\s*])[\s\S]*?[^\s*](?)(?![^<]*(<\/pre>|<\/code>|<\/a>))/g, + replacement: (_extras, match, g1, g2) => { + if (g1.includes('_')) { + return `${g1}${g2}`; + } + + return g2.includes('') || this.containsNonPairTag(g2) ? match : `${g2}`; + }, }, { name: 'strikethrough', From 101c46838016b0fd11a9a397c3b19349fad6924c Mon Sep 17 00:00:00 2001 From: Jingtai Piao Date: Fri, 12 Jul 2024 19:55:35 +1000 Subject: [PATCH 2/4] Revert "Fix bold when underscore ahead of asterisk" This reverts commit cb45b5be1f0a6441a901cafe4385a30a4d5c9f54. --- __tests__/ExpensiMark-HTML-test.js | 6 ------ lib/ExpensiMark.ts | 10 ++-------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 6c7f8e1b..0051dd4c 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -26,12 +26,6 @@ test('Test bold within code blocks is skipped', () => { expect(parser.replace(testString)).toBe(replacedString); }); -test('Test _ ahead asterisk still result in bold', () => { - const testString = 'bold\n```\n*not a bold*\n```\nThis is _*bold*'; - const replacedString = 'bold
*not a bold*
This is _bold'; - expect(parser.replace(testString)).toBe(replacedString); -}); - test('Test heading markdown replacement', () => { let testString = '# Heading should not have new line after it.\n'; expect(parser.replace(testString)).toBe('

Heading should not have new line after it.

'); diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index 554265b2..fc2cf408 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -461,14 +461,8 @@ export default class ExpensiMark { // \B will match everything that \b doesn't, so it works // for * and ~: https://www.rexegg.com/regex-boundaries.html#notb name: 'bold', - regex: /(?]*)(\b_|\B)\*(?![^<]*(?:<\/pre>|<\/code>|<\/a>))((?![\s*])[\s\S]*?[^\s*](?)(?![^<]*(<\/pre>|<\/code>|<\/a>))/g, - replacement: (_extras, match, g1, g2) => { - if (g1.includes('_')) { - return `${g1}${g2}`; - } - - return g2.includes('') || this.containsNonPairTag(g2) ? match : `${g2}`; - }, + regex: /(?]*)\B\*(?![^<]*(?:<\/pre>|<\/code>|<\/a>))((?![\s*])[\s\S]*?[^\s*](?)(?![^<]*(<\/pre>|<\/code>|<\/a>))/g, + replacement: (_extras, match, g1) => (g1.includes('') || this.containsNonPairTag(g1) ? match : `${g1}`), }, { name: 'strikethrough', From 068e00cbe2511ddfcdb8fe1859ae065480b17765 Mon Sep 17 00:00:00 2001 From: Jingtai Piao Date: Fri, 12 Jul 2024 19:56:43 +1000 Subject: [PATCH 3/4] Revert "Fix bold when underscore ahead of asterisk" This reverts commit cb45b5be1f0a6441a901cafe4385a30a4d5c9f54. --- __tests__/ExpensiMark-HTML-test.js | 6 ++++++ lib/ExpensiMark.ts | 10 ++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 0051dd4c..6c7f8e1b 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -26,6 +26,12 @@ test('Test bold within code blocks is skipped', () => { expect(parser.replace(testString)).toBe(replacedString); }); +test('Test _ ahead asterisk still result in bold', () => { + const testString = 'bold\n```\n*not a bold*\n```\nThis is _*bold*'; + const replacedString = 'bold
*not a bold*
This is _bold'; + expect(parser.replace(testString)).toBe(replacedString); +}); + test('Test heading markdown replacement', () => { let testString = '# Heading should not have new line after it.\n'; expect(parser.replace(testString)).toBe('

Heading should not have new line after it.

'); diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index fc2cf408..554265b2 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -461,8 +461,14 @@ export default class ExpensiMark { // \B will match everything that \b doesn't, so it works // for * and ~: https://www.rexegg.com/regex-boundaries.html#notb name: 'bold', - regex: /(?]*)\B\*(?![^<]*(?:<\/pre>|<\/code>|<\/a>))((?![\s*])[\s\S]*?[^\s*](?)(?![^<]*(<\/pre>|<\/code>|<\/a>))/g, - replacement: (_extras, match, g1) => (g1.includes('') || this.containsNonPairTag(g1) ? match : `${g1}`), + regex: /(?]*)(\b_|\B)\*(?![^<]*(?:<\/pre>|<\/code>|<\/a>))((?![\s*])[\s\S]*?[^\s*](?)(?![^<]*(<\/pre>|<\/code>|<\/a>))/g, + replacement: (_extras, match, g1, g2) => { + if (g1.includes('_')) { + return `${g1}${g2}`; + } + + return g2.includes('') || this.containsNonPairTag(g2) ? match : `${g2}`; + }, }, { name: 'strikethrough', From 73b6cd46c1b1d1cc6c5ac48be64a73087a51709c Mon Sep 17 00:00:00 2001 From: Jingtai Piao Date: Fri, 12 Jul 2024 21:20:30 +1000 Subject: [PATCH 4/4] Add more test cases --- __tests__/ExpensiMark-HTML-test.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 6c7f8e1b..df53ba14 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -26,9 +26,21 @@ test('Test bold within code blocks is skipped', () => { expect(parser.replace(testString)).toBe(replacedString); }); +test('Test special character plus _ ahead asterisk still result in bold', () => { + const testString = 'This is a !_*bold*'; + const replacedString = 'This is a !_bold'; + expect(parser.replace(testString)).toBe(replacedString); +}); + +test('Test a word plus _ ahead asterisk not result in bold', () => { + const testString = 'This is not a_*bold*'; + const replacedString = 'This is not a_*bold*'; + expect(parser.replace(testString)).toBe(replacedString); +}); + test('Test _ ahead asterisk still result in bold', () => { - const testString = 'bold\n```\n*not a bold*\n```\nThis is _*bold*'; - const replacedString = 'bold
*not a bold*
This is _bold'; + const testString = 'This is a ~_*bold*'; + const replacedString = 'This is a ~_bold'; expect(parser.replace(testString)).toBe(replacedString); });