Skip to content

Commit

Permalink
Merge pull request Expensify#705 from software-mansion-labs/@BartoszG…
Browse files Browse the repository at this point in the history
…rajdek/remove-data-code-raw

Remove data-code-raw for `pre` blocks
  • Loading branch information
youssef-lr authored Jun 5, 2024
2 parents 7fae0ab + c96893f commit 1ee6047
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 12 additions & 6 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,19 +505,19 @@ test('Test code fencing with ExpensiMark syntax outside', () => {

codeFenceExample = '*Test1 ```\ncode\n``` Test2*';
expect(parser.replace(codeFenceExample)).toBe('*Test1 <pre>code<br /></pre> Test2*');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('*Test1 <pre data-code-raw=\"\ncode\n\">code\n</pre> Test2*');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('*Test1 <pre>code\n</pre> Test2*');

codeFenceExample = '_Test1 ```\ncode\n``` Test2_';
expect(parser.replace(codeFenceExample)).toBe('_Test1 <pre>code<br /></pre> Test2_');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('_Test1 <pre data-code-raw=\"\ncode\n\">code\n</pre> Test2_');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('_Test1 <pre>code\n</pre> Test2_');

codeFenceExample = '~Test1 ```\ncode\n``` Test2~';
expect(parser.replace(codeFenceExample)).toBe('~Test1 <pre>code<br /></pre> Test2~');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('~Test1 <pre data-code-raw=\"\ncode\n\">code\n</pre> Test2~');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('~Test1 <pre>code\n</pre> Test2~');

codeFenceExample = '[```\ncode\n```](google.com)';
expect(parser.replace(codeFenceExample)).toBe('[<pre>code<br /></pre>](<a href=\"https://google.com\" target=\"_blank\" rel=\"noreferrer noopener\">google.com</a>)');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('[<pre data-code-raw=\"\ncode\n\">code\n</pre>](<a href=\"https://google.com\" data-raw-href=\"google.com\" data-link-variant=\"auto\" target=\"_blank\" rel=\"noreferrer noopener\">google.com</a>)');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('[<pre>code\n</pre>](<a href=\"https://google.com\" data-raw-href=\"google.com\" data-link-variant=\"auto\" target=\"_blank\" rel=\"noreferrer noopener\">google.com</a>)');
});

test('Test code fencing with additional backticks inside', () => {
Expand Down Expand Up @@ -1284,6 +1284,12 @@ test('Test for user mention with codefence style', () => {
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for room mention with codefence style', () => {
const testString = '```\n#room\n```';
const resultString = '<pre>#room<br /></pre>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for user mention with inlineCodeBlock style', () => {
const testString = '`@[email protected]`';
const resultString = '<code>@[email protected]</code>';
Expand Down Expand Up @@ -1793,14 +1799,14 @@ describe('when should keep raw input flag is enabled', () => {

test('quote with other markdowns', () => {
const quoteTestStartString = '> This is a *quote* that started on a new line.\nHere is a >quote that did not\n```\nhere is a codefenced quote\n>it should not be quoted\n```';
const quoteTestReplacedString = '<blockquote> This is a <strong>quote</strong> that started on a new line.</blockquote>\nHere is a &gt;quote that did not\n<pre data-code-raw=\"\nhere is a codefenced quote\n&gt;it should not be quoted\n\">here&#32;is&#32;a&#32;codefenced&#32;quote\n&gt;it&#32;should&#32;not&#32;be&#32;quoted\n</pre>';
const quoteTestReplacedString = '<blockquote> This is a <strong>quote</strong> that started on a new line.</blockquote>\nHere is a &gt;quote that did not\n<pre>here&#32;is&#32;a&#32;codefenced&#32;quote\n&gt;it&#32;should&#32;not&#32;be&#32;quoted\n</pre>';

expect(parser.replace(quoteTestStartString, {shouldKeepRawInput: true})).toBe(quoteTestReplacedString);
});

test('codeBlock with newlines', () => {
const quoteTestStartString = '```\nhello world\n```';
const quoteTestReplacedString = '<pre data-code-raw="\nhello world\n">hello&#32;world\n</pre>';
const quoteTestReplacedString = '<pre>hello&#32;world\n</pre>';

expect(parser.replace(quoteTestStartString, {shouldKeepRawInput: true})).toBe(quoteTestReplacedString);
});
Expand Down
7 changes: 3 additions & 4 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ export default class ExpensiMark {
return `<pre>${group}</pre>`;
},
rawInputReplacement: (match, __, textWithinFences) => {
const withinFences = match.replace(/(?:&#x60;&#x60;&#x60;)([\s\S]*?)(?:&#x60;&#x60;&#x60;)/g, '$1');
const group = textWithinFences.replace(/(?:(?![\n\r])\s)/g, '&#32;');
return `<pre data-code-raw="${withinFences}">${group}</pre>`;
const group = textWithinFences.replace(/(?:(?![\n\r])\s)/g, '&#32;').replace(/<emoji>|<\/emoji>/g, '');
return `<pre>${group}</pre>`;
},
},

Expand Down Expand Up @@ -185,7 +184,7 @@ export default class ExpensiMark {
{
name: 'reportMentions',

regex: /(?<![^ \n*~_])(#[\p{Ll}0-9-]{1,80})/gimu,
regex: /(?<![^ \n*~_])(#[\p{Ll}0-9-]{1,80})(?![^<]*(?:<\/pre>|<\/code>))/gimu,
replacement: '<mention-report>$1</mention-report>',
},

Expand Down

0 comments on commit 1ee6047

Please sign in to comment.