Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly convert plain text in brackets that is not a link #1132

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
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 @@ -227,10 +228,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 @@ -194,6 +194,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);
});
Loading