Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix underscore ahead of asterisk not get bolded #751

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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*](?<!\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
Loading