Skip to content

Commit 8ce6b91

Browse files
committed
fix(message-parser): URL issues (#448)
1 parent 491f98b commit 8ce6b91

File tree

5 files changed

+124
-117
lines changed

5 files changed

+124
-117
lines changed

packages/message-parser/src/grammar.pegjs

+83-110
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
task,
2222
orderedList,
2323
listItem,
24-
unorderedList
24+
unorderedList,
2525
} = require('./utils');
2626
}
2727

@@ -41,47 +41,31 @@ Blocks
4141
/ TaskList
4242
/ OrderedList
4343
/ UnorderedList
44-
// / Section
44+
45+
// / Section
4546

4647
Emphasis
4748
= Bold
4849
/ Italic
4950
/ Strikethrough
5051

51-
52-
Paragraph
53-
= value:
54-
( Whitespace
55-
/ Emoji
56-
/ References
57-
/ InlineCode
58-
/ AutolinkedPhone
59-
/ AutolinkedURL
60-
/ AutolinkedEmail
61-
/ Emphasis
62-
/ Color
63-
/ UserMention
64-
/ ChannelMention
65-
/ Any
66-
67-
)+
68-
EndOfLine? { return paragraph(reducePlainTexts(value)); }
52+
Paragraph = value:Inline { return paragraph(value); }
6953

7054
Inline
71-
= value:
72-
( Whitespace
73-
/ Emoji
74-
/ References
75-
/ InlineCode
76-
/ AutolinkedPhone
77-
/ AutolinkedURL
78-
/ AutolinkedEmail
79-
/ Emphasis
80-
/ Color
81-
/ UserMention
82-
/ ChannelMention
83-
/ Any
84-
)+
55+
= value:(
56+
Whitespace
57+
/ Emoji
58+
/ InlineCode
59+
/ References
60+
/ AutolinkedPhone
61+
/ AutolinkedURL
62+
/ AutolinkedEmail
63+
/ Emphasis
64+
/ Color
65+
/ UserMention
66+
/ ChannelMention
67+
/ Any
68+
)+
8569
EndOfLine? { return reducePlainTexts(value); }
8670

8771
Whitespace = w:" "+ { return plain(w.join('')); }
@@ -164,11 +148,13 @@ SectionText
164148
/ [\x61-\x7A]
165149
/ nonascii
166150

151+
Not_enter = text:($:(!"\n" s:. { return s; })+) { return plain(text.join('')); }
152+
167153
Heading
168-
= "# "+ text:Line { return heading([text], 1); }
169-
/ "## "+ text:Line { return heading([text], 2); }
170-
/ "### "+ text:Line { return heading([text], 3); }
171-
/ "#### "+ text:Line { return heading([text], 4); }
154+
= "# "+ text:Not_enter { return heading([text], 1); }
155+
/ "## "+ text:Not_enter { return heading([text], 2); }
156+
/ "### "+ text:Not_enter { return heading([text], 3); }
157+
/ "#### "+ text:Not_enter { return heading([text], 4); }
172158

173159
utf8_names_validation = text:[0-9a-zA-Z-_.]+ { return text.join(''); }
174160

@@ -264,84 +250,80 @@ Lists
264250
};
265251
}
266252

267-
Blockquote = b:BlockquoteItem+ {
268-
return quote(b)
269-
}
253+
Blockquote = b:BlockquoteItem+ { return quote(b); }
270254

271-
BlockquoteItem = "> " p:Paragraph { return p }
255+
BlockquoteItem = "> " p:Paragraph { return p; }
272256

273257
// - [ ] this is an incomplete item
274258
// - [x] this is a complete item
275-
TaskList
276-
= t:TaskItem+ {
277-
return tasks(t);
278-
}
259+
TaskList = t:TaskItem+ { return tasks(t); }
279260

280-
TaskItem = "- [x] " text:Inline { return task(text, true); }
281-
/ "- [ ] " text:Inline { return task(text, false); }
261+
TaskItem
262+
= "- [x] " text:Inline { return task(text, true); }
263+
/ "- [ ] " text:Inline { return task(text, false); }
282264

265+
UnorderedList
266+
= UnorderedList_
267+
/ UnorderedList__
283268

284-
UnorderedList = UnorderedList_ / UnorderedList__
269+
UnorderedList_ = lists:UnorderedListItem_+ { return unorderedList(lists); }
285270

271+
UnorderedList__ = lists:UnorderedListItem__+ { return unorderedList(lists); }
286272

287-
UnorderedList_ = lists:UnorderedListItem_+ {
288-
return unorderedList(lists)
289-
}
290-
291-
UnorderedList__ = lists:UnorderedListItem__+ {
292-
return unorderedList(lists)
293-
}
294-
295-
UnorderedListItem_
296-
= ("- ") text:Inline { return listItem(text, true); }
273+
UnorderedListItem_ = "- " text:Inline { return listItem(text, true); }
297274

