Skip to content

Commit

Permalink
fix: properly convert plain text in brackets that is not a link
Browse files Browse the repository at this point in the history
  • Loading branch information
paviad committed Aug 13, 2023
1 parent 19690db commit c9b0ebb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
task,
tasks,
unorderedList,
flatten,
} = require('./utils');
}}

Expand Down Expand Up @@ -226,10 +227,12 @@ InlineItem = Whitespace
*
*/
References
= "[" title:LinkTitle* "](" href:LinkRef ")" { return title.length ? link(href, reducePlainTexts(title)) : link(href); }
= "[" title:LinkTitle* "](" href:LinkRef ")" { return title.length ? link(href, reducePlainTexts(flatten(title))) : link(href); }
/ "<" href:LinkRef "|" title:LinkTitle2 ">" { return link(href, [plain(title)]); }

LinkTitle = (Whitespace / Emphasis) / anyTitle:$(!("](" .) .) { return plain(anyTitle) }
LinkTitle = (Whitespace / Emphasis) / balanced / anyTitle:$[^\[\]] { return plain(anyTitle) }

balanced = "[" t:LinkTitle* "]" { return reducePlainTexts([plain("["), ...(t || []), plain("]")]) }

LinkTitle2 = $([\x20-\x3B\x3D\x3F-\x60\x61-\x7B\x7D-\xFF] / NonASCII)+

Expand Down
6 changes: 6 additions & 0 deletions packages/message-parser/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ const joinEmoji = (
return current;
};

export const flatten = (values: Paragraph['value']): Paragraph['value'] =>
values.reduce(
(acc, v) => [...acc, ...(Array.isArray(v) ? v : [v])],
[] as Paragraph['value']
);

export const reducePlainTexts = (
values: Paragraph['value']
): Paragraph['value'] =>
Expand Down
21 changes: 21 additions & 0 deletions packages/message-parser/tests/link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,27 @@ Text after line break`,
]),
],
],
[
'[Jira [Task _emph_ foo] parentheses not working](rocket.chat)',
[
paragraph([
link('rocket.chat', [
plain('Jira [Task '),
italic([plain('emph')]),
plain(' foo] parentheses not working'),
]),
]),
],
],
[
'[Title 1] bla bla [Title 2](https://foo.com/title2)',
[
paragraph([
plain('[Title 1] bla bla '),
link('https://foo.com/title2', [plain('Title 2')]),
]),
],
],
])('parses %p', (input, output) => {
expect(parse(input)).toMatchObject(output);
});

0 comments on commit c9b0ebb

Please sign in to comment.