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:

    Prop

    Type

  • type: doc

    Markdown/MDX documents, available options:

    Prop

    Type

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

Customise MDX options at the collection level.

This API is only available on doc type.

source.config.ts
import { defineCollections } 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 applyMdxPreset to apply the default MDX preset.

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

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

postprocess

This API is only available on doc type.

You can pass build-time information to runtime using the postprocess API.

  • includeProcessedMarkdown

    Include the processed Markdown content (before being converted into HTML).

    import { defineDocs } from 'fumadocs-mdx/config';
    
    export default defineDocs({
      docs: {
        postprocess: {
          includeProcessedMarkdown: true,
        },
      },
    });

    You can obtain the included content with getText() (e.g. in Fumadocs):

    import { source } from '@/lib/source';
    
    const page = source.getPage('...');
    console.log(await page.data.getText('processed'));
  • valueToExport

    Some remark plugins store their output in vfile.data (compile-time memory) which cannot be accessed from your code.

    You can specify the name of properties to include, they will get converted into ESM exports that you can access when importing the MDX file.

    import { defineDocs } from 'fumadocs-mdx/config';
    
    export default defineDocs({
      docs: {
        postprocess: {
          valueToExport: ['dataName'],
        },
      },
    });

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 content directory from defineDocs:

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

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

schema

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

How is this guide?

Last updated on

On this page