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

Allow emoji as link text #749

Merged
merged 4 commits into from
Jul 11, 2024
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
13 changes: 7 additions & 6 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,21 @@ test('Test markdown replacement for invalid emails', () => {

test('Test markdown replacement for emojis with emails', () => {
const testString =
'Do not replace the emoji with link ' +
'Replace the emoji with link ' +
'[😄](abc@gmail.com) ' +
'[😄]( abc@gmail.com) ' +
'[😄] abc@gmail.com ' +
'[😄]((abc@gmail.com)) ' +
'[😄abc@gmail.com](abc@gmail.com) ' +
'[😄 abc@gmail.com ](abc@gmail.com) ';
const result =
'Do not replace the emoji with link ' +
'[<emoji>😄</emoji>](<a href="mailto:abc@gmail.com">abc@gmail.com</a>) ' +
'Replace the emoji with link ' +
'<a href=\"mailto:abc@gmail.com\"><emoji>😄</emoji></a> ' +
'[<emoji>😄</emoji>]( <a href="mailto:abc@gmail.com">abc@gmail.com</a>) ' +
'[<emoji>😄</emoji>] <a href="mailto:abc@gmail.com">abc@gmail.com</a> ' +
'[<emoji>😄</emoji>]((<a href="mailto:abc@gmail.com">abc@gmail.com</a>)) ' +
'[<emoji>😄</emoji><a href="mailto:abc@gmail.com">abc@gmail.com</a>](<a href="mailto:abc@gmail.com">abc@gmail.com</a>) ' +
'[<emoji>😄</emoji> <a href="mailto:abc@gmail.com">abc@gmail.com</a> ](<a href="mailto:abc@gmail.com">abc@gmail.com</a>) ';
'<a href="mailto:abc@gmail.com"><emoji>😄</emoji>abc@gmail.com</a> ' +
'<a href="mailto:abc@gmail.com"><emoji>😄</emoji> abc@gmail.com</a> ';
expect(parser.replace(testString)).toBe(result);
});

Expand Down Expand Up @@ -1198,9 +1198,10 @@ test('Test for link with no content', () => {
expect(parser.replace(testString)).toBe(resultString);
});

// Replace the emoji with email link
test('Test for link with emoji', () => {
const testString = '[😀](www.link.com)';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const testString = '[😀](www.link.com)';
const testString =
'Replace the emoji with email link ' +
'[😀](www.link.com)';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as comment

const resultString = '[<emoji>😀</emoji>](<a href="https://www.link.com" target="_blank" rel="noreferrer noopener">www.link.com</a>)';
const resultString = '<a href="https://www.link.com" target="_blank" rel="noreferrer noopener"><emoji>😀</emoji></a>';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resultString = '<a href="https://www.link.com" target="_blank" rel="noreferrer noopener"><emoji>😀</emoji></a>';
const resultString =
'Replace the emoji with email link ' +
'<a href="https://www.link.com" target="_blank" rel="noreferrer noopener"><emoji>😀</emoji></a>';

expect(parser.replace(testString)).toBe(resultString);
});

Expand Down
8 changes: 0 additions & 8 deletions lib/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,6 @@ const CONST = {
*/
MARKDOWN_EMAIL: EMAIL_BASE_REGEX,

/**
* Regex matching an text containing an Emoji
*
* @type RegExp
*/
// eslint-disable-next-line no-misleading-character-class
EMOJIS: /[\p{Extended_Pictographic}\u200d\u{1f1e6}-\u{1f1ff}\u{1f3fb}-\u{1f3ff}\u{e0020}-\u{e007f}\u20E3\uFE0F]|[#*0-9]\uFE0F?\u20E3/gu,

/**
* Regex matching an text containing an Emoji that can be a single emoji or made up by some different emojis
*
Expand Down
8 changes: 4 additions & 4 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export default class ExpensiMark {
return this.modifyTextForEmailLinks(regex, textToProcess, replacement as ReplacementFn, shouldKeepRawInput);
},
replacement: (_extras, match, g1, g2) => {
if (g1.match(Constants.CONST.REG_EXP.EMOJIS) || !g1.trim()) {
Copy link

@rayane-djouah rayane-djouah Jul 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove CONST.REG_EXP.EMOJIS from constants file as it is not used anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (!g1.trim()) {
return match;
}
const label = g1.trim();
Expand All @@ -182,7 +182,7 @@ export default class ExpensiMark {
return `<a href="${href}">${formattedLabel}</a>`;
},
rawInputReplacement: (_extras, match, g1, g2, g3) => {
if (g1.match(Constants.CONST.REG_EXP.EMOJIS) || !g1.trim()) {
if (!g1.trim()) {
return match;
}

Expand Down Expand Up @@ -249,13 +249,13 @@ export default class ExpensiMark {
name: 'link',
process: (textToProcess, replacement) => this.modifyTextForUrlLinks(MARKDOWN_LINK_REGEX, textToProcess, replacement as ReplacementFn),
replacement: (_extras, match, g1, g2) => {
if (g1.match(Constants.CONST.REG_EXP.EMOJIS) || !g1.trim()) {
if (!g1.trim()) {
return match;
}
return `<a href="${Str.sanitizeURL(g2)}" target="_blank" rel="noreferrer noopener">${g1.trim()}</a>`;
},
rawInputReplacement: (_extras, match, g1, g2) => {
if (g1.match(Constants.CONST.REG_EXP.EMOJIS) || !g1.trim()) {
if (!g1.trim()) {
return match;
}
return `<a href="${Str.sanitizeURL(g2)}" data-raw-href="${g2}" data-link-variant="labeled" target="_blank" rel="noreferrer noopener">${g1.trim()}</a>`;
Expand Down
Loading