-
-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathmodule.ts
More file actions
469 lines (412 loc) · 16.7 KB
/
module.ts
File metadata and controls
469 lines (412 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { stat } from 'node:fs/promises'
import {
defineNuxtModule,
createResolver,
addTemplate,
addTypeTemplate,
addImports,
addServerImports,
addPlugin,
hasNuxtModule,
updateTemplates,
addComponent,
installModule,
addVitePlugin,
} from '@nuxt/kit'
import type { Nuxt } from '@nuxt/schema'
import type { ModuleOptions as MDCModuleOptions } from '@nuxtjs/mdc'
import { hash } from 'ohash'
import { join } from 'pathe'
import htmlTags from '@nuxtjs/mdc/runtime/parser/utils/html-tags-list'
import { kebabCase, pascalCase } from 'scule'
import defu from 'defu'
import { version } from '../package.json'
import { generateCollectionInsert, generateCollectionTableDefinition } from './utils/collection'
import { componentsManifestTemplate, contentTypesTemplate, fullDatabaseRawDumpTemplate, manifestTemplate, moduleTemplates } from './utils/templates'
import type { ResolvedCollection } from './types/collection'
import type { ModuleOptions } from './types/module'
import { getContentChecksum, logger, chunks, NuxtContentHMRUnplugin } from './utils/dev'
import { loadContentConfig } from './utils/config'
import { createParser } from './utils/content'
import { configureMDCModule } from './utils/mdc'
import { findPreset } from './presets'
import type { Manifest } from './types/manifest'
import { setupPreview, setupPreviewWithAPI, shouldEnablePreview } from './utils/preview/module'
import { parseSourceBase } from './utils/source'
import { databaseVersion, getLocalDatabase, refineDatabaseConfig, resolveDatabaseAdapter } from './utils/database'
import type { ParsedContentFile } from './types'
import { initiateValidatorsContext } from './utils/dependencies'
// Export public utils
export * from './utils'
export type * from './types'
const moduleDefaults: Partial<ModuleOptions> = {
_localDatabase: {
type: 'sqlite',
filename: '.data/content/contents.sqlite',
},
preview: {},
watch: { enabled: true },
renderer: {
alias: {},
anchorLinks: {
h2: true,
h3: true,
h4: true,
},
},
build: {
pathMeta: {},
markdown: {},
yaml: {},
csv: {
delimiter: ',',
json: true,
},
},
experimental: {
nativeSqlite: false,
},
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@nuxt/content',
configKey: 'content',
version,
compatibility: {
nuxt: '>=4.1.0 || ^3.19.0',
},
docs: 'https://content.nuxt.com',
},
defaults: moduleDefaults,
moduleDependencies(nuxt) {
const nuxtOptions = nuxt.options as unknown as { content: ModuleOptions }
const contentOptions = defu(nuxtOptions.content, moduleDefaults)
return {
'@nuxtjs/mdc': {
overrides: {
highlight: contentOptions.build?.markdown?.highlight,
components: {
prose: true,
map: contentOptions.renderer.alias,
},
headings: {
anchorLinks: contentOptions.renderer.anchorLinks,
},
remarkPlugins: contentOptions.build?.markdown?.remarkPlugins,
rehypePlugins: contentOptions.build?.markdown?.rehypePlugins,
},
defaults: {
highlight: { noApiRoute: true },
},
},
}
},
async setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
const manifest: Manifest = {
checksumStructure: {},
checksum: {},
dump: {},
components: [],
collections: [],
}
// Detect installed validators and them into content context
await initiateValidatorsContext()
const { collections } = await loadContentConfig(nuxt, options)
manifest.collections = collections
nuxt.callHook('content:manifest', manifest)
nuxt.options.vite.optimizeDeps = defu(nuxt.options.vite.optimizeDeps, {
exclude: ['@sqlite.org/sqlite-wasm'],
})
// Ignore content directory files in building
nuxt.options.ignore = [...(nuxt.options.ignore || []), 'content/**']
// Helpers are designed to be enviroment agnostic
addImports([
{ name: 'queryCollection', from: resolver.resolve('./runtime/client') },
{ name: 'queryCollectionSearchSections', from: resolver.resolve('./runtime/client') },
{ name: 'queryCollectionNavigation', from: resolver.resolve('./runtime/client') },
{ name: 'queryCollectionItemSurroundings', from: resolver.resolve('./runtime/client') },
])
addServerImports([
{ name: 'queryCollection', from: resolver.resolve('./runtime/nitro') },
{ name: 'queryCollectionSearchSections', from: resolver.resolve('./runtime/nitro') },
{ name: 'queryCollectionNavigation', from: resolver.resolve('./runtime/nitro') },
{ name: 'queryCollectionItemSurroundings', from: resolver.resolve('./runtime/nitro') },
])
addComponent({ name: 'ContentRenderer', filePath: resolver.resolve('./runtime/components/ContentRenderer.vue') })
// Add Templates & aliases
addTemplate(fullDatabaseRawDumpTemplate(manifest))
nuxt.options.alias = defu(nuxt.options.alias, {
'#content/components': addTemplate(componentsManifestTemplate(manifest)).dst,
'#content/manifest': addTemplate(manifestTemplate(manifest)).dst,
})
// Add content types to Nuxt and Nitro
const typesTemplateDst = addTypeTemplate(contentTypesTemplate(manifest.collections)).dst
nuxt.options.nitro.typescript = defu(nuxt.options.nitro.typescript, {
tsConfig: {
include: [typesTemplateDst],
},
})
// Register user components
const _layers = [...nuxt.options._layers].reverse()
for (const layer of _layers) {
const path = resolver.resolve(layer.config.srcDir, 'components/content')
const dirStat = await stat(path).catch((): null => null)
if (dirStat && dirStat.isDirectory()) {
nuxt.hook('components:dirs', (dirs) => {
dirs.unshift({ path, pathPrefix: false, prefix: '' })
})
}
}
// Prerender database.sql routes for each collection to fetch dump
nuxt.options.routeRules ||= {}
nuxt.options.routeRules![`/__nuxt_content/**`] = {
...nuxt.options.routeRules![`/__nuxt_content/**`],
// @ts-expect-error - Prevent nuxtseo from indexing nuxt-content routes
robots: false,
cache: false,
}
manifest.collections.forEach((collection) => {
if (!collection.private) {
const key = `/__nuxt_content/${collection.name}/sql_dump.txt`
nuxt.options.routeRules![key] = { ...nuxt.options.routeRules![key], prerender: true }
}
})
nuxt.hook('nitro:config', async (config) => {
const preset = findPreset(nuxt)
await preset.setupNitro(config, { manifest, resolver, moduleOptions: options, nuxt })
const resolveOptions = { resolver, sqliteConnector: options.experimental?.sqliteConnector || (options.experimental?.nativeSqlite ? 'native' : undefined) }
config.alias ||= {}
config.alias['#content/adapter'] = await resolveDatabaseAdapter(config.runtimeConfig!.content!.database?.type || options.database.type, resolveOptions)
config.alias['#content/local-adapter'] = await resolveDatabaseAdapter(options._localDatabase!.type || 'sqlite', resolveOptions)
config.handlers ||= []
manifest.collections.forEach((collection) => {
config.handlers!.push({
route: `/__nuxt_content/${collection.name}/query`,
handler: resolver.resolve('./runtime/api/query.post'),
})
})
// Handle HMR changes
if (nuxt.options.dev && options.watch?.enabled !== false) {
addPlugin({ src: resolver.resolve('./runtime/plugins/websocket.dev'), mode: 'client' })
addVitePlugin(NuxtContentHMRUnplugin.vite({
nuxt,
moduleOptions: options,
manifest,
}))
}
})
if (hasNuxtModule('nuxt-llms')) {
installModule(resolver.resolve('./features/llms'))
}
await configureMDCModule(options, nuxt)
nuxt.hook('modules:done', async () => {
const preset = findPreset(nuxt)
await preset?.setup?.(options, nuxt, { resolver, manifest })
// Provide default database configuration here since nuxt is merging defaults and user options
options.database ||= { type: 'sqlite', filename: './contents.sqlite' }
await refineDatabaseConfig(options._localDatabase, { rootDir: nuxt.options.rootDir, updateSqliteFileName: true })
await refineDatabaseConfig(options.database, { rootDir: nuxt.options.rootDir })
// Module Options
nuxt.options.runtimeConfig.public.content = {
wsUrl: '',
}
nuxt.options.runtimeConfig.content = {
databaseVersion,
version,
database: options.database,
localDatabase: options._localDatabase!,
integrityCheck: true,
} as never
})
if (nuxt.options._prepare) {
return
}
// Generate collections and sql dump to update templates local database
// `modules:done` is triggered for all environments
nuxt.hook('modules:done', async () => {
const fest = await processCollectionItems(nuxt, manifest.collections, options)
// Update manifest
manifest.checksumStructure = fest.checksumStructure
manifest.checksum = fest.checksum
manifest.dump = fest.dump
manifest.components = fest.components
await updateTemplates({
filter: template => [
moduleTemplates.fullRawDump,
moduleTemplates.fullCompressedDump,
moduleTemplates.manifest,
moduleTemplates.components,
].includes(template.filename),
})
// Handle preview mode
if (hasNuxtModule('nuxt-studio')) {
await setupPreview(options, nuxt, resolver, manifest)
}
if (shouldEnablePreview(nuxt, options)) {
await setupPreviewWithAPI(options, nuxt, resolver, manifest)
}
})
},
})
async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollection[], options: ModuleOptions) {
const collectionDump: Record<string, string[]> = {}
const collectionChecksum: Record<string, string> = {}
const collectionChecksumStructure: Record<string, string> = {}
const db = await getLocalDatabase(options._localDatabase, {
sqliteConnector: options.experimental?.sqliteConnector || (options.experimental?.nativeSqlite ? 'native' : undefined),
})
const databaseContents = await db.fetchDevelopmentCache()
const configHash = hash({
mdcHighlight: (nuxt.options as unknown as { mdc: MDCModuleOptions }).mdc?.highlight,
contentBuild: options.build?.markdown,
})
const infoCollection = collections.find(c => c.name === 'info')!
const startTime = performance.now()
let filesCount = 0
let cachedFilesCount = 0
let parsedFilesCount = 0
// Store components used in the content provided by
// custom parsers using the `__metadata.components` field.
// This will allow to correctly generate production imports
const usedComponents: Array<string> = []
// Remove all existing content collections to start with a clean state
db.dropContentTables()
// Create database dump
for await (const collection of collections) {
if (collection.name === 'info') {
continue
}
const collectionHash = hash(collection)
const collectionQueries = generateCollectionTableDefinition(collection, { drop: true })
.split('\n').map(q => `${q} -- structure`)
if (!collection.source) {
continue
}
const parse = await createParser(collection, nuxt)
const structureVersion = collectionChecksumStructure[collection.name] = hash(collectionQueries)
for await (const source of collection.source) {
if (source.prepare) {
// @ts-expect-error - `__rootDir` is a private property to store the layer's cwd
const rootDir = collection.__rootDir || nuxt.options.rootDir
await source.prepare({ rootDir })
}
const { fixed } = parseSourceBase(source)
const cwd = source.cwd
const _keys = await source.getKeys?.() || []
filesCount += _keys.length
/**
* list is an array of tuples
* 0: filePath/key
* 1: queries
* 2: hash
*/
const list: Array<[string, Array<string>, string]> = []
for await (const chunk of chunks(_keys, 25)) {
await Promise.all(chunk.map(async (key) => {
const keyInCollection = join(collection.name, source?.prefix || '', key)
const fullPath = join(cwd, fixed, key)
const cache = databaseContents[keyInCollection]
try {
const content = await source.getItem?.(key) || ''
const checksum = getContentChecksum(configHash + collectionHash + content)
let parsedContent: ParsedContentFile
if (cache && cache.checksum === checksum) {
cachedFilesCount += 1
parsedContent = JSON.parse(cache.value)
}
else {
parsedFilesCount += 1
parsedContent = await parse({
id: keyInCollection,
body: content,
path: fullPath,
collectionType: collection.type,
})
if (parsedContent) {
db.insertDevelopmentCache(keyInCollection, JSON.stringify(parsedContent), checksum)
}
}
// Add manually provided components from the content
if (parsedContent?.__metadata?.components) {
usedComponents.push(...parsedContent.__metadata.components)
}
const { queries, hash } = generateCollectionInsert(collection, parsedContent)
list.push([key, queries, hash])
}
catch (e: unknown) {
logger.warn(`"${keyInCollection}" is ignored because parsing is failed. Error: ${e instanceof Error ? e.message : 'Unknown error'}`)
}
}))
}
// Sort by file name to ensure consistent order
list.sort((a, b) => String(a[0]).localeCompare(String(b[0])))
collectionQueries.push(...list.flatMap(([, sql, hash]) => sql.map(q => `${q} -- ${hash}`)))
}
const version = collectionChecksum[collection.name] = `${databaseVersion}--${hash(collectionQueries)}`
collectionDump[collection.name] = [
// we have to start the series of queries
// by telling everyone that we are setting up the collection so no
// other request start doing the same work and fail
// so we create a new entry in the info table saying that it is not ready yet
// NOTE: all queries having the structure comment at the end, will be ignored at init if no
// structure changes are detected in the structureVersion
`${generateCollectionTableDefinition(infoCollection, { drop: false })} -- structure`,
...generateCollectionInsert(infoCollection, { id: `checksum_${collection.name}`, version, structureVersion, ready: false }).queries.map(row => `${row} -- meta`),
// Insert queries for the collection
...collectionQueries,
// and finally when we are finished, we update the info table to say that the init is done
`UPDATE ${infoCollection.tableName} SET ready = true WHERE id = 'checksum_${collection.name}'; -- meta`,
]
}
const sqlDumpList = Object.values(collectionDump).flatMap(a => a)
// Drop info table and recreate it
db.exec(`DROP TABLE IF EXISTS ${infoCollection.tableName}`)
// Use transaction for faster execution (SQLite only)
try {
if (db.supportsTransactions) {
db.exec('BEGIN TRANSACTION')
}
for (const sql of sqlDumpList) {
db.exec(sql)
}
if (db.supportsTransactions) {
db.exec('COMMIT')
}
}
catch (error) {
if (db.supportsTransactions) {
try {
db.exec('ROLLBACK')
}
catch {
// Ignore rollback errors, original error takes precedence
}
}
throw error
}
const tags = sqlDumpList.flatMap((sql: string): RegExpMatchArray | [] => sql.match(/(?<=(^|,|\[)\[")[^"]+(?=")/g) || [])
const uniqueTags = [
...Object.values(options.renderer.alias || {}),
...new Set(tags),
...new Set(usedComponents),
]
.map(tag => getMappedTag(tag, options?.renderer?.alias))
.filter(tag => !htmlTags.has(kebabCase(tag)))
.map(tag => pascalCase(tag))
const endTime = performance.now()
logger.success(`Processed ${collections.length} collections and ${filesCount} files in ${(endTime - startTime).toFixed(2)}ms (${cachedFilesCount} cached, ${parsedFilesCount} parsed)`)
return {
checksumStructure: collectionChecksumStructure,
checksum: collectionChecksum,
dump: collectionDump,
components: uniqueTags,
}
}
const proseTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'a', 'strong', 'em', 's', 'code', 'span', 'blockquote', 'pre', 'hr', 'img', 'ul', 'ol', 'li', 'table', 'thead', 'tbody', 'tr', 'th', 'td']
function getMappedTag(tag: string, additionalTags: Record<string, string> = {}) {
if (proseTags.includes(tag)) {
return `prose-${tag}`
}
return additionalTags[tag] || tag
}