@nuasite/cms-core 0.70.1 → 0.70.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -132,24 +132,64 @@ function extractFrontmatterBlock(content: string): string | null {
132
132
  return match?.[1] ?? null
133
133
  }
134
134
 
135
- function parseFrontmatter(content: string): Record<string, unknown> | null {
135
+ /** `@position`/`@group` comment directives of one entry's frontmatter, by field name. */
136
+ export type FieldDirectives = Record<string, { position?: 'sidebar' | 'header'; group?: string }>
137
+
138
+ /** One entry's frontmatter block and what the scan parses out of it. */
139
+ export interface ParsedFrontmatter {
140
+ block: string
141
+ data: Record<string, unknown> | null
142
+ directives: FieldDirectives
143
+ }
144
+
145
+ /**
146
+ * Parsed frontmatter of each markdown entry, by root-relative file path. A scan reuses an entry's
147
+ * parse while its frontmatter block is unchanged and drops the paths it no longer finds, so the
148
+ * memo never outlives the files it describes.
149
+ */
150
+ export type FrontmatterCache = Map<string, ParsedFrontmatter>
151
+
152
+ /** One scan's view of a {@link FrontmatterCache}: the paths this scan parsed, to sweep the rest. */
153
+ interface FrontmatterMemo {
154
+ cache: FrontmatterCache
155
+ seen: Set<string>
156
+ }
157
+
158
+ function parseFrontmatter(content: string, filePath: string, memo: FrontmatterMemo | undefined): ParsedFrontmatter | null {
136
159
  const block = extractFrontmatterBlock(content)
137
160
  if (!block) return null
138
- return parseYaml(block) as Record<string, unknown> | null
161
+ memo?.seen.add(filePath)
162
+ const cached = memo?.cache.get(filePath)
163
+ // `data` is copied out of the memo, so a scan's caller can mutate its entries without changing
164
+ // the next scan's; directives never leave the scanner.
165
+ if (cached?.block === block) return { ...cached, data: structuredClone(cached.data) }
166
+ const parsed: ParsedFrontmatter = {
167
+ block,
168
+ data: parseYaml(block) as Record<string, unknown> | null,
169
+ directives: parseFieldDirectives(block),
170
+ }
171
+ if (!memo) return parsed
172
+ memo.cache.set(filePath, parsed)
173
+ return { ...parsed, data: structuredClone(parsed.data) }
174
+ }
175
+
176
+ function sweepFrontmatterMemo(memo: FrontmatterMemo): void {
177
+ for (const filePath of memo.cache.keys()) {
178
+ if (!memo.seen.has(filePath)) memo.cache.delete(filePath)
179
+ }
139
180
  }
140
181
 
141
182
  /**
142
- * Parse @position and @group comment directives from raw YAML frontmatter.
183
+ * Parse @position and @group comment directives from a raw YAML frontmatter block.
143
184
  * Uses the YAML AST which preserves comments via `commentBefore` on nodes.
144
185
  */
145
- function parseFieldDirectives(content: string): Record<string, { position?: 'sidebar' | 'header'; group?: string }> {
146
- const block = extractFrontmatterBlock(content)
147
- if (!block || !DIRECTIVE_TEXT.test(block)) return {}
186
+ function parseFieldDirectives(block: string): FieldDirectives {
187
+ if (!DIRECTIVE_TEXT.test(block)) return {}
148
188
 
149
189
  const doc = parseDocument(block)
150
190
  if (!isMap(doc.contents)) return {}
151
191
 
152
- const result: Record<string, { position?: 'sidebar' | 'header'; group?: string }> = {}
192
+ const result: FieldDirectives = {}
153
193
 
154
194
  for (const pair of doc.contents.items) {
155
195
  if (!isPair(pair) || !isScalar(pair.key)) continue
@@ -446,6 +486,7 @@ async function buildCollectionDefinition(
446
486
  sources: Array<{ slug: string; relPath: string }>,
447
487
  collectionName: string,
448
488
  contentDir: string,
489
+ frontmatterMemo: FrontmatterMemo | undefined,
449
490
  ): Promise<CollectionDefinition | null> {
450
491
  if (sources.length === 0) return null
451
492
 
@@ -469,10 +510,10 @@ async function buildCollectionDefinition(
469
510
  const source = sources[i]!
470
511
  const content = fileContents[i]!
471
512
  if (content === null) continue
472
- const frontmatter = parseFrontmatter(content)
513
+ const parsed = parseFrontmatter(content, path.join(basePath, source.relPath), frontmatterMemo)
514
+ const frontmatter = parsed?.data ?? null
473
515
 
474
- const directives = parseFieldDirectives(content)
475
- for (const [key, value] of Object.entries(directives)) {
516
+ for (const [key, value] of Object.entries(parsed?.directives ?? {})) {
476
517
  if (!allDirectives[key]) {
477
518
  allDirectives[key] = value
478
519
  }
@@ -582,6 +623,7 @@ async function scanCollection(
582
623
  collectionPath: string,
583
624
  collectionName: string,
584
625
  contentDir: string,
626
+ frontmatterMemo: FrontmatterMemo | undefined,
585
627
  ): Promise<CollectionDefinition | null> {
586
628
  const dirEntries = await fs.list(collectionPath)
587
629
  if (dirEntries.length === 0) return null
@@ -614,7 +656,7 @@ async function scanCollection(
614
656
  }
615
657
 
616
658
  if (sources.length === 0) return null
617
- return await buildCollectionDefinition(fs, collectionPath, sources, collectionName, contentDir)
659
+ return await buildCollectionDefinition(fs, collectionPath, sources, collectionName, contentDir, frontmatterMemo)
618
660
  }
619
661
 
620
662
  /**
@@ -650,6 +692,7 @@ async function scanGlobCollection(
650
692
  baseRel: string,
651
693
  pattern: string,
652
694
  contentDir: string,
695
+ frontmatterMemo: FrontmatterMemo | undefined,
653
696
  ): Promise<CollectionDefinition | null> {
654
697
  const rels = await globLoaderFiles(fs, baseRel, pattern)
655
698
 
@@ -660,7 +703,7 @@ async function scanGlobCollection(
660
703
  .filter(relToBase => relToBase.endsWith('.md') || relToBase.endsWith('.mdx'))
661
704
  .map(relToBase => ({ slug: relToBase.replace(/\.(md|mdx)$/, ''), relPath: relToBase }))
662
705
  if (mdSources.length > 0) {
663
- return await buildCollectionDefinition(fs, baseRel, mdSources, collectionName, contentDir)
706
+ return await buildCollectionDefinition(fs, baseRel, mdSources, collectionName, contentDir, frontmatterMemo)
664
707
  }
665
708
 
666
709
  const dataSources = rels
@@ -1157,15 +1200,17 @@ function patternCoversWholeCollection(pattern: string, definition: CollectionDef
1157
1200
  *
1158
1201
  * `contentDir` is a root-relative directory (default `src/content`); all I/O is
1159
1202
  * resolved against the injected `CmsFileSystem`'s root. `parseCache` keeps the
1160
- * mtime-keyed content-config parse result across calls within one core instance.
1203
+ * mtime-keyed content-config parse result across calls within one core instance, and
1204
+ * `options.frontmatterCache` each entry's parsed frontmatter (see {@link FrontmatterCache}).
1161
1205
  */
1162
1206
  export async function scanCollections(
1163
1207
  fs: CmsFileSystem,
1164
1208
  contentDir: string = 'src/content',
1165
1209
  parseCache: ParseCache = new Map(),
1166
- options: { report?: boolean } = {},
1210
+ options: { report?: boolean; frontmatterCache?: FrontmatterCache } = {},
1167
1211
  ): Promise<Record<string, CollectionDefinition>> {
1168
1212
  const collections: Record<string, CollectionDefinition> = {}
1213
+ const frontmatterMemo: FrontmatterMemo | undefined = options.frontmatterCache ? { cache: options.frontmatterCache, seen: new Set() } : undefined
1169
1214
  /** Collections whose scan threw, by name, as they go into the scan report. */
1170
1215
  const failedScans = new Map<string, UnresolvedCollection>()
1171
1216
 
@@ -1178,7 +1223,7 @@ export async function scanCollections(
1178
1223
  .map(async entry => {
1179
1224
  try {
1180
1225
  const collectionPath = path.join(contentDir, entry.name)
1181
- const definition = await scanCollection(fs, collectionPath, entry.name, contentDir)
1226
+ const definition = await scanCollection(fs, collectionPath, entry.name, contentDir, frontmatterMemo)
1182
1227
  ?? await scanDataCollection(fs, collectionPath, entry.name, contentDir)
1183
1228
  if (definition) {
1184
1229
  collections[entry.name] = definition
@@ -1228,7 +1273,14 @@ export async function scanCollections(
1228
1273
 
1229
1274
  let definition: CollectionDefinition | null
1230
1275
  try {
1231
- definition = await scanGlobCollection(fs, collectionName, parsedCollection.loaderBase, parsedCollection.loaderPattern, contentDir)
1276
+ definition = await scanGlobCollection(
1277
+ fs,
1278
+ collectionName,
1279
+ parsedCollection.loaderBase,
1280
+ parsedCollection.loaderPattern,
1281
+ contentDir,
1282
+ frontmatterMemo,
1283
+ )
1232
1284
  } catch (error) {
1233
1285
  console.warn(`[cms] Skipping glob collection "${collectionName}" — scan failed:`, error)
1234
1286
  failedScans.set(collectionName, {
@@ -1249,6 +1301,7 @@ export async function scanCollections(
1249
1301
  }
1250
1302
  collections[collectionName] = definition
1251
1303
  }
1304
+ if (frontmatterMemo) sweepFrontmatterMemo(frontmatterMemo)
1252
1305
 
1253
1306
  applyParsedConfig(collections, parsed)
1254
1307
  detectReferenceFields(collections, parsed)
@@ -302,44 +302,66 @@ function parseSource(source: string): t.File | null {
302
302
  export const CONTENT_CONFIG_PATHS = ['src/content/config.ts', 'src/content.config.ts'] as const
303
303
 
304
304
  /**
305
- * Where Astro also takes a content config from and this reader does not look. A project with
306
- * one of these has a config; it is only out of the reader's sight.
305
+ * Every content config file name, in the order Astro 7 looks for them: it loads the first that exists. It looks at
306
+ * the legacy `src/content/config.*` names only when none of the others exists, and then fails the build unless the
307
+ * Astro config sets `legacy.collectionsBackwardsCompat`.
307
308
  */
308
- export const UNREAD_CONTENT_CONFIG_PATHS = [
309
+ export const ASTRO_CONTENT_CONFIG_ORDER = [
309
310
  'src/content.config.mjs',
310
311
  'src/content.config.js',
311
312
  'src/content.config.mts',
312
- 'src/content/config.mjs',
313
+ 'src/content.config.ts',
314
+ 'src/content/config.ts',
313
315
  'src/content/config.js',
316
+ 'src/content/config.mjs',
314
317
  'src/content/config.mts',
315
318
  ] as const
316
319
 
320
+ const READ_CONTENT_CONFIG_PATHS: ReadonlySet<string> = new Set(CONTENT_CONFIG_PATHS)
321
+
317
322
  /**
318
- * Why `parseContentConfig` came back empty. Three different facts share that one return value, and
319
- * only two of them are a defect: a site with no content collections is a legitimate site.
323
+ * Where Astro also takes a content config from and this reader does not look. A project with
324
+ * one of these has a config; it is only out of the reader's sight.
320
325
  */
321
- export type EmptyContentConfigReason = 'missing' | 'unparseable' | 'no-collections'
326
+ export const UNREAD_CONTENT_CONFIG_PATHS: readonly string[] = ASTRO_CONTENT_CONFIG_ORDER.filter(configPath =>
327
+ !READ_CONTENT_CONFIG_PATHS.has(configPath)
328
+ )
329
+
330
+ /** The content config file Astro loads, or `null` when the project has none. */
331
+ export async function findLoadedContentConfig(fs: CmsFileSystem): Promise<string | null> {
332
+ for (const configPath of ASTRO_CONTENT_CONFIG_ORDER) {
333
+ if (await fs.exists(configPath)) return configPath
334
+ }
335
+ return null
336
+ }
337
+
338
+ /** Whether this reader reads the content config at `configPath`. */
339
+ export const isReadContentConfig = (configPath: string): boolean => READ_CONTENT_CONFIG_PATHS.has(configPath)
322
340
 
323
341
  /**
324
- * Tell the three apart, for a caller that has to report the empty map rather than just handle it.
342
+ * Why `parseContentConfig` came back empty. Four different facts share that one return value, and only two of them
343
+ * are a defect: `unparseable`, and `missing` beside entries. A site with `no-collections` is a legitimate site, and an
344
+ * `unread` config is one Astro loads from a file name this reader does not look at.
345
+ */
346
+ export type EmptyContentConfigReason = 'missing' | 'unread' | 'unparseable' | 'no-collections'
347
+
348
+ /**
349
+ * Tell the four apart, for a caller that has to report the empty map rather than just handle it.
325
350
  *
326
351
  * A parse failure anywhere wins over an empty parse: a config that will not read is a problem to
327
352
  * name even when a second one beside it reads cleanly and declares nothing.
328
353
  */
329
354
  export async function classifyEmptyContentConfig(fs: CmsFileSystem): Promise<EmptyContentConfigReason> {
330
- let found = false
355
+ const loaded = await findLoadedContentConfig(fs)
356
+ if (loaded === null) return 'missing'
357
+ if (!isReadContentConfig(loaded)) return 'unread'
331
358
  for (const configPath of CONTENT_CONFIG_PATHS) {
332
- try {
333
- await fs.stat(configPath)
334
- } catch {
335
- continue
336
- }
337
- found = true
359
+ if (!(await fs.exists(configPath))) continue
338
360
  if (parseSource(await fs.readFile(configPath)) === null) {
339
361
  return 'unparseable'
340
362
  }
341
363
  }
342
- return found ? 'no-collections' : 'missing'
364
+ return 'no-collections'
343
365
  }
344
366
 
345
367
  /**
package/src/core.ts CHANGED
@@ -15,7 +15,7 @@ import type {
15
15
  RedirectOperationResponse,
16
16
  UpdateRedirectRequest,
17
17
  } from '@nuasite/cms-types'
18
- import { collectionScanReport, scanCollections } from './collection-scanner.ts'
18
+ import { collectionScanReport, type FrontmatterCache, scanCollections } from './collection-scanner.ts'
19
19
  import { scanComponentDefinitions } from './component-registry.ts'
20
20
  import { type ParseCache } from './content-config-ast.ts'
21
21
  import { type CmsCommit, createStagedFileSystem } from './fs/staged.ts'
@@ -132,6 +132,8 @@ export function createCmsCore(fs: CmsFileSystem, opts: CmsCoreOptions = {}): Cms
132
132
  const commit = opts.commit
133
133
  // One mtime-keyed content-config parse cache per core instance (per project root).
134
134
  const parseCache: ParseCache = new Map()
135
+ // Each entry's parsed frontmatter, so a rescan after one save parses only that entry.
136
+ const frontmatterCache: FrontmatterCache = new Map()
135
137
 
136
138
  function entryDeps(over: CmsFileSystem): EntryOpsDeps {
137
139
  return {
@@ -156,7 +158,7 @@ export function createCmsCore(fs: CmsFileSystem, opts: CmsCoreOptions = {}): Cms
156
158
 
157
159
  return {
158
160
  scanCollections(options) {
159
- return scanCollections(fs, contentDir, parseCache, { report: options?.report ?? false })
161
+ return scanCollections(fs, contentDir, parseCache, { report: options?.report ?? false, frontmatterCache })
160
162
  },
161
163
  collectionScanReport,
162
164
  scanComponents() {
package/src/portable.ts CHANGED
@@ -36,9 +36,10 @@ export {
36
36
  } from './check-source.ts'
37
37
  export { checkEditorWrites, type WriteCheckInput } from './check-write.ts'
38
38
  export { checkContent, type CheckContentOptions, type CheckFinding, type CheckReport, type CheckSeverity, formatCheckReport } from './check.ts'
39
- export { collectionScanReport, scanCollections } from './collection-scanner.ts'
39
+ export { collectionScanReport, type FrontmatterCache, scanCollections } from './collection-scanner.ts'
40
40
  export { scanComponentDefinitions } from './component-registry.ts'
41
41
  export {
42
+ ASTRO_CONTENT_CONFIG_ORDER,
42
43
  classifyEmptyContentConfig,
43
44
  CONTENT_CONFIG_PATHS,
44
45
  type ContentConfigRead,