From 8b0346e3d33135c580f9a7a4aba369ff61b1fbd0 Mon Sep 17 00:00:00 2001 From: Mark Toman Date: Wed, 22 Feb 2023 18:24:10 +0100 Subject: [PATCH 1/2] Strip ~_* from multi-line auto-emails --- __tests__/ExpensiMark-HTML-test.js | 53 ++++++++++++++++++++++++++++++ lib/ExpensiMark.js | 6 ++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index ffff702a9a03..e2a59a83e2fd 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -60,6 +60,59 @@ test('Test multi-line strikethrough markdown replacement', () => { expect(parser.replace(testString)).toBe(replacedString); }); +// Emails containing *_~ are successfully wrapped in a mailto anchor tag +test('Test markdown replacement for emails containing bold/strikethrough/italic', () => { + let testInput = 'a~b@gmail.com'; + expect(parser.replace(testInput)).toBe('a~b@gmail.com'); + + testInput = 'a*b@gmail.com'; + expect(parser.replace(testInput)).toBe('a*b@gmail.com'); + + testInput = 'a_b@gmail.com'; + expect(parser.replace(testInput)).toBe('a_b@gmail.com'); + + testInput = 'a~*_b@gmail.com'; + expect(parser.replace(testInput)).toBe('a~*_b@gmail.com'); +}); + +// Single-line emails wrapped in *_~ are successfully wrapped in a mailto anchor tag +test('Test markdown replacement for emails wrapped in bold/strikethrough/italic in a single line', () => { + let testInput = '~abc@gmail.com~'; + expect(parser.replace(testInput)).toBe('abc@gmail.com'); + + testInput = '*abc@gmail.com*'; + expect(parser.replace(testInput)).toBe('abc@gmail.com'); + + testInput = '_abc@gmail.com_'; + expect(parser.replace(testInput)).toBe('abc@gmail.com'); + + testInput = '~*_abc@gmail.com_*~'; + expect(parser.replace(testInput)).toBe('abc@gmail.com'); +}); + +// Multi-line emails wrapped in *_~ are successfully wrapped in a mailto anchor tag +test('Test markdown replacement for emails wrapped in bold/strikethrough/italic in multi-line', () => { + let testInput = '~abc@gmail.com\ndef@gmail.com~'; + let result = 'abc@gmail.com
' + + 'def@gmail.com
'; + expect(parser.replace(testInput)).toBe(result); + + testInput = '*abc@gmail.com\ndef@gmail.com*'; + result = 'abc@gmail.com
' + + 'def@gmail.com
'; + expect(parser.replace(testInput)).toBe(result); + + testInput = '_abc@gmail.com\ndef@gmail.com_'; + result = 'abc@gmail.com
' + + 'def@gmail.com
'; + expect(parser.replace(testInput)).toBe(result); + + testInput = '~*_abc@gmail.com\ndef@gmail.com_*~'; + result = 'abc@gmail.com
' + + 'def@gmail.com
'; + expect(parser.replace(testInput)).toBe(result); +}); + // Markdown style links replaced successfully test('Test markdown style links', () => { const testString = 'Go to [Expensify](https://www.expensify.com) to learn more. [Expensify](www.expensify.com) [Expensify](expensify.com) [It\'s really the coolest](expensify.com) [`Some` Special cases - + . = , \'](expensify.com/some?query=par|am)'; diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index b19f4de0e099..1e1603930d21 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -93,11 +93,13 @@ export default class ExpensiMark { /** * Automatically links emails that are not in a link. Runs before the autolinker as it will not link an * email that is in a link + * Prevent emails from starting with [~_*] (such emails should not be supported), which is the reliable + * way to strip any enclosing ~_* in multi-line emails and in general. */ { name: 'autoEmail', - regex: /(?![^<]*>|[^<>]*<\\)([_*~]*)([_*~]*)([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+?(\.[a-zA-Z]+)+)\2\1(?![^<]*(<\/pre>|<\/code>|<\/a>))/gim, - replacement: (match, g1, g2, g3) => `${g1}${g2}${g3}${g2}${g1}`, + regex: /(?![^<]*>|[^<>]*<\\)([a-zA-Z0-9.!#$%&'+/=?^`{|}-][a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]*@[a-zA-Z0-9-]+?(\.[a-zA-Z]+)+)(?![^<]*(<\/pre>|<\/code>|<\/a>))/gim, + replacement: '$1', }, /** From 795c0bfe5b2067b9540d54fe2a532cb0cc50370d Mon Sep 17 00:00:00 2001 From: Mark Toman Date: Thu, 23 Feb 2023 21:38:50 +0100 Subject: [PATCH 2/2] Improve comment --- lib/ExpensiMark.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index 1e1603930d21..2fecaaa2bfd2 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -93,8 +93,7 @@ export default class ExpensiMark { /** * Automatically links emails that are not in a link. Runs before the autolinker as it will not link an * email that is in a link - * Prevent emails from starting with [~_*] (such emails should not be supported), which is the reliable - * way to strip any enclosing ~_* in multi-line emails and in general. + * Prevent emails from starting with [~_*]. Such emails should not be supported. */ { name: 'autoEmail',