Skip to content

Commit

Permalink
fix(message-parser): URL issues (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed May 18, 2021
1 parent 491f98b commit 8ce6b91
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 117 deletions.
193 changes: 83 additions & 110 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
task,
orderedList,
listItem,
unorderedList
unorderedList,
} = require('./utils');
}

Expand All @@ -41,47 +41,31 @@ Blocks
/ TaskList
/ OrderedList
/ UnorderedList
// / Section

// / Section

Emphasis
= Bold
/ Italic
/ Strikethrough


Paragraph
= value:
( Whitespace
/ Emoji
/ References
/ InlineCode
/ AutolinkedPhone
/ AutolinkedURL
/ AutolinkedEmail
/ Emphasis
/ Color
/ UserMention
/ ChannelMention
/ Any

)+
EndOfLine? { return paragraph(reducePlainTexts(value)); }
Paragraph = value:Inline { return paragraph(value); }

Inline
= value:
( Whitespace
/ Emoji
/ References
/ InlineCode
/ AutolinkedPhone
/ AutolinkedURL
/ AutolinkedEmail
/ Emphasis
/ Color
/ UserMention
/ ChannelMention
/ Any
)+
= value:(
Whitespace
/ Emoji
/ InlineCode
/ References
/ AutolinkedPhone
/ AutolinkedURL
/ AutolinkedEmail
/ Emphasis
/ Color
/ UserMention
/ ChannelMention
/ Any
)+
EndOfLine? { return reducePlainTexts(value); }

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

Not_enter = text:($:(!"\n" s:. { return s; })+) { return plain(text.join('')); }

Heading
= "# "+ text:Line { return heading([text], 1); }
/ "## "+ text:Line { return heading([text], 2); }
/ "### "+ text:Line { return heading([text], 3); }
/ "#### "+ text:Line { return heading([text], 4); }
= "# "+ text:Not_enter { return heading([text], 1); }
/ "## "+ text:Not_enter { return heading([text], 2); }
/ "### "+ text:Not_enter { return heading([text], 3); }
/ "#### "+ text:Not_enter { return heading([text], 4); }

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

Expand Down Expand Up @@ -264,84 +250,80 @@ Lists
};
}

Blockquote = b:BlockquoteItem+ {
return quote(b)
}
Blockquote = b:BlockquoteItem+ { return quote(b); }

BlockquoteItem = "> " p:Paragraph { return p }
BlockquoteItem = "> " p:Paragraph { return p; }

// - [ ] this is an incomplete item
// - [x] this is a complete item
TaskList
= t:TaskItem+ {
return tasks(t);
}
TaskList = t:TaskItem+ { return tasks(t); }

TaskItem = "- [x] " text:Inline { return task(text, true); }
/ "- [ ] " text:Inline { return task(text, false); }
TaskItem
= "- [x] " text:Inline { return task(text, true); }
/ "- [ ] " text:Inline { return task(text, false); }

UnorderedList
= UnorderedList_
/ UnorderedList__

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

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

UnorderedList_ = lists:UnorderedListItem_+ {
return unorderedList(lists)
}

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

UnorderedListItem_
= ("- ") text:Inline { return listItem(text, true); }
UnorderedListItem_ = "- " text:Inline { return listItem(text, true); }

UnorderedListItem__
= ("* ") text:UnorderedListItem__Inline { return listItem(text, true); }
= "* " text:UnorderedListItem__Inline { return listItem(text, true); }

UnorderedListItem__Inline
= value:
( Whitespace
/ Emoji
/ References
/ InlineCode
/ AutolinkedPhone
/ AutolinkedURL
/ AutolinkedEmail
/ Emphasis
/ Color
/ UserMention
/ ChannelMention
/ !'*' a:Any { return a }
)+
!'*' EndOfLine? { return reducePlainTexts(value); }

OrderedList
= lists:OrderedListItem+ {
return orderedList(lists)
}
= value:(
Whitespace
/ Emoji
/ References
/ InlineCode
/ AutolinkedPhone
/ AutolinkedURL
/ AutolinkedEmail
/ Emphasis
/ Color
/ UserMention
/ ChannelMention
/ !"*" a:Any { return a; }
)+
!"*"
EndOfLine? { return reducePlainTexts(value); }

OrderedList = lists:OrderedListItem+ { return orderedList(lists); }

OrderedListItem
= (digit1_9+ "\x2E ") text:Inline { return listItem(text, true); }
= (digit1_9+ "\x2E ") text:Inline { return listItem(text, true); }

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

InlineCode = "`" text:Line "`" { return inlineCode(text); }
InlineCode
= "`" text:InlineCode__+ "`" { return inlineCode(plain(text.join(''))); }

InlineCode__ = $(!"`" !"\n" $:.)

LineCode__any = $:(!"\n" !"```" t:. { return t; })+

