From ac3145030e12a7d6bd442015f6b23ab61c3d148e Mon Sep 17 00:00:00 2001 From: Kiri111enz <61368350+Kiri111enz@users.noreply.github.com> Date: Mon, 11 Dec 2023 17:01:41 +0300 Subject: [PATCH] fix: cut plugin && add tests (#338) --- src/transform/plugins/cut.ts | 6 +++- test/cut.test.ts | 70 ++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/transform/plugins/cut.ts b/src/transform/plugins/cut.ts index a8e4b043..5942d33c 100644 --- a/src/transform/plugins/cut.ts +++ b/src/transform/plugins/cut.ts @@ -68,15 +68,19 @@ const cut: MarkdownItPluginCb = (md, {path, log}) => { const newCloseToken = new state.Token('yfm_cut_close', 'div', -1); newCloseToken.map = tokens[closeTokenIdx].map; + const insertTokens = tokens.slice(i + 3, closeTokenIdx); + const rest = insertTokens.length % 3; + const insideTokens = [ newOpenToken, titleOpen, titleInline, titleClose, contentOpen, - ...tokens.slice(i + 3, closeTokenIdx), + ...insertTokens.slice(0, insertTokens.length - rest), contentClose, newCloseToken, + ...insertTokens.slice(insertTokens.length - rest), ]; tokens.splice(i, closeTokenIdx - i + 3, ...insideTokens); diff --git a/test/cut.test.ts b/test/cut.test.ts index 92e422f5..873c15ff 100644 --- a/test/cut.test.ts +++ b/test/cut.test.ts @@ -15,13 +15,14 @@ describe('Cut plugin', () => { expect( transformYfm( '{% cut "Cut title" %}\n' + '\n' + 'Cut content\n' + '\n' + '{% endcut %}', - ), + ).replace(/(\r\n|\n|\r)/gm, ''), ).toBe( '
Cut content
\n' + + 'Cut content
' + 'Cut content 1
\nCut content 1
Cut content 2
\nCut content 2
Outer content
\n' + + 'Outer content
' + 'Inner content
\nInner content
Content we want to hide
\nContent we want to hide
Some text
Some text
Some text
Some text
Some text