UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

213 lines (212 loc) • 7.28 kB
import { n as esmScope, t as createCodeOutput, u as toMdxExport } from "./utils-C7sTEOQI.js"; import { a as listCode, m as walkBranches, s as staticTagRegex, u as isStaticOutput } from "./code-output-BZYBFRsN.js"; import { n as headingIdRegex } from "./heading-id-C-jg9hhz.js"; import { remarkHeading } from "./mdx-plugins/remark-heading.js"; import { tableRowToStructuredData } from "./search/index.js"; import { remark } from "remark"; import { VFile } from "vfile"; import remarkGfm from "remark-gfm"; // ../shared/structured-records.ts /** * The records of structured data of a document, in document order: slices of its Markdown for search, see the * `target` of the code output. */ function createStructuredRecords(out, { isType, mdxTypes, tableRow, data: file }) { const { s } = out; const records = []; /** records are stringified after the edits of every node */ const pending = []; let covered; let tables = 0; const push = (item, when = []) => records.push({ ...item, when }); const openapi = file.frontmatter?._openapi?.structuredData; for (const item of openapi?.headings ?? []) push(item); for (const item of openapi?.contents ?? []) push(item); const cells = (row) => { const cells = []; for (const cell of row.children) cells.push(s.inner(cell).trim()); return cells; }; const flush = () => { for (const record of pending) record(); pending.length = 0; }; return { /** * Edit a node for records, and record it when it is one. * * @param when - read the conditions of the branches of `<show>` around the node */ collect(node, when) { const { type } = node; const isElement = type === "mdxJsxFlowElement" || type === "mdxJsxTextElement"; out.edit(node); const { data } = node; if (data?.structuredData) { const items = data.structuredData.contents; const conditions = when(); pending.push(() => { for (const item of items) push(item, conditions); }); covered = s.range(node) ?? covered; return; } if (!isType(node)) return; const range = s.range(node); if (!range || covered && s.within(range, covered)) return; const line = s.source.indexOf("\n", range.start); if (isElement && line !== -1 && line < range.end && !mdxTypes(node)) return; covered = range; const conditions = when(); if (type === "table") { const table = `table-${tables++}`; pending.push(() => { const [head, ...rows] = node.children; const header = cells(head); for (const row of rows) push(tableRow({ table, row: cells(row), header }), conditions); }); } else if (type === "heading") { const id = data?.hProperties?.id; if (typeof id !== "string") { console.warn("[remark-structure] hProperties.id is missing in heading node, it is required to generate heading data. You can add remark-heading prior to remark-structure to generate heading IDs."); return; } pending.push(() => { const content = s.inner(node).replace(headingIdRegex, "").trim(); push({ id, code: file._headingIds?.[id], content }, conditions); }); } else pending.push(() => { const content = s.stringify(node).trim(); if (content.length > 0) push({ heading: void 0, content }, conditions); }); }, /** the structured data of the source: authored expressions as their source, the content of every branch */ toStatic() { flush(); const contents = []; for (const record of records) contents.push(out.toStatic(record.content)); return toStructuredData(records, contents); }, /** the code of the records that show for the props of a page, a function of the props: see `structured-data.ts` */ toCode(scope) { flush(); const items = []; for (const record of records) { let code = `{content: ${out.toCode(record.content)}`; if ("id" in record) code += `, id: ${record.code ?? JSON.stringify(record.id)}`; else { if (record.heading !== void 0) code += `, heading: ${JSON.stringify(record.heading)}`; if (record.table) code += `, table: ${JSON.stringify(record.table)}`; } items.push({ code: `${code}}`, when: record.when }); } return listCode(items, false, out.componentsCode(scope, "props.components")); } }; } /** * Structured data from its records in document order and their content as text: content goes under the last heading * before it, records are trimmed and empty ones are left out. */ function toStructuredData(records, contents) { const data = { contents: [], headings: [] }; let heading; for (let i = 0; i < records.length; i++) { const { id, table, heading: own = heading } = records[i]; const content = contents[i].trim(); if (id !== void 0) heading = id; if (!content) continue; if (id !== void 0) data.headings.push({ id, content }); else data.contents.push(table ? { heading: own, content, table } : { heading: own, content }); } return data; } // src/mdx-plugins/remark-structure.ts const remarkStructureDefaultOptions = { types: [ "heading", "paragraph", "blockquote", "table", "mdxJsxFlowElement" ], mdxTypes(node) { return !node.children || node.children.length === 0; }, filterElement(node) { return staticTagRegex.test(node.name ?? ""); }, exportAs: false }; /** * Extract content into structured data, as Markdown sliced from the authored source. Links and JSX elements are * replaced by their content, images are removed. * * For a content source that renders pages for props, the export is a function of the props giving the records that show, their expressions and * elements render like in processed Markdown. `vfile.data.structuredData` is static. * * By default, the output is stored into VFile (`vfile.data.structuredData`), you can specify `exportAs` to export it. */ function remarkStructure({ types = remarkStructureDefaultOptions.types, mdxTypes = remarkStructureDefaultOptions.mdxTypes, filterElement, exportAs = remarkStructureDefaultOptions.exportAs } = {}) { const isType = Array.isArray(types) ? (node) => types.includes(node.type) : types; return (tree, file) => { const isStatic = isStaticOutput(file.data); const out = createCodeOutput(file, { target: "search", isStatic, keep: filterElement }); const records = createStructuredRecords(out, { isType, mdxTypes: (node) => mdxTypes(node), tableRow: tableRowToStructuredData, data: file.data }); walkBranches(tree, (node, when) => records.collect(node, () => when)); const data = records.toStatic(); file.data.structuredData = data; if (!exportAs) return; const name = typeof exportAs === "string" ? exportAs : "structuredData"; tree.children.unshift(isStatic ? toMdxExport(name, data) : out.esm(`export const ${name} = ${records.toCode(esmScope(tree))};`)); }; } /** * Extract data from markdown/mdx content */ function structure(content, remarkPlugins = [], options = {}) { const processor = remark().use(remarkGfm).use(remarkPlugins).use(remarkHeading).use(remarkStructure, options); const file = new VFile(content); processor.runSync(processor.parse(file), file); return file.data.structuredData; } export { remarkStructureDefaultOptions as n, structure as r, remarkStructure as t };