@nuasite/cms 0.25.0 → 0.27.0

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.
@@ -4,7 +4,7 @@ import { isMap, isPair, isScalar, isSeq, LineCounter, parseDocument } from 'yaml
4
4
 
5
5
  import { getProjectRoot } from '../config'
6
6
  import type { CollectionDefinition } from '../types'
7
- import { getMarkdownFileCache } from './cache'
7
+ import { getCollectionTextIndex, getMarkdownFileCache, setCollectionTextIndex } from './cache'
8
8
  import { normalizeText } from './snippet-utils'
9
9
  import type { CollectionInfo, MarkdownContent, SourceLocation } from './types'
10
10
 
@@ -12,8 +12,6 @@ import type { CollectionInfo, MarkdownContent, SourceLocation } from './types'
12
12
  // Collection Text Index — pre-built reverse index for fast text→source lookups
13
13
  // ============================================================================
14
14
 
15
- /** Pre-built index: normalizedText → SourceLocation (with collection metadata) */
16
- let collectionTextIndex: Map<string, SourceLocation[]> | null = null
17
15
  let collectionTextIndexPromise: Promise<void> | null = null
18
16
 
19
17
  /**
@@ -23,7 +21,7 @@ let collectionTextIndexPromise: Promise<void> | null = null
23
21
  export async function buildCollectionTextIndex(
24
22
  collections: Record<string, CollectionDefinition>,
25
23
  ): Promise<void> {
26
- if (collectionTextIndex) return
24
+ if (getCollectionTextIndex()) return
27
25
  if (collectionTextIndexPromise) return collectionTextIndexPromise
28
26
  collectionTextIndexPromise = doBuildCollectionTextIndex(collections)
29
27
  try {
@@ -76,7 +74,7 @@ async function doBuildCollectionTextIndex(
76
74
  }))
77
75
  }
78
76
 
79
- collectionTextIndex = index
77
+ setCollectionTextIndex(index)
80
78
  }
81
79
 
82
80
  /**
@@ -168,9 +166,10 @@ export function lookupCollectionText(
168
166
  textContent: string,
169
167
  referenceIndex?: Map<string, Array<{ collection: string; fieldName: string; isArray?: boolean }>>,
170
168
  ): SourceLocation | undefined {
171
- if (!collectionTextIndex) return undefined
169
+ const index = getCollectionTextIndex()
170
+ if (!index) return undefined
172
171
  const normalized = normalizeText(textContent)
173
- const locations = collectionTextIndex.get(normalized)
172
+ const locations = index.get(normalized)
174
173
  if (!locations || locations.length === 0) return undefined
175
174
 
176
175
  // Prefer locations from referenced collections
@@ -184,11 +183,6 @@ export function lookupCollectionText(
184
183
  return locations[0]
185
184
  }
186
185
 
187
- /** Clear the collection text index (called when collection files change) */
188
- export function clearCollectionTextIndex(): void {
189
- collectionTextIndex = null
190
- }
191
-
192
186
  // ============================================================================
193
187
  // Markdown File Cache
194
188
  // ============================================================================
@@ -25,7 +25,6 @@ export { findImageSourceLocation } from './image-finder'
25
25
  // Collection/markdown finding
26
26
  export {
27
27
  buildCollectionTextIndex,
28
- clearCollectionTextIndex,
29
28
  findCollectionSource,
30
29
  findFieldInCollectionEntry,
31
30
  findMarkdownSourceLocation,
@@ -17,9 +17,9 @@ import {
17
17
  getTextSearchIndex,
18
18
  isSearchIndexInitialized,
19
19
  removeFileFromIndexes,
20
+ setCollectionTextIndex,
20
21
  setSearchIndexInitialized,
21
22
  } from './cache'
22
- import { clearCollectionTextIndex } from './collection-finder'
23
23
  import { extractImageSnippet, extractInnerHtmlFromSnippet, normalizeText } from './snippet-utils'
24
24
  import type { CachedParsedFile, SearchIndexEntry, SourceLocation } from './types'
25
25
 
@@ -167,7 +167,7 @@ async function doReindexDirtyFiles(): Promise<void> {
167
167
  // Also clear the markdown file cache and collection text index
168
168
  // so collection content is re-read and re-indexed from disk
169
169
  getMarkdownFileCache().clear()
170
- clearCollectionTextIndex()
170
+ setCollectionTextIndex(null)
171
171
 
172
172
  for (const absPath of filesToReindex) {
173
173
  const relFile = path.relative(projectRoot, absPath)
@@ -28,6 +28,9 @@ export function createVitePlugin(context: VitePluginContext): Plugin[] {
28
28
  if (id === '/@cms/components' || id === 'virtual:cms-components') {
29
29
  return '\0virtual:cms-components'
30
30
  }
31
+ if (id === 'virtual:cms-component-preview') {
32
+ return '\0virtual:cms-component-preview'
33
+ }
31
34
  },
32
35
  load(id) {
33
36
  if (id === '\0virtual:cms-manifest') {
@@ -36,6 +39,12 @@ export function createVitePlugin(context: VitePluginContext): Plugin[] {
36
39
  if (id === '\0virtual:cms-components') {
37
40
  return `export default ${JSON.stringify(componentDefinitions)};`
38
41
  }
42
+ if (id === '\0virtual:cms-component-preview') {
43
+ const entries = Object.values(componentDefinitions).map(
44
+ (def) => ` ${JSON.stringify(def.file)}: () => import(${JSON.stringify('/' + def.file)})`,
45
+ )
46
+ return `export const components = {\n${entries.join(',\n')}\n};`
47
+ }
39
48
  },
40
49
  }
41
50