Skip to content

Commit

Permalink
Merge pull request #550 from tienifr/fix/move-autoEmail-to-before-men…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
cead22 authored Jul 12, 2023
2 parents 0c685f8 + 3961539 commit d7e4249
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
60 changes: 60 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,3 +1063,63 @@ test('Skip rendering invalid markdown',() => {
testString = '> *This is multiline\nbold text*';
expect(parser.replace(testString)).toBe('<blockquote>*This is multiline</blockquote>bold text*');
});

test('Test for email with [email protected]@gmail.com', () => {
const testString = '[email protected]@gmail.com';
const resultString = '<a href=\"mailto:[email protected]\">[email protected]</a>@gmail.com';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for email with [email protected]@gmail.com', () => {
const testString = '[email protected]@gmail.com';
const resultString = '<a href=\"mailto:[email protected]\">[email protected]</a>@gmail.com';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for email with test@[email protected]', () => {
const testString = 'test@[email protected]';
const resultString = 'test@<a href=\"mailto:[email protected]\">[email protected]</a>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for email with test+1@[email protected]', () => {
const testString = 'test+1@[email protected]';
const resultString = 'test+1@<a href=\"mailto:[email protected]\">[email protected]</a>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test user mention with @[email protected]', () => {
const testString = '@[email protected]';
const resultString = '<mention-user>@[email protected]</mention-user>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test user mention with @[email protected]', () => {
const testString = '@[email protected]';
const resultString = '<mention-user>@[email protected]</mention-user>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test user mention with @[email protected]', () => {
const testString = '@[email protected]';
const resultString = '<mention-user>@[email protected]</mention-user>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test here mention with @here_@here_.com', () => {
const testString = '@here_@here_.com';
const resultString = '<mention-here>@here</mention-here><em><mention-here>@here</mention-here></em>.com';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test here mention with @here@', () => {
const testString = '@here@';
const resultString = '<mention-here>@here</mention-here>@';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test here mention with @here@here', () => {
const testString = '@here@here';
const resultString = '<mention-here>@here</mention-here><mention-here>@here</mention-here>';
expect(parser.replace(testString)).toBe(resultString);
});
1 change: 0 additions & 1 deletion __tests__/Str-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ describe('Str.isValidMention', () => {
expect(Str.isValidMention('*@[email protected]*')).toBeTruthy();
expect(Str.isValidMention(' @[email protected]')).toBeTruthy();
expect(Str.isValidMention('~@[email protected]~')).toBeTruthy();
expect(Str.isValidMention('#@[email protected]')).toBeTruthy();
expect(Str.isValidMention('_@[email protected]_')).toBeTruthy();
expect(Str.isValidMention('`@[email protected]`')).toBeFalsy();
expect(Str.isValidMention('\'@[email protected]\'')).toBeTruthy();
Expand Down
8 changes: 4 additions & 4 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ export default class ExpensiMark {
*/
{
name: 'hereMentions',
regex: /[`.a-zA-Z]?@here(?=_\b|\b)(?!((?:(?!<a).)+)?<\/a>|[^<]*(<\/pre>|<\/code>))/gm,
replacement: (match) => {
regex: /([a-zA-Z0-9.!$%&+/=?^`{|}_-]?)(@here)([a-zA-Z0-9.!$%&+/=?^`{|}_-]?)(?=\b)(?!(@[a-zA-Z0-9-]+?(\.[a-zA-Z]+)+)|((?:(?!<a).)+)?<\/a>|[^<]*(<\/pre>|<\/code>))/gm,
replacement: (match, g1, g2, g3) => {
if (!Str.isValidMention(match)) {
return match;
}
return `<mention-here>${match}</mention-here>`;
return `${g1}<mention-here>${g2}</mention-here>${g3}`;
},
},

Expand All @@ -110,7 +110,7 @@ export default class ExpensiMark {
*/
{
name: 'userMentions',
regex: new RegExp(`[\`.a-zA-Z]?@+${CONST.REG_EXP.EMAIL_PART}(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>))`, 'gm'),
regex: new RegExp(`[a-zA-Z0-9.!$%&+/=?^\`{|}-]?@+${CONST.REG_EXP.EMAIL_PART}(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>))`, 'gm'),
replacement: (match) => {
if (!Str.isValidMention(match)) {
return match;
Expand Down
12 changes: 10 additions & 2 deletions lib/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,16 @@ const Str = {
* @returns {bool}
*/
isValidMention(mention) {
// A valid mention starts with a space, *, _, #, ', ", or @ (with no preceding characters).
return /[\s*~_#'"@]/g.test(mention.charAt(0));
// Mentions can start @ proceeded by a space, eg "ping @[email protected]"
if (/[\s@]/g.test(mention.charAt(0))) {
return true;
}

// Mentions can also start and end with a *, _, ~, ', or " (with no other preceding characters)
// eg "ping *@[email protected]*"
const firstChar = mention.charAt(0);
const lastChar = mention.charAt(mention.length - 1);
return /[*~_'"]/g.test(firstChar) && /[*~_'"]/g.test(lastChar) && firstChar === lastChar;
},

/**
Expand Down

0 comments on commit d7e4249

Please sign in to comment.