Skip to content

Commit

Permalink
fix(message-parser): Escaping only necessary characters (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocostadev authored Nov 10, 2022
1 parent aa9bc06 commit 29dd497
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
6 changes: 1 addition & 5 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Inline

Whitespace = w:$" "+ { return plain(w); }

Escaped = "\\" t:$. { return plain(t); }
Escaped = "\\" t:$("*" / "_" / "~" / "`" / "#" / ".") { return plain(t); }

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

Expand Down Expand Up @@ -297,10 +297,6 @@ unicode
return String.fromCharCode(parseInt(digits, 16));
}

escape
= unicode
/ "\\" ch:[^\r\n\f0-9a-f]i { return ch; }

AutolinkedPhone = p:Phone { return link('tel:' + p.number, plain(p.text)); }

AutolinkedURL = u:URL { return link(u); }
Expand Down
16 changes: 8 additions & 8 deletions packages/message-parser/tests/escaped.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parse } from '../src';
import { paragraph, plain, bold } from '../src/utils';

test.each([
['¯\\\\\\_(ツ)_/¯', [paragraph([plain('¯\\_(ツ)_/¯')])]],
['¯\\\\_(ツ)_/¯', [paragraph([plain('¯\\_(ツ)_/¯')])]],
[
'\\*escaped as*bold*escaped*',
[
Expand All @@ -14,24 +14,24 @@ test.each([
],
],
['\\*not bold*', [paragraph([plain('*not bold*')])]],
['*_~`#.'.split('').join('\\'), [paragraph([plain('*_~`#.')])]],
['\\*not emphasized*', [paragraph([plain('*not emphasized*')])]],
['\\<br/> tag plain text', [paragraph([plain('\\<br/> tag plain text')])]],
[
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'.split('').join('\\'),
[paragraph([plain('!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')])],
'\\[it is not a link](/foo)',
[paragraph([plain('\\[it is not a link](/foo)')])],
],
['\\*not emphasized*', [paragraph([plain('*not emphasized*')])]],
['\\<br/> not a tag', [paragraph([plain('<br/> not a tag')])]],
['\\[not a link](/foo)', [paragraph([plain('[not a link](/foo)')])]],
['\\`not code`', [paragraph([plain('`not code`')])]],
['1\\. not a list', [paragraph([plain('1. not a list')])]],
['\\* not a list', [paragraph([plain('* not a list')])]],
['\\# not a heading', [paragraph([plain('# not a heading')])]],
[
'\\[foo]: /url "not a reference"',
[paragraph([plain('[foo]: /url "not a reference"')])],
[paragraph([plain('\\[foo]: /url "not a reference"')])],
],
[
'\\&ouml; not a character entity',
[paragraph([plain('&ouml; not a character entity')])],
[paragraph([plain('\\&ouml; not a character entity')])],
],
])('parses %p', (input, output) => {
expect(parse(input)).toMatchObject(output);
Expand Down

0 comments on commit 29dd497

Please sign in to comment.