diff --git a/src/transform/plugins/tabs.ts b/src/transform/plugins/tabs.ts index e24e8e12..6029e33d 100644 --- a/src/transform/plugins/tabs.ts +++ b/src/transform/plugins/tabs.ts @@ -8,13 +8,14 @@ const TAB_RE = /`?{% list (tabs) %}`?/; type Tab = { name: string; tokens: Token[]; + listItem: Token; }; function findTabs(tokens: Token[], idx: number) { const tabs = []; let i = idx, nestedLevel = -1, - pending: Tab = {name: '', tokens: []}; + pending: Tab = {name: '', tokens: [], listItem: new Token('list_item_open', '', 0)}; while (i < tokens.length) { const token = tokens[i]; @@ -31,7 +32,7 @@ function findTabs(tokens: Token[], idx: number) { if (nestedLevel) { pending.tokens.push(token); } else { - pending = {name: '', tokens: []}; + pending = {name: '', tokens: [], listItem: token}; } break; case 'list_item_close': @@ -96,6 +97,22 @@ function insertTabs(tabs: Tab[], state: StateCore, start: number, end: number) { const tabListOpen = new state.Token('tab-list_open', 'div', 1); const tabListClose = new state.Token('tab-list_close', 'div', -1); + if (tabs.length) { + const [start] = tabs[0].listItem.map ?? [null]; + // eslint-disable-next-line no-eq-null, eqeqeq + if (start == null) { + throw new Error('failed to parse line mapping'); + } + + const [_, end] = tabs[tabs.length - 1].listItem.map ?? [null, null]; + // eslint-disable-next-line no-eq-null, eqeqeq + if (end == null) { + throw new Error('failed to parse line mapping'); + } + + tabListOpen.map = [start, end]; + } + tabsOpen.block = true; tabsClose.block = true; tabListOpen.block = true; @@ -111,10 +128,10 @@ function insertTabs(tabs: Tab[], state: StateCore, start: number, end: number) { const tabClose = new state.Token('tab_close', 'div', -1); const tabPanelOpen = new state.Token('tab-panel_open', 'div', 1); const tabPanelClose = new state.Token('tab-panel_close', 'div', -1); - const tabId = generateID(); const tabPanelId = generateID(); + tabOpen.map = tabs[i].listItem.map; tabText.content = tabs[i].name; tabInline.children = [tabText]; tabOpen.block = true;