Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with parsing links containing mailto: prefix followed by an email #604

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ test('Test markdown style email link with various styles', () => {
+ '[ Expensify ]([email protected])' // edge spaces in []
+ '[ Expensify Email ]([email protected])' // space between words in []
+ '[[email protected]]([email protected])' // same email between [] and ()
+ '[[email protected]](mailto:[email protected])' // same email between [] and () with mailto: in href
+ '[mailto:[email protected]]([email protected])' // same email between [] and () with mailto: in text
+ '[mailto:[email protected]](mailto:[email protected])' // same email between [] and () with mailto: in text and href
+ '[[email protected]]([email protected])' // different emails between [] and ()
+ '[(Expensify)]([email protected])' // () in []
+ '[Expensify [Test]([email protected]) Test]([email protected])' // []() in []
Expand All @@ -839,6 +842,9 @@ test('Test markdown style email link with various styles', () => {
+ '<a href="mailto:[email protected]">Expensify</a>'
+ '<a href="mailto:[email protected]">Expensify Email</a>'
+ '<a href="mailto:[email protected]">[email protected]</a>'
+ '<a href="mailto:[email protected]">[email protected]</a>'
+ '<a href="mailto:[email protected]">[email protected]</a>'
+ '<a href="mailto:[email protected]">[email protected]</a>'
+ '<a href="mailto:[email protected]">[email protected]</a>'
+ '<a href="mailto:[email protected]">(Expensify)</a>'
+ '[Expensify <a href="mailto:[email protected]">Test</a> Test](<a href="mailto:[email protected]">[email protected]</a>)'
Expand Down
9 changes: 6 additions & 3 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,18 @@ export default class ExpensiMark {
name: 'email',
process: (textToProcess, replacement) => {
const regex = new RegExp(
`(?!\\[\\s*\\])\\[([^[\\]]*)]\\(${CONST.REG_EXP.MARKDOWN_EMAIL}\\)`, 'gim'
`(?!\\[\\s*\\])\\[([^[\\]]*)]\\((mailto:)?${CONST.REG_EXP.MARKDOWN_EMAIL}\\)`, 'gim'
);
return this.modifyTextForEmailLinks(regex, textToProcess, replacement);
},
replacement: (match, g1, g2) => {
if (g1.match(CONST.REG_EXP.EMOJIS) || !g1.trim()) {
return match;
}
return `<a href="mailto:${g2}">${g1.trim()}</a>`;
const label = g1.trim();
const href = `mailto:${g2}`;
const formattedLabel = label === href ? g2 : label;
return `<a href="${href}">${formattedLabel}</a>`;
},
},

Expand Down Expand Up @@ -572,7 +575,7 @@ export default class ExpensiMark {
filterRules: ['bold', 'strikethrough', 'italic'],
shouldEscapeText: false,
});
replacedText = replacedText.concat(replacement(match[0], linkText, match[2]));
replacedText = replacedText.concat(replacement(match[0], linkText, match[3]));
startIndex = match.index + (match[0].length);

// Now we move to the next match that the js regex found in the text
Expand Down
Loading