diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 433a13bc..84583cc1 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -38,7 +38,7 @@ test('Test heading markdown replacement', () => { // Sections starting with > are successfully wrapped with
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 = '
This is a quote that started on a new line.
Here is a >quote that did not
here is a codefenced quote
>it should not be quoted
'; + const quoteTestReplacedString = '
This is a quote that started on a new line.
Here is a >quote that did not
here is a codefenced quote
>it should not be quoted
'; expect(parser.replace(quoteTestStartString)).toBe(quoteTestReplacedString); }); @@ -957,7 +957,7 @@ 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
Hello,I’mtext
The next line should not be quoted
>Hello,I’mtext
since its inside a codefence'; + const resultString = 'The next line should be quoted
Hello,I’mtext
The next line should not be quoted
>Hello,I’mtext
since its inside a codefence'; expect(parser.replace(testString)).toBe(resultString); }); @@ -965,7 +965,7 @@ 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 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
Hello,I’mtext
The next line should not be quoted
>Hello,I’mtext
'; + const resultString = 'The next line should be quoted
Hello,I’mtext
The next line should not be quoted
>Hello,I’mtext
'; expect(parser.replace(testString)).toBe(resultString); }); @@ -973,7 +973,7 @@ test('Test quotes markdown replacement with text matching inside and outside cod 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
>Hello,I’mtext
The next line should not be quoted
Hello,I’mtext
'; + const resultString = 'The next line should be quoted
>Hello,I’mtext
The next line should not be quoted
Hello,I’mtext
'; expect(parser.replace(testString)).toBe(resultString); }); @@ -981,7 +981,7 @@ test('Test quotes markdown replacement with text matching inside and outside cod test('Test quotes markdown replacement and removing
from
 and 

', () => { 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
>Hello,I’mtext
The next line should not be quoted'; + const resultString = 'The next line should be quoted
>Hello,I’mtext
The next line should not be quoted'; expect(parser.replace(testString)).toBe(resultString); }); @@ -1464,6 +1464,58 @@ test('Test strikethrough with link with URL that contains tilde', () => { expect(parser.replace(testString)).toBe('Example Link'); }); +test('Linebreak between end of text and start of code block should be remained', () => { + const testCases = [ + { + testString: '```\ncode1\n```\ntext\n```\ncode2\n```', + resultString: '
code1
text
code2
', + }, + { + testString: 'text\n```\ncode\n```', + resultString: 'text
code
', + }, + { + testString: '|\n```\ncode\n```', + resultString: '|
code
', + }, + { + testString: 'text1```code```text2', + resultString: 'text1
code
text2', + }, + { + testString: 'text1 ``` code ``` text2', + resultString: 'text1
 code 
text2', + }, + { + testString: 'text1\n```code```\ntext2', + resultString: 'text1
code
text2', + }, + { + testString: 'text1\n``` code ```\ntext2', + resultString: 'text1
 code 
text2', + }, + { + testString: 'text1\n```\n\ncode\n```\ntext2', + resultString: 'text1

code
text2', + }, + { + testString: 'text1\n```\n\ncode\n\n```\ntext2', + resultString: 'text1

code

text2', + }, + { + testString: 'text1\n```\n\n\ncode\n\n```\ntext2', + resultString: 'text1


code

text2', + }, + { + testString: 'text1\n```\n\ncode\n\n\n```\ntext2', + resultString: 'text1

code


text2', + }, + ]; + testCases.forEach(({testString, resultString}) => { + expect(parser.replace(testString)).toBe(resultString); + }); +}); + test('Test autoEmail with markdown of
, , ,  and  tag', () => {
     const testString = '`code`test@gmail.com '
         + '```code block```test@gmail.com '
diff --git a/__tests__/ExpensiMark-Markdown-test.js b/__tests__/ExpensiMark-Markdown-test.js
index 8806790f..488e63f4 100644
--- a/__tests__/ExpensiMark-Markdown-test.js
+++ b/__tests__/ExpensiMark-Markdown-test.js
@@ -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: '
code1
text
code2
', + resultString: '```\ncode1\n```\ntext\n```\ncode2\n```', + }, + { + testString: 'text
code
', + resultString: 'text\n```\ncode\n```', + }, + { + testString: '|
code
', + resultString: '|\n```\ncode\n```', + }, + { + testString: 'text1
code
text2', + resultString: 'text1```\ncode\n```\ntext2', + }, + { + testString: 'text1
 code 
text2', + resultString: 'text1 ```\n code \n```\n text2', + }, + { + testString: 'text1
code
text2', + resultString: 'text1\n```\ncode\n```\ntext2', + }, + { + testString: 'text1
 code 
text2', + resultString: 'text1\n```\n code \n```\ntext2', + }, + { + testString: 'text1

code
text2', + resultString: 'text1\n```\n\ncode\n```\ntext2', + }, + { + testString: 'text1

code

text2', + resultString: 'text1\n```\n\ncode\n\n```\ntext2', + }, + { + testString: 'text1


code

text2', + resultString: 'text1\n```\n\n\ncode\n\n```\ntext2', + }, + { + testString: 'text1

code


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 = '@user@domain.com'; expect(parser.htmlToMarkdown(testString)).toBe('@user@domain.com'); diff --git a/lib/ExpensiMark.d.ts b/lib/ExpensiMark.d.ts index 529e7b52..6bd61dc9 100644 --- a/lib/ExpensiMark.d.ts +++ b/lib/ExpensiMark.d.ts @@ -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; diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index e12a5ff1..4a4135c6 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -230,12 +230,6 @@ export default class ExpensiMark { regex: /<\/pre>\s*/gi, replacement: '
', }, - { - // We're removing
because when
and
 occur together, an extra line is added.
-                name: 'replacebr',
-                regex: /
\s*/gi,
-                replacement: ' 
',
-            },
             {
                 // We're removing 
because when

and
occur together, an extra line is added. name: 'replaceh1br',