fumadocs-core
Version:
The React.js library for building a documentation website
96 lines (95 loc) • 3.64 kB
JavaScript
import { createStringifier } from "./mdx-plugins/stringifier.js";
import { o as moduleScope, r as createCodeOutput$1 } from "./code-output-BZYBFRsN.js";
import { valueToEstree } from "estree-util-value-to-estree";
import { Parser } from "acorn";
import acornJsx from "acorn-jsx";
// src/mdx-plugins/utils.ts
function isJsxElement(node) {
return node.type === "mdxJsxFlowElement" || node.type === "mdxJsxTextElement";
}
/**
* Visit a tree in document order, returning `skip` skips the children of a node. Unlike `unist-util-visit`, it doesn't
* look up the index of each node in its parent, which is quadratic on wide trees.
*/
function walk(node, visitor, index, parent) {
if (visitor(node, index, parent) === "skip" || !node.children) return;
for (let i = 0; i < node.children.length; i++) walk(node.children[i], visitor, i, node);
}
function flattenNode(node) {
if ("children" in node) return node.children.map(flattenNode).join("");
if ("value" in node && typeof node.value === "string") return node.value;
return "";
}
function flattenNodeHast(node) {
if ("children" in node) return node.children.map(flattenNodeHast).join("");
return "value" in node && typeof node.value === "string" ? node.value : "";
}
function toMdxExport(name, value) {
return toEsm(`export let ${name} = _fumadocs0;`, [valueToEstree(value)]);
}
function handleTag(value, tag) {
const idx = value.indexOf(tag);
if (idx !== -1) return value.slice(0, idx).trimEnd() + value.slice(idx + tag.length);
return false;
}
const parser = Parser.extend(acornJsx());
const placeholderRegex = /^_fumadocs(\d+)$/;
function parse(code) {
return parser.parse(code, {
ecmaVersion: "latest",
sourceType: "module"
});
}
/** the code of an ESM block, the identifiers `_fumadocs<index>` are the expressions */
function toEsm(code, expressions = []) {
const estree = parse(code);
if (expressions.length > 0) fill(estree, expressions);
return {
type: "mdxjsEsm",
value: "",
data: { estree }
};
}
function fill(node, expressions) {
const record = node;
for (const key in record) {
const value = record[key];
if (typeof value !== "object" || value === null) continue;
const match = value.type === "Identifier" && placeholderRegex.exec(value.name);
if (match) record[key] = expressions[Number(match[1])];
else fill(value, expressions);
}
}
/** the value of a JSX attribute from code */
function toAttributeValue(code) {
return {
type: "mdxJsxAttributeValueExpression",
value: code,
data: { estree: parse(code) }
};
}
/** the shared code output, on the stringifier of mdast: estree expressions of replacements go in by placeholders */
function createCodeOutput(file, options) {
const expressions = [];
const out = createCodeOutput$1({
...options,
stringifier: (expression) => createStringifier(file, options.target, (value, fallback) => expression(`_fumadocs${expressions.push(value) - 1}`, fallback)),
parse,
literal: (value) => `_fumadocs${expressions.push({
type: "Literal",
value
}) - 1}`
});
return Object.assign(out, { esm: (code) => toEsm(code, expressions) });
}
/** read the names declared by the ESM of a document, wherever its nodes are (like in the content of includes) */
function esmScope(tree) {
return () => {
const scope = /* @__PURE__ */ new Set();
walk(tree, (node) => {
if (node.type === "mdxjsEsm" && node.data?.estree) moduleScope(node.data.estree.body, scope);
});
return scope;
};
}
export { handleTag as a, toAttributeValue as c, walk as d, flattenNodeHast as i, toEsm as l, esmScope as n, isJsxElement as o, flattenNode as r, parse as s, createCodeOutput as t, toMdxExport as u };