MDX Collection
Compile Markdown/MDX into JavaScript files.
Usage
import { defineConfig } from "fuma-content/config";
import { mdxCollection } from "fuma-content/collections/mdx";
import { z } from "zod";
const docs = mdxCollection({
dir: "content",
// optional: define frontmatter schema (Standard Schema compatible)
frontmatter: z.object({
title: z.string(),
}),
// optional: MDX options
options: () => ({
jsxImportSource: "react",
}),
});
export default defineConfig({
collections: { docs },
});It generates:
<name>.ts: server-side access to compiled properties.<name>.browser.ts: client-side access, with framework integrations for React.
import { docs } from "content/docs";
for (const file of docs.list()) {
const id = file.id;
// compiled properties & typed frontmatter
const { frontmatter, ...rest } = file.compiled;
}
// or from ID:
console.log(docs.get("index.mdx"));Lazy Mode
It leverages async imports to support lazy loading, or lazy compilation (Vite only).
import { defineConfig } from "fuma-content/config";
import { mdxCollection } from "fuma-content/collections/mdx";
const docs = mdxCollection({
dir: "content",
lazy: true,
});The compiled properties now require calling load() to access.
import { docs } from "content/docs";
for (const file of docs.list()) {
const id = file.id;
// typed frontmatter
console.log(file.frontmatter);
// compiled properties
console.log(await file.load());
}Lazy mode only effects the server-side integration, client-side usage is not changed.
Post Process
You can store build-time properties and access them at runtime.
processedMarkdown: the processed Markdown content, useful for generating content for LLM.mdast: the processed MDAST, useful for runtime AST analysis.linkReferences: extract link references (e.g. href), useful for reference analysis.
import { defineConfig } from "fuma-content/config";
import { mdxCollection } from "fuma-content/collections/mdx";
import { z } from "zod";
const docs = mdxCollection({
dir: "content",
postprocess: {
processedMarkdown: true,
// or
processedMarkdown: { as: "markdownContent" },
mdast: true,
// or
mdast: { as: "ast" },
linkReferences: true,
// or
linkReferences: { as: "links" },
},
});Fuma Content will generate types automatically.