Skip to content

Commit

Permalink
fix(message-parser): User mention with @ character (#894)
Browse files Browse the repository at this point in the history
* Fix user mention with email pattern and add tests

* adding languages support to mention and channel parser

Co-authored-by: gabriellsh <[email protected]>
  • Loading branch information
hugocostadev and gabriellsh authored Nov 10, 2022
1 parent 25419be commit aa9bc06
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,20 @@ anyText
/ [\x61-\x7A] // a b c d e f g h i j k l m n o p q r s t u v w x y z
/ nonascii

utf8_names_validation = $[0-9a-zA-Z-_.]+
utf8_names_validation = $([-_.] / alphaChar / decimalNumberChar)+

matrix_server_validation = ":" utf8_names_validation
username_matrix_server_validation = ":" utf8_names_validation

username_email_validation = "@" utf8_names_validation

UserMention
= t:Text "@"+ user:utf8_names_validation {
return reducePlainTexts([t, plain('@' + user)])[0];
}
/ "@"+ user:$(utf8_names_validation matrix_server_validation) {
/ "@"+ user:$(utf8_names_validation username_matrix_server_validation) {
return mentionUser(user);
}
/ "@"+ user:$(utf8_names_validation username_email_validation) {
return mentionUser(user);
}
/ "@"+ user:utf8_names_validation { return mentionUser(user); }
Expand Down
16 changes: 16 additions & 0 deletions packages/message-parser/tests/mention.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ test.each([
'@marcos.defendi:matrix.org',
[paragraph([mentionUser('marcos.defendi:matrix.org')])],
],
['@[email protected]', [paragraph([mentionUser('[email protected]')])]],
[
'@[email protected]',
[paragraph([mentionUser('[email protected]')])],
],
['@téstãçâò', [paragraph([mentionUser('téstãçâò')])]],
['@สมชาย', [paragraph([mentionUser('สมชาย')])]],
['@李祖阳', [paragraph([mentionUser('李祖阳')])]],
['@あおい', [paragraph([mentionUser('あおい')])]],
['@アオイ', [paragraph([mentionUser('アオイ')])]],
['@Владимир', [paragraph([mentionUser('Владимир')])]],
['@Кириллица', [paragraph([mentionUser('Кириллица')])]],
[
'test @Кириллица test',
[paragraph([plain('test '), mentionUser('Кириллица'), plain(' test')])],
],
])('parses %p', (input, output) => {
expect(parse(input)).toMatchObject(output);
});

0 comments on commit aa9bc06

Please sign in to comment.