收藏
为你的应用收集内容数据
定义集合
🌐 Define Collections
定义一个集合来解析特定的一组文件。
🌐 Define a collection to parse a certain set of files.
import { defineCollections } from 'fumadocs-mdx/config';
import { z } from 'zod';
export const blog = defineCollections({
type: 'doc',
dir: './content/blog',
schema: z.object({
// schema
}),
// other options
});type
被接受的收藏类型。
🌐 The accepted type of collection.
import { defineCollections } from 'fumadocs-mdx/config';
// only scan for json/yaml files
export const metaFiles = defineCollections({
type: 'meta',
// options
});-
type: meta接受 JSON/YAML 文件,可用选项:
Prop
Type
-
type: docMarkdown/MDX 文档,可用选项:
Prop
Type
schema
用于验证文件数据的模式(doc 类型的前置数据,meta 类型的内容)。
🌐 The schema to validate file data (frontmatter on doc type, content on meta type).
import { defineCollections } from 'fumadocs-mdx/config';
import { z } from 'zod';
export const blog = defineCollections({
type: 'doc',
dir: './content/blog',
schema: z.object({
name: z.string(),
}),
});支持包括 Zod 在内的 Standard Schema 兼容库。
请注意,验证是在构建时进行的,因此输出必须是可序列化的。你也可以传递一个接收转换上下文的函数。
🌐 Note that the validation is done at build time, hence the output must be serializable. You can also pass a function that receives the transform context.
import { defineCollections } from 'fumadocs-mdx/config';
import { z } from 'zod';
export const blog = defineCollections({
type: 'doc',
dir: './content/blog',
schema: (ctx) => {
return z.object({
name: z.string(),
testPath: z.string().default(
// original file path
ctx.path,
),
});
},
});mdxOptions
在集合级别自定义 MDX 选项。
🌐 Customise MDX options at the collection level.
此 API 仅在 doc 类型上可用。
import { defineCollections } from 'fumadocs-mdx/config';
export const blog = defineCollections({
type: 'doc',
mdxOptions: {
// mdx options
},
});根据设计,这将移除由全局配置和 Fumadocs MDX 应用的所有默认设置。你可以完全控制 MDX 选项。
🌐 By design, this will remove all default settings applied by your global config and Fumadocs MDX. You have full control over MDX options.
你可以使用 applyMdxPreset 来应用默认的 MDX 预设。
🌐 You can use applyMdxPreset to apply the default MDX preset.
import { defineCollections } from 'fumadocs-mdx/config';
export const blog = defineCollections({
type: 'doc',
mdxOptions: applyMdxPreset({
// extend the preset
}),
});postprocess
此 API 仅在 doc 类型上可用。
你可以使用 postprocess API 将构建时信息传递给运行时。
🌐 You can pass build-time information to runtime using the postprocess API.
-
includeProcessedMarkdown包含处理过的 Markdown 内容(在转换为 HTML 之前)。
import { defineDocs } from 'fumadocs-mdx/config'; export default defineDocs({ docs: { postprocess: { includeProcessedMarkdown: true, }, }, });你可以使用
getText()(例如在 Fumadocs 中)获取包含的内容:import { source } from '@/lib/source'; const page = source.getPage('...'); console.log(await page.data.getText('processed')); -
valueToExport一些注释插件会将它们的输出存储在
vfile.data(编译时内存)中,你的代码无法访问这些内存。你可以指定要包含的属性名称,它们将被转换为 ESM 导出,你在导入 MDX 文件时可以访问这些导出。
import { defineDocs } from 'fumadocs-mdx/config'; export default defineDocs({ docs: { postprocess: { valueToExport: ['dataName'], }, }, });
定义文档
🌐 Define Docs
为 Fumadocs 定义一个集合。
🌐 Define a collection for Fumadocs.
import { defineDocs } from 'fumadocs-mdx/config';
export const docs = defineDocs({
dir: '/my/content/dir',
docs: {
// optional, options of `doc` collection
},
meta: {
// optional, options of `meta` collection
},
});dir
你应该从 defineDocs 自定义内容目录,而不是按每个集合来操作:
🌐 Instead of per collection, you should customise content directory from defineDocs:
import { defineDocs } from 'fumadocs-mdx/config';
export const docs = defineDocs({
dir: 'content/guides',
});schema
你可以扩展 docs 和 meta 的默认 Zod 4 模式。
🌐 You can extend the default Zod 4 schema of docs and meta.
import { frontmatterSchema, metaSchema, defineDocs } from 'fumadocs-mdx/config';
import { z } from 'zod';
export const docs = defineDocs({
docs: {
schema: frontmatterSchema.extend({
index: z.boolean().default(false),
}),
},
meta: {
schema: metaSchema.extend({
// other props
}),
},
});Last updated on
