From 068e00cbe2511ddfcdb8fe1859ae065480b17765 Mon Sep 17 00:00:00 2001 From: Jingtai Piao Date: Fri, 12 Jul 2024 19:56:43 +1000 Subject: [PATCH] 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',