Fumadocs

集合

您的应用的 content 数据集合

定义集合

定义一个集合来解析特定文件集。

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

集合的接受类型。

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: doc

    Markdown/MDX 文档,可用选项:

    Prop

    Type

schema

用于验证文件数据的模式(doc 类型的前置元数据,meta 类型的内容)。

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(),
  }),
});

支持与 Standard Schema 兼容的库,包括 Zod。

请注意,验证是在构建时进行的,因此输出必须是可序列化的。 您也可以传递一个接收转换上下文的函数。

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 选项。

此 API 仅适用于 doc 类型。

source.config.ts
import { defineCollections, getDefaultMDXOptions } from 'fumadocs-mdx/config';

export const blog = defineCollections({
  type: 'doc',
  mdxOptions: {
    // mdx options
  },
});

根据设计,这将移除您的全局配置和 Fumadocs MDX 应用的所有默认设置。 您可以完全控制 MDX 选项。

您可以使用 getDefaultMDXOptions 来应用默认的 MDX 预设。

source.config.ts
import { defineCollections, getDefaultMDXOptions } from 'fumadocs-mdx/config';

export const blog = defineCollections({
  type: 'doc',
  mdxOptions: getDefaultMDXOptions({
    // extend the preset
  }),
});

postprocess

此 API 仅适用于 doc 类型。

请参阅 Postprocess

定义文档

为 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 中自定义:

import { defineDocs } from 'fumadocs-mdx/config';

export const docs = defineDocs({
  dir: 'content/guides',
});

schema

您可以扩展 docsmeta 的默认 Zod 4 模式。

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
    }),
  },
});

How is this guide?

Last updated on