fumadocs-core
Version:
The React.js library for building a documentation website
91 lines (90 loc) • 3.21 kB
JavaScript
import { d as walk, l as toEsm, n as esmScope, r as flattenNode, s as parse } from "../utils-C7sTEOQI.js";
import { m as walkBranches } from "../code-output-BZYBFRsN.js";
import { n as headingIdRegex, t as headingIdCode } from "../heading-id-C-jg9hhz.js";
import Slugger from "github-slugger";
import { toEstree } from "hast-util-to-estree";
// src/mdx-plugins/remark-heading.ts
/**
* Add heading ids and extract TOC.
*
* For a content source that renders pages for props, the IDs of headings with authored expressions or in branches of
* `<show>` are slugged when rendering: {@link rehypeHeading} gives them to the headings.
*/
function remarkHeading({ slug, slugImportSource, customId = true, generateToc = true } = {}) {
let slugger;
const hasRuntime = !slug || slugImportSource !== void 0;
if (!slug) {
slugger = new Slugger();
slug = (_root, _heading, text) => slugger.slug(text);
}
return (root, file) => {
const toc = [];
const ids = headingIdCode(slugImportSource);
const fromCode = hasRuntime && file.data.unstable_runtimeOutputs;
slugger?.reset();
walkBranches(root, (heading, when) => {
if (heading.type !== "heading") return;
const props = (heading.data ||= {}).hProperties ||= {};
const lastNode = heading.children.at(-1);
if (lastNode?.type === "text" && customId) {
const match = headingIdRegex.exec(lastNode.value);
if (match?.[1]) {
props.id = match[1];
lastNode.value = lastNode.value.slice(0, match.index);
}
}
let text;
if (!props.id) {
text = flattenNode(heading);
props.id = slug(root, heading, text);
const code = fromCode && ids.next(heading.children, when);
if (code) (file.data._headingIds ??= {})[props.id] = code;
} else {
slug(root, heading, props.id);
if (fromCode) ids.next([{
type: "text",
value: props.id
}], when);
}
if (generateToc) toc.push({
title: text ?? flattenNode(heading),
url: `#${props.id}`,
depth: heading.depth
});
return "skip";
});
const esm = ids.esm(parse, esmScope(root));
if (esm) root.children.unshift(toEsm(esm));
if (generateToc) file.data.toc = toc;
};
}
/**
* Give headings their IDs from code, the IDs that {@link remarkHeading} computes for a content source that renders
* pages for props. It runs after the plugins that read headings: a heading with an ID from code is JSX.
*/
function rehypeHeading() {
return (tree, file) => {
const ids = file.data._headingIds;
if (!ids) return;
walk(tree, (node, index, parent) => {
const code = node.type === "element" && ids[node.properties.id];
if (!code || !parent) return;
const estree = toEstree(node, {
elementAttributeNameCase: "react",
stylePropertyNameCase: "dom"
});
const { expression } = estree.body[0];
for (const attribute of expression.openingElement.attributes) if (attribute.type === "JSXAttribute" && attribute.name.name === "id") attribute.value = {
type: "JSXExpressionContainer",
expression: toEsm(code).data.estree.body[0].expression
};
parent.children[index] = {
type: "mdxFlowExpression",
value: "",
data: { estree }
};
return "skip";
});
};
}
export { rehypeHeading, remarkHeading };