LineCode "LineCode"
= text:[^"\n"\`]+ "`"? { return codeLine(plain(text.join(''))); }
/ "\n"+ text:[^"\n"\`]+ "`"? { return codeLine(plain(text.join(''))); }
= text:LineCode__any { return codeLine(plain(text.join(''))); }
/ "\n"+ text:LineCode__any { return codeLine(plain(text.join(''))); }

MultiplelLineCode
= "```" t:Codetype? "\n" value:LineCode+ "\n"+ "```" {
return code(value, t);
}
= "```" t:Codetype? "\n" value:LineCode+ "\n```" { return code(value, t); }

// [Visit GitHub!](www.github.com)
LinkTitle = "[" text:(Emphasis / Line) "]" { return text; }

LinkTitle = "[" text:(Emphasis / Line / Whitespace) "]" { return text; }

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

References = title:LinkTitle href:LinkRef { return link(href, title); }
References
= "[]" href:LinkRef { return link(href); }
/ title:LinkTitle href:LinkRef { return link(href, title); }

/* Macros */

Expand Down Expand Up @@ -414,6 +396,10 @@ alpha = [a-zA-Z]

digit = [0-9]

alpha_digit
= alpha
/ digit

digit1_9 = [1-9]

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

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

domainChar = !safe !extra !EndOfLine !Space .
domainChar = !"/" !safe !extra !EndOfLine !Space .

/**
*
Expand Down Expand Up @@ -497,20 +483,7 @@ phonePrefix
*
*/

URL
= s:urlScheme a:urlAuthority p:urlPath? q:urlQuery? f:urlFragment? {
const href = [s, a, p, q, f].filter(Boolean).join('');
// const url = {
// href,
// scheme: s,
// authority: a,
// path: p,
// query: q,
// fragment: f,
// };

return href;
}
URL = $(s:urlScheme a:urlAuthority p:urlPath? q:urlQuery? f:urlFragment?)

urlScheme
= $(
Expand Down Expand Up @@ -553,11 +526,11 @@ urlAuthority = $("//" urlAuthorityUserInfo? urlAuthorityHost)

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

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

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

urlAuthorityHost = urlAuthorityHostName (":" urlAuthorityPort)?
urlAuthorityHost = t:urlAuthorityHostName (":" urlAuthorityPort)?

urlAuthorityHostName
= domainName
Expand All @@ -566,11 +539,11 @@ urlAuthorityHostName
urlAuthorityPort
= digits // TODO: from "0" to "65535"

urlPath = $("/" $(alpha / digit / safe)+ urlPath*)
urlPath = $("/" $(alpha_digit / safe)+ urlPath*)

urlQuery = $("?" $(alpha / digit / safe)+)
urlQuery = $("?" $(alpha_digit / safe)*)

urlFragment = $("#" $(alpha / digit / safe)+)
urlFragment = $("#" $(alpha_digit / safe)*)

/**
*
Expand Down
6 changes: 6 additions & 0 deletions packages/message-parser/tests/codeFence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ code
\`\`\``,
[code([codeLine(plain('code'))])],
],
[
`\`\`\`
var a = "teste";
\`\`\``,
[code([codeLine(plain('var a = "teste";'))])],
],
[
`\`\`\`javascript
code
Expand Down
15 changes: 9 additions & 6 deletions packages/message-parser/tests/inlineCode.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { parser } from '../src';
import { inlineCode, paragraph, plain } from '../src/utils';

test.each([[`\`code\``, [paragraph([inlineCode(plain('code'))])]]])(
'parses %p',
(input, output) => {
expect(parser(input)).toMatchObject(output);
}
);
test.each([
[
'`[asd](https://localhost)`',
[paragraph([inlineCode(plain('[asd](https://localhost)'))])],
],
[`\`code\``, [paragraph([inlineCode(plain('code'))])]],
])('parses %p', (input, output) => {
expect(parser(input)).toMatchObject(output);
});
14 changes: 14 additions & 0 deletions packages/message-parser/tests/link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { parser } from '../src';
import { link, paragraph, plain, bold, strike, italic } from '../src/utils';

test.each([
['[](https://rocket.chat)', [paragraph([link('https://rocket.chat')])]],
[
'[ ](https://rocket.chat)',
[paragraph([link('https://rocket.chat', plain(' '))])],
],

[
'[ test](https://rocket.chat)',
[paragraph([link('https://rocket.chat', plain(' test'))])],
],
[
'[ test ](https://rocket.chat)',
[paragraph([link('https://rocket.chat', plain(' test '))])],
],
[
'[title](https://rocket.chat)',
[paragraph([link('https://rocket.chat', plain('title'))])],
Expand Down
13 changes: 12 additions & 1 deletion packages/message-parser/tests/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ import { parser } from '../src';
import { link, paragraph, plain } from '../src/utils';

test.each([
['https://test', [paragraph([plain('https://test')])]],
[
'https://www.npmjs.com/package/@rocket.chat/message-parser',
[
paragraph([
link('https://www.npmjs.com/package/@rocket.chat/message-parser'),
]),
],
],
['http:/rocket.chat/teste', [paragraph([plain('http:/rocket.chat/teste')])]],
['http:/rocket.chat/', [paragraph([plain('http:/rocket.chat/')])]],
['https://test', [paragraph([plain('https://test')])]],
[
'httpsss://rocket.chat/test',
[paragraph([link('httpsss://rocket.chat/test')])],
Expand All @@ -27,6 +36,8 @@ test.each([
'https://localhost:3000#fragment',
[paragraph([link('https://localhost:3000#fragment')])],
],
['https://localhost:3000#', [paragraph([link('https://localhost:3000#')])]],
['https://localhost:3000?', [paragraph([link('https://localhost:3000?')])]],
[
'ftp://user:pass@localhost:21/etc/hosts',
[paragraph([link('ftp://user:pass@localhost:21/etc/hosts')])],
Expand Down

0 comments on commit 8ce6b91

Please sign in to comment.