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

Issue 26490 remove replacebr #606

Merged
merged 7 commits into from
Nov 21, 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
62 changes: 57 additions & 5 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('Test heading markdown replacement', () => {
// Sections starting with > are successfully wrapped with <blockquote></blockquote>
test('Test quote markdown replacement', () => {
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>Here is a &gt;quote that did not <pre>here&#32;is&#32;a&#32;codefenced&#32;quote<br />&gt;it&#32;should&#32;not&#32;be&#32;quoted<br /></pre>';
const quoteTestReplacedString = '<blockquote>This is a <strong>quote</strong> that started on a new line.</blockquote>Here is a &gt;quote that did not<br /><pre>here&#32;is&#32;a&#32;codefenced&#32;quote<br />&gt;it&#32;should&#32;not&#32;be&#32;quoted<br /></pre>';

expect(parser.replace(quoteTestStartString)).toBe(quoteTestReplacedString);
});
Expand Down Expand Up @@ -957,31 +957,31 @@ test('Test quotes markdown replacement with text matching inside and outside cod
test('Test quotes markdown replacement with text matching inside and outside codefence at the same line', () => {
const testString = 'The next line should be quoted\n>Hello,I’mtext\nThe next line should not be quoted\n```>Hello,I’mtext```\nsince its inside a codefence';

const resultString = 'The next line should be quoted<br /><blockquote>Hello,I’mtext</blockquote>The next line should not be quoted <pre>&gt;Hello,I’mtext</pre>since its inside a codefence';
const resultString = 'The next line should be quoted<br /><blockquote>Hello,I’mtext</blockquote>The next line should not be quoted<br /><pre>&gt;Hello,I’mtext</pre>since its inside a codefence';

expect(parser.replace(testString)).toBe(resultString);
});

test('Test quotes markdown replacement with text matching inside and outside codefence at the end of the text', () => {
const testString = 'The next line should be quoted\n>Hello,I’mtext\nThe next line should not be quoted\n```>Hello,I’mtext```';

const resultString = 'The next line should be quoted<br /><blockquote>Hello,I’mtext</blockquote>The next line should not be quoted <pre>&gt;Hello,I’mtext</pre>';
const resultString = 'The next line should be quoted<br /><blockquote>Hello,I’mtext</blockquote>The next line should not be quoted<br /><pre>&gt;Hello,I’mtext</pre>';

expect(parser.replace(testString)).toBe(resultString);
});

test('Test quotes markdown replacement with text matching inside and outside codefence with quotes at the end of the text', () => {
const testString = 'The next line should be quoted\n```>Hello,I’mtext```\nThe next line should not be quoted\n>Hello,I’mtext';

const resultString = 'The next line should be quoted <pre>&gt;Hello,I’mtext</pre>The next line should not be quoted<br /><blockquote>Hello,I’mtext</blockquote>';
const resultString = 'The next line should be quoted<br /><pre>&gt;Hello,I’mtext</pre>The next line should not be quoted<br /><blockquote>Hello,I’mtext</blockquote>';

expect(parser.replace(testString)).toBe(resultString);
});

test('Test quotes markdown replacement and removing <br/> from <br/><pre> and </pre><br/>', () => {
const testString = 'The next line should be quoted\n```>Hello,I’mtext```\nThe next line should not be quoted';

const resultString = 'The next line should be quoted <pre>&gt;Hello,I’mtext</pre>The next line should not be quoted';
const resultString = 'The next line should be quoted<br /><pre>&gt;Hello,I’mtext</pre>The next line should not be quoted';

expect(parser.replace(testString)).toBe(resultString);
});
Expand Down Expand Up @@ -1464,6 +1464,58 @@ test('Test strikethrough with link with URL that contains tilde', () => {
expect(parser.replace(testString)).toBe('<del><a href="https://example.com/?~example=~~~ex~" target="_blank" rel="noreferrer noopener">Example Link</a></del>');
});

test('Linebreak between end of text and start of code block should be remained', () => {
const testCases = [
{
testString: '```\ncode1\n```\ntext\n```\ncode2\n```',
resultString: '<pre>code1<br /></pre>text<br /><pre>code2<br /></pre>',
},
{
testString: 'text\n```\ncode\n```',
resultString: 'text<br /><pre>code<br /></pre>',
},
{
testString: '|\n```\ncode\n```',
resultString: '|<br /><pre>code<br /></pre>',
},
{
testString: 'text1```code```text2',
resultString: 'text1<pre>code</pre>text2',
},
{
testString: 'text1 ``` code ``` text2',
resultString: 'text1 <pre>&#32;code&#32;</pre> text2',
},
{
testString: 'text1\n```code```\ntext2',
resultString: 'text1<br /><pre>code</pre>text2',
},
{
testString: 'text1\n``` code ```\ntext2',
resultString: 'text1<br /><pre>&#32;code&#32;</pre>text2',
},
{
testString: 'text1\n```\n\ncode\n```\ntext2',
resultString: 'text1<br /><pre><br />code<br /></pre>text2',
},
{
testString: 'text1\n```\n\ncode\n\n```\ntext2',
resultString: 'text1<br /><pre><br />code<br /><br /></pre>text2',
},
{
testString: 'text1\n```\n\n\ncode\n\n```\ntext2',
resultString: 'text1<br /><pre><br /><br />code<br /><br /></pre>text2',
},
{
testString: 'text1\n```\n\ncode\n\n\n```\ntext2',
resultString: 'text1<br /><pre><br />code<br /><br /><br /></pre>text2',
},
];
testCases.forEach(({testString, resultString}) => {
expect(parser.replace(testString)).toBe(resultString);
});
});

test('Test autoEmail with markdown of <pre>, <code>, <a>, <mention-user> and <em> tag', () => {
const testString = '`code`[email protected] '
+ '```code block```[email protected] '
Expand Down
52 changes: 52 additions & 0 deletions __tests__/ExpensiMark-Markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,58 @@ test('Test codeFence copy from selection does not add extra new line', () => {
expect(parser.htmlToMarkdown(testString)).toBe('test heading\n```\nCode snippet\n```\n> [link](https://www.example.com)')
});

test('Linebreak should be remained for text between code block', () => {
const testCases = [
{
testString: '<pre>code1<br></pre>text<br><pre>code2<br></pre>',
resultString: '```\ncode1\n```\ntext\n```\ncode2\n```',
},
{
testString: 'text<br><pre>code<br></pre>',
resultString: 'text\n```\ncode\n```',
},
{
testString: '|<br><pre>code<br></pre>',
resultString: '|\n```\ncode\n```',
},
{
testString: 'text1<pre>code</pre>text2',
resultString: 'text1```\ncode\n```\ntext2',
},
{
testString: 'text1 <pre>&#32;code&#32;</pre> text2',
resultString: 'text1 ```\n code \n```\n text2',
},
{
testString: 'text1<br><pre>code</pre>text2',
resultString: 'text1\n```\ncode\n```\ntext2',
},
{
testString: 'text1<br><pre>&#32;code&#32;</pre>text2',
resultString: 'text1\n```\n code \n```\ntext2',
},
{
testString: 'text1<br><pre><br>code<br></pre>text2',
resultString: 'text1\n```\n\ncode\n```\ntext2',
},
{
testString: 'text1<br><pre><br>code<br><br></pre>text2',
resultString: 'text1\n```\n\ncode\n\n```\ntext2',
},
{
testString: 'text1<br><pre><br><br>code<br><br></pre>text2',
resultString: 'text1\n```\n\n\ncode\n\n```\ntext2',
},
{
testString: 'text1<br><pre><br>code<br><br><br></pre>text2',
resultString: 'text1\n```\n\ncode\n\n\n```\ntext2',
},
];
testCases.forEach(({testString, resultString}) => {
expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});
});

test('Mention html to markdown', () => {
let testString = '<mention-user>@[email protected]</mention-user>';
expect(parser.htmlToMarkdown(testString)).toBe('@[email protected]');
Expand Down
2 changes: 1 addition & 1 deletion lib/ExpensiMark.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare type Replacement = (...args: string[]) => string;
declare type Name = "codeFence" | "inlineCodeBlock" | "email" | "link" | "hereMentions" | "userMentions" | "autoEmail" | "autolink" | "quote" | "italic" | "bold" | "strikethrough" | "heading1" | "newline" | "replacepre" | "replacebr" | "listItem" | "exclude" | "anchor" | "breakline" | "blockquoteWrapHeadingOpen" | "blockquoteWrapHeadingClose" | "blockElementOpen" | "blockElementClose" | "stripTag";
declare type Name = "codeFence" | "inlineCodeBlock" | "email" | "link" | "hereMentions" | "userMentions" | "autoEmail" | "autolink" | "quote" | "italic" | "bold" | "strikethrough" | "heading1" | "newline" | "replacepre" | "listItem" | "exclude" | "anchor" | "breakline" | "blockquoteWrapHeadingOpen" | "blockquoteWrapHeadingClose" | "blockElementOpen" | "blockElementClose" | "stripTag";
declare type Rule = {
name: Name;
process?: (textToProcess: string, replacement: Replacement) => string;
Expand Down
6 changes: 0 additions & 6 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,6 @@ export default class ExpensiMark {
regex: /<\/pre>\s*<br\s*[/]?>/gi,
replacement: '</pre>',
},
{
// We're removing <br /> because when <br /> and <pre> occur together, an extra line is added.
name: 'replacebr',
regex: /<br\s*[/]?><pre>\s*/gi,
replacement: ' <pre>',
},
{
// We're removing <br /> because when <h1> and <br /> occur together, an extra line is added.
name: 'replaceh1br',
Expand Down
Loading