From 47df1c5fc504edc06730bd94afb4377ae3c96c40 Mon Sep 17 00:00:00 2001 From: Tsaqif Date: Mon, 9 Sep 2024 08:31:03 +0700 Subject: [PATCH 1/3] Add support for inline code with space only character as content Signed-off-by: Tsaqif --- __tests__/ExpensiMark-HTML-test.js | 29 ++++++++++++++++++----------- lib/ExpensiMark.ts | 8 ++++++-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index d952690e..bba2665f 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -79,7 +79,7 @@ test('Test italic markdown replacement', () => { const italicTestStartString = 'Note that _this is correctly italicized_\n' + 'Note that `_this is correctly not italicized_` and _following inline contents are italicized_'; const italicTestReplacedString = - 'Note that this is correctly italicized
' + 'Note that _this is correctly not italicized_ and following inline contents are italicized'; + 'Note that this is correctly italicized
' + 'Note that _this is correctly not italicized_ and following inline contents are italicized'; expect(parser.replace(italicTestStartString)).toBe(italicTestReplacedString); }); @@ -402,7 +402,7 @@ test('Test critical markdown style links', () => { 'second no http:// ' + 'third ' + 'third no https:// ' + - 'link [inside another link](https://google.com) ' + + 'link [inside another link](https://google.com) ' + 'link with an @ in it ' + 'link with [brackets] inside of it ' + 'link with smart quotes ‘’“” ' + @@ -499,7 +499,7 @@ test('Test inline code with multiple backtick symbols as content', () => { test('Test inline code blocks with ExpensiMark syntax inside', () => { const inlineCodeStartString = '`This is how you can write ~strikethrough~, *bold*, and _italics_`'; - expect(parser.replace(inlineCodeStartString)).toBe('This is how you can write ~strikethrough~, *bold*, and _italics_'); + expect(parser.replace(inlineCodeStartString)).toBe('This is how you can write ~strikethrough~, *bold*, and _italics_'); }); test('Test inline code blocks inside ExpensiMark', () => { @@ -1243,17 +1243,24 @@ test('Test for backticks with complete escaped backtick characters inside it', ( expect(parser.replace(testString)).toBe(resultString); }); -// Backticks with no content are not replaced with -test('Test for backticks with no content', () => { - const testString = '` `'; - const resultString = '` `'; +// Backticks with only tab characters inside it are not replaced with +test('Test for backticks only tab characters inside it', () => { + const testString = '`\u0009`'; + const resultString = '`\u0009`'; expect(parser.replace(testString)).toBe(resultString); }); -// Code-fence with no content is not replaced with
-test('Test for codefence with no content', () => {
+// Backticks with only space characters are replaced with 
+test('Test for backticks with only space characters as content', () => {
+    const testString = '`  `';
+    const resultString = '  ';
+    expect(parser.replace(testString)).toBe(resultString);
+});
+
+// Code-fence with spaces as content
+test('Test for inline code block with triple backtick with spaces as content', () => {
     const testString = '```   ```';
-    const resultString = '```   ```';
+    const resultString = '``   ``';
     expect(parser.replace(testString)).toBe(resultString);
 });
 
@@ -1410,7 +1417,7 @@ test('Test for user mention with text with codefence style', () => {
 
 test('Test for user mention with text with inlineCodeBlock style', () => {
     const testString = '`hi @username@expensify.com`';
-    const resultString = 'hi @username@expensify.com';
+    const resultString = 'hi @username@expensify.com';
     expect(parser.replace(testString)).toBe(resultString);
 });
 
diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts
index 2b327ee4..1a737364 100644
--- a/lib/ExpensiMark.ts
+++ b/lib/ExpensiMark.ts
@@ -190,8 +190,12 @@ export default class ExpensiMark {
                 // Use the url escaped version of a backtick (`) symbol. Mobile platforms do not support lookbehinds,
                 // so capture the first and third group and place them in the replacement.
                 // but we should not replace backtick symbols if they include 
 tags between them.
-                regex: /(\B|_|)`((?:`)*)(?!`)(.*?\S+?.*?)(?|[^<]*<\/video>)/gm,
-                replacement: '$1$2$3$4$6',
+                // At least one non-whitespace character or a specific whitespace character (" " and "\u00A0") 
+                // must be present inside the backticks.
+                regex: /(\B|_|)`((?:`)*(?!`).*?[\S| |\u00A0]+?.*?(?|[^<]*<\/video>)/gm,
+                replacement: (_extras, _match, g1, g2, g3) => {
+                    return `${g1}${g2.replaceAll(' ', ' ')}${g3}`;
+                },
             },
 
             /**

From 3628dce59ef600fb042d840e5fd36f491b7e9835 Mon Sep 17 00:00:00 2001
From: Tsaqif 
Date: Mon, 9 Sep 2024 08:43:08 +0700
Subject: [PATCH 2/3] Fix lint

Signed-off-by: Tsaqif 
---
 lib/ExpensiMark.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts
index 1a737364..4f0336f7 100644
--- a/lib/ExpensiMark.ts
+++ b/lib/ExpensiMark.ts
@@ -190,7 +190,7 @@ export default class ExpensiMark {
                 // Use the url escaped version of a backtick (`) symbol. Mobile platforms do not support lookbehinds,
                 // so capture the first and third group and place them in the replacement.
                 // but we should not replace backtick symbols if they include 
 tags between them.
-                // At least one non-whitespace character or a specific whitespace character (" " and "\u00A0") 
+                // At least one non-whitespace character or a specific whitespace character (" " and "\u00A0")
                 // must be present inside the backticks.
                 regex: /(\B|_|)`((?:`)*(?!`).*?[\S| |\u00A0]+?.*?(?|[^<]*<\/video>)/gm,
                 replacement: (_extras, _match, g1, g2, g3) => {

From 33e5eeacbdf470237df8e454ee42389b6ba20a6c Mon Sep 17 00:00:00 2001
From: Tsaqif 
Date: Tue, 10 Sep 2024 07:30:13 +0700
Subject: [PATCH 3/3] Replace with nbsp when necessary

Signed-off-by: Tsaqif 
---
 __tests__/ExpensiMark-HTML-test.js | 10 +++++-----
 lib/ExpensiMark.ts                 |  3 ++-
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js
index bba2665f..1417cc5a 100644
--- a/__tests__/ExpensiMark-HTML-test.js
+++ b/__tests__/ExpensiMark-HTML-test.js
@@ -79,7 +79,7 @@ test('Test italic markdown replacement', () => {
     const italicTestStartString = 'Note that _this is correctly italicized_\n' + 'Note that `_this is correctly not italicized_` and _following inline contents are italicized_';
 
     const italicTestReplacedString =
-        'Note that this is correctly italicized
' + 'Note that _this is correctly not italicized_ and following inline contents are italicized'; + 'Note that this is correctly italicized
' + 'Note that _this is correctly not italicized_ and following inline contents are italicized'; expect(parser.replace(italicTestStartString)).toBe(italicTestReplacedString); }); @@ -402,7 +402,7 @@ test('Test critical markdown style links', () => { 'second no http:// ' + 'third ' + 'third no https:// ' + - 'link [inside another link](https://google.com) ' + + 'link [inside another link](https://google.com) ' + 'link with an @ in it ' + 'link with [brackets] inside of it ' + 'link with smart quotes ‘’“” ' + @@ -499,7 +499,7 @@ test('Test inline code with multiple backtick symbols as content', () => { test('Test inline code blocks with ExpensiMark syntax inside', () => { const inlineCodeStartString = '`This is how you can write ~strikethrough~, *bold*, and _italics_`'; - expect(parser.replace(inlineCodeStartString)).toBe('This is how you can write ~strikethrough~, *bold*, and _italics_'); + expect(parser.replace(inlineCodeStartString)).toBe('This is how you can write ~strikethrough~, *bold*, and _italics_'); }); test('Test inline code blocks inside ExpensiMark', () => { @@ -1260,7 +1260,7 @@ test('Test for backticks with only space characters as content', () => { // Code-fence with spaces as content test('Test for inline code block with triple backtick with spaces as content', () => { const testString = '``` ```'; - const resultString = '``   ``'; + const resultString = '`` ``'; expect(parser.replace(testString)).toBe(resultString); }); @@ -1417,7 +1417,7 @@ test('Test for user mention with text with codefence style', () => { test('Test for user mention with text with inlineCodeBlock style', () => { const testString = '`hi @username@expensify.com`'; - const resultString = 'hi @username@expensify.com'; + const resultString = 'hi @username@expensify.com'; expect(parser.replace(testString)).toBe(resultString); }); diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index 4f0336f7..0bd16404 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -194,7 +194,8 @@ export default class ExpensiMark { // must be present inside the backticks. regex: /(\B|_|)`((?:`)*(?!`).*?[\S| |\u00A0]+?.*?(?|[^<]*<\/video>)/gm, replacement: (_extras, _match, g1, g2, g3) => { - return `${g1}${g2.replaceAll(' ', ' ')}${g3}`; + const g2Value = g2.trim() === '' ? g2.replaceAll(' ', ' ') : g2; + return `${g1}${g2Value}${g3}`; }, },