Skip to content

Commit cf85cd4

Browse files
authored
feat(config): provide defineContentConfig utility (#2891)
1 parent 9e016ee commit cf85cd4

File tree

7 files changed

+76
-55
lines changed

7 files changed

+76
-55
lines changed

docs/content.config.ts

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
import { defineCollection, z } from '@nuxt/content'
1+
import { defineContentConfig, defineCollection, z } from '@nuxt/content'
22

3-
export const collections = {
4-
docs: defineCollection({
5-
type: 'page',
6-
source: 'docs/**',
7-
schema: z.object({
8-
links: z.array(z.object({
9-
label: z.string(),
10-
icon: z.string(),
11-
avatar: z.object({
12-
src: z.string(),
13-
alt: z.string(),
14-
}).optional(),
15-
to: z.string(),
16-
target: z.string().optional(),
17-
})).optional(),
3+
export default defineContentConfig({
4+
collections: {
5+
docs: defineCollection({
6+
type: 'page',
7+
source: 'docs/**',
8+
schema: z.object({
9+
links: z.array(z.object({
10+
label: z.string(),
11+
icon: z.string(),
12+
avatar: z.object({
13+
src: z.string(),
14+
alt: z.string(),
15+
}).optional(),
16+
to: z.string(),
17+
target: z.string().optional(),
18+
})).optional(),
19+
}),
1820
}),
19-
}),
20-
home: defineCollection({
21-
type: 'page',
22-
source: 'index.md',
23-
}),
24-
}
21+
home: defineCollection({
22+
type: 'page',
23+
source: 'index.md',
24+
}),
25+
},
26+
})

docs/content/docs/1.getting-started/2.installation.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ export default defineNuxtConfig({
4040
Create a `content.config.ts` file in your project root directory:
4141

4242
```ts [content.config.ts]
43-
import { defineCollection } from '@nuxt/content'
44-
45-
export const collections = {
46-
content: defineCollection({
47-
type: 'page',
48-
source: '**/*.md'
49-
})
50-
}
43+
import { defineContentConfig, defineCollection } from '@nuxt/content'
44+
45+
export default defineContentConfig({
46+
collections: {
47+
content: defineCollection({
48+
type: 'page',
49+
source: '**/*.md'
50+
})
51+
}
52+
})
5153
```
5254

5355
This configuration creates a default `content` collection that processes all Markdown files located in the `content` folder of your project. You can customize the collection settings based on your needs.

docs/content/index.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -317,20 +317,22 @@ orientation: horizontal
317317
```
318318

319319
```ts [content.config.ts]
320-
import { defineCollection, z } from '@nuxt/content'
320+
import { defineContentConfig, defineCollection, z } from '@nuxt/content'
321321

322-
export const collections = {
323-
blog: defineCollection({
324-
source: 'blog/*.md',
325-
type: 'page',
326-
// Define custom schema for docs collection
327-
schema: z.object({
328-
tags: z.array(z.string()),
329-
image: z.string(),
330-
date: z.Date()
322+
export default defineContentConfig({
323+
collections: {
324+
blog: defineCollection({
325+
source: 'blog/*.md',
326+
type: 'page',
327+
// Define custom schema for docs collection
328+
schema: z.object({
329+
tags: z.array(z.string()),
330+
image: z.string(),
331+
date: z.Date()
332+
})
331333
})
332-
})
333-
}
334+
}
335+
})
334336
```
335337
:::
336338

examples/basic/content.config.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { defineCollection } from '@nuxt/content'
1+
import { defineContentConfig, defineCollection } from '@nuxt/content'
22

3-
export const collections = {
4-
content: defineCollection({
5-
type: 'page',
6-
source: '**',
7-
}),
8-
}
3+
export default defineContentConfig({
4+
collections: {
5+
content: defineCollection({
6+
type: 'page',
7+
source: '**',
8+
}),
9+
},
10+
})

playground/content.config.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineCollection, z } from '@nuxt/content'
1+
import { defineContentConfig, defineCollection, z } from '@nuxt/content'
22

33
const content = defineCollection({
44
type: 'page',
@@ -42,7 +42,7 @@ const pages = defineCollection({
4242
source: 'pages/**',
4343
})
4444

45-
export const collections = {
45+
const collections = {
4646
content,
4747
data,
4848
pages,
@@ -77,3 +77,5 @@ export const collections = {
7777
},
7878
}),
7979
}
80+
81+
export default defineContentConfig({ collections })

src/utils/config.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { stat } from 'node:fs/promises'
2-
import { loadConfig } from 'c12'
2+
import { loadConfig, createDefineConfig } from 'c12'
33
import type { Nuxt } from '@nuxt/schema'
44
import { join } from 'pathe'
5-
import type { ResolvedCollection } from '../types'
5+
import type { ResolvedCollection, DefinedCollection } from '../types'
66
import { defineCollection, resolveCollections } from './collection'
77
import { logger } from './dev'
88

9-
const defaultConfig = {
9+
type NuxtContentConfig = {
10+
collections: Record<string, DefinedCollection>
11+
}
12+
13+
const defaultConfig: NuxtContentConfig = {
1014
collections: {
1115
content: defineCollection({
1216
type: 'page',
@@ -15,8 +19,14 @@ const defaultConfig = {
1519
},
1620
}
1721

22+
export const defineContentConfig = createDefineConfig<NuxtContentConfig>()
23+
1824
export async function loadContentConfig(rootDir: string, opts: { defaultFallback?: boolean } = {}) {
19-
const { config, configFile } = await loadConfig({ name: 'content', cwd: rootDir })
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
(globalThis as any).defineContentConfig = (c: any) => c
27+
const { config, configFile } = await loadConfig<NuxtContentConfig>({ name: 'content', cwd: rootDir })
28+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
29+
delete (globalThis as any).defineContentConfig
2030

2131
if ((!configFile || configFile === 'content.config') && opts.defaultFallback) {
2232
logger.warn('`content.config.ts` is not found, falling back to default collection. In order to have full control over your collections, create the config file in project root. See: https://content.nuxt.com/getting-started/installation')

src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { metaSchema, pageSchema } from './schema'
22
export { defineCollection } from './collection'
3+
export { defineContentConfig } from './config'
34
export { z } from './zod'

0 commit comments

Comments
 (0)