Skip to content

Commit d1d5337

Browse files
fix(message-parser): Link/URL parser issues (#855)
* feat(fuselage): Adds wordbreak in Box Component (#853) * Fixing URL/links parsing issues * restoring files from base branch * replacing backstick with single quotes Co-authored-by: Douglas Fabris <[email protected]>
1 parent cfd5803 commit d1d5337

11 files changed

+231
-135
lines changed

packages/message-parser/src/definitions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export type Link = {
138138
type: 'LINK';
139139
value: {
140140
src: Plain;
141-
label: Markup;
141+
label: Markup[];
142142
};
143143
};
144144

packages/message-parser/src/grammar.pegjs

+28-58
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,12 @@ Space
171171
/ "\t"
172172

173173
anyText
174-
= [\x20-\x27] /* ! " # $ % & ' ( ) */
174+
= [\x20-\x27] // ! " # $ % & '
175175
/ [\x2B-\x40] // + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @
176176
/ [\x41-\x5A] // 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
177177
/ [\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
178178
/ nonascii
179179

180-
SectionText
181-
= [-]+
182-
/ [\x20-\x40] // ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @
183-
/ [\x41-\x60] // 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 [ \ ] ^ _ `
184-
/ [\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
185-
/ nonascii
186-
187180
utf8_names_validation = $[0-9a-zA-Z-_.]+
188181

189182
matrix_server_validation = ":" utf8_names_validation
@@ -283,9 +276,9 @@ LinkTitle2
283276

284277
References
285278
= "[](" href:LinkRef ")" { return link(href); }
286-
/ "[" title:LinkTitle "](" href:LinkRef ")" { return link(href, title); }
279+
/ "[" title:LinkTitle* "](" href:LinkRef ")" { return link(href, title); }
287280
/ "<" href:LinkRef "|" title:LinkTitle2 ">" {
288-
return link(href, plain(title));
281+
return link(href, [plain(title)]);
289282
}
290283

291284
/* Macros */
@@ -303,32 +296,11 @@ escape
303296
= unicode
304297
/ "\\" ch:[^\r\n\f0-9a-f]i { return ch; }
305298

306-
nmstart
307-
= [_a-z]i
308-
/ nonascii
309-
/ escape
310-
311-
nmchar
312-
= [_a-z0-9-]i
313-
/ nonascii
314-
/ escape
315-
316-
string1
317-
= "\"" chars:$([^\n\r\f\\"] / "\\" nl:nl { return ''; } / escape)* "\"" {
318-
return chars;
319-
}
320-
321-
nl
322-
= "\n"
323-
/ "\r\n"
324-
/ "\r"
325-
/ "\f"
326-
327-
AutolinkedPhone = p:Phone { return link('tel:' + p.number, plain(p.text)); }
299+
AutolinkedPhone = p:Phone { return link('tel:' + p.number, [plain(p.text)]); }
328300

329301
AutolinkedURL = u:URL { return link(u); }
330302

331-
AutolinkedEmail = e:Email { return link('mailto:' + e, plain(e)); }
303+
AutolinkedEmail = e:Email { return link('mailto:' + e, [plain(e)]); }
332304

333305
alpha = [a-zA-Z]
334306

@@ -377,11 +349,11 @@ hexByte = a:hexdigit b:hexdigit { return parseInt(a + b, 16); }
377349

378350
domainName
379351
= "localhost"
380-
/ $(domainNameLabel ("." domainChar domainNameLabel)+)
352+
/ $(domainNameLabel ("." (!digit domainChar) domainNameLabel)+)
381353

382354
domainNameLabel = $(domainChar+ $("-" domainChar+)*)
383355

384-
domainChar = !"\\" !"/" !"|" !">" !"<" !safe !extra !EndOfLine !Space .
356+
domainChar = !"\\" !"/" !"|" !">" !"<" !"%" !safe !extra !EndOfLine !Space .
385357

386358
/**
387359
*
@@ -411,23 +383,8 @@ phonePrefix
411383
*/
412384

413385
URL
414-
= $(
415-
s:urlScheme
416-
a:urlAuthority
417-
p:urlPath?
418-
q:urlQuery?
419-
f:urlFragment?
420-
g:urlPath?
421-
h:urlQuery?
422-
)
423-
/ $(
424-
urlAuthorityHost
425-
p:urlPath?
426-
q:urlQuery?
427-
f:urlFragment?
428-
g:urlPath?
429-
h:urlQuery?
430-
)
386+
= $(urlScheme urlAuthority urlBody)
387+
/ $(urlAuthorityHost urlBody)
431388

432389
urlScheme
433390
= $(
@@ -466,6 +423,25 @@ urlScheme
466423
":"
467424
)
468425

426+
urlBody
427+
= (
428+
!Whitespace
429+
(
430+
anyText
431+
/ "*"
432+
/ "["
433+
/ "\/"
434+
/ "]"
435+
/ "^"
436+
/ "_"
437+
/ "`"
438+
/ "{"
439+
/ "}"
440+
/ "~"
441+
/ "("
442+
)
443+
)*
444+
469445
urlAuthority = $("//" urlAuthorityUserInfo? urlAuthorityHost)
470446

471447
urlAuthorityUserInfo = $(urlAuthorityUser (":" urlAuthorityPassword)? "@")
@@ -483,12 +459,6 @@ urlAuthorityHostName
483459
urlAuthorityPort
484460
= digits // TODO: from "0" to "65535"
485461

486-
urlPath = $("/" $(!"?" !"/" !"#" !")" !">" !"|" !" " .)* urlPath*)
487-
488-
urlQuery = $("?" $(alpha_digit / safe)*)
489-
490-
urlFragment = $("#" $(alpha_digit / extra / safe)*)
491-
492462
/**
493463
*
494464
* Email

packages/message-parser/src/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ const isValidLink = (link: string) => {
7878
export const link = (() => {
7979
const fn = generate('LINK');
8080

81-
return (src: string, label?: Markup) => {
81+
return (src: string, label?: Markup[]) => {
8282
const href = isValidLink(src) || src.startsWith('//') ? src : `//${src}`;
8383

84-
return fn({ src: plain(href), label: label || plain(src) });
84+
return fn({ src: plain(href), label: label || [plain(src)] });
8585
};
8686
})();
8787

packages/message-parser/tests/email.test.ts

+17-18
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { link, paragraph, plain } from '../src/utils';
44
test.each([
55
[
66
7-
[paragraph([link('mailto:[email protected]', plain('[email protected]'))])],
7+
[paragraph([link('mailto:[email protected]', [plain('[email protected]')])])],
88
],
99
[
1010
"[email protected] is Joe's email",
1111
[
1212
paragraph([
13-
link('mailto:[email protected]', plain('[email protected]')),
13+
link('mailto:[email protected]', [plain('[email protected]')]),
1414
plain(" is Joe's email"),
1515
]),
1616
],
@@ -19,7 +19,7 @@ test.each([
1919
"[email protected] is Joe's email",
2020
[
2121
paragraph([
22-
link('mailto:[email protected]', plain('[email protected]')),
22+
link('mailto:[email protected]', [plain('[email protected]')]),
2323
plain(" is Joe's email"),
2424
]),
2525
],
@@ -29,7 +29,7 @@ test.each([
2929
[
3030
paragraph([
3131
plain("Joe's email is "),
32-
link('mailto:[email protected]', plain('[email protected]')),
32+
link('mailto:[email protected]', [plain('[email protected]')]),
3333
plain(' because it is'),
3434
]),
3535
],
@@ -39,7 +39,7 @@ test.each([
3939
[
4040
paragraph([
4141
plain("Joe's email is "),
42-
link('mailto:[email protected]', plain('[email protected]')),
42+
link('mailto:[email protected]', [plain('[email protected]')]),
4343
]),
4444
],
4545
],
@@ -48,7 +48,7 @@ test.each([
4848
[
4949
paragraph([
5050
plain("Joe's email is "),
51-
link('mailto:[email protected]', plain('[email protected]')),
51+
link('mailto:[email protected]', [plain('[email protected]')]),
5252
plain('. Try emailing him'),
5353
]),
5454
],
@@ -58,7 +58,7 @@ test.each([
5858
[
5959
paragraph([
6060
plain("Joe's email is "),
61-
link('mailto:[email protected]', plain('[email protected]')),
61+
link('mailto:[email protected]', [plain('[email protected]')]),
6262
]),
6363
],
6464
],
@@ -67,7 +67,7 @@ test.each([
6767
[
6868
paragraph([
6969
plain("Joe's email is "),
70-
link('mailto:[email protected]', plain('[email protected]')),
70+
link('mailto:[email protected]', [plain('[email protected]')]),
7171
]),
7272
],
7373
],
@@ -76,7 +76,7 @@ test.each([
7676
[
7777
paragraph([
7878
plain("Joe's email is ("),
79-
link('mailto:[email protected]', plain('[email protected]')),
79+
link('mailto:[email protected]', [plain('[email protected]')]),
8080
plain(')'),
8181
]),
8282
],
@@ -86,7 +86,7 @@ test.each([
8686
[
8787
paragraph([
8888
plain("Joe's email is ("),
89-
link('mailto:[email protected]', plain('[email protected]')),
89+
link('mailto:[email protected]', [plain('[email protected]')]),
9090
plain(')'),
9191
]),
9292
],
@@ -96,7 +96,7 @@ test.each([
9696
[
9797
paragraph([
9898
plain("Joe's email is ("),
99-
link("mailto:joe'[email protected]", plain("joe'[email protected]")),
99+
link("mailto:joe'[email protected]", [plain("joe'[email protected]")]),
100100
plain(')'),
101101
]),
102102
],
@@ -106,7 +106,7 @@ test.each([
106106
[
107107
paragraph([
108108
plain("Joe's email is "),
109-
link('mailto:mañana@mañana.com', plain('mañana@mañana.com')),
109+
link('mailto:mañana@mañana.com', [plain('mañana@mañana.com')]),
110110
]),
111111
],
112112
],
@@ -115,10 +115,9 @@ test.each([
115115
[
116116
paragraph([
117117
plain("Joe's email is "),
118-
link(
119-
'mailto:Кириллица@Кириллица.com',
120-
plain('Кириллица@Кириллица.com')
121-
),
118+
link('mailto:Кириллица@Кириллица.com', [
119+
plain('Кириллица@Кириллица.com'),
120+
]),
122121
]),
123122
],
124123
],
@@ -128,7 +127,7 @@ test.each([
128127
[
129128
paragraph([
130129
plain('My email is '),
131-
link('mailto:[email protected]', plain('[email protected]')),
130+
link('mailto:[email protected]', [plain('[email protected]')]),
132131
]),
133132
],
134133
],
@@ -137,7 +136,7 @@ test.each([
137136
[
138137
paragraph([
139138
plain('My email is '),
140-
link('mailto:[email protected]', plain('[email protected]')),
139+
link('mailto:[email protected]', [plain('[email protected]')]),
141140
]),
142141
],
143142
],

packages/message-parser/tests/emphasis.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ test.each([
1616
[
1717
paragraph([
1818
italic([
19-
link(
20-
'https://gist.github.com/24dddfa97bef58f46ac2ce0f80c58ba4',
21-
plain('A brand new Gist')
22-
),
19+
link('https://gist.github.com/24dddfa97bef58f46ac2ce0f80c58ba4', [
20+
plain('A brand new Gist'),
21+
]),
2322
]),
2423
]),
2524
],

0 commit comments

Comments
 (0)