298275
UnorderedListItem__
299-
= ("* ") text:UnorderedListItem__Inline { return listItem(text, true); }
276+
= "* " text:UnorderedListItem__Inline { return listItem(text, true); }
300277

301278
UnorderedListItem__Inline
302-
= value:
303-
( Whitespace
304-
/ Emoji
305-
/ References
306-
/ InlineCode
307-
/ AutolinkedPhone
308-
/ AutolinkedURL
309-
/ AutolinkedEmail
310-
/ Emphasis
311-
/ Color
312-
/ UserMention
313-
/ ChannelMention
314-
/ !'*' a:Any { return a }
315-
)+
316-
!'*' EndOfLine? { return reducePlainTexts(value); }
317-
318-
OrderedList
319-
= lists:OrderedListItem+ {
320-
return orderedList(lists)
321-
}
279+
= value:(
280+
Whitespace
281+
/ Emoji
282+
/ References
283+
/ InlineCode
284+
/ AutolinkedPhone
285+
/ AutolinkedURL
286+
/ AutolinkedEmail
287+
/ Emphasis
288+
/ Color
289+
/ UserMention
290+
/ ChannelMention
291+
/ !"*" a:Any { return a; }
292+
)+
293+
!"*"
294+
EndOfLine? { return reducePlainTexts(value); }
295+
296+
OrderedList = lists:OrderedListItem+ { return orderedList(lists); }
297+
322298
OrderedListItem
323-
= (digit1_9+ "\x2E ") text:Inline { return listItem(text, true); }
299+
= (digit1_9+ "\x2E ") text:Inline { return listItem(text, true); }
324300

325301
Codetype = t:[a-zA-Z0-9 \_\-.]+ { return t.join(''); }
326302

327-
InlineCode = "`" text:Line "`" { return inlineCode(text); }
303+
InlineCode
304+
= "`" text:InlineCode__+ "`" { return inlineCode(plain(text.join(''))); }
305+
306+
InlineCode__ = $(!"`" !"\n" $:.)
307+
308+
LineCode__any = $:(!"\n" !"```" t:. { return t; })+
328309

