Skip to content

Commit

Permalink
feat(config): provide defineContentConfig utility (#2891)
Browse files Browse the repository at this point in the history
  • Loading branch information
ineshbose authored Dec 9, 2024
1 parent 9e016ee commit cf85cd4
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 55 deletions.
46 changes: 24 additions & 22 deletions docs/content.config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { defineCollection, z } from '@nuxt/content'
import { defineContentConfig, defineCollection, z } from '@nuxt/content'

export const collections = {
docs: defineCollection({
type: 'page',
source: 'docs/**',
schema: z.object({
links: z.array(z.object({
label: z.string(),
icon: z.string(),
avatar: z.object({
src: z.string(),
alt: z.string(),
}).optional(),
to: z.string(),
target: z.string().optional(),
})).optional(),
export default defineContentConfig({
collections: {
docs: defineCollection({
type: 'page',
source: 'docs/**',
schema: z.object({
links: z.array(z.object({
label: z.string(),
icon: z.string(),
avatar: z.object({
src: z.string(),
alt: z.string(),
}).optional(),
to: z.string(),
target: z.string().optional(),
})).optional(),
}),
}),
}),
home: defineCollection({
type: 'page',
source: 'index.md',
}),
}
home: defineCollection({
type: 'page',
source: 'index.md',
}),
},
})
18 changes: 10 additions & 8 deletions docs/content/docs/1.getting-started/2.installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ export default defineNuxtConfig({
Create a `content.config.ts` file in your project root directory:

```ts [content.config.ts]
import { defineCollection } from '@nuxt/content'

export const collections = {
content: defineCollection({
type: 'page',
source: '**/*.md'
})
}
import { defineContentConfig, defineCollection } from '@nuxt/content'

export default defineContentConfig({
collections: {
content: defineCollection({
type: 'page',
source: '**/*.md'
})
}
})
```

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.
Expand Down
26 changes: 14 additions & 12 deletions docs/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,22 @@ orientation: horizontal
```

```ts [content.config.ts]
import { defineCollection, z } from '@nuxt/content'
import { defineContentConfig, defineCollection, z } from '@nuxt/content'

export const collections = {
blog: defineCollection({
source: 'blog/*.md',
type: 'page',
// Define custom schema for docs collection
schema: z.object({
tags: z.array(z.string()),
image: z.string(),
date: z.Date()
export default defineContentConfig({
collections: {
blog: defineCollection({
source: 'blog/*.md',
type: 'page',
// Define custom schema for docs collection
schema: z.object({
tags: z.array(z.string()),
image: z.string(),
date: z.Date()
})
})
})
}
}
})
```
:::

Expand Down
16 changes: 9 additions & 7 deletions examples/basic/content.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { defineCollection } from '@nuxt/content'
import { defineContentConfig, defineCollection } from '@nuxt/content'

export const collections = {
content: defineCollection({
type: 'page',
source: '**',
}),
}
export default defineContentConfig({
collections: {
content: defineCollection({
type: 'page',
source: '**',
}),
},
})
6 changes: 4 additions & 2 deletions playground/content.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineCollection, z } from '@nuxt/content'
import { defineContentConfig, defineCollection, z } from '@nuxt/content'

const content = defineCollection({
type: 'page',
Expand Down Expand Up @@ -42,7 +42,7 @@ const pages = defineCollection({
source: 'pages/**',
})

export const collections = {
const collections = {
content,
data,
pages,
Expand Down Expand Up @@ -77,3 +77,5 @@ export const collections = {
},
}),
}

export default defineContentConfig({ collections })
18 changes: 14 additions & 4 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { stat } from 'node:fs/promises'
import { loadConfig } from 'c12'
import { loadConfig, createDefineConfig } from 'c12'
import type { Nuxt } from '@nuxt/schema'
import { join } from 'pathe'
import type { ResolvedCollection } from '../types'
import type { ResolvedCollection, DefinedCollection } from '../types'
import { defineCollection, resolveCollections } from './collection'
import { logger } from './dev'

const defaultConfig = {
type NuxtContentConfig = {
collections: Record<string, DefinedCollection>
}

const defaultConfig: NuxtContentConfig = {
collections: {
content: defineCollection({
type: 'page',
Expand All @@ -15,8 +19,14 @@ const defaultConfig = {
},
}

export const defineContentConfig = createDefineConfig<NuxtContentConfig>()

export async function loadContentConfig(rootDir: string, opts: { defaultFallback?: boolean } = {}) {
const { config, configFile } = await loadConfig({ name: 'content', cwd: rootDir })
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).defineContentConfig = (c: any) => c
const { config, configFile } = await loadConfig<NuxtContentConfig>({ name: 'content', cwd: rootDir })
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (globalThis as any).defineContentConfig

if ((!configFile || configFile === 'content.config') && opts.defaultFallback) {
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')
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { metaSchema, pageSchema } from './schema'
export { defineCollection } from './collection'
export { defineContentConfig } from './config'
export { z } from './zod'

0 comments on commit cf85cd4

Please sign in to comment.