Skip to content

Commit a489ebb

Browse files
committed
fix: properly convert plain text in brackets that is not a link
closes #1131
1 parent 10faaf9 commit a489ebb

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

packages/message-parser/src/grammar.pegjs

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
task,
3232
tasks,
3333
unorderedList,
34+
flatten,
3435
} = require('./utils');
3536
}}
3637

@@ -227,10 +228,12 @@ InlineItem = Whitespace
227228
*
228229
*/
229230
References
230-
= "[" title:LinkTitle* "](" href:LinkRef ")" { return title.length ? link(href, reducePlainTexts(title)) : link(href); }
231+
= "[" title:LinkTitle* "](" href:LinkRef ")" { return title.length ? link(href, reducePlainTexts(flatten(title))) : link(href); }
231232
/ "<" href:LinkRef "|" title:LinkTitle2 ">" { return link(href, [plain(title)]); }
232233

233-
LinkTitle = (Whitespace / Emphasis) / anyTitle:$(!("](" .) .) { return plain(anyTitle) }
234+
LinkTitle = (Whitespace / Emphasis) / balanced / anyTitle:$[^\[\]] { return plain(anyTitle) }
235+
236+
balanced = "[" t:LinkTitle* "]" { return reducePlainTexts([plain("["), ...(t || []), plain("]")]) }
234237

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

packages/message-parser/src/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ const joinEmoji = (
194194
return current;
195195
};
196196

197+
export const flatten = (values: Paragraph['value']): Paragraph['value'] =>
198+
values.reduce(
199+
(acc, v) => [...acc, ...(Array.isArray(v) ? v : [v])],
200+
[] as Paragraph['value']
201+
);
202+
197203
export const reducePlainTexts = (
198204
values: Paragraph['value']
199205
): Paragraph['value'] =>

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

+21
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,27 @@ Text after line break`,
560560
]),
561561
],
562562
],
563+
[
564+
'[Jira [Task _emph_ foo] parentheses not working](rocket.chat)',
565+
[
566+
paragraph([
567+
link('rocket.chat', [
568+
plain('Jira [Task '),
569+
italic([plain('emph')]),
570+
plain(' foo] parentheses not working'),
571+
]),
572+
]),
573+
],
574+
],
575+
[
576+
'[Title 1] bla bla [Title 2](https://foo.com/title2)',
577+
[
578+
paragraph([
579+
plain('[Title 1] bla bla '),
580+
link('https://foo.com/title2', [plain('Title 2')]),
581+
]),
582+
],
583+
],
563584
])('parses %p', (input, output) => {
564585
expect(parse(input)).toMatchObject(output);
565586
});

0 commit comments

Comments
 (0)