329310
LineCode "LineCode"
330-
= text:[^"\n"\`]+ "`"? { return codeLine(plain(text.join(''))); }
331-
/ "\n"+ text:[^"\n"\`]+ "`"? { return codeLine(plain(text.join(''))); }
311+
= text:LineCode__any { return codeLine(plain(text.join(''))); }
312+
/ "\n"+ text:LineCode__any { return codeLine(plain(text.join(''))); }
332313

333314
MultiplelLineCode
334-
= "```" t:Codetype? "\n" value:LineCode+ "\n"+ "```" {
335-
return code(value, t);
336-
}
315+
= "```" t:Codetype? "\n" value:LineCode+ "\n```" { return code(value, t); }
337316

338317
// [Visit GitHub!](www.github.com)
339-
LinkTitle = "[" text:(Emphasis / Line) "]" { return text; }
318+
319+
LinkTitle = "[" text:(Emphasis / Line / Whitespace) "]" { return text; }
340320

341321
LinkRef
342322
= "(" text:(URL / p:Phone { return 'tel:' + p.number; }) ")" { return text; }
343323

344-
References = title:LinkTitle href:LinkRef { return link(href, title); }
324+
References
325+
= "[]" href:LinkRef { return link(href); }
326+
/ title:LinkTitle href:LinkRef { return link(href, title); }
345327

346328
/* Macros */
347329

@@ -414,6 +396,10 @@ alpha = [a-zA-Z]
414396

415397
digit = [0-9]
416398

399+
alpha_digit
400+
= alpha
401+
/ digit
402+
417403
digit1_9 = [1-9]
418404

419405
digits = d:digit+ { return d.join(''); }
@@ -454,7 +440,7 @@ domainName
454440

455441
domainNameLabel = $(domainChar domainChar+ $("-" domainChar+)*)
456442

457-
domainChar = !safe !extra !EndOfLine !Space .
443+
domainChar = !"/" !safe !extra !EndOfLine !Space .
458444

459445
/**
460446
*
@@ -497,20 +483,7 @@ phonePrefix
497483
*
498484
*/
499485

500-
URL
501-
= s:urlScheme a:urlAuthority p:urlPath? q:urlQuery? f:urlFragment? {
502-
const href = [s, a, p, q, f].filter(Boolean).join('');
503-
// const url = {
504-
// href,
505-
// scheme: s,
506-
// authority: a,
507-
// path: p,
508-
// query: q,
509-
// fragment: f,
510-
// };
511-
512-
return href;
513-
}
486+
URL = $(s:urlScheme a:urlAuthority p:urlPath? q:urlQuery? f:urlFragment?)
514487

515488
urlScheme
516489
= $(
@@ -553,11 +526,11 @@ urlAuthority = $("//" urlAuthorityUserInfo? urlAuthorityHost)
553526

554527
urlAuthorityUserInfo = $(urlAuthorityUser (":" urlAuthorityPassword)? "@")
555528

556-
urlAuthorityUser = $(alpha / digit / "$" / "-" / "_" / "." / "&" / "=")+
529+
urlAuthorityUser = $(alpha_digit / !"@" !"/" safe)+
557530

558-
urlAuthorityPassword = $(alpha / digit / "$" / "-" / "_" / "." / "&" / "=")+
531+
urlAuthorityPassword = $(alpha_digit / !"@" !"/" safe)+
559532

560-
urlAuthorityHost = urlAuthorityHostName (":" urlAuthorityPort)?
533+
urlAuthorityHost = t:urlAuthorityHostName (":" urlAuthorityPort)?
561534

562535
urlAuthorityHostName
563536
= domainName
@@ -566,11 +539,11 @@ urlAuthorityHostName
566539
urlAuthorityPort
567540
= digits // TODO: from "0" to "65535"
568541

569-
urlPath = $("/" $(alpha / digit / safe)+ urlPath*)
542+
urlPath = $("/" $(alpha_digit / safe)+ urlPath*)
570543

571-
urlQuery = $("?" $(alpha / digit / safe)+)
544+
urlQuery = $("?" $(alpha_digit / safe)*)
572545

573-
urlFragment = $("#" $(alpha / digit / safe)+)
546+
urlFragment = $("#" $(alpha_digit / safe)*)
574547

575548
/**
576549
*

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

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ code
88
\`\`\``,
99
[code([codeLine(plain('code'))])],
1010
],
11+
[
12+
`\`\`\`
13+
var a = "teste";
14+
\`\`\``,
15+
[code([codeLine(plain('var a = "teste";'))])],
16+
],
1117
[
1218
`\`\`\`javascript
1319
code
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { parser } from '../src';
22
import { inlineCode, paragraph, plain } from '../src/utils';
33

4-
test.each([[`\`code\``, [paragraph([inlineCode(plain('code'))])]]])(
5-
'parses %p',
6-
(input, output) => {
7-
expect(parser(input)).toMatchObject(output);
8-
}
9-
);
4+
test.each([
5+
[
6+
'`[asd](https://localhost)`',
7+
[paragraph([inlineCode(plain('[asd](https://localhost)'))])],
8+
],
9+
[`\`code\``, [paragraph([inlineCode(plain('code'))])]],
10+
])('parses %p', (input, output) => {
11+
expect(parser(input)).toMatchObject(output);
12+
});

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

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ import { parser } from '../src';
22
import { link, paragraph, plain, bold, strike, italic } from '../src/utils';
33

44
test.each([
5+
['[](https://rocket.chat)', [paragraph([link('https://rocket.chat')])]],
6+
[
7+
'[ ](https://rocket.chat)',
8+
[paragraph([link('https://rocket.chat', plain(' '))])],
9+
],
10+
11+
[
12+
'[ test](https://rocket.chat)',
13+
[paragraph([link('https://rocket.chat', plain(' test'))])],
14+
],
15+
[
16+
'[ test ](https://rocket.chat)',
17+
[paragraph([link('https://rocket.chat', plain(' test '))])],
18+
],
519
[
620
'[title](https://rocket.chat)',
721
[paragraph([link('https://rocket.chat', plain('title'))])],

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ import { parser } from '../src';
22
import { link, paragraph, plain } from '../src/utils';
33

44
test.each([
5-
['https://test', [paragraph([plain('https://test')])]],
5+
[
6+
'https://www.npmjs.com/package/@rocket.chat/message-parser',
7+
[
8+
paragraph([
9+
link('https://www.npmjs.com/package/@rocket.chat/message-parser'),
10+
]),
11+
],
12+
],
613
['http:/rocket.chat/teste', [paragraph([plain('http:/rocket.chat/teste')])]],
14+
['http:/rocket.chat/', [paragraph([plain('http:/rocket.chat/')])]],
15+
['https://test', [paragraph([plain('https://test')])]],
716
[
817
'httpsss://rocket.chat/test',
918
[paragraph([link('httpsss://rocket.chat/test')])],
@@ -27,6 +36,8 @@ test.each([
2736
'https://localhost:3000#fragment',
2837
[paragraph([link('https://localhost:3000#fragment')])],
2938
],
39+
['https://localhost:3000#', [paragraph([link('https://localhost:3000#')])]],
40+
['https://localhost:3000?', [paragraph([link('https://localhost:3000?')])]],
3041
[
3142
'ftp://user:pass@localhost:21/etc/hosts',
3243
[paragraph([link('ftp://user:pass@localhost:21/etc/hosts')])],

0 commit comments

Comments
 (0)