From ac0d340431297b6d21fd777fd2d5e6c90e0d0849 Mon Sep 17 00:00:00 2001 From: Nikos Bright Date: Fri, 13 Oct 2023 18:01:41 -0400 Subject: [PATCH 1/6] remove replacebr rule --- lib/ExpensiMark.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index 2d77cc3a..7a6038fd 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -229,12 +229,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', From f611c4f86c0801da0197956e2ccf83e4ab2c83b3 Mon Sep 17 00:00:00 2001 From: Nikos Bright Date: Sat, 14 Oct 2023 11:02:46 -0400 Subject: [PATCH 2/6] remove replacebr from type --- lib/ExpensiMark.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From dd4a5dada53d0d1791e2a670b5fffd6ed5b873b2 Mon Sep 17 00:00:00 2001 From: Nikos Bright Date: Sat, 14 Oct 2023 11:46:17 -0400 Subject: [PATCH 3/6] test updated --- __tests__/ExpensiMark-HTML-test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 281b0981..3c90dc76 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -32,7 +32,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); }); @@ -921,7 +921,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); }); @@ -929,7 +929,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); }); @@ -937,7 +937,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); }); @@ -945,7 +945,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); }); From 9065305345cf18657ed20975f22d6bf6bb07bb89 Mon Sep 17 00:00:00 2001 From: Nikos Bright Date: Tue, 17 Oct 2023 08:53:27 -0400 Subject: [PATCH 4/6] test added for linebreak cases --- __tests__/ExpensiMark-HTML-test.js | 11 +++++++++++ __tests__/ExpensiMark-Markdown-test.js | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 3c90dc76..6e2d6f1d 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -1412,3 +1412,14 @@ test('Test strikethrough with link with URL that contains tilde', () => { testString = '~[Example Link](https://example.com/?~example=~~~ex~)~'; expect(parser.replace(testString)).toBe('Example Link'); }); + +test('Linebreak between end of text and start of code block should be remained', () => { + let testString = '```\ncode\n```\ntext\n```\ncode\n```'; + expect(parser.replace(testString)).toBe('
code
text
code
'); + + testString = 'A\n```\ncode\n```'; + expect(parser.replace(testString)).toBe('A
code
'); + + testString = '|\n```\ncode\n```'; + expect(parser.replace(testString)).toBe('|
code
'); +}); diff --git a/__tests__/ExpensiMark-Markdown-test.js b/__tests__/ExpensiMark-Markdown-test.js index 56624cec..a291c995 100644 --- a/__tests__/ExpensiMark-Markdown-test.js +++ b/__tests__/ExpensiMark-Markdown-test.js @@ -690,3 +690,14 @@ test('Test codeFence copy from selection does not add extra new line', () => { testString = '

test heading

Code snippet\n

link

'; 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', () => { + let testString = '
code
text
code
'; + expect(parser.htmlToMarkdown(testString)).toBe('```\ncode\n```\ntext\n```\ncode\n```'); + + testString = 'A
code
'; + expect(parser.htmlToMarkdown(testString)).toBe('A\n```\ncode\n```'); + + testString = '|
code
'; + expect(parser.htmlToMarkdown(testString)).toBe('|\n```\ncode\n```'); +}); From 7992a004cd3abaddb74f4444c409e9675956cb79 Mon Sep 17 00:00:00 2001 From: Nikos Bright Date: Fri, 17 Nov 2023 11:14:57 -0500 Subject: [PATCH 5/6] use testCases array for testing --- __tests__/ExpensiMark-HTML-test.js | 25 +++++++++++++++++-------- __tests__/ExpensiMark-Markdown-test.js | 25 +++++++++++++++++-------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 522478b0..70600117 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -1465,14 +1465,23 @@ test('Test strikethrough with link with URL that contains tilde', () => { }); test('Linebreak between end of text and start of code block should be remained', () => { - let testString = '```\ncode\n```\ntext\n```\ncode\n```'; - expect(parser.replace(testString)).toBe('
code
text
code
'); - - testString = 'A\n```\ncode\n```'; - expect(parser.replace(testString)).toBe('A
code
'); - - testString = '|\n```\ncode\n```'; - expect(parser.replace(testString)).toBe('|
code
'); + const testCases = [ + { + testString: '```\ncode\n```\ntext\n```\ncode\n```', + resultString: '
code
text
code
', + }, + { + testString: 'A\n```\ncode\n```', + resultString: 'A
code
', + }, + { + testString: '|\n```\ncode\n```', + resultString: '|
code
', + }, + ]; + testCases.forEach(({testString, resultString}) => { + expect(parser.replace(testString)).toBe(resultString); + }); }); test('Test autoEmail with markdown of
, , ,  and  tag', () => {
diff --git a/__tests__/ExpensiMark-Markdown-test.js b/__tests__/ExpensiMark-Markdown-test.js
index 36a1e252..1b98295b 100644
--- a/__tests__/ExpensiMark-Markdown-test.js
+++ b/__tests__/ExpensiMark-Markdown-test.js
@@ -692,14 +692,23 @@ test('Test codeFence copy from selection does not add extra new line', () => {
 });
 
 test('Linebreak should be remained for text between code block', () => {
-    let testString = '
code
text
code
'; - expect(parser.htmlToMarkdown(testString)).toBe('```\ncode\n```\ntext\n```\ncode\n```'); - - testString = 'A
code
'; - expect(parser.htmlToMarkdown(testString)).toBe('A\n```\ncode\n```'); - - testString = '|
code
'; - expect(parser.htmlToMarkdown(testString)).toBe('|\n```\ncode\n```'); + const testCases = [ + { + testString: '
code
text
code
', + resultString: '```\ncode\n```\ntext\n```\ncode\n```', + }, + { + testString: 'A
code
', + resultString: 'A\n```\ncode\n```', + }, + { + testString: '|
code
', + resultString: '|\n```\ncode\n```', + }, + ]; + testCases.forEach(({testString, resultString}) => { + expect(parser.htmlToMarkdown(testString)).toBe(resultString); + }); }); test('Mention html to markdown', () => { From e417c8fea72b9b973b67e5b9a443ba9991e65d58 Mon Sep 17 00:00:00 2001 From: Nikos Bright Date: Sun, 19 Nov 2023 10:02:05 -0500 Subject: [PATCH 6/6] more testCases added for codeblock cases --- __tests__/ExpensiMark-HTML-test.js | 40 +++++++++++++++++++++++--- __tests__/ExpensiMark-Markdown-test.js | 40 +++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 70600117..84583cc1 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -1467,17 +1467,49 @@ test('Test strikethrough with link with URL that contains tilde', () => { test('Linebreak between end of text and start of code block should be remained', () => { const testCases = [ { - testString: '```\ncode\n```\ntext\n```\ncode\n```', - resultString: '
code
text
code
', + testString: '```\ncode1\n```\ntext\n```\ncode2\n```', + resultString: '
code1
text
code2
', }, { - testString: 'A\n```\ncode\n```', - resultString: 'A
code
', + 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); diff --git a/__tests__/ExpensiMark-Markdown-test.js b/__tests__/ExpensiMark-Markdown-test.js index 1b98295b..488e63f4 100644 --- a/__tests__/ExpensiMark-Markdown-test.js +++ b/__tests__/ExpensiMark-Markdown-test.js @@ -694,17 +694,49 @@ test('Test codeFence copy from selection does not add extra new line', () => { test('Linebreak should be remained for text between code block', () => { const testCases = [ { - testString: '
code
text
code
', - resultString: '```\ncode\n```\ntext\n```\ncode\n```', + testString: '
code1
text
code2
', + resultString: '```\ncode1\n```\ntext\n```\ncode2\n```', }, { - testString: 'A
code
', - resultString: 'A\n```\ncode\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);