Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('getUserMentionRegex', () => {
] as User[];
const templatePrefix = '@';
const result = getUserMentionRegex(mentionedUsers, templatePrefix);
expect(result).toEqual(/(@{1}|@{2})/g);
expect(result).toEqual(/(@\{1\}|@\{2\})/g);
});

it('should return a correct regex pattern; userId includes some patterns need to be escaped', () => {
Expand All @@ -26,7 +26,7 @@ describe('getUserMentionRegex', () => {
] as User[];
const templatePrefix = '@';
const result = getUserMentionRegex(mentionedUsers, templatePrefix);
expect(result).toEqual(/(@{1\*}|@{2\+})/g);
expect(result).toEqual(/(@\{1\*\}|@\{2\+\})/g);
});
});

Expand Down
8 changes: 6 additions & 2 deletions src/modules/Message/utils/tokens/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ export function getUserMentionRegex(mentionedUsers: User[], templatePrefix_: str
// If user.id includes these patterns, need to convert it into an escaped one
/([.*+?^${}()|[\]\\])/g,
'\\$1');
return `${templatePrefix}{${userId}}`;
/**
* //{ And //} are also for escaping
* because curly braces `{}` are metacharacters in regular expressions used to specify repetition
*/
return `${templatePrefix}\\{${userId}\\}`;
}).join('|')})`, 'g');
}

export function identifyMentions({
tokens,
mentionedUsers = [],
templatePrefix = USER_MENTION_PREFIX,
}: IdentifyMentionsType): (MentionToken|UndeterminedToken)[] {
}: IdentifyMentionsType): (MentionToken | UndeterminedToken)[] {
if (!mentionedUsers?.length) {
return tokens;
}
Expand Down