diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index b18aecca..3266ce07 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -693,11 +693,10 @@ test('Test url replacements', () => { 'http://example.com/foo/*/bar/*/test.txt ' + 'test-.com ' + '-test.com ' + - '@test.com ' + - '@test.com test.com ' + - '@test.com @test.com '; + '@test.com ' + + '@test.com test.com ' + + '@test.com @test.com '; - // Fixme [short-mention] this errors on "test.com @test.com @test.com { { testString: 'expensify.com -expensify.com @expensify.com', resultString: - 'expensify.com -expensify.com @expensify.com', + 'expensify.com -expensify.com @expensify.com', }, { testString: 'https//www.expensify.com', @@ -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); }); @@ -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 = '@john.doe'; - expect(parser.replace(testString)).toBe(resultString); -}); - test('Test for user mention with @@username@domain.com', () => { const testString = '@@username@expensify.com'; const resultString = '@@username@expensify.com'; @@ -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 = '@johnny'; + expect(parser.replace(testString)).toBe(resultString); + }); + + test('short mentions should work for @firstname.lastname', () => { + const testString = '@john.doe'; + const resultString = '@john.doe'; + expect(parser.replace(testString)).toBe(resultString); + }); + + test('short mentions should work and not break @here after mention', () => { + const testString = '@john.doe@here'; + const resultString = '@john.doe@here'; + expect(parser.replace(testString)).toBe(resultString); + }); +}); + // Examples that should match for here mentions: test('Test for here mention with @here', () => { const testString = '@here'; @@ -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 = '@here' + ' @here' + @@ -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 = 'test+1@gmail.com@gmail.com'; + const resultString = 'test+1@gmail.com@gmail.com'; 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 = 'test@gmail.com@gmail.com'; + const resultString = 'test@gmail.com@gmail.com'; expect(parser.replace(testString)).toBe(resultString); }); diff --git a/lib/CONST.ts b/lib/CONST.ts index 431b7b4b..7c920706 100644 --- a/lib/CONST.ts +++ b/lib/CONST.ts @@ -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: { diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index d468f936..543c0f50 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -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})(?!((?:(?!|[^<]*(<\\/pre>|<\\/code>))`, + `${Constants.CONST.REG_EXP.PRE_MENTION_TEXT_PART}(@${Constants.CONST.REG_EXP.EMAIL_PART}|@${Constants.CONST.REG_EXP.PHONE_PART})(?!((?:(?!|[^<]*(<\\/pre>|<\\/code>))`, 'gim', ), replacement: (_extras, match, g1, g2) => { @@ -484,11 +484,25 @@ export default class ExpensiMark { rawInputReplacement: '$1$2', }, + /** + * 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))(?!([:\\/\\\\]))(?.*))\\S{3,254}(?=\\k$))(?!((?:(?!|[^<]*(<\\/pre>|<\\/code>|<\\/mention-user>|<\\/mention-here>))", + `${Constants.CONST.REG_EXP.PRE_MENTION_TEXT_PART}(@(?=((?=[\\w]+[\\w'#%+-]+(?:\\.[\\w'#%+-]+)*)[\\w\\.'#%+-]{1,64}(?= |_|\\b))(?!([:\\/\\\\]))(?.*))(?!here)\\S{3,254}(?=\\k$))(?!((?:(?!|[^<]*(<\\/pre>|<\\/code>|<\\/mention-user>|<\\/mention-here>))`, 'gim', ), replacement: (_extras, match, g1, g2) => { @@ -497,15 +511,12 @@ export default class ExpensiMark { } return `${g1}${g2}`; }, - // 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}${g2}`; - // }, + }, + + { + name: 'hereMentionAfterShortMentions', + regex: /(<\/mention-short>)(@here)(?=\b)/gm, + replacement: '$1$2', }, {