Fumadocs

Collections

Collection of content data for your app

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

    Accept JSON/YAML Files, available options:

    PropTypeDefault
    type
    "meta"
    -
    schema
    CollectionSchema<Schema, { path: string; source: string; }>
    -
    dir
    string | string[]
    -
    files
    string[]
    -
  • type: doc

    Markdown/MDX Documents, available options:

    PropTypeDefault
    type
    "doc"
    -
    mdxOptions
    MDXOptions
    -
    async
    Async
    -
    schema
    CollectionSchema<Schema, { path: string; source: string; }>
    -
    dir
    string | string[]
    -
    files
    string[]
    -

dir

Directories to scan input files.

schema

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

Standard Schema compatible libraries, including Zod are supported.

Note that the validation is done by build time, hence the output must be serializable. You can also pass a function and 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

Customise MDX options on collection level.

source.config.ts
import { defineCollections, getDefaultMDXOptions } from 'fumadocs-mdx/config';
 
export const blog = defineCollections({
  type: 'doc',
  mdxOptions: {
    // mdx options
  },
});

By design, this will remove all default settings applied by your global config and Fumadocs MDX. You have full control over MDX options.

You can use getDefaultMDXOptions to apply default configurations, it accepts the extended MDX Options.

source.config.ts
import { defineCollections, getDefaultMDXOptions } from 'fumadocs-mdx/config';
 
export const blog = defineCollections({
  type: 'doc',
  mdxOptions: getDefaultMDXOptions({
    // extended mdx options
  }),
});

This API only available on doc type.

Define Docs

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

Instead of per collection, you should customise dir from defineDocs:

import { defineDocs } from 'fumadocs-mdx/config';
 
export const docs = defineDocs({
  dir: 'my/content/dir',
});

schema

You can extend the default Zod 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
    }),
  },
});

How is this guide?

On this page