UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

136 lines (135 loc) • 4.19 kB
import { d as showCondition, l as groupLists } from "./code-output-BZYBFRsN.js"; // ../shared/steps.ts /** the properties of the element of a node */ function properties(node) { return node.data?.hProperties; } const StepRegex = /^(\d+)\.\s(.+)$/; const StepTag = "[step]"; function removeTag(text) { const idx = text.indexOf(StepTag); return idx === -1 ? text : text.slice(0, idx).trimEnd() + text.slice(idx + 6); } /** * Read a heading as a step, `1. Title` or `Title [step]`: its text with the syntax of the step, and the text without * it. */ function readStep(heading) { const content = heading.children; const head = content[0]; const match = head?.type === "text" && StepRegex.exec(head.value); if (match) return { text: head, value: match[2], tag: false }; const tail = content[content.length - 1]; if (tail?.type === "text" && tail.value.includes(StepTag)) return { text: tail, value: removeTag(tail.value), tag: true }; } /** * Group the steps among sibling nodes: headings like `1. Title` or `Title [step]`. Each step is wrapped with its * content, a heading of another kind at their level or above ends the list. * * @returns the siblings, `undefined` without steps */ function groupSteps(children, options) { const { steps, step, set, searchSource } = options; const div = (className, children) => ({ type: "mdxJsxFlowElement", name: "div", attributes: [{ type: "mdxJsxAttribute", name: "className", value: className }], children }); return groupLists(children, (node, first) => { if (node.type !== "heading" || properties(node)?.["data-fd-step"] !== void 0) return; if (first && node.depth !== first.depth) return node.depth < first.depth ? false : void 0; return readStep(node) ?? false; }, (nodes, items) => { const headings = /* @__PURE__ */ new Set(); for (const { node, data } of items) { set(data.text, "value", data.value); if (data.tag) searchSource(data.text, removeTag); set(node, "data", { ...node.data, hProperties: { ...properties(node), "data-fd-step": headings.add(node).size } }); } /** the steps: their heading, then their content */ const wrapped = []; /** wrap each step of the nodes with its content, the nodes before the first step stay */ const wrap = (nodes) => { const out = []; let current; for (const node of nodes) { const count = wrapped.length; const inner = showCondition(node) ? wrap(node.children) : void 0; if (headings.has(node)) { wrapped.push(current = [node]); out.push(div(step, current)); } else if (wrapped.length > count) { current = void 0; set(node, "children", inner); out.push(node); } else if (current) current.push(node); else out.push(node); } return out; }; const list = div(steps, wrap(nodes)); for (const content of wrapped) { const inner = groupSteps(content.slice(1), options); if (inner) content.splice(1, content.length, ...inner); } return [list]; }, (branch, children) => set(branch, "children", children)); } /** * Number the steps of a list output that is read in document order, like the TOC: a variable of each list of steps * counts its steps that show. A list can be inside a step of another list. */ function stepNumberCode() { /** the lists a step can go on: the depth of their headings and the number of their last step */ const open = []; let lists = 0; let declaration = ""; return { /** the code that declares the variables */ get declaration() { return declaration; }, /** * The code of the number of a step, every step of a list is given in document order. * * @param step - its number among the steps of its list */ next(step, depth) { let list = open[open.length - 1]; while (step > 1 && list && (list.depth !== depth || list.last !== step - 1)) { open.pop(); list = open[open.length - 1]; } if (step === 1 || !list) { open.push(list = { depth, last: step, name: `_step${++lists}` }); declaration += `let ${list.name} = ${step - 1};\n`; } list.last = step; return `++${list.name}`; } }; } export { stepNumberCode as n, groupSteps as t };