Skip to content

Commit

Permalink
fix: auto tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Nov 19, 2024
1 parent d1f9071 commit aa956be
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 342 deletions.
48 changes: 38 additions & 10 deletions lib/auto-tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ function isDirective(node) {
return node.type === 'containerDirective' && node.name === 'TabItem';
}

function findGroups(nodes) {
const groups = [];
let currentGroup = false;

for (const item of nodes) {
const isStart = item.is === 'TabItem-tag' && item.isStartTag;
const isEnd = item.is === 'TabItem-tag' && item.isEndTag;

if (isStart) {
currentGroup = { items: [item], start: nodes.indexOf(item) };
} else if (currentGroup) {
currentGroup.items.push(item);
}

if (isEnd && currentGroup) {
groups.push(currentGroup);
currentGroup = false;
}
}

return groups.reverse();
}

/* eslint-disable no-param-reassign */
export function autoTabs({ labels = {} } = {}) {
assert(
Expand Down Expand Up @@ -152,23 +175,28 @@ export function autoTabs({ labels = {} } = {}) {
};
});

visit(tree, isEndItem, (node, endIndex, parent) => {
visit(tree, isEndItem, (node, endIndex) => {
haveTabs = true;

visit(tree, isFirstItem(endIndex), (_, startIndex) => {
const io = parent.children.splice(
startIndex,
endIndex - startIndex + 1,
);
node.isEndTag = true;

parent.children.splice(startIndex, 0, {
visit(tree, isFirstItem(endIndex), (_) => {
_.isStartTag = true;
});
});

const io = findGroups(tree.children);

for (const { items, start } of io) {
if (items.length > 0) {
tree.children.splice(start, items.length, {
type: 'mdxJsxFlowElement',
name: 'Tabs',
is: 'Tabs-tag',
children: io,
children: items,
});
});
});
}
}

if (haveTabs && needImport(tree.children, '@theme/Tabs')) {
tree.children.unshift(
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remark-docusaurus",
"version": "0.4.4",
"version": "0.4.5",
"description": "Remark plugin for docusaurus features",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -49,8 +49,8 @@
"devDependencies": {
"@bring-it/npm": "^0.5.9",
"@nice-move/cli": "^0.11.16",
"@nice-move/eslint-config-base": "^0.11.27",
"@nice-move/prettier-config": "^0.14.8",
"@nice-move/eslint-config-base": "^0.11.28",
"@nice-move/prettier-config": "^0.14.10",
"ava": "^6.2.0",
"eslint": "^8.57.1",
"eslint-plugin-ava": "^14.0.0",
Expand Down
Loading

0 comments on commit aa956be

Please sign in to comment.