From f5f4b86105cfb273be9673e92a23c44b61c7b556 Mon Sep 17 00:00:00 2001 From: d3m1d0v Date: Fri, 12 Jul 2024 14:58:17 +0300 Subject: [PATCH] fix: fixed parsing of tab contents without indentation --- src/plugin/transform.ts | 6 ++-- test/plugin.test.ts | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/plugin/transform.ts b/src/plugin/transform.ts index 318c466..ad98a26 100644 --- a/src/plugin/transform.ts +++ b/src/plugin/transform.ts @@ -337,14 +337,14 @@ export function transform({ const tabsGroup = match[2] || `${DEFAULT_TABS_GROUP_PREFIX}${generateID()}`; const orientation = (match[4] || 'horizontal') as TabsOrientation; - const {tabs, index} = findTabs(state.tokens, i + 3); + const {tabs} = findTabs(state.tokens, i + 3); if (tabs.length > 0) { insertTabs( tabs, state, orientation, - {start: i, end: index + 3}, + {start: i, end: closeTokenIdx + 2}, { containerClasses, tabsGroup, @@ -354,7 +354,7 @@ export function transform({ i++; tabsAreInserted = true; } else { - state.tokens.splice(i, index - i); + state.tokens.splice(i, closeTokenIdx - i + 3); } } diff --git a/test/plugin.test.ts b/test/plugin.test.ts index 18f4f91..ff3a0f0 100644 --- a/test/plugin.test.ts +++ b/test/plugin.test.ts @@ -173,6 +173,79 @@ describe('plugin', () => { expect(tokens.map((token) => token.type)).toEqual(nestedTokenTypes); }); + it('should return valid token stream if content without indentation', () => { + // ACT + const {tokens} = makeTransform({ + content: [ + '{% list tabs %}', + '', + '- tab1', + '', + '> quote', + '', + '{% endlist %}', + ], + }); + + // ASSERT + expect(tokens.map((token) => token.type)).toEqual([ + 'tabs_open', + 'tab-list_open', + 'tab_open', + 'inline', + 'tab_close', + 'tab-list_close', + 'tab-panel_open', + 'tab-panel_close', + 'tabs_close', + ]); + }); + + it('should remove empty tabs', () => { + // ACT + const {tokens} = makeTransform({ + content: [ + 'before', + '', + '{% list tabs %}', + '', + '', + '', + '{% endlist %}', + '', + 'after', + ], + }); + + // ASSERT + expect(tokens.map((token) => token.type)).toEqual([ + "paragraph_open", + "inline", + "paragraph_close", + "paragraph_open", + "inline", + "paragraph_close", + ]); + expect(tokens[1].content).toBe('before'); + expect(tokens[4].content).toBe('after') + }); + + it('should remove tabs with invalid markup inside', () => { + // ACT + const {tokens} = makeTransform({ + content: [ + '{% list tabs %}', + '', + '> 123', + '', + '{% endlist %}', + ], + }); + + // ASSERT + expect(tokens.map((token) => token.type)).toEqual([]); + }); + describe('options', () => { test('should add an extra className to container node', () => { // ACT