fumadocs-mdx
Version:
The built-in source for Fumadocs
111 lines (109 loc) • 3.1 kB
JavaScript
import * as path$1 from "node:path";
//#region src/runtime/server.ts
function server(options = {}) {
const { doc: { passthroughs: docPassthroughs = [] } = {} } = options;
function fileInfo(file, base) {
if (file.startsWith("./")) file = file.slice(2);
return {
path: file,
fullPath: path$1.join(base, file)
};
}
function mapDocData(entry) {
const data = {
body: entry.default,
toc: entry.toc,
structuredData: entry.structuredData,
_exports: entry
};
for (const key of docPassthroughs) data[key] = entry[key];
return data;
}
return {
async doc(_name, base, glob) {
return await Promise.all(Object.entries(glob).map(async ([k, v]) => {
const data = typeof v === "function" ? await v() : v;
return {
...mapDocData(data),
...data.frontmatter,
...createDocMethods(fileInfo(k, base), () => data)
};
}));
},
async docLazy(_name, base, head, body) {
return await Promise.all(Object.entries(head).map(async ([k, v]) => {
const data = typeof v === "function" ? await v() : v;
const content = body[k];
return {
...data,
...createDocMethods(fileInfo(k, base), content),
async load() {
return mapDocData(await content());
}
};
}));
},
async meta(_name, base, glob) {
return await Promise.all(Object.entries(glob).map(async ([k, v]) => {
const data = typeof v === "function" ? await v() : v;
return {
info: fileInfo(k, base),
...data
};
}));
},
async docs(name, base, metaGlob, docGlob) {
return {
docs: await this.doc(name, base, docGlob),
meta: await this.meta(name, base, metaGlob),
toFumadocsSource() {
return toFumadocsSource(this.docs, this.meta);
}
};
},
async docsLazy(name, base, metaGlob, docHeadGlob, docBodyGlob) {
return {
docs: await this.docLazy(name, base, docHeadGlob, docBodyGlob),
meta: await this.meta(name, base, metaGlob),
toFumadocsSource() {
return toFumadocsSource(this.docs, this.meta);
}
};
}
};
}
function toFumadocsSource(pages, metas) {
const files = [];
for (const entry of pages) files.push({
type: "page",
path: entry.info.path,
absolutePath: entry.info.fullPath,
data: entry
});
for (const entry of metas) files.push({
type: "meta",
path: entry.info.path,
absolutePath: entry.info.fullPath,
data: entry
});
return { files };
}
function createDocMethods(info, load) {
return {
info,
async getText(type) {
if (type === "raw") return (await (await import("node:fs/promises")).readFile(info.fullPath)).toString();
const data = await load();
if (typeof data._markdown !== "string") throw new Error("getText('processed') requires `includeProcessedMarkdown` to be enabled in your collection config.");
return data._markdown;
},
async getMDAST() {
const data = await load();
if (!data._mdast) throw new Error("getMDAST() requires `includeMDAST` to be enabled in your collection config.");
return JSON.parse(data._mdast);
}
};
}
//#endregion
export { server, toFumadocsSource };
//# sourceMappingURL=server.js.map