UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

36 lines (35 loc) • 1.27 kB
import { toJsx } from "../utils/hast-to-jsx.js"; import { use, useMemo } from "react"; import { remark } from "remark"; import remarkRehype from "remark-rehype"; import { VFile } from "vfile"; // src/content/md.ts function createMarkdownRenderer({ rehypePlugins = [], remarkPlugins = [], remarkRehypeOptions } = {}) { const processor = remark().use(remarkPlugins).use(remarkRehype, remarkRehypeOptions).use(rehypePlugins); const promises = {}; function parse(file) { return processor.parse(file); } return { Markdown(props) { const { async = false, children } = props; const file = new VFile(children); const id = `${file.path}:${file.value}`; if (async) { promises[id] ??= processor.run(parse(file), file); const out = use(promises[id]); return toJsx(out, { components: props.components }); } const v = useMemo(() => processor.runSync(parse(file), file), [id]); return toJsx(v, { components: props.components }); }, async MarkdownServer(props) { const file = new VFile(props.children); return toJsx(await processor.run(parse(file), file), { components: props.components }); } }; } function Markdown(props) { return createMarkdownRenderer(props).MarkdownServer(props); } export { Markdown, createMarkdownRenderer };