Skip to content

Commit

Permalink
fix(message-parser): Relative URL being displayed as plain text (#736)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hugocostadev and tassoevan authored Jun 23, 2022
1 parent 9c0c0c1 commit 3ccd090
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
14 changes: 11 additions & 3 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ Inline
/ Image
/ References
/ AutolinkedPhone
/ AutolinkedURL
/ AutolinkedEmail
/ AutolinkedURL
/ Emphasis
/ color
/ emoticon
Expand All @@ -151,7 +151,7 @@ Whitespace = w:$" "+ { return plain(w); }

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

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

// = Line

Expand Down Expand Up @@ -414,10 +414,18 @@ URL
g:urlPath?
h:urlQuery?
)
/ $(
urlAuthorityHost
p:urlPath?
q:urlQuery?
f:urlFragment?
g:urlPath?
h:urlQuery?
)

urlScheme
= $(
[A-Za-z]
[[A-Za-z]
[A-Za-z0-9+.-]
[A-Za-z0-9+.-]?
[A-Za-z0-9+.-]?
Expand Down
11 changes: 9 additions & 2 deletions packages/message-parser/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ export const strike = generate('STRIKE');
export const codeLine = generate('CODE_LINE');
export const link = (() => {
const fn = generate('LINK');
return (src: string, label?: Markup) =>
fn({ src: plain(src), label: label || plain(src) });

return (src: string, label?: Markup) => {
const href =
src !== '' && !src.startsWith('http') && !src.startsWith('//')
? `//${src}`
: src;

return fn({ src: plain(href), label: label || plain(src) });
};
})();

export const image = (() => {
Expand Down
27 changes: 24 additions & 3 deletions packages/message-parser/tests/link.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { parser } from '../src';
import { link, paragraph, plain, bold, strike, italic } from '../src/utils';
import {
link,
paragraph,
plain,
bold,
strike,
italic,
quote,
} from '../src/utils';

test.each([
[
Expand All @@ -10,9 +18,14 @@ test.each([
[
`<https://domain.com|Test
>`,
[paragraph([plain('<https://domain.com|Test')]), paragraph([plain('>')])],
],
[
`<https://domain.com|Test
> quote here`,
[
paragraph([plain('<'), link('https://domain.com'), plain('|Test')]),
paragraph([plain('>')]),
paragraph([plain('<https://domain.com|Test')]),
quote([paragraph([plain('quote here')])]),
],
],
[
Expand Down Expand Up @@ -214,6 +227,14 @@ test.each([
]),
],
],
['google.com', [paragraph([link('//google.com', plain('google.com'))])]],
['www.google.com', [paragraph([link('www.google.com')])]],
['rocket.chat:8080', [paragraph([link('rocket.chat:8080')])]],
['ShouldNotBeALink', [paragraph([plain('ShouldNotBeALink')])]],
[
'http:/ google.com',
[paragraph([plain('http:/ '), link('//google.com', plain('google.com'))])],
],
])('parses %p', (input, output) => {
expect(parser(input)).toMatchObject(output);
});
2 changes: 1 addition & 1 deletion packages/message-parser/tests/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test.each([
],
],
['http:/rocket.chat/teste', [paragraph([plain('http:/rocket.chat/teste')])]],
['http:/rocket.chat/', [paragraph([plain('http:/rocket.chat/')])]],
['https:/rocket.chat/', [paragraph([plain('https:/rocket.chat/')])]],
['https://test', [paragraph([plain('https://test')])]],
[
'httpsss://rocket.chat/test',
Expand Down

0 comments on commit 3ccd090

Please sign in to comment.