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 Jan 7, 2025
1 parent 569c1e5 commit 3c69bbe
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 @@ -1324,6 +1325,16 @@ test('Test for user mention with @username@domain.com', () => {
const resultString = '<mention-user>@username@expensify.com</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 @username@domain.com', () => {
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';
Expand Down Expand Up @@ -1376,7 +1387,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 @@ -417,6 +417,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 3c69bbe

Please sign in to comment.