@nuasite/cms-core 0.64.3 → 0.65.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.
Files changed (187) hide show
  1. package/dist/asset-paths.d.ts.map +1 -0
  2. package/dist/asset-paths.js +55 -0
  3. package/dist/astro-source-analysis.d.ts.map +1 -0
  4. package/dist/astro-source-analysis.js +503 -0
  5. package/dist/byte-range.d.ts.map +1 -0
  6. package/dist/byte-range.js +31 -0
  7. package/dist/{types/check-entries.d.ts → check-entries.d.ts} +3 -3
  8. package/dist/check-entries.d.ts.map +1 -0
  9. package/dist/check-entries.js +98 -0
  10. package/dist/{types/check-live.d.ts → check-live.d.ts} +4 -4
  11. package/dist/check-live.d.ts.map +1 -0
  12. package/dist/check-live.js +49 -0
  13. package/dist/{types/check-shape.d.ts → check-shape.d.ts} +4 -4
  14. package/dist/check-shape.d.ts.map +1 -0
  15. package/dist/check-shape.js +119 -0
  16. package/dist/{types/check-write.d.ts → check-write.d.ts} +4 -4
  17. package/dist/check-write.d.ts.map +1 -0
  18. package/dist/check-write.js +273 -0
  19. package/dist/{types/check.d.ts → check.d.ts} +2 -2
  20. package/dist/check.d.ts.map +1 -0
  21. package/dist/check.js +366 -0
  22. package/dist/{types/collection-scanner.d.ts → collection-scanner.d.ts} +2 -2
  23. package/dist/collection-scanner.d.ts.map +1 -0
  24. package/dist/collection-scanner.js +1483 -0
  25. package/dist/{types/component-registry.d.ts → component-registry.d.ts} +1 -1
  26. package/dist/component-registry.d.ts.map +1 -0
  27. package/dist/component-registry.js +271 -0
  28. package/dist/{types/content-config-ast.d.ts → content-config-ast.d.ts} +1 -1
  29. package/dist/content-config-ast.d.ts.map +1 -0
  30. package/dist/content-config-ast.js +1614 -0
  31. package/dist/{types/core.d.ts → core.d.ts} +3 -3
  32. package/dist/core.d.ts.map +1 -0
  33. package/dist/core.js +94 -0
  34. package/dist/editor-write-model.d.ts.map +1 -0
  35. package/dist/editor-write-model.js +101 -0
  36. package/dist/fs/glob.d.ts.map +1 -0
  37. package/dist/fs/glob.js +79 -0
  38. package/dist/{types/fs → fs}/node-fs.d.ts +1 -1
  39. package/dist/fs/node-fs.d.ts.map +1 -0
  40. package/dist/fs/node-fs.js +199 -0
  41. package/dist/{types/fs → fs}/staged.d.ts +1 -1
  42. package/dist/fs/staged.d.ts.map +1 -0
  43. package/dist/fs/staged.js +203 -0
  44. package/dist/fs/types.d.ts.map +1 -0
  45. package/dist/fs/types.js +0 -0
  46. package/dist/{types/handlers → handlers}/entry-ops.d.ts +2 -2
  47. package/dist/handlers/entry-ops.d.ts.map +1 -0
  48. package/dist/handlers/entry-ops.js +846 -0
  49. package/dist/{types/handlers → handlers}/page-ops.d.ts +1 -1
  50. package/dist/handlers/page-ops.d.ts.map +1 -0
  51. package/dist/handlers/page-ops.js +165 -0
  52. package/dist/{types/handlers → handlers}/redirect-ops.d.ts +1 -1
  53. package/dist/handlers/redirect-ops.d.ts.map +1 -0
  54. package/dist/handlers/redirect-ops.js +111 -0
  55. package/dist/index.d.ts +5 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +4 -0
  58. package/dist/media/contember.d.ts.map +1 -0
  59. package/dist/media/contember.js +79 -0
  60. package/dist/media/errors.d.ts.map +1 -0
  61. package/dist/media/errors.js +22 -0
  62. package/dist/{types/media → media}/file-system.d.ts +1 -1
  63. package/dist/media/file-system.d.ts.map +1 -0
  64. package/dist/media/file-system.js +124 -0
  65. package/dist/{types/media → media}/index.d.ts +8 -8
  66. package/dist/media/index.d.ts.map +1 -0
  67. package/dist/media/index.js +8 -0
  68. package/dist/media/local.d.ts.map +1 -0
  69. package/dist/media/local.js +209 -0
  70. package/dist/media/mime.d.ts.map +1 -0
  71. package/dist/media/mime.js +21 -0
  72. package/dist/media/names.d.ts.map +1 -0
  73. package/dist/media/names.js +23 -0
  74. package/dist/{types/media → media}/project-images.d.ts +1 -1
  75. package/dist/media/project-images.d.ts.map +1 -0
  76. package/dist/media/project-images.js +99 -0
  77. package/dist/media/s3.d.ts.map +1 -0
  78. package/dist/media/s3.js +160 -0
  79. package/dist/media/type-filter.d.ts.map +1 -0
  80. package/dist/media/type-filter.js +164 -0
  81. package/dist/{types/portable.d.ts → portable.d.ts} +25 -25
  82. package/dist/portable.d.ts.map +1 -0
  83. package/dist/portable.js +27 -0
  84. package/dist/{types/project-config-ast.d.ts → project-config-ast.d.ts} +1 -1
  85. package/dist/project-config-ast.d.ts.map +1 -0
  86. package/dist/project-config-ast.js +324 -0
  87. package/dist/public-static-file.d.ts.map +1 -0
  88. package/dist/public-static-file.js +40 -0
  89. package/dist/redirects.d.ts.map +1 -0
  90. package/dist/redirects.js +185 -0
  91. package/dist/route-scan.d.ts.map +1 -0
  92. package/dist/route-scan.js +23 -0
  93. package/dist/schema-port.d.ts.map +1 -0
  94. package/dist/schema-port.js +30 -0
  95. package/dist/{types/schema-probe.d.ts → schema-probe.d.ts} +2 -2
  96. package/dist/schema-probe.d.ts.map +1 -0
  97. package/dist/schema-probe.js +119 -0
  98. package/dist/shared.d.ts.map +1 -0
  99. package/dist/shared.js +163 -0
  100. package/dist/tsconfig.tsbuildinfo +1 -0
  101. package/package.json +12 -12
  102. package/src/check-entries.ts +4 -4
  103. package/src/check-live.ts +4 -4
  104. package/src/check-shape.ts +5 -5
  105. package/src/check-write.ts +6 -6
  106. package/src/check.ts +15 -9
  107. package/src/collection-scanner.ts +3 -3
  108. package/src/component-registry.ts +1 -1
  109. package/src/content-config-ast.ts +1 -1
  110. package/src/core.ts +8 -8
  111. package/src/editor-write-model.ts +1 -1
  112. package/src/fs/node-fs.ts +2 -2
  113. package/src/fs/staged.ts +6 -3
  114. package/src/handlers/entry-ops.ts +7 -7
  115. package/src/handlers/page-ops.ts +2 -2
  116. package/src/handlers/redirect-ops.ts +2 -2
  117. package/src/index.ts +4 -4
  118. package/src/media/contember.ts +1 -1
  119. package/src/media/file-system.ts +4 -4
  120. package/src/media/index.ts +8 -8
  121. package/src/media/local.ts +3 -3
  122. package/src/media/project-images.ts +2 -2
  123. package/src/media/s3.ts +3 -3
  124. package/src/portable.ts +25 -25
  125. package/src/project-config-ast.ts +1 -1
  126. package/src/route-scan.ts +1 -1
  127. package/src/schema-probe.ts +2 -2
  128. package/src/tsconfig.json +5 -1
  129. package/dist/types/asset-paths.d.ts.map +0 -1
  130. package/dist/types/astro-source-analysis.d.ts.map +0 -1
  131. package/dist/types/byte-range.d.ts.map +0 -1
  132. package/dist/types/check-entries.d.ts.map +0 -1
  133. package/dist/types/check-live.d.ts.map +0 -1
  134. package/dist/types/check-shape.d.ts.map +0 -1
  135. package/dist/types/check-write.d.ts.map +0 -1
  136. package/dist/types/check.d.ts.map +0 -1
  137. package/dist/types/collection-scanner.d.ts.map +0 -1
  138. package/dist/types/component-registry.d.ts.map +0 -1
  139. package/dist/types/content-config-ast.d.ts.map +0 -1
  140. package/dist/types/core.d.ts.map +0 -1
  141. package/dist/types/editor-write-model.d.ts.map +0 -1
  142. package/dist/types/fs/glob.d.ts.map +0 -1
  143. package/dist/types/fs/node-fs.d.ts.map +0 -1
  144. package/dist/types/fs/staged.d.ts.map +0 -1
  145. package/dist/types/fs/types.d.ts.map +0 -1
  146. package/dist/types/handlers/entry-ops.d.ts.map +0 -1
  147. package/dist/types/handlers/page-ops.d.ts.map +0 -1
  148. package/dist/types/handlers/redirect-ops.d.ts.map +0 -1
  149. package/dist/types/index.d.ts +0 -5
  150. package/dist/types/index.d.ts.map +0 -1
  151. package/dist/types/media/contember.d.ts.map +0 -1
  152. package/dist/types/media/errors.d.ts.map +0 -1
  153. package/dist/types/media/file-system.d.ts.map +0 -1
  154. package/dist/types/media/index.d.ts.map +0 -1
  155. package/dist/types/media/local.d.ts.map +0 -1
  156. package/dist/types/media/mime.d.ts.map +0 -1
  157. package/dist/types/media/names.d.ts.map +0 -1
  158. package/dist/types/media/project-images.d.ts.map +0 -1
  159. package/dist/types/media/s3.d.ts.map +0 -1
  160. package/dist/types/media/type-filter.d.ts.map +0 -1
  161. package/dist/types/portable.d.ts.map +0 -1
  162. package/dist/types/project-config-ast.d.ts.map +0 -1
  163. package/dist/types/public-static-file.d.ts.map +0 -1
  164. package/dist/types/redirects.d.ts.map +0 -1
  165. package/dist/types/route-scan.d.ts.map +0 -1
  166. package/dist/types/schema-port.d.ts.map +0 -1
  167. package/dist/types/schema-probe.d.ts.map +0 -1
  168. package/dist/types/shared.d.ts.map +0 -1
  169. package/dist/types/tsconfig.tsbuildinfo +0 -1
  170. /package/dist/{types/asset-paths.d.ts → asset-paths.d.ts} +0 -0
  171. /package/dist/{types/astro-source-analysis.d.ts → astro-source-analysis.d.ts} +0 -0
  172. /package/dist/{types/byte-range.d.ts → byte-range.d.ts} +0 -0
  173. /package/dist/{types/editor-write-model.d.ts → editor-write-model.d.ts} +0 -0
  174. /package/dist/{types/fs → fs}/glob.d.ts +0 -0
  175. /package/dist/{types/fs → fs}/types.d.ts +0 -0
  176. /package/dist/{types/media → media}/contember.d.ts +0 -0
  177. /package/dist/{types/media → media}/errors.d.ts +0 -0
  178. /package/dist/{types/media → media}/local.d.ts +0 -0
  179. /package/dist/{types/media → media}/mime.d.ts +0 -0
  180. /package/dist/{types/media → media}/names.d.ts +0 -0
  181. /package/dist/{types/media → media}/s3.d.ts +0 -0
  182. /package/dist/{types/media → media}/type-filter.d.ts +0 -0
  183. /package/dist/{types/public-static-file.d.ts → public-static-file.d.ts} +0 -0
  184. /package/dist/{types/redirects.d.ts → redirects.d.ts} +0 -0
  185. /package/dist/{types/route-scan.d.ts → route-scan.d.ts} +0 -0
  186. /package/dist/{types/schema-port.d.ts → schema-port.d.ts} +0 -0
  187. /package/dist/{types/shared.d.ts → shared.d.ts} +0 -0
