Skip to content

Commit

Permalink
lint, test cases of _ prefixed email inside italic
Browse files Browse the repository at this point in the history
  • Loading branch information
Antasel committed Aug 17, 2023
1 parent 19328fa commit 9ce4d11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,15 @@ test('Test emails within other markdown', () => {
+ '```test@example.com```\n'
+ '`test@example.com`\n'
+ '_test@example.com_ '
+ '_test@example.com__ '
+ '__test@example.com__ '
+ '__test@example.com_';
const result = '<blockquote><a href="mailto:test@example.com">test@example.com</a></blockquote>'
+ '<pre>test@example.com</pre>'
+ '<code>test@example.com</code><br />'
+ '<em><a href="mailto:test@example.com">test@example.com</a></em> '
+ '<em><a href="mailto:test@example.com">test@example.com</a></em>_ '
+ '<em><a href="mailto:_test@example.com">_test@example.com</a></em>_ '
+ '<em><a href="mailto:_test@example.com">_test@example.com</a></em>';
expect(parser.replace(testString)).toBe(result);
});
Expand Down
18 changes: 10 additions & 8 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ export default class ExpensiMark {
regex: /(\b_+|\b)(?!_blank")_((?![\s_])[\s\S]*?[^\s_])_(?![^\W_])(?![^<]*(<\/pre>|<\/code>|<\/a>|<\/mention-user>|_blank))/g,

// We want to add extraLeadingUnderscores back before the <em> tag unless textWithinUnderscores starts with valid email
replacement: (match, extraLeadingUnderscores, textWithinUnderscores) => (
textWithinUnderscores.includes('<pre>') || this.containsNonPairTag(textWithinUnderscores)
? match
: Boolean(String(textWithinUnderscores).match(`^${CONST.REG_EXP.MARKDOWN_EMAIL}`))
? `<em>${extraLeadingUnderscores}${textWithinUnderscores}</em>`
: `${extraLeadingUnderscores}<em>${textWithinUnderscores}</em>`
),
replacement: (match, extraLeadingUnderscores, textWithinUnderscores) => {
if (textWithinUnderscores.includes('<pre>') || this.containsNonPairTag(textWithinUnderscores)) {
return match;
}
if (String(textWithinUnderscores).match(`^${CONST.REG_EXP.MARKDOWN_EMAIL}`)) {
return `<em>${extraLeadingUnderscores}${textWithinUnderscores}</em>`;
}
return `${extraLeadingUnderscores}<em>${textWithinUnderscores}</em>`;
},
},

/**
Expand All @@ -175,7 +177,7 @@ export default class ExpensiMark {
{
name: 'autoEmail',
regex: new RegExp(
`(?![^<]*>|[^<>]*<\\/(?!em))(?:(?<= |[^\\w'#%+-])|^|\\b)${CONST.REG_EXP.MARKDOWN_EMAIL}(?!((?:(?!<a).)+)?<\/a>|[^<]*(<\/pre>|<\/code>))`,
`(?![^<]*>|[^<>]*<\\/(?!em))(?:(?<= |[^\\w'#%+-])|^|\\b)${CONST.REG_EXP.MARKDOWN_EMAIL}(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>))`,
'gim',
),
replacement: '<a href="mailto:$1">$1</a>',
Expand Down

0 comments on commit 9ce4d11

Please sign in to comment.