Skip to content

Commit 3ccd090

Browse files
fix(message-parser): Relative URL being displayed as plain text (#736)
* fix(message-parser): Fixed Relative URL being displayed as plain text * fix: Rollback URL protocol/schema max chars * fix: Adding tests and fixing URL wrong format Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat>
1 parent 9c0c0c1 commit 3ccd090

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

packages/message-parser/src/grammar.pegjs

+11-3
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ Inline
135135
/ Image
136136
/ References
137137
/ AutolinkedPhone
138-
/ AutolinkedURL
139138
/ AutolinkedEmail
139+
/ AutolinkedURL
140140
/ Emphasis
141141
/ color
142142
/ emoticon
@@ -151,7 +151,7 @@ Whitespace = w:$" "+ { return plain(w); }
151151

152152
Escaped = "\\" t:$. { return plain(t); }
153153

154-
Any = !EndOfLine t:$. { return plain(t); }
154+
Any = !EndOfLine t:$. u:$URL? { return plain(t + u); }
155155

156156
// = Line
157157

@@ -414,10 +414,18 @@ URL
414414
g:urlPath?
415415
h:urlQuery?
416416
)
417+
/ $(
418+
urlAuthorityHost
419+
p:urlPath?
420+
q:urlQuery?
421+
f:urlFragment?
422+
g:urlPath?
423+
h:urlQuery?
424+
)
417425

418426
urlScheme
419427
= $(
420-
[A-Za-z]
428+
[[A-Za-z]
421429
[A-Za-z0-9+.-]
422430
[A-Za-z0-9+.-]?
423431
[A-Za-z0-9+.-]?

packages/message-parser/src/utils.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,15 @@ export const strike = generate('STRIKE');
6969
export const codeLine = generate('CODE_LINE');
7070
export const link = (() => {
7171
const fn = generate('LINK');
72-
return (src: string, label?: Markup) =>
73-
fn({ src: plain(src), label: label || plain(src) });
72+
73+
return (src: string, label?: Markup) => {
74+
const href =
75+
src !== '' && !src.startsWith('http') && !src.startsWith('//')
76+
? `//${src}`
77+
: src;
78+
79+
return fn({ src: plain(href), label: label || plain(src) });
80+
};
7481
})();
7582

7683
export const image = (() => {

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

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { parser } from '../src';
2-
import { link, paragraph, plain, bold, strike, italic } from '../src/utils';
2+
import {
3+
link,
4+
paragraph,
5+
plain,
6+
bold,
7+
strike,
8+
italic,
9+
quote,
10+
} from '../src/utils';
311

412
test.each([
513
[
@@ -10,9 +18,14 @@ test.each([
1018
[
1119
`<https://domain.com|Test
1220
>`,
21+
[paragraph([plain('<https://domain.com|Test')]), paragraph([plain('>')])],
22+
],
23+
[
24+
`<https://domain.com|Test
25+
> quote here`,
1326
[
14-
paragraph([plain('<'), link('https://domain.com'), plain('|Test')]),
15-
paragraph([plain('>')]),
27+
paragraph([plain('<https://domain.com|Test')]),
28+
quote([paragraph([plain('quote here')])]),
1629
],
1730
],
1831
[
@@ -214,6 +227,14 @@ test.each([
214227
]),
215228
],
216229
],
230+
['google.com', [paragraph([link('//google.com', plain('google.com'))])]],
231+
['www.google.com', [paragraph([link('www.google.com')])]],
232+
['rocket.chat:8080', [paragraph([link('rocket.chat:8080')])]],
233+
['ShouldNotBeALink', [paragraph([plain('ShouldNotBeALink')])]],
234+
[
235+
'http:/ google.com',
236+
[paragraph([plain('http:/ '), link('//google.com', plain('google.com'))])],
237+
],
217238
])('parses %p', (input, output) => {
218239
expect(parser(input)).toMatchObject(output);
219240
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test.each([
2525
],
2626
],
2727
['http:/rocket.chat/teste', [paragraph([plain('http:/rocket.chat/teste')])]],
28-
['http:/rocket.chat/', [paragraph([plain('http:/rocket.chat/')])]],
28+
['https:/rocket.chat/', [paragraph([plain('https:/rocket.chat/')])]],
2929
['https://test', [paragraph([plain('https://test')])]],
3030
[
3131
'httpsss://rocket.chat/test',

0 commit comments

Comments
 (0)