|
| 1 | +--- |
| 2 | +title: Collections |
| 3 | +description: Collection of content data for your app |
| 4 | +--- |
| 5 | + |
| 6 | +## Define Collections |
| 7 | + |
| 8 | +Define a collection to parse a certain set of files. |
| 9 | + |
| 10 | +```ts |
| 11 | +import { defineCollections } from 'fumadocs-mdx/config'; |
| 12 | +import { z } from 'zod'; |
| 13 | + |
| 14 | +export const blog = defineCollections({ |
| 15 | + type: 'doc', |
| 16 | + dir: './content/blog', |
| 17 | + schema: z.object({ |
| 18 | + // allowed... |
| 19 | + }), |
| 20 | +}); |
| 21 | +``` |
| 22 | + |
| 23 | +```json doc-gen:typescript |
| 24 | +{ |
| 25 | + "file": "./content/docs/mdx/props.ts", |
| 26 | + "name": "Collections" |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### Options |
| 31 | + |
| 32 | +#### `dir` |
| 33 | + |
| 34 | +Directories to scan input files. |
| 35 | + |
| 36 | +#### Schema |
| 37 | + |
| 38 | +The Zod schema to validate file data (frontmatter on `doc` type, content on `meta` type). |
| 39 | + |
| 40 | +```ts |
| 41 | +import { defineCollections } from 'fumadocs-mdx/config'; |
| 42 | +import { z } from 'zod'; |
| 43 | + |
| 44 | +export const blog = defineCollections({ |
| 45 | + type: 'doc', |
| 46 | + dir: './content/blog', |
| 47 | + schema: z.object({ |
| 48 | + name: z.string(), |
| 49 | + }), |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +You can add additional properties to the output. |
| 54 | +Note that the validation is done by build time, hence the output must be serializable. |
| 55 | + |
| 56 | +You can also pass a function and receives the transform context. |
| 57 | + |
| 58 | +```ts |
| 59 | +import { defineCollections } from 'fumadocs-mdx/config'; |
| 60 | +import { z } from 'zod'; |
| 61 | + |
| 62 | +export const blog = defineCollections({ |
| 63 | + type: 'doc', |
| 64 | + dir: './content/blog', |
| 65 | + schema: (ctx) => { |
| 66 | + return z.object({ |
| 67 | + name: z.string(), |
| 68 | + testPath: z.string().default(ctx.path), |
| 69 | + }); |
| 70 | + }, |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +#### Type |
| 75 | + |
| 76 | +The accepted type of collection. |
| 77 | + |
| 78 | +| Type | Description | |
| 79 | +| ------ | ---------------------- | |
| 80 | +| `meta` | JSON/YAML File | |
| 81 | +| `doc` | Markdown/MDX Documents | |
| 82 | + |
| 83 | +#### MDX Options |
| 84 | + |
| 85 | +You can also customise MDX options from collections. |
| 86 | +Notice that passing `mdxOptions` to collection overrides all defaults from global config. |
| 87 | + |
| 88 | +```ts |
| 89 | +import { defineCollections, getDefaultMDXOptions } from 'fumadocs-mdx/config'; |
| 90 | + |
| 91 | +export const blog = defineCollections({ |
| 92 | + type: 'doc', |
| 93 | + mdxOptions: getDefaultMDXOptions({ |
| 94 | + // mdx options |
| 95 | + }), |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +We use `getDefaultMDXOptions` to apply default MDX options, it accepts the [Default MDX Options](/docs/mdx/mdx). |
| 100 | + |
| 101 | +For full control over MDX options, you can pass MDX options without `getDefaultMDXOptions`, which means no defaults will be applied (except the ones from MDX.js). |
| 102 | + |
| 103 | +> This API only available on `doc` type. |
| 104 | +
|
| 105 | +#### Transform |
| 106 | + |
| 107 | +A function to perform runtime transformation on collection entries. |
| 108 | + |
| 109 | +See [Transform](/docs/mdx/transform). |
| 110 | + |
| 111 | +## Define Docs |
| 112 | + |
| 113 | +You can use `defineDocs` to define the required collections to work with Fumadocs. |
| 114 | +It offers the same API as `defineCollections`. |
| 115 | + |
| 116 | +```ts |
| 117 | +import { defineDocs } from 'fumadocs-mdx/config'; |
| 118 | + |
| 119 | +export const { docs, meta } = defineDocs({ |
| 120 | + docs: { |
| 121 | + // optional, you can pass options to each collection |
| 122 | + }, |
| 123 | + meta: { |
| 124 | + // optional, you can pass options to each collection |
| 125 | + }, |
| 126 | +}); |
| 127 | +``` |
| 128 | + |
| 129 | +The `docs` and `meta` are collections on their own. |
| 130 | +You can pass collection options to them as shown above. |
| 131 | + |
| 132 | +### Extend `schema` |
| 133 | + |
| 134 | +You can extend the default Zod schema of `docs` and `meta`. |
| 135 | + |
| 136 | +```ts |
| 137 | +import { frontmatterSchema, metaSchema, defineDocs } from 'fumadocs-mdx/config'; |
| 138 | +import { z } from 'zod'; |
| 139 | + |
| 140 | +export const { docs, meta } = defineDocs({ |
| 141 | + docs: { |
| 142 | + schema: frontmatterSchema.extend({ |
| 143 | + index: z.boolean().default(false), |
| 144 | + }), |
| 145 | + }, |
| 146 | + meta: { |
| 147 | + schema: metaSchema.extend({ |
| 148 | + // other props |
| 149 | + }), |
| 150 | + }, |
| 151 | +}); |
| 152 | +``` |
0 commit comments