Skip to content

Commit

Permalink
Merge pull request #751 from jp928/issues/44032
Browse files Browse the repository at this point in the history
Fix underscore ahead of asterisk not get bolded
  • Loading branch information
johnmlee101 authored Jul 15, 2024
2 parents 001cce7 + 73b6cd4 commit 7149928
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 18 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ 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 !_<strong>bold</strong>';
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 = 'This is a ~_*bold*';
const replacedString = 'This is a ~_<strong>bold</strong>';
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('<h1>Heading should not have new line after it.</h1>');
Expand Down
10 changes: 8 additions & 2 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,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*](?<!\s))\*\B(?![^<]*>)(?![^<]*(<\/pre>|<\/code>|<\/a>))/g,
replacement: (_extras, match, g1) => (g1.includes('</pre>') || this.containsNonPairTag(g1) ? match : `<strong>${g1}</strong>`),
regex: /(?<!<[^>]*)(\b_|\B)\*(?![^<]*(?:<\/pre>|<\/code>|<\/a>))((?![\s*])[\s\S]*?[^\s*](?<!\s))\*\B(?![^<]*>)(?![^<]*(<\/pre>|<\/code>|<\/a>))/g,
replacement: (_extras, match, g1, g2) => {
if (g1.includes('_')) {
return `${g1}<strong>${g2}</strong>`;
}

return g2.includes('</pre>') || this.containsNonPairTag(g2) ? match : `<strong>${g2}</strong>`;
},
},
{
name: 'strikethrough',
Expand Down

0 comments on commit 7149928

Please sign in to comment.