Skip to content

Commit

Permalink
Add handling possible short mentions tags to ExpensiMark
Browse files Browse the repository at this point in the history
  • Loading branch information
Kicu committed Dec 21, 2024
1 parent 67c905c commit 275cc20
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
extends: ['expensify', 'prettier'],
parser: '@typescript-eslint/parser',
env: {
jest: true,
},
overrides: [
{
files: ['*.js', '*.jsx'],
Expand Down
15 changes: 13 additions & 2 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable max-len */
/* eslint-disable max-len,no-useless-concat */
import ExpensiMark from '../lib/ExpensiMark';

const parser = new ExpensiMark();
Expand Down Expand Up @@ -931,6 +931,7 @@ test('Test urls autolinks correctly', () => {
},
];

// Fixme @expensify.com is a now correct "possible" short mention
testCases.forEach((testCase) => {
expect(parser.replace(testCase.testString)).toBe(testCase.resultString);
});
Expand Down Expand Up @@ -1320,6 +1321,16 @@ test('Test for user mention with @[email protected]', () => {
const resultString = '<mention-user>@[email protected]</mention-user>';
expect(parser.replace(testString)).toBe(resultString);
});
// Todo
// popraw psujący się @here testy
// Todo wszystkie edge kejsy w których short mention jest ambiguous
// short-mention -> possible-short-mention

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

test('Test for user mention with @@[email protected]', () => {
const testString = '@@[email protected]';
Expand Down Expand Up @@ -1372,7 +1383,7 @@ test('Test for user mention without leading whitespace', () => {

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

Expand Down
23 changes: 23 additions & 0 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,29 @@ export default class ExpensiMark {
},
},

{
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>))",
'gim',
),
replacement: (_extras, match, g1, g2) => {
if (!Str.isValidMention(match)) {
return match;
}
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: 'quote',

Expand Down

0 comments on commit 275cc20

Please sign in to comment.