UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

155 lines (154 loc) • 5.15 kB
import { parseCodeBlockAttributes } from "./mdx-plugins/codeblock-utils.js"; import { c as toAttributeValue, d as walk } from "./utils-C7sTEOQI.js"; import { a as listCode, l as groupLists, p as showElement, t as and } from "./code-output-BZYBFRsN.js"; // ../shared/code-tab.ts const elements = { CodeBlockTabs: { list: "CodeBlockTabsList", trigger: "CodeBlockTabsTrigger", tab: "CodeBlockTab" }, Tabs: { list: "TabsList", trigger: "TabsTrigger", tab: "TabsContent" } }; /** * Group the code blocks with a tab among sibling nodes into tabs. A tab is where its code blocks are, so the tab of a * code block in a branch shows with the branch. * * @returns the siblings, `undefined` without tabs */ function groupCodeTabs(children, { type, inElement, tab, set, title, expression }) { const names = elements[type]; const asItems = type === "Tabs" && !title; const attribute = (name, value) => ({ type: "mdxJsxAttribute", name, value }); const element = (name, attributes, children) => ({ type: "mdxJsxFlowElement", name, attributes, children }); return groupLists(children, (node) => tab(node) ?? false, (nodes, items) => { const codes = /* @__PURE__ */ new Map(); /** the tabs in document order, with the code of the condition a tab shows on */ const tabs = /* @__PURE__ */ new Map(); for (const { node, data, when } of items) { codes.set(node, data); const test = and(when); const other = tabs.get(data.value); tabs.set(data.value, tabs.has(data.value) ? other && test && `${other} || ${test}` : test); } /** the tabs of sibling nodes: a branch stays, with the tabs of the code blocks inside it */ const content = (nodes) => { const out = []; const tabs = /* @__PURE__ */ new Map(); for (const node of nodes) { const code = codes.get(node); if (!code) { set(node, "children", content(node.children)); out.push(node); continue; } let list = tabs.get(code.value); if (!list) { tabs.set(code.value, list = []); out.push(element(asItems ? "Tab" : names.tab, [attribute("value", code.value)], list)); } set(node, "meta", code.meta); list.push(node); } return out; }; const out = content(nodes); if (!asItems) { const triggers = []; for (const [value, when] of tabs) { const trigger = element(names.trigger, [attribute("value", value)], title ? title(value) : [{ type: "text", value }]); triggers.push(when ? element(showElement, [attribute("when", expression(when))], [trigger]) : trigger); } out.unshift(element(names.list, [], triggers)); } if (inElement) return out; const attributes = []; const list = []; let conditional = false; for (const [name, when] of tabs) { list.push({ code: JSON.stringify(name), when: when ? [when] : [] }); if (when) conditional = true; } /** the code of the tabs that show */ const shown = listCode(list, true); if (conditional) attributes.push(attribute("key", expression(`${shown}.join()`))); if (asItems) attributes.push(attribute("items", expression(shown))); else { const [[first, when]] = tabs; attributes.push(attribute("defaultValue", when ? expression(`${shown}[0]`) : first)); } const { group } = items[0].data; if (type === "CodeBlockTabs" && group) attributes.push(attribute("groupId", group), attribute("persist", null)); return [element(type, attributes, out)]; }, (branch, children) => set(branch, "children", children)); } // src/mdx-plugins/remark-code-tab.ts function readTab(node) { if (node.type !== "code" || !node.meta) return; const { attributes, rest } = parseCodeBlockAttributes(node.meta, ["tab", "tab-group"]); const { tab: value, "tab-group": group } = attributes; if (typeof value !== "string") return; node.data ??= {}; node.data.tab = value; if (typeof group === "string") node.data.tabGroup = group; return { value, group: node.data.tabGroup, meta: rest }; } function remarkCodeTab({ parseMdx = false, Tabs = "CodeBlockTabs" } = {}) { return (tree) => { walk(tree, (node) => { if (!("children" in node)) return "skip"; let type = Tabs; let withMdx = parseMdx; const inElement = node.type === "mdxJsxFlowElement" && (node.name === "CodeBlockTabs" || node.name === "Tabs"); if (inElement) { type = node.name; if (type === "Tabs") withMdx &&= node.attributes.every((attribute) => attribute.type !== "mdxJsxAttribute" || attribute.name !== "items"); } const children = groupCodeTabs(node.children, { type, inElement, tab: readTab, set(node, key, value) { node[key] = value; }, title: withMdx ? (value) => mdxToAst(this, value) : void 0, expression: toAttributeValue }); if (children) node.children = children; }); }; } /** the nodes of the MDX title of a tab */ function mdxToAst(processor, name) { const out = []; for (const child of processor.parse(name).children) if (child.type === "paragraph") out.push(...child.children); else out.push(child); for (const node of out) walk(node, (child) => { delete child.position; }); return out; } export { remarkCodeTab as t };