From a489ebbccf515aa514ca1e55fc63bde4a658edd2 Mon Sep 17 00:00:00 2001 From: Aviad Pineles Date: Sun, 13 Aug 2023 20:46:23 +0300 Subject: [PATCH] fix: properly convert plain text in brackets that is not a link closes #1131 --- packages/message-parser/src/grammar.pegjs | 7 +++++-- packages/message-parser/src/utils.ts | 6 ++++++ packages/message-parser/tests/link.test.ts | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/message-parser/src/grammar.pegjs b/packages/message-parser/src/grammar.pegjs index 5067520027..fcec813fd5 100644 --- a/packages/message-parser/src/grammar.pegjs +++ b/packages/message-parser/src/grammar.pegjs @@ -31,6 +31,7 @@ task, tasks, unorderedList, + flatten, } = require('./utils'); }} @@ -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)+ diff --git a/packages/message-parser/src/utils.ts b/packages/message-parser/src/utils.ts index 5902d8e33e..df4f8e6b92 100644 --- a/packages/message-parser/src/utils.ts +++ b/packages/message-parser/src/utils.ts @@ -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'] => diff --git a/packages/message-parser/tests/link.test.ts b/packages/message-parser/tests/link.test.ts index 2f84fd218f..cfc1a4f9de 100644 --- a/packages/message-parser/tests/link.test.ts +++ b/packages/message-parser/tests/link.test.ts @@ -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); });