Skip to content

Commit 18ed4df

Browse files
committed
unwrap tabs
1 parent cdd9506 commit 18ed4df

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

package-lock.json

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"fast-xml-parser": "5.0.8",
7171
"github-slugger": "2.0.0",
7272
"globals": "16.0.0",
73+
"hast-util-select": "6.0.4",
7374
"hastscript": "9.0.1",
7475
"he": "1.2.0",
7576
"jsonc-parser": "3.3.1",

src/plugins/rehype/filter-elements.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SKIP, visit, type VisitorResult } from "unist-util-visit";
22
import type { Root, Element, Parents } from "hast";
3+
import { selectAll } from "hast-util-select";
34

45
const remove = (index: number, parent: Parents): VisitorResult => {
56
parent.children.splice(index, 1);
@@ -92,6 +93,7 @@ const ALLOWED_ELEMENTS = [
9293
"tr",
9394
// Custom elements
9495
"rule-id",
96+
"starlight-tabs",
9597
];
9698

9799
const ALLOWED_ATTRIBUTES: Record<string, string[]> = {
@@ -150,6 +152,47 @@ export default function () {
150152
],
151153
});
152154
}
155+
156+
if (tag === "starlight-tabs") {
157+
const tabs = selectAll('[role="tab"]', element);
158+
const panels = selectAll('[role="tabpanel"]', element);
159+
160+
element.tagName = "ul";
161+
element.properties = {};
162+
element.children = [];
163+
164+
for (const tab of tabs) {
165+
const id = (tab.properties?.id as string)?.split("tab-")[1];
166+
if (!id) continue;
167+
168+
const panel = panels.find(
169+
(panel) => panel.properties?.id === `tab-panel-${id}`,
170+
);
171+
if (!panel) continue;
172+
173+
const label = tab.children
174+
.filter((child) => child.type === "text" && child.value.trim())
175+
.map((child) => child.type === "text" && child.value.trim())
176+
.join("");
177+
178+
const el = {
179+
type: "element",
180+
tagName: "li",
181+
properties: {},
182+
children: [
183+
{
184+
type: "element",
185+
tagName: "p",
186+
children: [{ type: "text", value: label }],
187+
properties: {},
188+
},
189+
panel,
190+
],
191+
} as Element;
192+
193+
element.children.push(el);
194+
}
195+
}
153196
}
154197
});
155198
};

0 commit comments

Comments
 (0)