fumadocs-core
Version:
The React.js library for building a documentation website
114 lines (113 loc) • 3.69 kB
JavaScript
import { a as handleTag, l as toEsm, n as esmScope } from "../utils-C7sTEOQI.js";
import { a as listCode, i as jsxComponents, m as walkBranches, n as componentsCode, u as isStaticOutput } from "../code-output-BZYBFRsN.js";
import { n as stepNumberCode } from "../steps-CGouhZm4.js";
import { toEstree } from "hast-util-to-estree";
// src/mdx-plugins/rehype-toc.ts
const TocOnlyTag = "[toc]";
const NoTocTag = "[!toc]";
const HeadingTags = /* @__PURE__ */ new Set([
"h1",
"h2",
"h3",
"h4",
"h5",
"h6"
]);
/**
* Read the step number `remarkSteps` assigns to a heading.
*/
function getStep(properties) {
const raw = properties["data-fd-step"] ?? properties.dataFdStep;
if (typeof raw === "number") return raw;
if (typeof raw === "string" && raw.trim() !== "") {
const step = Number(raw);
if (Number.isFinite(step)) return step;
}
}
function rehypeToc({ exportToc = true } = {}) {
if (exportToc === true) exportToc = {
as: "esm",
name: "toc"
};
return (tree, file) => {
/** `hidden` is a step that isn't in the TOC, which counts among the steps */
const items = [];
walkBranches(tree, (node, when, parent) => {
if (node.type !== "element" || !HeadingTags.has(node.tagName)) return;
const { id } = node.properties;
const last = node.children.at(-1);
if (typeof id !== "string" || !last) return "skip";
const text = last.type === "text" ? last : void 0;
const item = {
title: node,
depth: Number(node.tagName[1]),
url: `#${id}`,
_step: getStep(node.properties),
when
};
const noToc = text && handleTag(text.value, NoTocTag);
if (typeof noToc === "string") {
text.value = noToc;
if (item._step !== void 0) items.push({
...item,
hidden: true
});
return "skip";
}
const tocOnly = text && handleTag(text.value, TocOnlyTag);
if (typeof tocOnly === "string") {
text.value = tocOnly;
const siblings = parent.children;
siblings.splice(siblings.indexOf(node), 1);
}
items.push(item);
return "skip";
});
if (exportToc === false) return;
if (exportToc.as === "esm") {
const titles = [];
const list = [];
const components = /* @__PURE__ */ new Set();
const isStatic = isStaticOutput(file.data);
const numbers = file.data._show ? stepNumberCode() : void 0;
for (const { title, when, url, depth, _step, hidden } of items) {
const step = _step === void 0 ? void 0 : numbers?.next(_step, depth) ?? String(_step);
if (hidden) {
if (numbers) list.push({
code: step,
when,
run: true
});
continue;
}
const { children } = toJsx(title);
jsxComponents(children, components);
const i = titles.push({
type: "JSXFragment",
openingFragment: { type: "JSXOpeningFragment" },
closingFragment: { type: "JSXClosingFragment" },
children
});
const id = file.data._headingIds?.[url.slice(1)];
let code = `{depth: ${depth}, url: ${id ? `"#" + ${id}` : JSON.stringify(url)}`;
if (step) code += `, _step: ${step}`;
list.push({
code: `${code}, title: _fumadocs${i - 1}}`,
when
});
}
const declaration = componentsCode(components, esmScope(tree), "props.components ?? {}", "((props) => props.children)");
tree.children.push(toEsm(`export const ${exportToc.name} = ${listCode(list, isStatic, (numbers?.declaration ?? "") + declaration)};`, titles));
} else {
file.data.rehypeToc = [];
for (const { when: _, hidden, ...item } of items) if (!hidden) file.data.rehypeToc.push(item);
}
};
}
function toJsx(element) {
return toEstree(element, {
elementAttributeNameCase: "react",
stylePropertyNameCase: "dom"
}).body[0].expression;
}
export { rehypeToc };