From 0bd65274a68b4bdd8745e62716d4220b30684c75 Mon Sep 17 00:00:00 2001 From: martincupela Date: Tue, 18 Jun 2024 16:13:43 +0200 Subject: [PATCH] fix: keep line breaks in message text that contains multiple markdown elements --- .../__snapshots__/renderText.test.js.snap | 24 +++++++++++++++++++ .../renderText/__tests__/renderText.test.js | 5 ++++ .../remarkPlugins/keepLineBreaksPlugin.ts | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/components/Message/renderText/__tests__/__snapshots__/renderText.test.js.snap b/src/components/Message/renderText/__tests__/__snapshots__/renderText.test.js.snap index ed734be297..f5a1fc41f5 100644 --- a/src/components/Message/renderText/__tests__/__snapshots__/renderText.test.js.snap +++ b/src/components/Message/renderText/__tests__/__snapshots__/renderText.test.js.snap @@ -214,6 +214,30 @@ Array [ ] `; +exports[`keepLineBreaksPlugin present keeps line between lines with strong text 1`] = ` +Array [ +

+ This is + + the first + + line +

, + " +", +
, + " +", + " +", + " +", +

+ This is the second line +

, +] +`; + exports[`keepLineBreaksPlugin present keeps line breaks around a blockquote 1`] = ` Array [

diff --git a/src/components/Message/renderText/__tests__/renderText.test.js b/src/components/Message/renderText/__tests__/renderText.test.js index 737b5f8a70..38433282bb 100644 --- a/src/components/Message/renderText/__tests__/renderText.test.js +++ b/src/components/Message/renderText/__tests__/renderText.test.js @@ -277,6 +277,7 @@ describe('keepLineBreaksPlugin', () => { const blockquoteText = `a${lineBreaks}>b${lineBreaks}c`; const withStrikeThroughText = `a${lineBreaks}${strikeThroughText}${lineBreaks}b`; const tableText = `a${lineBreaks}| a | b | c | d |\n| - | :- | -: | :-: |\n| a | b | c | d |${lineBreaks}c`; + const multilineWithStrongText = 'This is **the first** line\n\nThis is the second line'; const doRenderText = (text, present) => { const Markdown = renderText( @@ -365,6 +366,10 @@ describe('keepLineBreaksPlugin', () => { const tree = doRenderText(tableText, present); expect(tree).toMatchSnapshot(); }); + it(`keeps line between lines with strong text`, () => { + const tree = doRenderText(multilineWithStrongText, present); + expect(tree).toMatchSnapshot(); + }); }); }); diff --git a/src/components/Message/renderText/remarkPlugins/keepLineBreaksPlugin.ts b/src/components/Message/renderText/remarkPlugins/keepLineBreaksPlugin.ts index 76ed94de3e..3a3bf4720c 100644 --- a/src/components/Message/renderText/remarkPlugins/keepLineBreaksPlugin.ts +++ b/src/components/Message/renderText/remarkPlugins/keepLineBreaksPlugin.ts @@ -12,7 +12,7 @@ const visitor: Visitor = (node, index, parent) => { const prevSibling = parent.children.at(index - 1); if (!prevSibling?.position) return; - if (node.position.start.line === prevSibling.position.start.line) return false; + if (node.position.start.line === prevSibling.position.start.line) return; const ownStartLine = node.position.start.line; const prevEndLine = prevSibling.position.end.line;