fumadocs-core
Version:
The React.js library for building a documentation website
87 lines (86 loc) • 3.58 kB
JavaScript
import { d as showCondition, i as jsxComponents, n as componentsCode, t as and } from "./code-output-BZYBFRsN.js";
// ../shared/heading-id.ts
/** a custom ID at the end of heading text, like `[#id]` */
const headingIdRegex = /\s*\[#(?<slug>[^]+?)]\s*$/;
/** the default runtime of ID code, function bodies read it from `arguments[0]` like the JSX runtime */
const headingRuntime = "fumadocs-core/mdx-plugins/remark-heading.runtime";
/**
* The text of inline content as code, the items of an array of its parts: authored expressions are values, the
* content of a branch of `<show>` is there when it shows. Like in React, only strings and numbers are text.
*
* @param onCode - called when a part is code
*/
function textParts(nodes, onCode) {
let parts = "";
for (const node of nodes) {
const branch = showCondition(node);
if (node.type === "mdxTextExpression") {
onCode();
parts += `...[${node.value}\n],`;
} else if (branch) {
onCode();
parts += `...((${branch}) ? [${textParts(node.children, onCode)}] : []),`;
} else if (node.children) parts += textParts(node.children, onCode);
else if (typeof node.value === "string") parts += `${JSON.stringify(node.value)},`;
}
return parts;
}
/**
* IDs slugged when rendering, from the first heading that has an authored expression, or is in or around a branch of
* `<show>`. The headings are slugged in document order, so only the ones that show count for duplicates.
*
* @param runtime - the module that the code imports `createHeadingIds()` from
*/
function headingIdCode(runtime = headingRuntime) {
const entries = [];
let dynamic = false;
const onCode = () => {
dynamic = true;
};
return {
/**
* The code of the ID of the next heading in document order, when it renders from code.
*
* @param when - the conditions of the branches around the heading
*/
next(nodes, when) {
const test = and(when);
if (test) dynamic = true;
const id = `_slug([${textParts(nodes, onCode)}])`;
entries.push(test ? `${test} ? ${id} : ""` : id);
if (dynamic) return `_headingIds(props)[${entries.length - 1}]`;
},
/**
* The ESM code that the IDs from code read. It declares the components of authored code, see
* {@link componentsCode}: only strings and numbers are text, so the components don't have to be there.
*
* @param parse - parse a JSX expression
*/
esm(parse, scope) {
if (!dynamic) return;
const list = `[${entries.join(", ")}]`;
const declaration = componentsCode(jsxComponents(parse(list)), scope, "props.components ?? {}");
return `import { createHeadingIds as _createHeadingIds } from ${JSON.stringify(runtime)};\nexport const _headingIds = _createHeadingIds((props, _slug) => {\n${declaration}\nreturn ${list};\n});`;
}
};
}
/**
* Write the ID of a heading in stringified Markdown, the one that `remarkHeading` gave it unless it has a custom ID.
*
* @param ids - the IDs from code, `false` removes custom IDs
*/
function stringifyHeadingId({ s, expression }, node, ids) {
const start = s.range(node)?.start;
const last = node.children[node.children.length - 1];
const end = last && s.range(last)?.end;
if (start === void 0 || end === void 0) return;
const marker = headingIdRegex.exec(s.source.slice(start, end));
const id = node.data?.hProperties?.id;
if (ids === false) {
if (marker) s.edit(start + marker.index, end, "", node);
} else if (id != null && !marker) {
const code = ids?.[id];
s.edit(end, end, ` [#${code ? expression(code, id) : id}]`, node);
}
}
export { headingIdRegex as n, stringifyHeadingId as r, headingIdCode as t };