Skip to content

Commit

Permalink
Merge pull request #504 from marktoman/14695-fix-multiline-autoemails
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkiWines authored Feb 23, 2023
2 parents a2942d0 + 795c0bf commit 69e32de
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
53 changes: 53 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 href="mailto:a~b@gmail.com">a~b@gmail.com</a>');

testInput = 'a*b@gmail.com';
expect(parser.replace(testInput)).toBe('<a href="mailto:a*b@gmail.com">a*b@gmail.com</a>');

testInput = 'a_b@gmail.com';
expect(parser.replace(testInput)).toBe('<a href="mailto:a_b@gmail.com">a_b@gmail.com</a>');

testInput = 'a~*_b@gmail.com';
expect(parser.replace(testInput)).toBe('<a href="mailto:a~*_b@gmail.com">a~*_b@gmail.com</a>');
});

// 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('<del><a href="mailto:abc@gmail.com">abc@gmail.com</a></del>');

testInput = '*abc@gmail.com*';
expect(parser.replace(testInput)).toBe('<strong><a href="mailto:abc@gmail.com">abc@gmail.com</a></strong>');

testInput = '_abc@gmail.com_';
expect(parser.replace(testInput)).toBe('<em><a href="mailto:abc@gmail.com">abc@gmail.com</a></em>');

testInput = '~*_abc@gmail.com_*~';
expect(parser.replace(testInput)).toBe('<del><strong><em><a href="mailto:abc@gmail.com">abc@gmail.com</a></em></strong></del>');
});

// 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 = '<del><a href="mailto:abc@gmail.com">abc@gmail.com</a><br />'
+ '<a href="mailto:def@gmail.com">def@gmail.com</a></del>';
expect(parser.replace(testInput)).toBe(result);

testInput = '*abc@gmail.com\ndef@gmail.com*';
result = '<strong><a href="mailto:abc@gmail.com">abc@gmail.com</a><br />'
+ '<a href="mailto:def@gmail.com">def@gmail.com</a></strong>';
expect(parser.replace(testInput)).toBe(result);

testInput = '_abc@gmail.com\ndef@gmail.com_';
result = '<em><a href="mailto:abc@gmail.com">abc@gmail.com</a><br />'
+ '<a href="mailto:def@gmail.com">def@gmail.com</a></em>';
expect(parser.replace(testInput)).toBe(result);

testInput = '~*_abc@gmail.com\ndef@gmail.com_*~';
result = '<del><strong><em><a href="mailto:abc@gmail.com">abc@gmail.com</a><br />'
+ '<a href="mailto:def@gmail.com">def@gmail.com</a></em></strong></del>';
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)';
Expand Down
5 changes: 3 additions & 2 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ 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.
*/
{
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}<a href="mailto:${g3}">${g3}</a>${g2}${g1}`,
regex: /(?![^<]*>|[^<>]*<\\)([a-zA-Z0-9.!#$%&'+/=?^`{|}-][a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]*@[a-zA-Z0-9-]+?(\.[a-zA-Z]+)+)(?![^<]*(<\/pre>|<\/code>|<\/a>))/gim,
replacement: '<a href="mailto:$1">$1</a>',
},

/**
Expand Down

0 comments on commit 69e32de

Please sign in to comment.