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

Prevent parsing backticks if are only content of inline code block #628

Merged
merged 2 commits into from
Dec 27, 2023
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
12 changes: 11 additions & 1 deletion __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ test('Test inline code blocks', () => {
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is <code>JavaScript</code>. How about you?');
});

test('Test inline code with one backtick as content', () => {
const inlineCodeStartString = '```';
expect(parser.replace(inlineCodeStartString)).toBe('&#x60;&#x60;&#x60;');
});

test('Test inline code with multiple backtick symbols as content', () => {
const inlineCodeStartString = '``````';
expect(parser.replace(inlineCodeStartString)).toBe('&#x60;&#x60;&#x60;&#x60;&#x60;&#x60;');
});

test('Test inline code blocks with ExpensiMark syntax inside', () => {
const inlineCodeStartString = '`This is how you can write ~strikethrough~, *bold*, and _italics_`';
expect(parser.replace(inlineCodeStartString)).toBe('<code>This is how you can write ~strikethrough~, *bold*, and _italics_</code>');
Expand Down Expand Up @@ -1067,7 +1077,7 @@ test('Test for backticks with no content', () => {
// Code-fence with no content is not replaced with <pre>
test('Test for codefence with no content', () => {
const testString = '``` ```';
const resultString = '<code>&#x60;</code> <code>&#x60;</code>';
const resultString = '&#x60;&#x60;&#x60; &#x60;&#x60;&#x60;';
expect(parser.replace(testString)).toBe(resultString);
});

Expand Down
10 changes: 9 additions & 1 deletion lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ export default class ExpensiMark {
// so capture the first and third group and place them in the replacement.
// but we should not replace backtick symbols if they include <pre> tags between them.
regex: /(\B|_|)&#x60;(?:(?!(?:(?!&#x60;).)*?<pre>))(.*?\S.*?)&#x60;(\B|_|)(?!&#x60;|[^<]*<\/pre>)/g,
replacement: '$1<code>$2</code>$3',
replacement: (match, g1, g2, g3) => {
const regex = /^[&#x60;]+$/i;

// if content of the inline code block is only backtick symbols, we should not replace them with <code> tag
if (regex.test(g2)) {
return match;
}
return `${g1}<code>${g2}</code>${g3}`;
}
},

/**
Expand Down