fumadocs-core
Version:
The React.js library for building a documentation website
53 lines (52 loc) • 2.09 kB
JavaScript
import { u as isStaticOutput } from "../code-output-BZYBFRsN.js";
import { n as esmScope, o as isJsxElement, t as createCodeOutput } from "../utils-C7sTEOQI.js";
import { r as stringifyHeadingId } from "../heading-id-C-jg9hhz.js";
// src/mdx-plugins/remark-llms.ts
/**
* Generate Markdown for LLMs from the authored source, other plugins can edit it with
* `replaceSource()` from `fumadocs-core/mdx-plugins/stringifier`.
*
* Authored expressions and the branches of `<show>` are rendered from code.
*/
function remarkLLMs({ as = "_markdown", headingIds = true, _data = false, mdxAsPlaceholder, output, filterElement } = {}) {
return (tree, file) => {
const isStatic = isStaticOutput(file.data, output && output === "string");
const out = createCodeOutput(file, {
target: "markdown",
isStatic
});
const { s } = out;
const visit = (nodes) => {
for (const node of nodes) {
const visibility = filterElement?.(node) ?? true;
if (visibility === false) {
s.replace(node, "");
continue;
}
if ("children" in node) visit(node.children);
if (visibility === "children-only") s.replace(node, (s) => s.inner(node));
else if (node.type === "heading") stringifyHeadingId(out, node, headingIds && (isStatic ? void 0 : file.data._headingIds));
else if (isJsxElement(node) && node.name && mdxAsPlaceholder?.includes(node.name)) s.replace(node, (s) => toPlaceholder(node, out.toStatic(s.inner(node))));
else out.edit(node);
}
};
visit(tree.children);
const text = s.stringify(tree).trim();
const value = text ? `${text}\n` : "";
tree.children.unshift(out.esm(isStatic ? `export const ${as} = ${out.toConcat(value)};` : out.markdownCode(as, value, esmScope(tree))));
if (_data) file.data.markdown = out.toStatic(value);
};
}
function toPlaceholder(node, children) {
const attributes = {};
for (const attr of node.attributes) {
if (attr.type === "mdxJsxExpressionAttribute") continue;
attributes[attr.name] = attr.value;
}
return `\0${JSON.stringify({
name: node.name,
children,
attributes
})}\0`;
}
export { remarkLLMs };