Skip to content

Commit

Permalink
fix(plugins/tabs): save line mapping for tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
moki committed Jun 30, 2023
1 parent 2e90818 commit 3e695db
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/transform/plugins/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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':
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 3e695db

Please sign in to comment.