|
1 | 1 | import { SKIP, visit, type VisitorResult } from "unist-util-visit"; |
2 | 2 | import type { Root, Element, Parents } from "hast"; |
| 3 | +import { selectAll } from "hast-util-select"; |
3 | 4 |
|
4 | 5 | const remove = (index: number, parent: Parents): VisitorResult => { |
5 | 6 | parent.children.splice(index, 1); |
@@ -92,6 +93,7 @@ const ALLOWED_ELEMENTS = [ |
92 | 93 | "tr", |
93 | 94 | // Custom elements |
94 | 95 | "rule-id", |
| 96 | + "starlight-tabs", |
95 | 97 | ]; |
96 | 98 |
|
97 | 99 | const ALLOWED_ATTRIBUTES: Record<string, string[]> = { |
@@ -150,6 +152,47 @@ export default function () { |
150 | 152 | ], |
151 | 153 | }); |
152 | 154 | } |
| 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 | + } |
153 | 196 | } |
154 | 197 | }); |
155 | 198 | }; |
|
0 commit comments