@@ -0,0 +1,1483 @@
1
+ import path from 'node:path';
2
+ import { isMap, isPair, isScalar, parse as parseYaml, parseDocument } from 'yaml';
3
+ import { classifyEmptyContentConfig, configOptionMentions, CONTENT_CONFIG_PATHS, readContentConfig, UNREAD_CONTENT_CONFIG_PATHS, } from "./content-config-ast.js";
4
+ import { resolvePathnameFromSpec, slugifyHref } from "./shared.js";
5
+ /** Regex patterns for type inference */
6
+ const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}/;
7
+ const URL_PATTERN = /^(https?:\/\/|\/)/;
8
+ const IMAGE_EXTENSIONS = /\.(jpg|jpeg|png|gif|webp|svg|avif)$/i;
9
+ const VIDEO_EXTENSIONS = /\.(mp4|webm)$/i;
10
+ /** Maximum unique values before treating as free-form text instead of select */
11
+ const MAX_SELECT_OPTIONS = 10;
12
+ /** Minimum length for textarea detection */
13
+ const TEXTAREA_MIN_LENGTH = 200;
14
+ /** Field names that default to sidebar position */
15
+ const SIDEBAR_FIELD_NAMES = new Set([
16
+ 'title',
17
+ 'date',
18
+ 'pubdate',
19
+ 'publishdate',
20
+ 'draft',
21
+ 'image',
22
+ 'featuredimage',
23
+ 'cover',
24
+ 'coverimage',
25
+ 'thumbnail',
26
+ 'author',
27
+ ]);
28
+ /** Matches `@position <value>` or `@group <value>` in YAML comment text (# already stripped by parser) */
29
+ const DIRECTIVE_PATTERN = /^\s*@(position|group)\s+(.+)$/;
30
+ /** Field names that should never be inferred as select (always free-text) */
31
+ const FREE_TEXT_FIELD_NAMES = new Set([
32
+ 'title',
33
+ 'name',
34
+ 'description',
35
+ 'summary',
36
+ 'excerpt',
37
+ 'subtitle',
38
+ 'heading',
39
+ 'headline',
40
+ 'slug',
41
+ 'alt',
42
+ 'caption',
43
+ ]);
44
+ /** Normalized names (lowercased, underscores/hyphens stripped) that mark a field as the publish toggle. */
45
+ const PUBLISH_TOGGLE_NAMES = new Set(['draft', 'isdraft', 'published', 'ispublished', 'unpublished']);
46
+ /** Normalized names that mark a field as the publish/release date anchor. */
47
+ const PUBLISH_DATE_NAMES = new Set([
48
+ 'date',
49
+ 'pubdate',
50
+ 'publishdate',
51
+ 'publisheddate',
52
+ 'publishedate',
53
+ 'publishedat',
54
+ 'datepublished',
55
+ ]);
56
+ /** Normalize a field name for case- and separator-insensitive matching against the *_NAMES sets above. */
57
+ function normalizeFieldName(name) {
58
+ return name.toLowerCase().replace(/[_-]/g, '');
59
+ }
60
+ /** Fallback order for an entry's browse title when the collection declares no `titleField`. */
61
+ const TITLE_FALLBACK_FIELDS = ['title', 'name', 'label'];
62
+ /**
63
+ * Derive the title an entry is listed under in the CMS browser and reference pickers.
64
+ *
65
+ * A declared `titleField` (`defineCmsCollection({ cms: { titleField } })`) wins outright —
66
+ * including over a present `title` — so a collection can name its own headline field.
67
+ * Otherwise the first non-empty string among `title` → `name` → `label` is used, and the
68
+ * caller falls back to the slug when nothing matches.
69
+ *
70
+ * Both the markdown branch (frontmatter) and the data branch (JSON/YAML) go through here,
71
+ * so the two can't drift apart again the way they had.
72
+ */
73
+ function deriveEntryTitle(data, titleField) {
74
+ if (titleField) {
75
+ const declared = data[titleField];
76
+ return typeof declared === 'string' && declared !== '' ? declared : undefined;
77
+ }
78
+ for (const key of TITLE_FALLBACK_FIELDS) {
79
+ const value = data[key];
80
+ if (typeof value === 'string' && value !== '')
81
+ return value;
82
+ }
83
+ return undefined;
84
+ }
85
+ const FRONTMATTER_PATTERN = /^---\r?\n([\s\S]*?)\r?\n---/;
86
+ function extractFrontmatterBlock(content) {
87
+ const match = content.match(FRONTMATTER_PATTERN);
88
+ return match?.[1] ?? null;
89
+ }
90
+ function parseFrontmatter(content) {
91
+ const block = extractFrontmatterBlock(content);
92
+ if (!block)
93
+ return null;
94
+ return parseYaml(block);
95
+ }
96
+ /**
97
+ * Parse @position and @group comment directives from raw YAML frontmatter.
98
+ * Uses the YAML AST which preserves comments via `commentBefore` on nodes.
99
+ */
100
+ function parseFieldDirectives(content) {
101
+ const block = extractFrontmatterBlock(content);
102
+ if (!block)
103
+ return {};
104
+ const doc = parseDocument(block);
105
+ if (!isMap(doc.contents))
106
+ return {};
107
+ const result = {};
108
+ for (const pair of doc.contents.items) {
109
+ if (!isPair(pair) || !isScalar(pair.key))
110
+ continue;
111
+ const comment = pair.key.commentBefore;
112
+ if (!comment)
113
+ continue;
114
+ const directives = {};
115
+ for (const line of comment.split('\n')) {
116
+ const match = line.trim().match(DIRECTIVE_PATTERN);
117
+ if (!match)
118
+ continue;
119
+ const [, dirKey, dirValue] = match;
120
+ if (dirKey === 'position' && (dirValue === 'sidebar' || dirValue === 'header')) {
121
+ directives.position = dirValue;
122
+ }
123
+ else if (dirKey === 'group' && dirValue) {
124
+ directives.group = dirValue.trim();
125
+ }
126
+ }
127
+ if (directives.position || directives.group) {
128
+ result[String(pair.key.value)] = directives;
129
+ }
130
+ }
131
+ return result;
132
+ }
133
+ /**
134
+ * Assign default positions to fields based on field name heuristics,
135
+ * then overlay frontmatter comment directives.
136
+ *
137
+ * Only the sidebar default is written. A field the heuristic does not recognise is left with no
138
+ * `position` at all, which is what "the editor decides" looks like on the wire — it used to be
139
+ * stamped `'header'`, and that broke the one consumer that read it: an editor rendering
140
+ * `position === 'header'` as a top strip put *every* unrecognised field in the strip and left the
141
+ * main column empty, which in turn meant a collection's declared `cms.sections` had no fields to
142
+ * order and never rendered. `'header'` now only ever comes from a `@position` directive or
143
+ * `n.text({ position: 'header' })`, so an editor can trust it as an author's choice.
144
+ *
145
+ * The default written here deliberately leaves `positionDeclared` unset, and the directive
146
+ * overlay deliberately sets it: on the wire the two produce the same `'sidebar'`, and a consumer
147
+ * that cannot tell a guess from an instruction ends up overriding the instruction.
148
+ */
149
+ function assignFieldMetadata(fields, directives) {
150
+ for (const field of fields) {
151
+ // Scanner defaults: well-known fields go to sidebar
152
+ if (SIDEBAR_FIELD_NAMES.has(normalizeFieldName(field.name)) || field.type === 'image' || field.type === 'boolean') {
153
+ field.position = 'sidebar';
154
+ }
155
+ // Overlay frontmatter comment directives
156
+ const directive = directives[field.name];
157
+ if (directive) {
158
+ if (directive.position) {
159
+ field.position = directive.position;
160
+ field.positionDeclared = true;
161
+ }
162
+ if (directive.group)
163
+ field.group = directive.group;
164
+ }
165
+ }
166
+ }
167
+ /**
168
+ * Infer the field type from a value
169
+ */
170
+ function inferFieldType(value, key) {
171
+ if (value === null || value === undefined) {
172
+ return 'text';
173
+ }
174
+ if (typeof value === 'boolean') {
175
+ return 'boolean';
176
+ }
177
+ if (typeof value === 'number') {
178
+ return 'number';
179
+ }
180
+ if (Array.isArray(value)) {
181
+ return 'array';
182
+ }
183
+ if (typeof value === 'object') {
184
+ return 'object';
185
+ }
186
+ if (typeof value === 'string') {
187
+ // Check for date pattern
188
+ if (DATE_PATTERN.test(value)) {
189
+ return 'date';
190
+ }
191
+ // Check for image paths
192
+ if (IMAGE_EXTENSIONS.test(value)) {
193
+ return 'image';
194
+ }
195
+ if (VIDEO_EXTENSIONS.test(value)) {
196
+ return 'video';
197
+ }
198
+ // Check for image-specific field names (exact word boundaries, not substrings)
199
+ const lowerKey = key.toLowerCase();
200
+ if (/(?:^|[_-])(?:image|thumbnail|cover|avatar|logo|icon|banner|photo)(?:$|[_-])/.test(lowerKey)) {
201
+ return 'image';
202
+ }
203
+ // Check for URLs
204
+ if (URL_PATTERN.test(value)) {
205
+ return 'url';
206
+ }
207
+ // Check for textarea (long text or contains newlines)
208
+ if (value.includes('\n') || value.length > TEXTAREA_MIN_LENGTH) {
209
+ return 'textarea';
210
+ }
211
+ return 'text';
212
+ }
213
+ return 'text';
214
+ }
215
+ /**
216
+ * Merge field observations from multiple files to determine final field definition.
217
+ * `depth` guards against pathological deeply-nested content blowing the stack —
218
+ * real-world YAML/JSON rarely exceeds 5 levels, so the cap is well above realistic use.
219
+ */
220
+ const MAX_NESTED_FIELD_DEPTH = 16;
221
+ function mergeFieldObservations(observations, depth = 0) {
222
+ if (depth >= MAX_NESTED_FIELD_DEPTH)
223
+ return [];
224
+ const fields = [];
225
+ for (const obs of observations) {
226
+ const nonNullValues = obs.values.filter(v => v !== null && v !== undefined);
227
+ if (nonNullValues.length === 0)
228
+ continue;
229
+ // Determine type by consensus (most common inferred type)
230
+ const typeCounts = new Map();
231
+ for (const value of nonNullValues) {
232
+ const type = inferFieldType(value, obs.name);
233
+ typeCounts.set(type, (typeCounts.get(type) || 0) + 1);
234
+ }
235
+ // Get most common type
236
+ let fieldType = 'text';
237
+ let maxCount = 0;
238
+ for (const [type, count] of typeCounts) {
239
+ if (count > maxCount) {
240
+ maxCount = count;
241
+ fieldType = type;
242
+ }
243
+ }
244
+ const field = {
245
+ name: obs.name,
246
+ type: fieldType,
247
+ required: obs.presentCount === obs.totalEntries,
248
+ examples: nonNullValues.slice(0, 3),
249
+ };
250
+ // For text fields, check if we should treat as select (limited unique values)
251
+ if (fieldType === 'text' && !FREE_TEXT_FIELD_NAMES.has(normalizeFieldName(obs.name))) {
252
+ const uniqueValues = [...new Set(nonNullValues.map(v => String(v)))];
253
+ const uniqueRatio = uniqueValues.length / nonNullValues.length;
254
+ // Only treat as select if unique values are limited AND not nearly all unique
255
+ // (a high unique ratio means entries have distinct values, indicating free-text)
256
+ if (uniqueValues.length > 0 && uniqueValues.length <= MAX_SELECT_OPTIONS && nonNullValues.length >= 2 && uniqueRatio <= 0.8) {
257
+ field.type = 'select';
258
+ field.options = uniqueValues.sort();
259
+ }
260
+ }
261
+ // For arrays, try to infer item type
262
+ if (fieldType === 'array') {
263
+ const allItems = nonNullValues.flatMap(v => (Array.isArray(v) ? v : []));
264
+ if (allItems.length > 0) {
265
+ const itemType = inferFieldType(allItems[0], obs.name);
266
+ field.itemType = itemType;
267
+ // Check if array items should be select
268
+ if (itemType === 'text') {
269
+ const uniqueItems = [...new Set(allItems.map(v => String(v)))];
270
+ if (uniqueItems.length <= MAX_SELECT_OPTIONS * 2) {
271
+ field.options = uniqueItems.sort();
272
+ }
273
+ }
274
+ // Infer sub-field definitions for array-of-objects
275
+ if (itemType === 'object') {
276
+ const objectItems = allItems.filter((v) => typeof v === 'object' && v !== null && !Array.isArray(v));
277
+ if (objectItems.length > 0) {
278
+ const subFieldMap = new Map();
279
+ for (const item of objectItems) {
280
+ collectFieldObservations(subFieldMap, item, objectItems.length);
281
+ }
282
+ field.fields = mergeFieldObservations(Array.from(subFieldMap.values()), depth + 1);
283
+ }
284
+ }
285
+ }
286
+ }
287
+ // For plain object values, recurse into sub-fields so the editor can render them.
288
+ if (fieldType === 'object') {
289
+ const objectValues = nonNullValues.filter((v) => typeof v === 'object' && v !== null && !Array.isArray(v));
290
+ if (objectValues.length > 0) {
291
+ const subFieldMap = new Map();
292
+ for (const item of objectValues) {
293
+ collectFieldObservations(subFieldMap, item, objectValues.length);
294
+ }
295
+ field.fields = mergeFieldObservations(Array.from(subFieldMap.values()), depth + 1);
296
+ }
297
+ }
298
+ fields.push(field);
299
+ }
300
+ return fields;
301
+ }
302
+ function collectFieldObservations(fieldMap, data, totalEntries) {
303
+ for (const [key, value] of Object.entries(data)) {
304
+ let obs = fieldMap.get(key);
305
+ if (!obs) {
306
+ obs = { name: key, values: [], presentCount: 0, totalEntries };
307
+ fieldMap.set(key, obs);
308
+ }
309
+ obs.values.push(value);
310
+ obs.presentCount++;
311
+ }
312
+ }
313
+ function assembleCollectionDefinition(collectionName, contentDir, fieldMap, entryInfos, entryCount, extra) {
314
+ for (const obs of fieldMap.values()) {
315
+ obs.totalEntries = entryCount;
316
+ }
317
+ entryInfos.sort((a, b) => (a.title ?? a.slug).localeCompare(b.title ?? b.slug));
318
+ const fields = mergeFieldObservations(Array.from(fieldMap.values()));
319
+ const label = collectionName.replace(/[-_]/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
320
+ return {
321
+ name: collectionName,
322
+ label,
323
+ path: path.join(contentDir, collectionName),
324
+ entryCount,
325
+ fields,
326
+ fileExtension: 'md',
327
+ entries: entryInfos,
328
+ ...extra,
329
+ };
330
+ }
331
+ /**
332
+ * Compute the root-relative base path of a collection's source directory.
333
+ * `basePath` is root-relative (e.g. `src/content/blog` for the default layout,
334
+ * or a glob loader base as the config spells it, like `./src/content/jsem/`).
335
+ * When it matches the default `<contentDir>/<name>` location, the canonical
336
+ * default path is returned; otherwise the base path is returned normalized,
337
+ * without the leading `./` and trailing slash, so it is a prefix of each entry's
338
+ * `sourcePath`, except when the base is the project root, which is reported as `.`.
339
+ */
340
+ function getCollectionSourceBasePath(basePath, collectionName, contentDir) {
341
+ const defaultCollectionPath = path.join(contentDir, collectionName);
342
+ const normalizedBasePath = normalizeBasePath(basePath);
343
+ if (normalizedBasePath === path.normalize(defaultCollectionPath)) {
344
+ return path.join(contentDir, collectionName);
345
+ }
346
+ return normalizedBasePath;
347
+ }
348
+ async function buildCollectionDefinition(fs, basePath, sources, collectionName, contentDir) {
349
+ if (sources.length === 0)
350
+ return null;
351
+ const sourceBasePath = getCollectionSourceBasePath(basePath, collectionName, contentDir);
352
+ const hasMd = sources.some(s => s.relPath.endsWith('.md'));
353
+ const fileExtension = hasMd ? 'md' : 'mdx';
354
+ const fieldMap = new Map();
355
+ const allDirectives = {};
356
+ const entryInfos = [];
357
+ let hasDraft = false;
358
+ // Read tolerantly: a file removed/renamed between the directory listing and the
359
+ // read (routine during dev on save/checkout) must skip that one entry, not reject
360
+ // the whole Promise.all and abort the scan.
361
+ const fileContents = await Promise.all(sources.map(s => fs.readFile(path.join(basePath, s.relPath)).catch(() => null)));
362
+ for (let i = 0; i < sources.length; i++) {
363
+ const source = sources[i];
364
+ const content = fileContents[i];
365
+ if (content === null)
366
+ continue;
367
+ const frontmatter = parseFrontmatter(content);
368
+ const directives = parseFieldDirectives(content);
369
+ for (const [key, value] of Object.entries(directives)) {
370
+ if (!allDirectives[key]) {
371
+ allDirectives[key] = value;
372
+ }
373
+ }
374
+ const entryInfo = {
375
+ slug: source.slug,
376
+ sourcePath: path.join(sourceBasePath, source.relPath),
377
+ };
378
+ if (frontmatter) {
379
+ // No `titleField` here: the content config isn't parsed yet at scan time,
380
+ // so a declared field is applied later in `applyParsedConfig`.
381
+ const title = deriveEntryTitle(frontmatter);
382
+ if (title !== undefined) {
383
+ entryInfo.title = title;
384
+ }
385
+ if (typeof frontmatter.draft === 'boolean' && frontmatter.draft) {
386
+ entryInfo.draft = true;
387
+ }
388
+ entryInfo.data = frontmatter;
389
+ }
390
+ entryInfos.push(entryInfo);
391
+ if (!frontmatter)
392
+ continue;
393
+ if (frontmatter.draft === true)
394
+ hasDraft = true;
395
+ collectFieldObservations(fieldMap, frontmatter, sources.length);
396
+ }
397
+ const def = assembleCollectionDefinition(collectionName, contentDir, fieldMap, entryInfos, sources.length, {
398
+ path: sourceBasePath,
399
+ supportsDraft: hasDraft,
400
+ fileExtension,
401
+ });
402
+ assignFieldMetadata(def.fields, allDirectives);
403
+ return def;
404
+ }
405
+ /**
406
+ * Build a DATA collection definition (JSON/YAML) from an explicit source list,
407
+ * relative to `basePath`. Mirrors {@link buildCollectionDefinition} but parses
408
+ * data files instead of markdown frontmatter, so it backs both the directory
409
+ * scan ({@link scanDataCollection}) and the glob-loader path
410
+ * ({@link scanGlobCollection}) — the latter for data collections whose `base`
411
+ * lives outside `contentDir`.
412
+ */
413
+ async function buildDataCollectionDefinition(fs, basePath, sources, collectionName, contentDir) {
414
+ if (sources.length === 0)
415
+ return null;
416
+ const sourceBasePath = getCollectionSourceBasePath(basePath, collectionName, contentDir);
417
+ const fieldMap = new Map();
418
+ const entryInfos = [];
419
+ const ext = sources.some(s => s.relPath.endsWith('.json'))
420
+ ? 'json'
421
+ : sources.some(s => s.relPath.endsWith('.yaml'))
422
+ ? 'yaml'
423
+ : 'yml';
424
+ const fileContents = await Promise.all(sources.map(s => fs.readFile(path.join(basePath, s.relPath)).catch(() => null)));
425
+ for (let i = 0; i < sources.length; i++) {
426
+ const source = sources[i];
427
+ const raw = fileContents[i];
428
+ if (raw === null)
429
+ continue;
430
+ let data = null;
431
+ try {
432
+ data = source.relPath.endsWith('.json') ? JSON.parse(raw) : parseYaml(raw);
433
+ }
434
+ catch {
435
+ continue;
436
+ }
437
+ if (!data || typeof data !== 'object')
438
+ continue;
439
+ // Shared with the markdown branch, so `title` wins over `name` here too — a
440
+ // deliberate change from the old `name`-first order; declare `titleField` to
441
+ // pin a different field.
442
+ const title = deriveEntryTitle(data);
443
+ entryInfos.push({
444
+ slug: source.slug,
445
+ title,
446
+ sourcePath: path.join(sourceBasePath, source.relPath),
447
+ data,
448
+ });
449
+ collectFieldObservations(fieldMap, data, sources.length);
450
+ }
451
+ return assembleCollectionDefinition(collectionName, contentDir, fieldMap, entryInfos, sources.length, {
452
+ path: sourceBasePath,
453
+ type: 'data',
454
+ fileExtension: ext,
455
+ });
456
+ }
457
+ /**
458
+ * Scan a single collection directory and infer its schema
459
+ */
460
+ async function scanCollection(fs, collectionPath, collectionName, contentDir) {
461
+ const dirEntries = await fs.list(collectionPath);
462
+ if (dirEntries.length === 0)
463
+ return null;
464
+ const sources = [];
465
+ const takenSlugs = new Set();
466
+ for (const entry of dirEntries) {
467
+ if (entry.isDirectory)
468
+ continue;
469
+ if (!entry.name.endsWith('.md') && !entry.name.endsWith('.mdx'))
470
+ continue;
471
+ const slug = entry.name.replace(/\.(md|mdx)$/, '');
472
+ sources.push({ slug, relPath: entry.name });
473
+ takenSlugs.add(slug);
474
+ }
475
+ // Hugo-style layout: <slug>/index.md(x). Flat files win on slug conflict.
476
+ const subdirs = dirEntries.filter(e => e.isDirectory && !e.name.startsWith('_') && !e.name.startsWith('.'));
477
+ const indexLookups = await Promise.all(subdirs.map(async (dir) => {
478
+ if (takenSlugs.has(dir.name))
479
+ return null;
480
+ for (const ext of ['md', 'mdx']) {
481
+ const relPath = path.join(dir.name, `index.${ext}`);
482
+ if (await fs.exists(path.join(collectionPath, relPath))) {
483
+ return { slug: dir.name, relPath };
484
+ }
485
+ }
486
+ return null;
487
+ }));
488
+ for (const entry of indexLookups) {
489
+ if (entry)
490
+ sources.push(entry);
491
+ }
492
+ if (sources.length === 0)
493
+ return null;
494
+ return await buildCollectionDefinition(fs, collectionPath, sources, collectionName, contentDir);
495
+ }
496
+ /**
497
+ * The files a glob loader matches, relative to its base. The scan leaves symbolic links out;
498
+ * the scan report follows them, as Astro does, to see what that left out.
499
+ */
500
+ async function globLoaderFiles(fs, baseRel, pattern, options) {
501
+ // The port's glob walks the root; scope it to the loader base and re-relativize matches.
502
+ // The scanner ignores files/dirs whose path segments start with `_` or `.` (private/hidden),
503
+ // matching the directory-walk behavior of the original scanner — the glob pattern alone
504
+ // would otherwise match them.
505
+ const isPrivate = (relToBase) => relToBase.split('/').some(seg => seg.startsWith('_') || seg.startsWith('.'));
506
+ // Such a directory is not walked either, so one that cannot be read does not cost the
507
+ // collection: none of its files would be kept.
508
+ const matches = await fs.glob(path.join(baseRel, pattern), { ...options, skipDirectory: dir => isPrivate(path.relative(baseRel, dir)) });
509
+ return matches
510
+ .map(rel => path.relative(baseRel, rel))
511
+ .filter(relToBase => !isPrivate(relToBase));
512
+ }
513
+ /**
514
+ * Scan a collection declared in content config via a glob loader (base + pattern),
515
+ * which may share a base directory with another collection (nested layout).
516
+ */
517
+ async function scanGlobCollection(fs, collectionName, baseRel, pattern, contentDir) {
518
+ const rels = await globLoaderFiles(fs, baseRel, pattern);
519
+ // Markdown collections win when both are present; otherwise build a data
520
+ // collection (yaml/json) — the loader pattern dictates which, and a data
521
+ // `base` may sit outside `contentDir` and nest entries in subdirectories.
522
+ const mdSources = rels
523
+ .filter(relToBase => relToBase.endsWith('.md') || relToBase.endsWith('.mdx'))
524
+ .map(relToBase => ({ slug: relToBase.replace(/\.(md|mdx)$/, ''), relPath: relToBase }));
525
+ if (mdSources.length > 0) {
526
+ return await buildCollectionDefinition(fs, baseRel, mdSources, collectionName, contentDir);
527
+ }
528
+ const dataSources = rels
529
+ .filter(relToBase => /\.(ya?ml|json)$/.test(relToBase))
530
+ .map(relToBase => ({ slug: relToBase.replace(/\.(ya?ml|json)$/, ''), relPath: relToBase }));
531
+ if (dataSources.length > 0) {
532
+ return await buildDataCollectionDefinition(fs, baseRel, dataSources, collectionName, contentDir);
533
+ }
534
+ return null;
535
+ }
536
+ /**
537
+ * Filter scanned fields to schema-only and apply per-field overrides (type, hints, required)
538
+ * in a single pass. Filtering must happen first since it can shrink `def.fields`.
539
+ */
540
+ function applyParsedConfig(collections, parsed) {
541
+ for (const [collectionName, parsedColl] of parsed) {
542
+ const def = collections[collectionName];
543
+ if (!def)
544
+ continue;
545
+ if (parsedColl.fields.length > 0) {
546
+ const schemaNames = new Set(parsedColl.fields.map(f => f.name));
547
+ def.fields = def.fields.filter(f => schemaNames.has(f.name));
548
+ }
549
+ const fieldsByName = new Map(def.fields.map(f => [f.name, f]));
550
+ for (const pf of parsedColl.fields) {
551
+ const field = fieldsByName.get(pf.name);
552
+ if (field)
553
+ applyParsedFieldOverrides(field, pf);
554
+ // Declared but in no entry yet — a field just added to the schema. Nested fields get the same.
555
+ else
556
+ def.fields.push(parsedFieldToFieldDefinition(pf));
557
+ }
558
+ // Declarative form layout from `defineCmsCollection({ cms })`.
559
+ if (parsedColl.layout)
560
+ def.layout = parsedColl.layout;
561
+ // Declarative page-URL rule from `defineCmsCollection({ cms: { pathname } })`.
562
+ if (parsedColl.pathname)
563
+ def.pathname = parsedColl.pathname;
564
+ // `defineCmsCollection({ cms: { fragment, previewOf } })`: the collection is rendered
565
+ // inside other pages and owns no URL. Consumers must not invent one for its entries.
566
+ if (parsedColl.fragment) {
567
+ def.fragment = parsedColl.fragment;
568
+ if (parsedColl.previewOf)
569
+ def.previewOf = parsedColl.previewOf;
570
+ }
571
+ // Collection-browser metadata from `defineCmsCollection({ cms: { label, hidden, order, group } })`.
572
+ // `hidden` is only a flag here: the definition stays in the map, because reference
573
+ // pickers and entry routes resolve through it. Browsers are the ones that skip it.
574
+ if (parsedColl.label)
575
+ def.label = parsedColl.label;
576
+ if (parsedColl.hidden)
577
+ def.hidden = parsedColl.hidden;
578
+ if (parsedColl.order !== undefined)
579
+ def.order = parsedColl.order;
580
+ if (parsedColl.group)
581
+ def.group = parsedColl.group;
582
+ // Declarative entry-title source from `defineCmsCollection({ cms: { titleField } })`.
583
+ // The scan derived titles by fallback; re-derive them from the declared field.
584
+ if (parsedColl.titleField) {
585
+ def.titleField = parsedColl.titleField;
586
+ applyCollectionTitleField(def, parsedColl.titleField);
587
+ }
588
+ }
589
+ }
590
+ /**
591
+ * Re-derive every entry's title from the collection's declared `titleField` and restore
592
+ * the title ordering `assembleCollectionDefinition` established — the scan sorted on the
593
+ * fallback titles, which the declared field just replaced. A later `orderBy` still wins.
594
+ */
595
+ function applyCollectionTitleField(def, titleField) {
596
+ if (!def.entries)
597
+ return;
598
+ for (const entry of def.entries) {
599
+ entry.title = entry.data ? deriveEntryTitle(entry.data, titleField) : undefined;
600
+ }
601
+ def.entries.sort((a, b) => (a.title ?? a.slug).localeCompare(b.title ?? b.slug));
602
+ }
603
+ /** Map a parsed field's layout hints onto the field definition (sidebar/position → position). */
604
+ function applyParsedFieldLayout(field, pf) {
605
+ const layout = pf.layout;
606
+ if (!layout)
607
+ return;
608
+ if (layout.label !== undefined)
609
+ field.label = layout.label;
610
+ if (layout.help !== undefined)
611
+ field.help = layout.help;
612
+ if (layout.group !== undefined)
613
+ field.group = layout.group;
614
+ if (layout.width !== undefined)
615
+ field.width = layout.width;
616
+ if (layout.order !== undefined)
617
+ field.order = layout.order;
618
+ // Either spelling is the author's answer, so both carry `positionDeclared` — that is the flag
619
+ // telling a layout apart from the scanner's name-and-type guess below, and the difference
620
+ // decides whether a rule like the headline hoist may override the placement.
621
+ if (layout.sidebar) {
622
+ field.position = 'sidebar';
623
+ field.positionDeclared = true;
624
+ }
625
+ if (layout.position) {
626
+ field.position = layout.position;
627
+ field.positionDeclared = true;
628
+ }
629
+ if (layout.derivedFrom) {
630
+ // A declared derivation beats `detectDerivedHrefFields`: that pass runs after this one
631
+ // and skips any field already carrying `derivedFrom`, so the config wins by construction.
632
+ field.derivedFrom = layout.derivedFrom;
633
+ // A derivation read out of the config is a statement, not the guess `detectDerivedHrefFields`
634
+ // makes from three sampled values — and only a statement is recomputed on write. Mark it so
635
+ // consumers reading the wire definition can tell the two apart; the guess never sets this.
636
+ field.derivedDeclared = true;
637
+ if (layout.derivedTransform)
638
+ field.derivedTransform = layout.derivedTransform;
639
+ // The value is computed on every write, so hand-editing it makes no sense — hide it
640
+ // unless the author explicitly asked for the opposite.
641
+ field.hidden = layout.hidden ?? true;
642
+ return;
643
+ }
644
+ if (layout.hidden)
645
+ field.hidden = true;
646
+ }
647
+ /**
648
+ * Apply parsed schema overrides to an inferred field, recursing into nested object/array fields.
649
+ *
650
+ * Note on schema-vs-inferred merging at nested levels: schema-declared sub-fields replace
651
+ * the inferred list rather than merging. Inferred-only sub-fields are *not* lost — the
652
+ * editor's `ObjectFields` recovers them via its `extraKeys` calculation (field value keys
653
+ * minus schemaNames), routes them through `FrontmatterField` (value-based auto-detect),
654
+ * and offers a remove button. Merging here would defeat that.
655
+ */
656
+ function applyParsedFieldOverrides(field, pf) {
657
+ if (pf.type) {
658
+ field.type = pf.type;
659
+ // Parsed options only ever come from a declared `(z|n).enum([…])`, so they are a
660
+ // closed set — unlike the options `mergeFieldObservations` infers from the entries
661
+ // it happened to see, which stay open to free text.
662
+ if (pf.options) {
663
+ field.options = pf.options;
664
+ field.optionsClosed = true;
665
+ }
666
+ }
667
+ if (pf.itemType)
668
+ field.itemType = pf.itemType;
669
+ if (pf.hints)
670
+ field.hints = pf.hints;
671
+ if (pf.astroImage)
672
+ field.astroImage = true;
673
+ field.required = pf.required;
674
+ applyParsedFieldLayout(field, pf);
675
+ if (pf.fields) {
676
+ const existingByName = new Map((field.fields ?? []).map(f => [f.name, f]));
677
+ field.fields = pf.fields.map((subPf) => {
678
+ const existing = existingByName.get(subPf.name);
679
+ if (existing) {
680
+ applyParsedFieldOverrides(existing, subPf);
681
+ return existing;
682
+ }
683
+ return parsedFieldToFieldDefinition(subPf);
684
+ });
685
+ }
686
+ }
687
+ /**
688
+ * Build a FieldDefinition from a parsed schema field when no inferred counterpart exists.
689
+ * Falls back to `'text'` when the parser couldn't pin a type — keeps the field visible
690
+ * and editable. Schema-declared-but-data-absent fields would otherwise vanish.
691
+ */
692
+ function parsedFieldToFieldDefinition(pf) {
693
+ const fd = {
694
+ name: pf.name,
695
+ // A parsed field with nested children but no explicit type is necessarily an object.
696
+ // Otherwise default to 'text' so users can still fill in schema-declared fields
697
+ // whose helper the parser didn't recognize.
698
+ type: pf.type ?? (pf.fields ? 'object' : 'text'),
699
+ required: pf.required,
700
+ };
701
+ if (pf.options) {
702
+ fd.options = pf.options;
703
+ fd.optionsClosed = true;
704
+ }
705
+ if (pf.itemType)
706
+ fd.itemType = pf.itemType;
707
+ if (pf.hints)
708
+ fd.hints = pf.hints;
709
+ if (pf.astroImage)
710
+ fd.astroImage = true;
711
+ if (pf.fields)
712
+ fd.fields = pf.fields.map(parsedFieldToFieldDefinition);
713
+ applyParsedFieldLayout(fd, pf);
714
+ return fd;
715
+ }
716
+ /** Apply orderBy configuration: set the field name and direction on the definition, then re-sort entries. */
717
+ function applyCollectionOrderBy(collections, parsed) {
718
+ for (const [collectionName, parsedColl] of parsed) {
719
+ const orderField = parsedColl.fields.find(f => f.orderBy);
720
+ if (!orderField?.orderBy)
721
+ continue;
722
+ const def = collections[collectionName];
723
+ if (!def)
724
+ continue;
725
+ const fieldName = orderField.name;
726
+ const direction = orderField.orderBy.direction;
727
+ def.orderBy = fieldName;
728
+ def.orderDirection = direction;
729
+ if (def.entries && def.entries.length > 1) {
730
+ const dir = direction === 'desc' ? -1 : 1;
731
+ def.entries.sort((a, b) => {
732
+ const aVal = a.data?.[fieldName];
733
+ const bVal = b.data?.[fieldName];
734
+ if (aVal == null && bVal == null)
735
+ return 0;
736
+ if (aVal == null)
737
+ return 1;
738
+ if (bVal == null)
739
+ return -1;
740
+ if (typeof aVal === 'number' && typeof bVal === 'number')
741
+ return (aVal - bVal) * dir;
742
+ if (aVal instanceof Date && bVal instanceof Date)
743
+ return (aVal.getTime() - bVal.getTime()) * dir;
744
+ return String(aVal).localeCompare(String(bVal)) * dir;
745
+ });
746
+ }
747
+ }
748
+ }
749
+ /**
750
+ * Detect reference fields. Prefers explicit `reference()` declarations from the content
751
+ * config; if none are found anywhere, falls back to heuristic slug matching.
752
+ */
753
+ function detectReferenceFields(collections, parsed) {
754
+ let appliedAny = false;
755
+ for (const [collectionName, parsedColl] of parsed) {
756
+ const def = collections[collectionName];
757
+ if (!def)
758
+ continue;
759
+ for (const pf of parsedColl.fields) {
760
+ if (!pf.reference)
761
+ continue;
762
+ const field = def.fields.find(f => f.name === pf.name);
763
+ if (!field)
764
+ continue;
765
+ appliedAny = true;
766
+ if (pf.reference.isArray) {
767
+ field.type = 'array';
768
+ field.itemType = 'reference';
769
+ }
770
+ else {
771
+ field.type = 'reference';
772
+ }
773
+ field.collection = pf.reference.target;
774
+ field.options = undefined;
775
+ }
776
+ }
777
+ if (!appliedAny)
778
+ detectReferenceFieldsBySlugMatch(collections);
779
+ }
780
+ function detectReferenceFieldsBySlugMatch(collections) {
781
+ const collectionSlugs = new Map();
782
+ for (const [name, def] of Object.entries(collections)) {
783
+ if (def.entries && def.entries.length > 0) {
784
+ collectionSlugs.set(name, new Set(def.entries.map(e => e.slug)));
785
+ }
786
+ }
787
+ for (const [collectionName, def] of Object.entries(collections)) {
788
+ for (const field of def.fields) {
789
+ if ((field.type === 'text' || field.type === 'select') && field.examples) {
790
+ const stringExamples = field.examples.filter((v) => typeof v === 'string');
791
+ if (stringExamples.length === 0)
792
+ continue;
793
+ // Find all candidate collections where all examples match slugs
794
+ const candidates = [];
795
+ for (const [targetName, slugs] of collectionSlugs) {
796
+ if (targetName === collectionName)
797
+ continue;
798
+ const matchCount = stringExamples.filter(v => slugs.has(v)).length;
799
+ if (matchCount > 0 && matchCount === stringExamples.length) {
800
+ candidates.push({ name: targetName, slugs });
801
+ }
802
+ }
803
+ let bestTarget;
804
+ if (candidates.length === 1) {
805
+ bestTarget = candidates[0].name;
806
+ }
807
+ else if (candidates.length > 1) {
808
+ // Multiple matches — disambiguate using all field values
809
+ const allValues = def.entries?.flatMap(e => {
810
+ const v = e.data?.[field.name];
811
+ return typeof v === 'string' ? [v] : [];
812
+ }) ?? stringExamples;
813
+ let bestOverlap = 0;
814
+ for (const c of candidates) {
815
+ const overlap = allValues.filter(v => c.slugs.has(v)).length;
816
+ if (overlap > bestOverlap) {
817
+ bestOverlap = overlap;
818
+ bestTarget = c.name;
819
+ }
820
+ }
821
+ }
822
+ if (bestTarget) {
823
+ field.type = 'reference';
824
+ field.collection = bestTarget;
825
+ field.options = undefined;
826
+ }
827
+ }
828
+ if (field.type === 'array' && field.itemType === 'text' && field.options) {
829
+ let bestTarget;
830
+ let bestOverlap = 0;
831
+ for (const [targetName, slugs] of collectionSlugs) {
832
+ if (targetName === collectionName)
833
+ continue;
834
+ const matchCount = field.options.filter(v => slugs.has(v)).length;
835
+ if (matchCount > 0 && matchCount >= field.options.length * 0.5) {
836
+ if (matchCount > bestOverlap) {
837
+ bestOverlap = matchCount;
838
+ bestTarget = targetName;
839
+ }
840
+ }
841
+ }
842
+ if (bestTarget) {
843
+ field.type = 'array';
844
+ field.itemType = 'reference';
845
+ field.collection = bestTarget;
846
+ field.options = undefined;
847
+ }
848
+ }
849
+ }
850
+ }
851
+ }
852
+ /**
853
+ * Tag fields with semantic roles so the editor UI can position them without
854
+ * matching on Astro-specific field names. Detection lives here — the layer
855
+ * that already knows it's parsing Astro content collections.
856
+ */
857
+ function assignSemanticRoles(collections) {
858
+ for (const def of Object.values(collections)) {
859
+ let toggle;
860
+ let dateByName;
861
+ let dateByType;
862
+ for (const field of def.fields) {
863
+ if (field.hidden || field.role)
864
+ continue;
865
+ const normalized = normalizeFieldName(field.name);
866
+ if (!toggle && field.type === 'boolean' && PUBLISH_TOGGLE_NAMES.has(normalized)) {
867
+ toggle = field;
868
+ }
869
+ else if (!dateByName && PUBLISH_DATE_NAMES.has(normalized)) {
870
+ dateByName = field;
871
+ }
872
+ else if (!dateByType && (field.type === 'date' || field.type === 'datetime')) {
873
+ dateByType = field;
874
+ }
875
+ }
876
+ if (toggle)
877
+ toggle.role = 'publish-toggle';
878
+ const date = dateByName ?? dateByType;
879
+ if (date)
880
+ date.role = 'publish-date';
881
+ }
882
+ }
883
+ /** The types an image's alt text field may have: it is prose, so a select or a URL is not one. */
884
+ const ALT_TEXT_TYPES = new Set(['text', 'textarea']);
885
+ /** Pair every image field, at any depth, with a sibling `<name>Alt` text field (`cover` → `coverAlt`). */
886
+ function pairImageAltFields(fields) {
887
+ const byName = new Map(fields.map(field => [field.name, field]));
888
+ for (const field of fields) {
889
+ if (field.fields)
890
+ pairImageAltFields(field.fields);
891
+ if (field.type !== 'image')
892
+ continue;
893
+ const alt = byName.get(`${field.name}Alt`);
894
+ if (alt && ALT_TEXT_TYPES.has(alt.type))
895
+ field.altField = alt.name;
896
+ }
897
+ }
898
+ /** Suffixes that indicate a field is a derived href/url/slug companion */
899
+ const HREF_SUFFIXES = ['href', 'url', 'link', 'slug', 'path'];
900
+ /**
901
+ * Detect fields like `categoryHref` that are derived from a source field (`category`).
902
+ * When every value is a slugified href of the source, mark it hidden with derivedFrom.
903
+ */
904
+ function detectDerivedHrefFields(collections) {
905
+ for (const def of Object.values(collections)) {
906
+ const fieldsByName = new Map(def.fields.map(f => [f.name, f]));
907
+ for (const field of def.fields) {
908
+ if (field.hidden || field.derivedFrom)
909
+ continue;
910
+ const lowerName = field.name.toLowerCase();
911
+ for (const suffix of HREF_SUFFIXES) {
912
+ if (!lowerName.endsWith(suffix))
913
+ continue;
914
+ const baseName = field.name.slice(0, -suffix.length);
915
+ if (!baseName)
916
+ continue;
917
+ // Case-insensitive lookup: exact match first, then scan by lowercased name
918
+ let sourceField = fieldsByName.get(baseName);
919
+ if (!sourceField) {
920
+ const lowerBase = baseName.toLowerCase();
921
+ for (const f of fieldsByName.values()) {
922
+ if (f.name.toLowerCase() === lowerBase) {
923
+ sourceField = f;
924
+ break;
925
+ }
926
+ }
927
+ }
928
+ if (!sourceField || !sourceField.examples || !field.examples)
929
+ continue;
930
+ const sourceExamples = sourceField.examples.filter((v) => typeof v === 'string');
931
+ const derivedExamples = field.examples.filter((v) => typeof v === 'string');
932
+ if (sourceExamples.length === 0 || derivedExamples.length === 0)
933
+ continue;
934
+ // Order-independent: check that every derived value matches some source value's href
935
+ const expectedHrefs = new Set(sourceExamples.map(slugifyHref));
936
+ const allMatch = derivedExamples.every(v => expectedHrefs.has(v));
937
+ if (allMatch) {
938
+ field.hidden = true;
939
+ field.derivedFrom = sourceField.name;
940
+ break;
941
+ }
942
+ }
943
+ }
944
+ }
945
+ }
946
+ /**
947
+ * Scan a data collection (JSON/YAML files) and infer its schema
948
+ */
949
+ async function scanDataCollection(fs, collectionPath, collectionName, contentDir) {
950
+ const dirEntries = await fs.list(collectionPath);
951
+ if (dirEntries.length === 0)
952
+ return null;
953
+ const sources = [];
954
+ const takenSlugs = new Set();
955
+ for (const entry of dirEntries) {
956
+ if (entry.isDirectory)
957
+ continue;
958
+ if (!entry.name.endsWith('.json') && !entry.name.endsWith('.yaml') && !entry.name.endsWith('.yml'))
959
+ continue;
960
+ const slug = entry.name.replace(/\.(json|ya?ml)$/, '');
961
+ sources.push({ slug, relPath: entry.name });
962
+ takenSlugs.add(slug);
963
+ }
964
+ // Hugo-style layout: <slug>/index.{json,yaml,yml}. Flat files win on slug conflict.
965
+ const subdirs = dirEntries.filter(e => e.isDirectory && !e.name.startsWith('_') && !e.name.startsWith('.'));
966
+ const indexLookups = await Promise.all(subdirs.map(async (dir) => {
967
+ if (takenSlugs.has(dir.name))
968
+ return null;
969
+ for (const indexExt of ['json', 'yaml', 'yml']) {
970
+ const relPath = path.join(dir.name, `index.${indexExt}`);
971
+ if (await fs.exists(path.join(collectionPath, relPath))) {
972
+ return { slug: dir.name, relPath };
973
+ }
974
+ }
975
+ return null;
976
+ }));
977
+ for (const entry of indexLookups) {
978
+ if (entry)
979
+ sources.push(entry);
980
+ }
981
+ return await buildDataCollectionDefinition(fs, collectionPath, sources, collectionName, contentDir);
982
+ }
983
+ /**
984
+ * Normalize a glob loader `base` (`./src/content/x`, `src/content/x/`) into a
985
+ * root-relative path comparable with a collection definition's `path`. The project
986
+ * root comes out as `.`. Entry reads and writes resolve their directory with this too,
987
+ * so they land where the scan reports a collection.
988
+ */
989
+ export function normalizeBasePath(basePath) {
990
+ return path.normalize(basePath.replace(/^\.[/\\]+/, '')).replace(/[/\\]+$/, '');
991
+ }
992
+ /**
993
+ * A loader pattern that selects the whole base directory rather than a slice of it:
994
+ * no subdirectory or filename prefix. `*`, `*.md`, `*.{md,mdx}` and `**\/*.md` qualify;
995
+ * `otazky/*.md` and `otazky-*.md` do not, because they pick a subset.
996
+ */
997
+ const WHOLE_DIRECTORY_PATTERN = /^(?:\*\*[/\\])*\*(?:\.(?:\{([^{}/\\]*)\}|([A-Za-z0-9]+)))?$/;
998
+ /**
999
+ * Does a config-declared glob loader cover everything the directory scan already found?
1000
+ * Beyond the pattern shape, the extensions have to line up too — `*.mdx` over a directory
1001
+ * of `.md` files is a narrower selection, so it stays its own (child) collection.
1002
+ */
1003
+ function patternCoversWholeCollection(pattern, definition) {
1004
+ const match = pattern.trim().replace(/^\.[/\\]+/, '').match(WHOLE_DIRECTORY_PATTERN);
1005
+ if (!match)
1006
+ return false;
1007
+ const [, braceGroup, singleExtension] = match;
1008
+ // A bare `*` / `**/*` takes whatever the directory holds.
1009
+ if (braceGroup === undefined && singleExtension === undefined)
1010
+ return true;
1011
+ const patternExtensions = new Set((braceGroup !== undefined ? braceGroup.split(',') : [singleExtension])
1012
+ .map(ext => ext.trim().replace(/^\./, '').toLowerCase())
1013
+ .filter(Boolean));
1014
+ const scannedExtensions = new Set((definition.entries ?? []).map(entry => path.extname(entry.sourcePath).slice(1).toLowerCase()).filter(Boolean));
1015
+ if (scannedExtensions.size === 0)
1016
+ return false;
1017
+ for (const ext of scannedExtensions) {
1018
+ if (!patternExtensions.has(ext))
1019
+ return false;
1020
+ }
1021
+ return true;
1022
+ }
1023
+ /**
1024
+ * Scan all collections in the content directory.
1025
+ *
1026
+ * `contentDir` is a root-relative directory (default `src/content`); all I/O is
1027
+ * resolved against the injected `CmsFileSystem`'s root. `parseCache` keeps the
1028
+ * mtime-keyed content-config parse result across calls within one core instance.
1029
+ */
1030
+ export async function scanCollections(fs, contentDir = 'src/content', parseCache = new Map(), options = {}) {
1031
+ const collections = {};
1032
+ /** Collections whose scan threw, by name, as they go into the scan report. */
1033
+ const failedScans = new Map();
1034
+ const entries = await fs.list(contentDir);
1035
+ // Each collection scans independently: a single unreadable/racing collection is
1036
+ // skipped (logged) rather than rejecting Promise.all and aborting every collection.
1037
+ const scanPromises = entries
1038
+ .filter(entry => entry.isDirectory && !entry.name.startsWith('_') && !entry.name.startsWith('.'))
1039
+ .map(async (entry) => {
1040
+ try {
1041
+ const collectionPath = path.join(contentDir, entry.name);
1042
+ const definition = await scanCollection(fs, collectionPath, entry.name, contentDir)
1043
+ ?? await scanDataCollection(fs, collectionPath, entry.name, contentDir);
1044
+ if (definition) {
1045
+ collections[entry.name] = definition;
1046
+ }
1047
+ }
1048
+ catch (error) {
1049
+ console.warn(`[cms] Skipping collection "${entry.name}" — scan failed:`, error);
1050
+ failedScans.set(entry.name, { name: entry.name, reason: 'scan-failed', base: path.join(contentDir, entry.name) });
1051
+ }
1052
+ });
1053
+ await Promise.all(scanPromises);
1054
+ // Post-scan: apply schema-driven field config, detect references, derived fields, and ordering
1055
+ const config = await readContentConfig(fs, parseCache);
1056
+ const { parsed } = config;
1057
+ // Where the directory scan put each collection, so a config-declared loader base can be
1058
+ // matched back to the definition that already covers that directory.
1059
+ const scannedNameByBasePath = new Map();
1060
+ for (const [name, definition] of Object.entries(collections)) {
1061
+ scannedNameByBasePath.set(normalizeBasePath(definition.path), name);
1062
+ }
1063
+ /** Scanned collections re-keyed under their config name: scanned key -> config key. */
1064
+ const rekeyedScannedNames = new Map();
1065
+ for (const [collectionName, parsedCollection] of parsed) {
1066
+ if (collections[collectionName])
1067
+ continue;
1068
+ if (!parsedCollection.loaderBase || !parsedCollection.loaderPattern)
1069
+ continue;
1070
+ // Same collection, different key (`heroSlides` over `content/hero-slides`): the loader
1071
+ // base is a directory the scan already covered and the pattern selects all of it. Re-key
1072
+ // the scanned definition instead of adding a second copy — entries and inferred fields
1073
+ // survive, and the config key wins because `applyParsedConfig`, references and
1074
+ // `defineCmsCollection` all key by it.
1075
+ const scannedName = scannedNameByBasePath.get(normalizeBasePath(parsedCollection.loaderBase));
1076
+ const scannedDefinition = scannedName ? collections[scannedName] : undefined;
1077
+ if (scannedName && scannedDefinition && patternCoversWholeCollection(parsedCollection.loaderPattern, scannedDefinition)) {
1078
+ delete collections[scannedName];
1079
+ scannedDefinition.name = collectionName;
1080
+ for (const other of Object.values(collections)) {
1081
+ if (other.parentCollection === scannedName)
1082
+ other.parentCollection = collectionName;
1083
+ }
1084
+ collections[collectionName] = scannedDefinition;
1085
+ rekeyedScannedNames.set(scannedName, collectionName);
1086
+ continue;
1087
+ }
1088
+ let definition;
1089
+ try {
1090
+ definition = await scanGlobCollection(fs, collectionName, parsedCollection.loaderBase, parsedCollection.loaderPattern, contentDir);
1091
+ }
1092
+ catch (error) {
1093
+ console.warn(`[cms] Skipping glob collection "${collectionName}" — scan failed:`, error);
1094
+ failedScans.set(collectionName, {
1095
+ name: collectionName,
1096
+ reason: 'scan-failed',
1097
+ base: normalizeBasePath(parsedCollection.loaderBase),
1098
+ pattern: parsedCollection.loaderPattern,
1099
+ });
1100
+ continue;
1101
+ }
1102
+ if (!definition)
1103
+ continue;
1104
+ // Nest under the collection that owns the shared base directory (e.g. jsem-otazky -> jsem),
1105
+ // so the CMS browser can group it under its parent page instead of listing it flat.
1106
+ const baseName = parsedCollection.loaderBase.replace(/[/\\]+$/, '').split(/[/\\]/).pop();
1107
+ const parentName = !baseName ? undefined : collections[baseName] ? baseName : rekeyedScannedNames.get(baseName);
1108
+ if (parentName && parentName !== collectionName) {
1109
+ definition.parentCollection = parentName;
1110
+ }
1111
+ collections[collectionName] = definition;
1112
+ }
1113
+ applyParsedConfig(collections, parsed);
1114
+ detectReferenceFields(collections, parsed);
1115
+ detectDerivedHrefFields(collections);
1116
+ for (const def of Object.values(collections))
1117
+ pairImageAltFields(def.fields);
1118
+ assignSemanticRoles(collections);
1119
+ applyCollectionOrderBy(collections, parsed);
1120
+ warnOnPathnameCollisions(collections);
1121
+ // The report has to describe the disk as this scan found it, so it is built here and not
1122
+ // when someone asks for it later; a caller that will not ask does not pay for it. And it
1123
+ // is an extra: failing to build it must not fail a scan that succeeded.
1124
+ if (options.report) {
1125
+ try {
1126
+ const contentDirectories = entries
1127
+ .filter(entry => entry.isDirectory && !entry.name.startsWith('_') && !entry.name.startsWith('.'))
1128
+ .map(entry => entry.name);
1129
+ scanReports.set(collections, await buildScanReport(fs, contentDir, collections, config, failedScans, contentDirectories));
1130
+ }
1131
+ catch (error) {
1132
+ console.warn('[cms] Collection scan report unavailable:', error);
1133
+ }
1134
+ }
1135
+ return collections;
1136
+ }
1137
+ /** The report of each scan, by the record that scan returned — see {@link collectionScanReport}. */
1138
+ const scanReports = new WeakMap();
1139
+ /**
1140
+ * What the scan that returned `collections` could not vouch for.
1141
+ *
1142
+ * Looked up by the record itself instead of being returned next to it: `scanCollections`
1143
+ * keeps its return type for the callers that only want the definitions, and a report cannot
1144
+ * be paired with another scan's collections. `undefined` for a scan that was not asked for a
1145
+ * report (`options.report`), for one whose report could not be built, and for any other
1146
+ * object (a copy, a hand-built record) — read that as "not reported", never as "nothing
1147
+ * unresolved".
1148
+ */
1149
+ export function collectionScanReport(collections) {
1150
+ return scanReports.get(collections);
1151
+ }
1152
+ /**
1153
+ * Patterns the `CmsFileSystem` port does not run the way Astro's glob does. Syntax it reads
1154
+ * literally or wrongly: a character class, an extglob, an escape, a `|` (alternation to
1155
+ * Astro), a `+` behind a brace (a quantifier), a brace without a comma (the name `{a}` to
1156
+ * Astro), and inside a brace another brace, a range (`{x,1..3}`) or a wildcard. And
1157
+ * shapes Astro's glob reads wider than the port: a `..` segment, also one a brace completes
1158
+ * (`..{/drafts,x}`), a trailing slash (also one an empty brace alternative leaves behind), a
1159
+ * negation behind `./`, and a final `/**`, which to Astro also matches a file named by what
1160
+ * stands before it. What such a pattern matches here says nothing about what the loader loads.
1161
+ * A lone leading `!` is not among them: it matches nothing in Astro, and nothing here.
1162
+ */
1163
+ const UNSUPPORTED_GLOB_SYNTAX = /[[\]()\\|]|\}\+|\{[^}]*(?:\{|\.\.|[*?]|,,)|\{,|,\}|\{[^,}]*\}|(?:^|[/{},])\.\.(?:[/{},]|$)|\/$|^(?:\.\/)+!|.\/\*\*$/;
1164
+ /**
1165
+ * A `..` segment in a pattern leads out of the base, so the files under the base do not bound
1166
+ * it. A brace next to the dots may be what makes them a segment.
1167
+ */
1168
+ const LEAVES_THE_BASE = /(?:^|[/{},])\.\.(?:[/{},]|$)/;
1169
+ /**
1170
+ * How many directories behind symbolic links the report reads for one collection before it
1171
+ * gives up on it. Links can lead into a tree of any size, and into each other.
1172
+ */
1173
+ const REPORT_LINKED_DIRECTORY_LIMIT = 20_000;
1174
+ /**
1175
+ * Files that may be entries. Astro's glob loads md, mdx (with the integration), json, yaml,
1176
+ * yml and toml; the other markdown spellings and mdoc are loaded only where an integration
1177
+ * adds them, and are counted anyway: a file too many is a report, a file too few a miss.
1178
+ * A loader's pattern may match more (a cover image next to an entry), and that is not content.
1179
+ */
1180
+ const ENTRY_FILE = /\.(?:md|mdx|mdoc|markdown|mdown|mkdn|mkd|mdwn|json|ya?ml|toml)$/i;
1181
+ /** The markdown half of {@link ENTRY_FILE}: what Astro takes from a content directory nothing declares. */
1182
+ const MARKDOWN_FILE = /\.(?:md|mdx|mdoc|markdown|mdown|mkdn|mkd|mdwn)$/i;
1183
+ /**
1184
+ * Whether Astro reads `base` as the relative directory the scan takes it for. Astro resolves
1185
+ * it as a URL against the project root: an absolute path or a scheme is somewhere else, an
1186
+ * escape, a query or a fragment is not part of the directory name, a backslash becomes a
1187
+ * slash, whitespace at the edges and tabs or line breaks anywhere are dropped, and a `..`
1188
+ * that leaves the root is resolved against it. And the port would read a glob character in
1189
+ * the base as part of the pattern.
1190
+ */
1191
+ function readsAsRelativeDirectory(base) {
1192
+ if (/[*?{}]/.test(base))
1193
+ return false;
1194
+ try {
1195
+ const asUrl = decodeURIComponent(new URL(base, 'file:///project-root/').pathname).replace(/\/{2,}/g, '/').replace(/\/+$/, '');
1196
+ const asPath = normalizeBasePath(base).split(path.sep).join('/');
1197
+ return asUrl === (asPath === '.' ? '/project-root' : `/project-root/${asPath}`);
1198
+ }
1199
+ catch {
1200
+ return false;
1201
+ }
1202
+ }
1203
+ /** Where a declaration says its files are, in the shape the report carries. */
1204
+ function declaredLocation(declaration, contentDir) {
1205
+ const location = {};
1206
+ if (declaration.noLoader)
1207
+ location.base = path.join(contentDir, declaration.name);
1208
+ if (declaration.loaderBase !== undefined)
1209
+ location.base = normalizeBasePath(declaration.loaderBase);
1210
+ if (declaration.loaderPattern !== undefined)
1211
+ location.pattern = declaration.loaderPattern;
1212
+ if (declaration.loaderFile !== undefined)
1213
+ location.file = normalizeBasePath(declaration.loaderFile);
1214
+ return location;
1215
+ }
1216
+ /**
1217
+ * The entry files of a content directory read without a loader, root-relative: all of them at
1218
+ * any depth, `_` and `.` paths aside. This is what Astro loads for a declaration without a
1219
+ * loader and for a content directory nothing declares, under the
1220
+ * `legacy.collectionsBackwardsCompat` flag that nua sets.
1221
+ */
1222
+ async function contentDirectoryFiles(fs, directory, entryFile) {
1223
+ const files = await globLoaderFiles(fs, directory, '**/*', { followSymlinks: true, maxLinkedDirectories: REPORT_LINKED_DIRECTORY_LIMIT });
1224
+ return files.filter(file => entryFile.test(file)).map(file => path.join(directory, file));
1225
+ }
1226
+ /**
1227
+ * The entry files a declared collection loads, root-relative, as far as that can be told
1228
+ * without running its loader. `exact` when the scan can evaluate the loader; otherwise the
1229
+ * files are every entry file the loader could match. `null` when not even that is known.
1230
+ */
1231
+ async function declaredEntryFiles(fs, contentDir, declaration) {
1232
+ if (declaration.noLoader) {
1233
+ // Astro resolves the name as a URL against the content directory, like a base.
1234
+ if (!readsAsRelativeDirectory(declaration.name))
1235
+ return null;
1236
+ return { exact: true, files: await contentDirectoryFiles(fs, path.join(contentDir, declaration.name), ENTRY_FILE) };
1237
+ }
1238
+ // Without a literal base there is nothing to look under, and one Astro reads as something
1239
+ // other than a relative path is no better.
1240
+ const declaredBase = declaration.loaderBase;
1241
+ if (declaredBase === undefined || !readsAsRelativeDirectory(declaredBase))
1242
+ return null;
1243
+ const base = normalizeBasePath(declaredBase);
1244
+ const pattern = declaration.loaderPattern;
1245
+ // A list of patterns is not run here either, but it can be read for where it leads.
1246
+ const patterns = pattern !== undefined ? [pattern] : declaration.loaderPatterns;
1247
+ // A pattern that cannot be read may lead anywhere.
1248
+ if (patterns === undefined)
1249
+ return null;
1250
+ // As spelled and as the port joins it to the base, which drops `.` segments and doubled slashes.
1251
+ const exact = pattern !== undefined && !UNSUPPORTED_GLOB_SYNTAX.test(pattern) && !UNSUPPORTED_GLOB_SYNTAX.test(path.posix.normalize(pattern));
1252
+ // Patterns the scan cannot evaluate select among the files under the base: all of them
1253
+ // are candidates. Unless one leads out of the base, and the whole project is not walked
1254
+ // on that off chance either.
1255
+ if (!exact && (base === '.' || patterns.some(one => LEAVES_THE_BASE.test(one))))
1256
+ return null;
1257
+ const matches = await fs.glob(path.join(base, exact ? pattern : '**/*'), {
1258
+ followSymlinks: true,
1259
+ maxLinkedDirectories: REPORT_LINKED_DIRECTORY_LIMIT,
1260
+ });
1261
+ // Astro's glob leaves dot paths out unless the pattern asks for them. It does load `_`
1262
+ // files, which the scan skips, so those stay in.
1263
+ const files = matches.filter(file => ENTRY_FILE.test(file) && patterns.some(one => mayTakeDotPath(one, path.relative(base, file))));
1264
+ return { exact, files };
1265
+ }
1266
+ /**
1267
+ * Whether a loader pattern can match `file` (relative to the base) as far as leading dots go.
1268
+ * Astro's glob keeps `*` and `**` from matching one, so a dotfile or a file in a dot directory
1269
+ * is loaded only when the pattern asks for it. It does by naming a dot at the start of a
1270
+ * segment or of an alternative. And by a wildcard at the start of an alternative, right
1271
+ * behind a brace or behind a `|`: Astro's glob guards `*` and `?` against a leading dot only
1272
+ * at the start of the pattern and behind a slash, so `{guide/,notes/}*.md` takes
1273
+ * `guide/.wip.md`. In both cases nothing is ruled out here. And by a character class or an
1274
+ * extglob (in patterns the scan does not run), which take a leading dot like any other
1275
+ * character: in the last segment a dotfile, in an earlier one a dot directory. Where a brace
1276
+ * or an extglob spans a slash the segments cannot be told apart, and either takes both.
1277
+ */
1278
+ function mayTakeDotPath(pattern, file) {
1279
+ const segments = file.split(/[/\\]/);
1280
+ const dotFile = segments.pop().startsWith('.');
1281
+ const dotDirectory = segments.some(segment => segment.startsWith('.'));
1282
+ if (!dotFile && !dotDirectory)
1283
+ return true;
1284
+ if (/(?:^|[/{},|!\\])\.[^./]|[{,}|][*?]/.test(pattern))
1285
+ return true;
1286
+ const cut = pattern.lastIndexOf('/');
1287
+ const blurred = /[{(][^})]*\//.test(pattern);
1288
+ const takesDot = (part) => /[[(]/.test(blurred ? pattern : part);
1289
+ return (!dotFile || takesDot(pattern.slice(cut + 1))) && (!dotDirectory || takesDot(pattern.slice(0, Math.max(cut, 0))));
1290
+ }
1291
+ /** Where Astro looks for its own config. It takes the first one there is. */
1292
+ const ASTRO_CONFIG_PATHS = ['astro.config.mjs', 'astro.config.js', 'astro.config.ts', 'astro.config.mts'];
1293
+ /**
1294
+ * The project's source directory when the Astro config moves it from `src`: the directory,
1295
+ * `undefined` for the default, or `null` when where Astro loads content from cannot be told.
1296
+ *
1297
+ * Read off the code of the one config file Astro uses. Two of its options decide the place:
1298
+ * `srcDir`, where the content config and the content directory are, and `root`, which every
1299
+ * loader's `base` is resolved against. Which mention of either name is Astro's option cannot
1300
+ * be told without evaluating the file (it may name another plugin's option, a variable, a
1301
+ * member), so every one of them has to be a property given a plain string: all `srcDir`s the
1302
+ * same directory inside the project, every `root` the project root itself, which the scan
1303
+ * takes it to be. Either name given an object literal is not Astro's option, which is a string.
1304
+ */
1305
+ async function customSrcDir(fs) {
1306
+ for (const configPath of ASTRO_CONFIG_PATHS) {
1307
+ const source = await fs.readFile(configPath).catch(() => null);
1308
+ if (source === null)
1309
+ continue;
1310
+ const roots = configOptionMentions(source, 'root');
1311
+ const srcDirs = configOptionMentions(source, 'srcDir');
1312
+ if (roots === null || srcDirs === null)
1313
+ return null;
1314
+ if (roots.some(root => root === null || normalizeBasePath(root) !== '.'))
1315
+ return null;
1316
+ if (srcDirs.length === 0)
1317
+ return undefined;
1318
+ const [srcDir, ...others] = srcDirs.map(literal => (literal === null ? null : normalizeBasePath(literal)));
1319
+ if (srcDir === null || srcDir === undefined || others.some(other => other !== srcDir))
1320
+ return null;
1321
+ if (srcDir === 'src')
1322
+ return undefined;
1323
+ return path.isAbsolute(srcDir) || srcDir.startsWith('..') ? null : srcDir;
1324
+ }
1325
+ return undefined;
1326
+ }
1327
+ /**
1328
+ * How much of the content config was read, for the report. Astro takes the config from more
1329
+ * file names than the reader looks at, and from another directory when `srcDir` says so, so
1330
+ * which files exist is part of the answer.
1331
+ */
1332
+ async function contentConfigStatus(fs, config) {
1333
+ const configPaths = [...CONTENT_CONFIG_PATHS, ...UNREAD_CONTENT_CONFIG_PATHS];
1334
+ const srcDir = await customSrcDir(fs);
1335
+ // Where Astro loads from cannot be told, so neither can whether a config is there, nor
1336
+ // whether what the scan found under the project root is what Astro loads.
1337
+ if (srcDir === null)
1338
+ return 'unreadable';
1339
+ if (srcDir !== undefined) {
1340
+ // The reader only looks under `src`, so a config found here was not read. Nothing here
1341
+ // settles nothing: the mention may not have been Astro's option, and then `src` is
1342
+ // where the config is, so `src` is judged as usual.
1343
+ for (const configPath of configPaths) {
1344
+ if (await fs.exists(path.join(srcDir, path.relative('src', configPath))))
1345
+ return 'unreadable';
1346
+ }
1347
+ }
1348
+ let configFiles = 0;
1349
+ for (const configPath of configPaths) {
1350
+ if (await fs.exists(configPath))
1351
+ configFiles++;
1352
+ }
1353
+ if (configFiles === 0)
1354
+ return 'missing';
1355
+ // With two config files, the one the reader took need not be the one Astro uses.
1356
+ if (config.partial || configFiles > 1)
1357
+ return 'partial';
1358
+ if (config.declared.length > 0)
1359
+ return 'ok';
1360
+ // Nothing declared: a site without collections, or a config that was not read at all
1361
+ // (it does not parse, or it is one of the names the reader does not look at).
1362
+ return await classifyEmptyContentConfig(fs) === 'no-collections' ? 'ok' : 'unreadable';
1363
+ }
1364
+ /**
1365
+ * Account for every collection the scan knows of and cannot vouch for.
1366
+ *
1367
+ * "Knows of" is every name the content config declares plus every content directory the
1368
+ * walk tried. A declared collection is vouched for when every entry file its loader loads is
1369
+ * an entry of the scan — which makes one that loads no files an empty collection, not an
1370
+ * unresolved one, and lets a site the scan reads whole report nothing. Being in `collections`
1371
+ * is not enough: a content directory of the declared name is read whatever the loader says.
1372
+ */
1373
+ async function buildScanReport(fs, contentDir, collections, config, failedScans, contentDirectories) {
1374
+ const contentConfig = await contentConfigStatus(fs, config);
1375
+ const unresolved = new Map();
1376
+ const declaredByName = new Map(config.declared.map(declaration => [declaration.name, declaration]));
1377
+ /** Every file that is an entry of some collection. */
1378
+ const listed = new Set();
1379
+ for (const definition of Object.values(collections)) {
1380
+ for (const entry of definition.entries ?? [])
1381
+ listed.add(path.normalize(entry.sourcePath));
1382
+ // `entryCount` is the source files found, `entries` the ones that could be read and parsed.
1383
+ if ((definition.entries?.length ?? 0) >= definition.entryCount)
1384
+ continue;
1385
+ const declaration = declaredByName.get(definition.name);
1386
+ unresolved.set(definition.name, {
1387
+ name: definition.name,
1388
+ reason: 'unlisted-entries',
1389
+ base: normalizeBasePath(definition.path),
1390
+ ...(declaration ? declaredLocation(declaration, contentDir) : {}),
1391
+ });
1392
+ }
1393
+ for (const declaration of config.declared) {
1394
+ const { name } = declaration;
1395
+ if (unresolved.has(name))
1396
+ continue;
1397
+ const location = declaredLocation(declaration, contentDir);
1398
+ const failed = collections[name] ? undefined : failedScans.get(name);
1399
+ if (failed) {
1400
+ unresolved.set(name, failed);
1401
+ continue;
1402
+ }
1403
+ let loads;
1404
+ try {
1405
+ loads = await declaredEntryFiles(fs, contentDir, declaration);
1406
+ }
1407
+ catch {
1408
+ // Looking where the loader loads from threw: a base outside the root, a directory that cannot be read.
1409
+ unresolved.set(name, { name, reason: 'scan-failed', ...location });
1410
+ continue;
1411
+ }
1412
+ if (loads === null) {
1413
+ unresolved.set(name, { name, reason: 'unsupported-loader', ...location });
1414
+ }
1415
+ else if (!loads.files.every(file => listed.has(path.normalize(file)))) {
1416
+ // A file the loader is known to load is missing from the entries — or, where the loader
1417
+ // cannot be evaluated, one it may load is.
1418
+ unresolved.set(name, { name, reason: loads.exact ? 'unlisted-entries' : 'unsupported-loader', ...location });
1419
+ }
1420
+ }
1421
+ // A content directory nothing declares. As nua configures Astro
1422
+ // (`legacy.collectionsBackwardsCompat`), each one is a collection of its markdown files at
1423
+ // any depth, unless some declared collection has a loader — then Astro leaves them alone.
1424
+ // Whether the flag is set cannot be read reliably, so it is taken to be: that way a
1425
+ // directory Astro does not load can be reported, and one it does load cannot be missed.
1426
+ // A declaration whose loader is unknown may have none, so only a loader that was seen counts.
1427
+ // A config that does not export `collections` gives Astro nothing to add to.
1428
+ const loadsUndeclared = config.exportsCollections && (contentConfig === 'ok' || contentConfig === 'partial')
1429
+ && !config.declared.some(declaration => declaration.hasLoader);
1430
+ if (loadsUndeclared) {
1431
+ for (const name of contentDirectories) {
1432
+ if (declaredByName.has(name) || unresolved.has(name) || failedScans.has(name))
1433
+ continue;
1434
+ const directory = path.join(contentDir, name);
1435
+ const files = await contentDirectoryFiles(fs, directory, MARKDOWN_FILE).catch(() => null);
1436
+ if (files === null)
1437
+ unresolved.set(name, { name, reason: 'scan-failed', base: directory });
1438
+ else if (!files.every(file => listed.has(path.normalize(file))))
1439
+ unresolved.set(name, { name, reason: 'unlisted-entries', base: directory });
1440
+ }
1441
+ }
1442
+ // What is left are content directories the config does not declare under that name. One a
1443
+ // declared collection loads from under another key (`heroSlides` over `hero-slides`) has
1444
+ // failed there already, and one whose declared loader did read it has not failed at all.
1445
+ for (const failed of failedScans.values()) {
1446
+ if (collections[failed.name] || unresolved.has(failed.name))
1447
+ continue;
1448
+ if ([...unresolved.values()].some(other => other.reason === 'scan-failed' && other.base === failed.base))
1449
+ continue;
1450
+ unresolved.set(failed.name, failed);
1451
+ }
1452
+ return {
1453
+ contentConfig,
1454
+ unresolved: [...unresolved.values()].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)),
1455
+ };
1456
+ }
1457
+ /**
1458
+ * Warn when a collection's declarative `cms.pathname` rule maps two distinct
1459
+ * entries to the same URL — e.g. an all-literal spec, or a spec whose `field`
1460
+ * segments are constant across entries. Such a rule silently collapses entries
1461
+ * onto one page, so surface it at scan time (once per scan) rather than letting
1462
+ * the manifest ship colliding pathnames with no signal. Runs after
1463
+ * `applyParsedConfig`, which is where `def.pathname` gets populated.
1464
+ */
1465
+ function warnOnPathnameCollisions(collections) {
1466
+ for (const def of Object.values(collections)) {
1467
+ if (!def.pathname || !def.entries)
1468
+ continue;
1469
+ const bySlug = new Map();
1470
+ for (const entry of def.entries) {
1471
+ const pathname = resolvePathnameFromSpec(def, entry.data);
1472
+ if (!pathname)
1473
+ continue;
1474
+ const prev = bySlug.get(pathname);
1475
+ if (prev) {
1476
+ console.warn(`[cms] collection "${def.name}": pathname rule maps both "${prev}" and "${entry.slug}" to "${pathname}" — these entries will collide on one URL`);
1477
+ }
1478
+ else {
1479
+ bySlug.set(pathname, entry.slug);
1480
+ }
1481
+ }
1482
+ }
1483
+ }