Skip to content

Commit

Permalink
Merge pull request #797 from tsa321/blankInlineCode
Browse files Browse the repository at this point in the history
Add support for inline code with space only character as content
  • Loading branch information
deetergp committed Sep 10, 2024
2 parents 5b78b0b + 33e5eea commit f3cc9a2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
21 changes: 14 additions & 7 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,17 +1243,24 @@ test('Test for backticks with complete escaped backtick characters inside it', (
expect(parser.replace(testString)).toBe(resultString);
});

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

// Code-fence with no content is not replaced with <pre>
test('Test for codefence with no content', () => {
// Backticks with only space characters are replaced with <code>
test('Test for backticks with only space characters as content', () => {
const testString = '` `';
const resultString = '<code>&nbsp;&nbsp;</code>';
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 = '&#x60;&#x60;&#x60; &#x60;&#x60;&#x60;';
const resultString = '<code>&#x60;&#x60; &#x60;&#x60;</code>';
expect(parser.replace(testString)).toBe(resultString);
});

Expand Down
9 changes: 7 additions & 2 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,13 @@ export default class ExpensiMark {
// Use the url escaped version of a backtick (`) symbol. Mobile platforms do not support lookbehinds,
// 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;)*)(?!&#x60;)(.*?\S+?.*?)(?<!&#x60;)((?:&#x60;)*)(&#x60;)(\B|_|)(?!&#x60;|[^<]*<\/pre>|[^<]*<\/video>)/gm,
replacement: '$1<code>$2$3$4</code>$6',
// 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>|[^<]*<\/video>)/gm,
replacement: (_extras, _match, g1, g2, g3) => {
const g2Value = g2.trim() === '' ? g2.replaceAll(' ', '&nbsp;') : g2;
return `${g1}<code>${g2Value}</code>${g3}`;
},
},

/**
Expand Down

0 comments on commit f3cc9a2

Please sign in to comment.