From 20a0fc268ed75d1153bd3c8770b43e866bf7eaf2 Mon Sep 17 00:00:00 2001 From: Kirill Omelianenko Date: Wed, 27 Dec 2023 12:03:50 +0300 Subject: [PATCH] fix: revert pr338 && add cut w/ code test --- src/transform/plugins/cut.ts | 6 +----- test/cut.test.ts | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/transform/plugins/cut.ts b/src/transform/plugins/cut.ts index 5942d33c..a8e4b043 100644 --- a/src/transform/plugins/cut.ts +++ b/src/transform/plugins/cut.ts @@ -68,19 +68,15 @@ 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, - ...insertTokens.slice(0, insertTokens.length - rest), + ...tokens.slice(i + 3, closeTokenIdx), 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 873c15ff..c98e773a 100644 --- a/test/cut.test.ts +++ b/test/cut.test.ts @@ -23,6 +23,24 @@ describe('Cut plugin', () => { ); }); + it('should render simple cut with code in it', () => { + expect( + transformYfm( + '{% cut "Cut title" %}\n' + + '\n' + + '```\n' + + 'Code\n' + + '```\n' + + '\n' + + '{% endcut %}', + ).replace(/(\r\n|\n|\r)/gm, ''), + ).toBe( + '
Cut title
' + + '
Code
' + + '
', + ); + }); + it('should render siblings cuts', () => { expect( transformYfm(