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

capture newline as the code fence content #730

Merged
merged 4 commits into from
Jul 2, 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
12 changes: 6 additions & 6 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,20 @@ 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>code\n</pre> Test2*');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('*Test1 <pre>\ncode\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>code\n</pre> Test2_');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('_Test1 <pre>\ncode\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>code\n</pre> Test2~');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('~Test1 <pre>\ncode\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>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>)',
'[<pre>\ncode\n</pre>](<a href="https://google.com" data-raw-href="google.com" data-link-variant="auto" target="_blank" rel="noreferrer noopener">google.com</a>)',
);
});

Expand Down Expand Up @@ -1846,14 +1846,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>here&#32;is&#32;a&#32;codefenced&#32;quote\n&gt;it&#32;should&#32;not&#32;be&#32;quoted\n</pre>';
'<blockquote> This is a <strong>quote</strong> that started on a new line.</blockquote>\nHere is a &gt;quote that did not\n<pre>\nhere&#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>hello&#32;world\n</pre>';
const quoteTestReplacedString = '<pre>\nhello&#32;world\n</pre>';

expect(parser.replace(quoteTestStartString, {shouldKeepRawInput: true})).toBe(quoteTestReplacedString);
});
Expand Down
8 changes: 4 additions & 4 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,21 @@ export default class ExpensiMark {
name: 'codeFence',

// &#x60; is a backtick symbol we are matching on three of them before then after a new line character
regex: /(&#x60;&#x60;&#x60;(?:\r\n|\n))((?:\s*?(?!(?:\r\n|\n)?&#x60;&#x60;&#x60;(?!&#x60;))[\S])+\s*?(?:\r\n|\n))(&#x60;&#x60;&#x60;)/g,
regex: /(&#x60;&#x60;&#x60;(\r\n|\n))((?:\s*?(?!(?:\r\n|\n)?&#x60;&#x60;&#x60;(?!&#x60;))[\S])+\s*?(?:\r\n|\n))(&#x60;&#x60;&#x60;)/g,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB. This has 4 capturing groups but we only need 2. Can you make the other two non-capturing groups?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is 6 groups in total including the negative lookahead and we use 3, but I will leave it as it is.


// We're using a function here to perform an additional replace on the content
// inside the backticks because Android is not able to use <pre> tags and does
// not respect whitespace characters at all like HTML does. We do not want to mess
// with the new lines here since they need to be converted into <br>. And we don't
// want to do this anywhere else since that would break HTML.
// &nbsp; will create styling issues so use &#32;
replacement: (_extras, _match, _g1, textWithinFences) => {
replacement: (_extras, _match, _g1, _g2, textWithinFences) => {
const group = textWithinFences.replace(/(?:(?![\n\r])\s)/g, '&#32;');
return `<pre>${group}</pre>`;
},
rawInputReplacement: (_extras, _match, _g1, textWithinFences) => {
rawInputReplacement: (_extras, _match, _g1, newLineCharacter, textWithinFences) => {
const group = textWithinFences.replace(/(?:(?![\n\r])\s)/g, '&#32;').replace(/<emoji>|<\/emoji>/g, '');
return `<pre>${group}</pre>`;
return `<pre>${newLineCharacter}${group}</pre>`;
},
},

Expand Down
Loading