Skip to content

Commit 3ca01f4

Browse files
committed
!refactor: simplify source definition
1 parent 4d89141 commit 3ca01f4

File tree

8 files changed

+48
-42
lines changed

8 files changed

+48
-42
lines changed

playground/content.config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { defineCollection, z } from '@nuxt/content'
33
const content = defineCollection({
44
type: 'page',
55
source: {
6-
path: '**',
7-
ignore: [
6+
include: '**',
7+
exclude: [
88
'data/**',
99
'pages/**',
1010
],
@@ -50,9 +50,9 @@ export const collections = {
5050
type: 'page',
5151
source: {
5252
repository: 'https://github.com/nuxt/content',
53-
path: 'docs/content/**',
53+
include: 'docs/content/**',
5454
prefix: '/content-v2',
55-
ignore: [
55+
exclude: [
5656
'**/_dir.yml',
5757
],
5858
},
@@ -61,9 +61,9 @@ export const collections = {
6161
type: 'page',
6262
source: {
6363
repository: 'https://github.com/nuxt/nuxt',
64-
path: 'docs/**',
64+
include: 'docs/**',
6565
prefix: '/nuxt',
66-
ignore: [
66+
exclude: [
6767
'**/_dir.yml',
6868
],
6969
},
@@ -72,7 +72,7 @@ export const collections = {
7272
type: 'page',
7373
source: {
7474
repository: 'https://github.com/vuejs/docs',
75-
path: 'src/**/*.md',
75+
include: 'src/**/*.md',
7676
prefix: '/vue',
7777
},
7878
}),

src/module.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { join, dirname, isAbsolute } from 'pathe'
1717
import fastGlob from 'fast-glob'
1818
import htmlTags from '@nuxtjs/mdc/runtime/parser/utils/html-tags-list'
1919
import { kebabCase, pascalCase } from 'scule'
20-
import { generateCollectionInsert, generateCollectionTableDefinition, parseSourceBase } from './utils/collection'
20+
import { generateCollectionInsert, generateCollectionTableDefinition } from './utils/collection'
2121
import { collectionsTemplate, componentsManifestTemplate, contentTypesTemplate, fullDatabaseRawDumpTemplate, manifestTemplate, moduleTemplates } from './utils/templates'
2222
import type { ResolvedCollection } from './types/collection'
2323
import type { ModuleOptions, SqliteDatabaseConfig } from './types/module'
@@ -239,10 +239,8 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
239239
await collection.source.prepare(nuxt)
240240
}
241241

242-
const { fixed, dynamic } = parseSourceBase(collection.source)
243-
const cwd = join(collection.source.cwd, fixed)
244-
245-
const _keys = await fastGlob(dynamic, { cwd, ignore: collection.source!.ignore || [], dot: true })
242+
const cwd = collection.source.cwd
243+
const _keys = await fastGlob(collection.source.include, { cwd, ignore: collection.source!.exclude || [], dot: true })
246244
.catch(() => [])
247245

248246
filesCount += _keys.length

src/types/collection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export interface Collections {}
99
export type CollectionType = 'page' | 'data'
1010

1111
export type CollectionSource = {
12+
include: string
13+
prefix?: string
14+
exclude?: string[]
1215
repository?: string
1316
cwd?: string
14-
path: string
15-
prefix?: string
16-
ignore?: string[]
1717
}
1818

1919
export interface ResolvedCollectionSource extends CollectionSource {

src/utils/collection.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function resolveSource(source: string | CollectionSource | undefined): ResolvedC
7171
}
7272

7373
if (typeof source === 'string') {
74-
return defineLocalSource({ path: source })
74+
return defineLocalSource({ include: source })
7575
}
7676

7777
if ((source as ResolvedCollectionSource)._resolved) {
@@ -85,14 +85,6 @@ function resolveSource(source: string | CollectionSource | undefined): ResolvedC
8585
return defineLocalSource(source)
8686
}
8787

88-
export function parseSourceBase(source: CollectionSource) {
89-
const [fixPart, ...rest] = source.path.includes('*') ? source.path.split('*') : ['', source.path]
90-
return {
91-
fixed: fixPart || '',
92-
dynamic: '*' + rest.join('*'),
93-
}
94-
}
95-
9688
// Convert collection data to SQL insert statement
9789
export function generateCollectionInsert(collection: ResolvedCollection, data: Record<string, unknown>) {
9890
const fields: string[] = []
@@ -103,7 +95,7 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: R
10395
const value = (collection.extendedSchema).shape[key]
10496
const underlyingType = getUnderlyingType(value as ZodType<unknown, ZodOptionalDef>)
10597

106-
let defaultValue = value._def.defaultValue ? value._def.defaultValue() : 'NULL'
98+
let defaultValue = value?._def.defaultValue ? value?._def.defaultValue() : 'NULL'
10799

108100
if (!(defaultValue instanceof Date) && typeof defaultValue === 'object') {
109101
defaultValue = JSON.stringify(defaultValue)
@@ -138,7 +130,7 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: R
138130
export function generateCollectionTableDefinition(collection: ResolvedCollection, opts: { drop?: boolean } = {}) {
139131
const sortedKeys = Object.keys((collection.extendedSchema).shape).sort()
140132
const sqlFields = sortedKeys.map((key) => {
141-
const type = (collection.extendedSchema).shape[key]
133+
const type = (collection.extendedSchema).shape[key]!
142134
const underlyingType = getUnderlyingType(type)
143135

144136
if (key === '_id') return `${key} TEXT PRIMARY KEY`

src/utils/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const defaultConfig = {
1010
collections: {
1111
content: defineCollection({
1212
type: 'page',
13-
source: '**/*.md',
13+
source: '**/*',
1414
}),
1515
},
1616
}

src/utils/source.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import { join } from 'pathe'
2+
import { withLeadingSlash, withoutTrailingSlash } from 'ufo'
23
import type { CollectionSource, ResolvedCollectionSource } from '../types/collection'
34
import { downloadRepository, parseGitHubUrl } from './git'
45

56
export function defineLocalSource(source: CollectionSource): ResolvedCollectionSource {
7+
const { fixed, dynamic } = parseSourceBase(source)
68
const resolvedSource: ResolvedCollectionSource = {
79
_resolved: true,
10+
prefix: withoutTrailingSlash(withLeadingSlash(fixed)),
811
...source,
9-
cwd: '',
12+
include: dynamic,
13+
cwd: withoutTrailingSlash(join('~~/content/', fixed)),
1014
prepare: async (nuxt) => {
11-
resolvedSource.cwd = source.cwd || join(nuxt.options.rootDir, 'content')
15+
resolvedSource.cwd = source.cwd
16+
? String(source.cwd).replace(/^~~\//, nuxt.options.rootDir)
17+
: join(nuxt.options.rootDir, 'content', fixed)
1218
},
1319
}
1420
return resolvedSource
@@ -34,3 +40,11 @@ export function defineGitHubSource(source: CollectionSource): ResolvedCollection
3440
}
3541
return resolvedSource
3642
}
43+
44+
export function parseSourceBase(source: CollectionSource) {
45+
const [fixPart, ...rest] = source.include.includes('*') ? source.include.split('*') : ['', source.include]
46+
return {
47+
fixed: fixPart || '',
48+
dynamic: '*' + rest.join('*'),
49+
}
50+
}

test/base.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('empty', async () => {
4242
expect(pagesCollection).toBeDefined()
4343
expect(pagesCollection?.type).toBe('page')
4444
expect(pagesCollection?.source).toBeDefined()
45-
expect(pagesCollection?.source?.path).toBe('**/*.md')
45+
expect(pagesCollection?.source?.include).toBe('**/*')
4646
})
4747
})
4848

test/unit/defineCollection.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ describe('defineCollection', () => {
1919
type: 'page',
2020
source: {
2121
_resolved: true,
22-
cwd: '',
23-
path: 'pages/**',
22+
include: '**',
23+
cwd: '~~/content/pages',
2424
},
2525
})
2626

@@ -50,9 +50,9 @@ describe('defineCollection', () => {
5050
const collection = defineCollection({
5151
type: 'page',
5252
source: {
53-
path: 'pages/**',
53+
include: 'pages/**',
5454
prefix: 'blog',
55-
ignore: ['pages/blog/index.md'],
55+
exclude: ['pages/blog/index.md'],
5656
},
5757
schema: z.object({
5858
customField: z.string(),
@@ -62,9 +62,10 @@ describe('defineCollection', () => {
6262
expect(collection).toMatchObject({
6363
type: 'page',
6464
source: {
65-
path: 'pages/**',
65+
include: '**',
6666
prefix: 'blog',
67-
ignore: ['pages/blog/index.md'],
67+
exclude: ['pages/blog/index.md'],
68+
cwd: '~~/content/pages',
6869
},
6970
})
7071

@@ -87,8 +88,8 @@ describe('defineCollection', () => {
8788
type: 'data',
8889
source: {
8990
_resolved: true,
90-
cwd: '',
91-
path: 'data/**',
91+
include: '**',
92+
cwd: '~~/content/data',
9293
},
9394
})
9495

@@ -103,9 +104,9 @@ describe('defineCollection', () => {
103104
const collection = defineCollection({
104105
type: 'data',
105106
source: {
106-
path: 'data/**',
107+
include: 'data/**',
107108
prefix: 'blog',
108-
ignore: ['data/blog/index.md'],
109+
exclude: ['data/blog/index.md'],
109110
},
110111
schema: z.object({
111112
customField: z.string(),
@@ -115,9 +116,10 @@ describe('defineCollection', () => {
115116
expect(collection).toMatchObject({
116117
type: 'data',
117118
source: {
118-
path: 'data/**',
119+
include: '**',
120+
cwd: '~~/content/data',
119121
prefix: 'blog',
120-
ignore: ['data/blog/index.md'],
122+
exclude: ['data/blog/index.md'],
121123
},
122124
})
123125
})

0 commit comments

Comments
 (0)