Skip to content

Commit

Permalink
Improve short mentions regex and make parser tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Kicu committed Jan 8, 2025
1 parent bb4cc97 commit f7e27c7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
41 changes: 26 additions & 15 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,10 @@ test('Test url replacements', () => {
'<a href="http://example.com/foo/*/bar/*/test.txt" target="_blank" rel="noreferrer noopener">http://example.com/foo/*/bar/*/test.txt</a> ' +
'test-.com ' +
'-<a href="https://test.com" target="_blank" rel="noreferrer noopener">test.com</a> ' +
'@test.com ' +
'@test.com <a href="https://test.com" target="_blank" rel="noreferrer noopener">test.com</a> ' +
'@test.com @test.com ';
'<mention-short>@test.com</mention-short> ' +
'<mention-short>@test.com</mention-short> <a href="https://test.com" target="_blank" rel="noreferrer noopener">test.com</a> ' +
'<mention-short>@test.com</mention-short> <mention-short>@test.com</mention-short> ';

// Fixme [short-mention] this errors on "test.com</a> @test.com @test.com <a" - probably @test.com should be a legit short-mention candidate
expect(parser.replace(urlTestStartString)).toBe(urlTestReplacedString);
});

Expand Down Expand Up @@ -877,7 +876,7 @@ test('Test urls autolinks correctly', () => {
{
testString: 'expensify.com -expensify.com @expensify.com',
resultString:
'<a href="https://expensify.com" target="_blank" rel="noreferrer noopener">expensify.com</a> -<a href="https://expensify.com" target="_blank" rel="noreferrer noopener">expensify.com</a> @expensify.com',
'<a href="https://expensify.com" target="_blank" rel="noreferrer noopener">expensify.com</a> -<a href="https://expensify.com" target="_blank" rel="noreferrer noopener">expensify.com</a> <mention-short>@expensify.com</mention-short>',
},
{
testString: 'https//www.expensify.com',
Expand Down Expand Up @@ -932,7 +931,6 @@ test('Test urls autolinks correctly', () => {
},
];

// Fixme [short-mention] @expensify.com should now be considered a short-mention "candidate"
testCases.forEach((testCase) => {
expect(parser.replace(testCase.testString)).toBe(testCase.resultString);
});
Expand Down Expand Up @@ -1327,12 +1325,6 @@ test('Test for user mention with @username@domain.com', () => {
expect(parser.replace(testString)).toBe(resultString);
});

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

test('Test for user mention with @@username@domain.com', () => {
const testString = '@@username@expensify.com';
const resultString = '@<mention-user>@username@expensify.com</mention-user>';
Expand Down Expand Up @@ -1460,6 +1452,26 @@ test('Test for @here mention with inlineCodeBlock style', () => {
expect(parser.replace(testString)).toBe(resultString);
});

describe('Tests for short mentions', () => {
test('short mentions should work for @username', () => {
const testString = '@johnny';
const resultString = '<mention-short>@johnny</mention-short>';
expect(parser.replace(testString)).toBe(resultString);
});

test('short mentions should work for @firstname.lastname', () => {
const testString = '@john.doe';
const resultString = '<mention-short>@john.doe</mention-short>';
expect(parser.replace(testString)).toBe(resultString);
});

test('short mentions should work and not break @here after mention', () => {
const testString = '@john.doe@here';
const resultString = '<mention-short>@john.doe</mention-short><mention-here>@here</mention-here>';
expect(parser.replace(testString)).toBe(resultString);
});
});

// Examples that should match for here mentions:
test('Test for here mention with @here', () => {
const testString = '@here';
Expand Down Expand Up @@ -1516,7 +1528,6 @@ test('Test for @here mention with italic, bold and strikethrough styles', () =>
' @here!' +
' @here?';

// Fixme [short-mention] these should now be short-mention candidates
const resultString =
'<mention-here>@here</mention-here>' +
' <em><mention-here>@here</mention-here></em>' +
Expand Down Expand Up @@ -1659,13 +1670,13 @@ test('Skip rendering invalid markdown', () => {

test('Test for email with test+1@gmail.com@gmail.com', () => {
const testString = 'test+1@gmail.com@gmail.com';
const resultString = '<a href="mailto:test+1@gmail.com">test+1@gmail.com</a>@gmail.com';
const resultString = '<a href="mailto:test+1@gmail.com">test+1@gmail.com</a><mention-short>@gmail.com</mention-short>';
expect(parser.replace(testString)).toBe(resultString);
});

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

Expand Down
5 changes: 5 additions & 0 deletions lib/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ const CONST = {
*/
EMOJI_RULE:
/[\p{Extended_Pictographic}](\u200D[\p{Extended_Pictographic}]|[\u{1F3FB}-\u{1F3FF}]|[\u{E0020}-\u{E007F}]|\uFE0F|\u20E3)*|[\u{1F1E6}-\u{1F1FF}]{2}|[#*0-9]\uFE0F?\u20E3/gu,

/**
* Regex to match a piece of text or @here, needed for both shortMention and userMention
*/
PRE_MENTION_TEXT_PART: '(@here|[a-zA-Z0-9.!$%&+=?^\\`{|}-]?)',
},

REPORT: {
Expand Down
33 changes: 22 additions & 11 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export default class ExpensiMark {
{
name: 'userMentions',
regex: new RegExp(
`(@here|[a-zA-Z0-9.!$%&+=?^\`{|}-]?)(@${Constants.CONST.REG_EXP.EMAIL_PART}|@${Constants.CONST.REG_EXP.PHONE_PART})(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>))`,
`${Constants.CONST.REG_EXP.PRE_MENTION_TEXT_PART}(@${Constants.CONST.REG_EXP.EMAIL_PART}|@${Constants.CONST.REG_EXP.PHONE_PART})(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>))`,
'gim',
),
replacement: (_extras, match, g1, g2) => {
Expand Down Expand Up @@ -484,11 +484,25 @@ export default class ExpensiMark {
rawInputReplacement: '$1<a href="mailto:$2" data-raw-href="$2" data-link-variant="auto">$2</a>',
},

/**
* This regex matches a short user mention in a string.
* A short-mention is a string that starts with the '@' symbol and is followed by a valid user's primary login without the email domain part
* Ex: @john.doe, @user12345, but NOT @user@email.com
*
* Notes:
* Phone is not a valid short mention.
* In reality these "short-mentions" are just possible candidates, because the parser has no way of verifying if there exists a user named ex: @john.examplename.
* The actual verification whether these mentions are pointing to real users is done in specific projects using ExpensiMark.
* Nevertheless, "@john.examplename" is a correct possible short-mention, and so would be parsed.
* This behaviour is similar to treating every user@something as valid user login.
*
* This regex will correctly preserve any @here mentions, the same way as "userMention" rule.
*/
{
name: 'shortMentions',

regex: new RegExp(
"(@here|[a-zA-Z0-9.!$%&+=?^\\`{|}-]?)(@(?=((?=[\\w]+[\\w'#%+-]+(?:\\.[\\w'#%+-]+)*)[\\w\\.'#%+-]{1,64}(?= |_|\\b))(?!([:\\/\\\\]))(?<end>.*))\\S{3,254}(?=\\k<end>$))(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>|<\\/mention-user>|<\\/mention-here>))",
`${Constants.CONST.REG_EXP.PRE_MENTION_TEXT_PART}(@(?=((?=[\\w]+[\\w'#%+-]+(?:\\.[\\w'#%+-]+)*)[\\w\\.'#%+-]{1,64}(?= |_|\\b))(?!([:\\/\\\\]))(?<end>.*))(?!here)\\S{3,254}(?=\\k<end>$))(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>|<\\/mention-user>|<\\/mention-here>))`,
'gim',
),
replacement: (_extras, match, g1, g2) => {
Expand All @@ -497,15 +511,12 @@ export default class ExpensiMark {
}
return `${g1}<mention-short>${g2}</mention-short>`;
},
// rawInputReplacement: (_extras, match, g1, g2) => {
// const phoneNumberRegex = new RegExp(`^${Constants.CONST.REG_EXP.PHONE_PART}$`);
// const mention = g2.slice(1);
// const mentionWithoutSMSDomain = str_1.default.removeSMSDomain(mention);
// if (!str_1.default.isValidMention(match) || (phoneNumberRegex.test(mentionWithoutSMSDomain) && !str_1.default.isValidPhoneNumber(mentionWithoutSMSDomain))) {
// return match;
// }
// return `${g1}<mention-user>${g2}</mention-user>`;
// },
},

{
name: 'hereMentionAfterShortMentions',
regex: /(<\/mention-short>)(@here)(?=\b)/gm,
replacement: '$1<mention-here>$2</mention-here>',
},

{
Expand Down

0 comments on commit f7e27c7

Please sign in to comment.