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 double backticks #827

Merged
merged 1 commit into from
Dec 31, 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
38 changes: 21 additions & 17 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,29 +472,29 @@ test('Test inline code blocks', () => {
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is <code>JavaScript</code>. How about you?');
});

test('Test inline code blocks with double backticks', () => {
const inlineCodeStartString = 'My favorite language is ``JavaScript``. How about you?';
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is <code>&#x60;JavaScript&#x60;</code>. How about you?');
test('Test double backticks', () => {
const testString = 'My favorite language is ``JavaScript``. How about you?';
expect(parser.replace(testString)).toBe('My favorite language is &#x60;&#x60;JavaScript&#x60;&#x60;. How about you?');
});

test('Test inline code blocks with triple backticks', () => {
const inlineCodeStartString = 'My favorite language is ```JavaScript```. How about you?';
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is <code>&#x60;&#x60;JavaScript&#x60;&#x60;</code>. How about you?');
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is &#x60;&#x60;<code>JavaScript</code>&#x60;&#x60;. How about you?');
});

test('Test multiple inline codes in one line', () => {
const inlineCodeStartString = 'My favorite language is `JavaScript`. How about you? I also like `Python`.';
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is <code>JavaScript</code>. How about you? I also like <code>Python</code>.');
});

test('Test inline code with one backtick as content', () => {
const inlineCodeStartString = '```';
expect(parser.replace(inlineCodeStartString)).toBe('&#x60;&#x60;&#x60;');
test('Test triple backticks', () => {
const testString = '```';
expect(parser.replace(testString)).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 multiple backtick symbols', () => {
const testString = '``````';
expect(parser.replace(testString)).toBe('&#x60;&#x60;&#x60;&#x60;&#x60;&#x60;');
});

test('Test inline code blocks with ExpensiMark syntax inside', () => {
Expand All @@ -508,9 +508,9 @@ test('Test inline code blocks inside ExpensiMark', () => {
expect(parser.replace(testString)).toBe(resultString);
});

test('Test inline code blocks with two backticks', () => {
test('Test words enclosed in double backticks', () => {
const testString = '``JavaScript``';
expect(parser.replace(testString)).toBe('<code>&#x60;JavaScript&#x60;</code>');
expect(parser.replace(testString)).toBe('&#x60;&#x60;JavaScript&#x60;&#x60;');
});

test('Test code fencing with ExpensiMark syntax inside', () => {
Expand Down Expand Up @@ -1240,10 +1240,9 @@ test('Test for backticks with complete escaped backtick characters inside it', (
expect(parser.replace(testString)).toBe(resultString);
});

// Backticks with only tab characters inside it are not replaced with <code>
test('Test for backticks only tab characters inside it', () => {
test('Test for inline code blocks with only tab characters as content', () => {
const testString = '`\u0009`';
const resultString = '&#x60;\u0009&#x60;';
const resultString = '<code> </code>';
expect(parser.replace(testString)).toBe(resultString);
});

Expand All @@ -1254,10 +1253,15 @@ test('Test for backticks with only space characters as content', () => {
expect(parser.replace(testString)).toBe(resultString);
});

// Code-fence with spaces as content
test('Test for inline code block with triple backtick with spaces as content', () => {
const testString = '``` ```';
const resultString = '<code>&#x60;&#x60; &#x60;&#x60;</code>';
const resultString = '&#x60;&#x60;<code>&nbsp;&nbsp;&nbsp;</code>&#x60;&#x60;';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for a normal code block, an empty code block, followed by a word and a backtick', () => {
const testString = '`hello` `` hi`';
const resultString = '<code>hello</code> &#x60;&#x60; hi&#x60;';
expect(parser.replace(testString)).toBe(resultString);
});

Expand Down
5 changes: 4 additions & 1 deletion lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,12 @@ export default class ExpensiMark {
// but we should not replace backtick symbols if they include <pre> tags between them.
// At least one non-whitespace character or a specific whitespace character (" " and "\u00A0")
// must be present inside the backticks.
regex: /(\B|_|)&#x60;((?:&#x60;)*(?!&#x60;).*?[\S| |\u00A0]+?.*?(?<!&#x60;)(?:&#x60;)*)&#x60;(\B|_|)(?!&#x60;|(?!<pre>)[^<]*(?:<(?!pre>)[^<]*)*<\/pre>|[^<]*<\/video>)/gm,
regex: /(\B|_|)&#x60;(.*?)&#x60;(\B|_|)(?!(?!<pre>)[^<]*(?:<(?!pre>)[^<]*)*<\/pre>|[^<]*<\/video>)/gm,
replacement: (_extras, _match, g1, g2, g3) => {
const g2Value = g2.trim() === '' ? g2.replaceAll(' ', '&nbsp;') : g2;
if (!g2Value) {
return _match;
}
return `${g1}<code>${g2Value}</code>${g3}`;
},
},
Expand Down
Loading