Skip to content

Commit be820c4

Browse files
committed
Docs: Improve Fumadocs MDX section
1 parent 0656e70 commit be820c4

File tree

29 files changed

+1337
-1428
lines changed

29 files changed

+1337
-1428
lines changed

.changeset/thin-panthers-raise.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@fumadocs/content-collections': patch
3+
'@fumadocs/mdx-remote': patch
4+
'fumadocs-typescript': patch
5+
'fumadocs-twoslash': patch
6+
'fumadocs-docgen': patch
7+
'fumadocs-openapi': patch
8+
'fumadocs-core': patch
9+
'fumadocs-mdx': patch
10+
'fumadocs-ui': patch
11+
---
12+
13+
Bump deps

apps/docs/app/layout.client.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { cn } from '@/utils/cn';
88
import { modes } from '@/utils/modes';
99

1010
const itemVariants = cva(
11-
'rounded-md px-2 py-1 transition-colors hover:text-fd-accent-foreground',
11+
'rounded-full px-2 py-1 transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground',
1212
{
1313
variants: {
1414
active: {
@@ -36,7 +36,7 @@ export function NavChildren(): React.ReactElement {
3636
const mode = useMode();
3737

3838
return (
39-
<div className="rounded-md border bg-fd-muted/80 p-1 text-sm text-fd-muted-foreground max-md:absolute max-md:left-1/2 max-md:-translate-x-1/2">
39+
<div className="rounded-full border bg-fd-muted p-0.5 text-sm text-fd-muted-foreground max-md:absolute max-md:left-1/2 max-md:-translate-x-1/2">
4040
{modes.map((m) => (
4141
<Link
4242
key={m.param}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

Comments
 (0)