@conloca/content-api 0.1.15 → 0.2.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 (281) hide show
  1. package/dist/asset-constraints.d.ts +16 -0
  2. package/dist/asset-constraints.d.ts.map +1 -0
  3. package/dist/asset-constraints.js +29 -0
  4. package/dist/asset-constraints.js.map +1 -0
  5. package/dist/asset-metadata-store.d.ts +60 -0
  6. package/dist/asset-metadata-store.d.ts.map +1 -0
  7. package/dist/asset-metadata-store.js +335 -0
  8. package/dist/asset-metadata-store.js.map +1 -0
  9. package/dist/asset-operations.d.ts +164 -0
  10. package/dist/asset-operations.d.ts.map +1 -0
  11. package/dist/asset-operations.js +784 -0
  12. package/dist/asset-operations.js.map +1 -0
  13. package/dist/asset-types.d.ts +72 -0
  14. package/dist/asset-types.d.ts.map +1 -0
  15. package/dist/asset-types.js +13 -0
  16. package/dist/asset-types.js.map +1 -0
  17. package/dist/asset-usage.d.ts +10 -0
  18. package/dist/asset-usage.d.ts.map +1 -0
  19. package/dist/asset-usage.js +49 -0
  20. package/dist/asset-usage.js.map +1 -0
  21. package/dist/base-content-index.d.ts +63 -0
  22. package/dist/base-content-index.d.ts.map +1 -0
  23. package/dist/base-content-index.js +200 -0
  24. package/dist/base-content-index.js.map +1 -0
  25. package/dist/cf-access.d.ts +31 -0
  26. package/dist/cf-access.d.ts.map +1 -0
  27. package/dist/cf-access.js +67 -0
  28. package/dist/cf-access.js.map +1 -0
  29. package/dist/component-registry.d.ts +50 -0
  30. package/dist/component-registry.d.ts.map +1 -0
  31. package/dist/component-registry.js +121 -0
  32. package/dist/component-registry.js.map +1 -0
  33. package/dist/component-registry.types.d.ts +11 -0
  34. package/dist/component-registry.types.d.ts.map +1 -0
  35. package/dist/component-registry.types.js +2 -0
  36. package/dist/component-registry.types.js.map +1 -0
  37. package/dist/component-vocabulary.d.ts +33 -0
  38. package/dist/component-vocabulary.d.ts.map +1 -0
  39. package/dist/component-vocabulary.js +105 -0
  40. package/dist/component-vocabulary.js.map +1 -0
  41. package/dist/content-api.interface.d.ts +64 -0
  42. package/dist/content-api.interface.d.ts.map +1 -0
  43. package/dist/content-api.interface.js +2 -0
  44. package/dist/content-api.interface.js.map +1 -0
  45. package/dist/content-filters.d.ts +31 -0
  46. package/dist/content-filters.d.ts.map +1 -0
  47. package/dist/content-filters.js +247 -0
  48. package/dist/content-filters.js.map +1 -0
  49. package/dist/content-index.d.ts +184 -0
  50. package/dist/content-index.d.ts.map +1 -0
  51. package/dist/content-index.js +546 -0
  52. package/dist/content-index.js.map +1 -0
  53. package/dist/content-operations.d.ts +157 -0
  54. package/dist/content-operations.d.ts.map +1 -0
  55. package/dist/content-operations.js +359 -0
  56. package/dist/content-operations.js.map +1 -0
  57. package/dist/content-path.d.ts +59 -0
  58. package/dist/content-path.d.ts.map +1 -0
  59. package/dist/content-path.js +88 -0
  60. package/dist/content-path.js.map +1 -0
  61. package/dist/content-reader.d.ts +10 -0
  62. package/dist/content-reader.d.ts.map +1 -0
  63. package/dist/content-reader.js +14 -0
  64. package/dist/content-reader.js.map +1 -0
  65. package/dist/content-utils.d.ts +175 -0
  66. package/dist/content-utils.d.ts.map +1 -0
  67. package/dist/content-utils.js +421 -0
  68. package/dist/content-utils.js.map +1 -0
  69. package/dist/content-watcher.d.ts +43 -0
  70. package/dist/content-watcher.d.ts.map +1 -0
  71. package/dist/content-watcher.js +86 -0
  72. package/dist/content-watcher.js.map +1 -0
  73. package/dist/data-index.d.ts +8 -0
  74. package/dist/data-index.d.ts.map +1 -0
  75. package/dist/data-index.js +8 -0
  76. package/dist/data-index.js.map +1 -0
  77. package/dist/data.d.ts +35 -0
  78. package/dist/data.d.ts.map +1 -0
  79. package/dist/data.js +104 -0
  80. package/dist/data.js.map +1 -0
  81. package/dist/editor-data-bindings.d.ts +10 -0
  82. package/dist/editor-data-bindings.d.ts.map +1 -0
  83. package/dist/editor-data-bindings.js +67 -0
  84. package/dist/editor-data-bindings.js.map +1 -0
  85. package/dist/etag-utils.d.ts +77 -0
  86. package/dist/etag-utils.d.ts.map +1 -0
  87. package/dist/etag-utils.js +161 -0
  88. package/dist/etag-utils.js.map +1 -0
  89. package/dist/filesystem-content-api.d.ts +162 -0
  90. package/dist/filesystem-content-api.d.ts.map +1 -0
  91. package/dist/filesystem-content-api.js +2307 -0
  92. package/dist/filesystem-content-api.js.map +1 -0
  93. package/dist/filesystem-reader-backend.d.ts +46 -0
  94. package/dist/filesystem-reader-backend.d.ts.map +1 -0
  95. package/dist/filesystem-reader-backend.js +119 -0
  96. package/dist/filesystem-reader-backend.js.map +1 -0
  97. package/dist/fragment-index.d.ts +8 -0
  98. package/dist/fragment-index.d.ts.map +1 -0
  99. package/dist/fragment-index.js +8 -0
  100. package/dist/fragment-index.js.map +1 -0
  101. package/dist/fragments.d.ts +16 -0
  102. package/dist/fragments.d.ts.map +1 -0
  103. package/dist/fragments.js +104 -0
  104. package/dist/fragments.js.map +1 -0
  105. package/dist/git-operations.d.ts +49 -0
  106. package/dist/git-operations.d.ts.map +1 -0
  107. package/dist/git-operations.js +256 -0
  108. package/dist/git-operations.js.map +1 -0
  109. package/dist/header-parser.d.ts +37 -0
  110. package/dist/header-parser.d.ts.map +1 -0
  111. package/dist/header-parser.js +233 -0
  112. package/dist/header-parser.js.map +1 -0
  113. package/dist/in-memory-content-api.d.ts +35 -0
  114. package/dist/in-memory-content-api.d.ts.map +1 -0
  115. package/dist/in-memory-content-api.js +609 -0
  116. package/dist/in-memory-content-api.js.map +1 -0
  117. package/dist/index.d.ts +26 -0
  118. package/dist/index.d.ts.map +1 -0
  119. package/dist/index.js +28 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/indexing.d.ts +5 -0
  122. package/dist/indexing.d.ts.map +1 -0
  123. package/dist/indexing.js +8 -0
  124. package/dist/indexing.js.map +1 -0
  125. package/dist/json-whitespace-view.d.ts +32 -0
  126. package/dist/json-whitespace-view.d.ts.map +1 -0
  127. package/dist/json-whitespace-view.js +153 -0
  128. package/dist/json-whitespace-view.js.map +1 -0
  129. package/dist/middleware.d.ts +20 -0
  130. package/dist/middleware.d.ts.map +1 -0
  131. package/dist/middleware.js +1082 -0
  132. package/dist/middleware.js.map +1 -0
  133. package/dist/node.d.ts +32 -0
  134. package/dist/node.d.ts.map +1 -0
  135. package/dist/node.js +27 -0
  136. package/dist/node.js.map +1 -0
  137. package/dist/page-references.d.ts +21 -0
  138. package/dist/page-references.d.ts.map +1 -0
  139. package/dist/page-references.js +50 -0
  140. package/dist/page-references.js.map +1 -0
  141. package/dist/read-date.d.ts +3 -0
  142. package/dist/read-date.d.ts.map +1 -0
  143. package/dist/read-date.js +9 -0
  144. package/dist/read-date.js.map +1 -0
  145. package/dist/reader-backend.d.ts +54 -0
  146. package/dist/reader-backend.d.ts.map +1 -0
  147. package/dist/reader-backend.js +2 -0
  148. package/dist/reader-backend.js.map +1 -0
  149. package/dist/reader.d.ts +15 -0
  150. package/dist/reader.d.ts.map +1 -0
  151. package/dist/reader.js +14 -0
  152. package/dist/reader.js.map +1 -0
  153. package/dist/schemas/index.d.ts +102 -0
  154. package/dist/schemas/index.d.ts.map +1 -0
  155. package/dist/schemas/index.js +88 -0
  156. package/dist/schemas/index.js.map +1 -0
  157. package/dist/site-index.d.ts +60 -0
  158. package/dist/site-index.d.ts.map +1 -0
  159. package/dist/site-index.js +291 -0
  160. package/dist/site-index.js.map +1 -0
  161. package/dist/site.d.ts +23 -0
  162. package/dist/site.d.ts.map +1 -0
  163. package/dist/site.js +180 -0
  164. package/dist/site.js.map +1 -0
  165. package/dist/types.d.ts +641 -0
  166. package/dist/types.d.ts.map +1 -0
  167. package/dist/types.js +45 -0
  168. package/dist/types.js.map +1 -0
  169. package/dist/url-safe-base64.d.ts +12 -0
  170. package/dist/url-safe-base64.d.ts.map +1 -0
  171. package/dist/url-safe-base64.js +41 -0
  172. package/dist/url-safe-base64.js.map +1 -0
  173. package/dist/url-validation.d.ts +28 -0
  174. package/dist/url-validation.d.ts.map +1 -0
  175. package/dist/url-validation.js +123 -0
  176. package/dist/url-validation.js.map +1 -0
  177. package/dist/utils/atomic-write.d.ts +6 -0
  178. package/dist/utils/atomic-write.d.ts.map +1 -0
  179. package/dist/utils/atomic-write.js +21 -0
  180. package/dist/utils/atomic-write.js.map +1 -0
  181. package/dist/utils.d.ts +46 -0
  182. package/dist/utils.d.ts.map +1 -0
  183. package/dist/utils.js +71 -0
  184. package/dist/utils.js.map +1 -0
  185. package/dist/vite-plugin.d.ts +12 -0
  186. package/dist/vite-plugin.d.ts.map +1 -0
  187. package/dist/vite-plugin.js +30 -0
  188. package/dist/vite-plugin.js.map +1 -0
  189. package/dist/vxjson-serialize.d.ts +13 -0
  190. package/dist/vxjson-serialize.d.ts.map +1 -0
  191. package/dist/vxjson-serialize.js +53 -0
  192. package/dist/vxjson-serialize.js.map +1 -0
  193. package/dist/vxjson.d.ts +113 -0
  194. package/dist/vxjson.d.ts.map +1 -0
  195. package/dist/vxjson.js +381 -0
  196. package/dist/vxjson.js.map +1 -0
  197. package/dist/xxhash.d.ts +10 -0
  198. package/dist/xxhash.d.ts.map +1 -0
  199. package/dist/xxhash.js +35 -0
  200. package/dist/xxhash.js.map +1 -0
  201. package/package.json +132 -34
  202. package/src/asset-constraints.ts +33 -0
  203. package/src/asset-metadata-store.ts +401 -0
  204. package/src/asset-operations.ts +914 -0
  205. package/src/asset-types.ts +74 -0
  206. package/src/asset-usage.ts +47 -0
  207. package/src/base-content-index.ts +227 -0
  208. package/src/cf-access.ts +85 -0
  209. package/src/component-registry.ts +145 -0
  210. package/src/component-registry.types.ts +12 -0
  211. package/src/component-vocabulary.ts +87 -0
  212. package/src/content-api.interface.ts +94 -0
  213. package/src/content-filters.ts +278 -0
  214. package/src/content-index.ts +642 -0
  215. package/src/content-operations.ts +497 -0
  216. package/src/content-path.ts +113 -0
  217. package/src/content-reader.ts +18 -0
  218. package/src/content-utils.ts +474 -0
  219. package/src/content-watcher.ts +122 -0
  220. package/src/data-index.ts +7 -0
  221. package/src/data.ts +134 -0
  222. package/src/editor-data-bindings.ts +66 -0
  223. package/src/etag-utils.ts +204 -0
  224. package/src/filesystem-content-api.ts +2685 -0
  225. package/src/filesystem-reader-backend.ts +135 -0
  226. package/src/fragment-index.ts +7 -0
  227. package/src/fragments.ts +113 -0
  228. package/src/git-operations.ts +340 -0
  229. package/src/header-parser.ts +272 -0
  230. package/src/in-memory-content-api.ts +760 -0
  231. package/src/index.ts +45 -0
  232. package/src/indexing.ts +8 -0
  233. package/src/json-whitespace-view.ts +155 -0
  234. package/src/middleware.ts +1341 -0
  235. package/src/node.ts +40 -0
  236. package/src/page-references.ts +76 -0
  237. package/src/read-date.ts +7 -0
  238. package/src/reader-backend.ts +54 -0
  239. package/src/reader.ts +15 -0
  240. package/src/schemas/index.ts +113 -0
  241. package/src/site-index.ts +330 -0
  242. package/src/site.ts +198 -0
  243. package/src/types.ts +759 -0
  244. package/src/url-safe-base64.ts +38 -0
  245. package/src/url-validation.ts +121 -0
  246. package/src/utils/atomic-write.ts +20 -0
  247. package/src/utils.ts +84 -0
  248. package/src/vite-plugin.ts +45 -0
  249. package/src/vxjson-serialize.ts +66 -0
  250. package/src/vxjson.ts +442 -0
  251. package/src/xxhash.ts +43 -0
  252. package/dist/component-registry.types-CBGONnYn.d.mts +0 -71
  253. package/dist/component-registry.types-CBGONnYn.d.mts.map +0 -1
  254. package/dist/content-operations-Dv67amz3.d.mts +0 -852
  255. package/dist/content-operations-Dv67amz3.d.mts.map +0 -1
  256. package/dist/data-Db1Iv9T0.mjs +0 -60
  257. package/dist/data-Db1Iv9T0.mjs.map +0 -1
  258. package/dist/filesystem-content-api-C1TM0GLo.d.mts +0 -119
  259. package/dist/filesystem-content-api-C1TM0GLo.d.mts.map +0 -1
  260. package/dist/filesystem-content-api-kxKY5IUw.mjs +0 -2678
  261. package/dist/filesystem-content-api-kxKY5IUw.mjs.map +0 -1
  262. package/dist/index.d.mts +0 -79
  263. package/dist/index.d.mts.map +0 -1
  264. package/dist/index.mjs +0 -47
  265. package/dist/index.mjs.map +0 -1
  266. package/dist/node.d.mts +0 -378
  267. package/dist/node.d.mts.map +0 -1
  268. package/dist/node.mjs +0 -2618
  269. package/dist/node.mjs.map +0 -1
  270. package/dist/reader.d.mts +0 -18
  271. package/dist/reader.d.mts.map +0 -1
  272. package/dist/reader.mjs +0 -22
  273. package/dist/reader.mjs.map +0 -1
  274. package/dist/schemas/index.d.mts +0 -90
  275. package/dist/schemas/index.d.mts.map +0 -1
  276. package/dist/schemas/index.mjs +0 -70
  277. package/dist/schemas/index.mjs.map +0 -1
  278. package/dist/site-vNtJ5jCh.mjs +0 -538
  279. package/dist/site-vNtJ5jCh.mjs.map +0 -1
  280. package/dist/types-DWt912tu.mjs +0 -33
  281. package/dist/types-DWt912tu.mjs.map +0 -1
@@ -0,0 +1,497 @@
1
+ import sortKeys from 'sort-keys';
2
+ import { isContentLocale } from './content-path.js';
3
+ import { generateContentId, isContentPublished, normalizeAndValidatePathname } from './content-utils.js';
4
+ import type { Fragments } from './fragments.js';
5
+ import type { Site } from './site.js';
6
+ import type { ContentMeta, CreateContentInput, LocaleUpdateData, LocaleVersion } from './types.js';
7
+
8
+ /**
9
+ * Shared business logic for content operations
10
+ * Used by both FileSystemContentAPI and InMemoryContentAPI
11
+ */
12
+
13
+ // Validation result types
14
+ export interface ValidationResult {
15
+ valid: boolean;
16
+ error?: string;
17
+ sortedMeta?: ContentMeta;
18
+ size?: number;
19
+ }
20
+
21
+ export interface CreateValidationResult {
22
+ valid: boolean;
23
+ error?: string;
24
+ reason?:
25
+ | 'metadata_too_large'
26
+ | 'pathname_taken'
27
+ | 'pathname_in_redirects'
28
+ | 'invalid_pathname'
29
+ | 'invalid_name'
30
+ | 'name_taken'
31
+ | 'invalid_collection'
32
+ | 'invalid_locale'
33
+ | 'missing_field'
34
+ | 'unknown_site'
35
+ | 'no_sites_configured';
36
+ existingId?: string;
37
+ /** The input field a caller has to change, when the validator knows which one it is. */
38
+ field?: string;
39
+ }
40
+
41
+ /**
42
+ * Validate and prepare metadata for storage
43
+ * Ensures title exists and metadata doesn't exceed 4KB
44
+ */
45
+ export function validateAndPrepareMetadata(meta: Partial<ContentMeta>): ValidationResult {
46
+ // Ensure title is present
47
+ if (!meta.title) {
48
+ return {
49
+ valid: false,
50
+ error: 'Title is required in metadata',
51
+ };
52
+ }
53
+
54
+ const sortedMeta = sortKeys(meta, { deep: true }) as ContentMeta; // We've validated title exists
55
+ const metaJson = JSON.stringify(sortedMeta, null, 2);
56
+ const metaSize = new TextEncoder().encode(metaJson).length;
57
+
58
+ if (metaSize > 4088) {
59
+ // Leave 8 bytes for safety
60
+ return {
61
+ valid: false,
62
+ error: `Metadata exceeds 4KB limit (${metaSize} bytes)`,
63
+ };
64
+ }
65
+
66
+ return {
67
+ valid: true,
68
+ sortedMeta,
69
+ size: metaSize,
70
+ };
71
+ }
72
+
73
+ /**
74
+ * Build pathname history update
75
+ */
76
+ export function buildPathnameHistory(
77
+ oldPathname: string | undefined,
78
+ newPathname: string | undefined,
79
+ isPublished: boolean,
80
+ now: string,
81
+ ): Record<string, string> {
82
+ const history: Record<string, string> = {};
83
+
84
+ if (oldPathname && newPathname && oldPathname !== newPathname && isPublished) {
85
+ history[oldPathname] = now;
86
+ }
87
+
88
+ return history;
89
+ }
90
+
91
+ /**
92
+ * Build a locale manifest
93
+ */
94
+ export function buildLocaleVersion(params: {
95
+ locale: string;
96
+ etag: string;
97
+ created: string;
98
+ modified: string;
99
+ meta: ContentMeta;
100
+ kind: 'fragment' | 'page' | 'data';
101
+ publishAt?: string;
102
+ unpublishAt?: string;
103
+ pathname?: string;
104
+ name?: string;
105
+ previousPathnames?: Record<string, string>;
106
+ }): LocaleVersion {
107
+ const manifest: LocaleVersion = {
108
+ locale: params.locale,
109
+ etag: params.etag,
110
+ created: params.created,
111
+ modified: params.modified,
112
+ meta: params.meta,
113
+ };
114
+
115
+ // Add optional fields only if they exist
116
+ if (params.publishAt) {
117
+ manifest.publishAt = params.publishAt;
118
+ }
119
+ if (params.unpublishAt) {
120
+ manifest.unpublishAt = params.unpublishAt;
121
+ }
122
+ if (params.pathname && params.kind === 'page') {
123
+ manifest.pathname = params.pathname;
124
+ }
125
+ if (params.name && (params.kind === 'fragment' || params.kind === 'data')) {
126
+ manifest.name = params.name;
127
+ }
128
+ if (params.previousPathnames && Object.keys(params.previousPathnames).length > 0) {
129
+ manifest.previousPathnames = params.previousPathnames;
130
+ }
131
+
132
+ return manifest;
133
+ }
134
+
135
+ /**
136
+ * Update publish dates on a locale manifest
137
+ */
138
+ export function updatePublishDates(manifest: LocaleVersion, data: LocaleUpdateData): void {
139
+ // Handle publishAt
140
+ if ('publishAt' in data) {
141
+ if (data.publishAt && typeof data.publishAt === 'string') {
142
+ manifest.publishAt = data.publishAt;
143
+ } else {
144
+ // Explicitly delete the field when value is falsy
145
+ delete manifest.publishAt;
146
+ }
147
+ }
148
+
149
+ // Handle unpublishAt
150
+ if ('unpublishAt' in data) {
151
+ if (data.unpublishAt && typeof data.unpublishAt === 'string') {
152
+ manifest.unpublishAt = data.unpublishAt;
153
+ } else {
154
+ // Explicitly delete the field when value is falsy
155
+ delete manifest.unpublishAt;
156
+ }
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Validate content creation data
162
+ */
163
+ export function validateCreateContent(
164
+ data: CreateContentInput,
165
+ sites: Record<string, Site>,
166
+ fragments: Fragments,
167
+ dataApi?: {
168
+ isDataNameValid: (name: string) => boolean;
169
+ getNameConflict: (collection: string, name: string) => string | null;
170
+ },
171
+ ): CreateValidationResult {
172
+ const misplaced = checkEntryPlace(data.collection, Object.keys(data.locales));
173
+ if (misplaced) return misplaced;
174
+
175
+ // Validate metadata for all locales
176
+ for (const localeData of Object.values(data.locales)) {
177
+ const meta = { ...data.meta, ...localeData.meta };
178
+ const validation = validateAndPrepareMetadata(meta);
179
+ if (!validation.valid) {
180
+ return {
181
+ valid: false,
182
+ error: validation.error,
183
+ reason: 'metadata_too_large',
184
+ };
185
+ }
186
+ }
187
+
188
+ // Validate pathname for pages
189
+ if (data.kind === 'page') {
190
+ // A content root that declares no sites at all cannot take a page, whatever
191
+ // site the caller names. Blaming the `site` field here sends people hunting
192
+ // for a site name when the real cause is the missing config file.
193
+ if (Object.keys(sites).length === 0) {
194
+ return {
195
+ valid: false,
196
+ error: 'No sites are configured. Create content/sites.json, or run `conloca init <directory> <site-name>`.',
197
+ reason: 'no_sites_configured',
198
+ field: 'site',
199
+ };
200
+ }
201
+
202
+ if (!data.site) {
203
+ return {
204
+ valid: false,
205
+ error: 'Site is required for pages',
206
+ reason: 'missing_field',
207
+ field: 'site',
208
+ };
209
+ }
210
+
211
+ // Own-key only: the site name is caller-supplied, so an inherited key like
212
+ // `constructor` used to pass this gate and then break on the non-Site.
213
+ const site = Object.hasOwn(sites, data.site) ? sites[data.site] : undefined;
214
+ if (!site) {
215
+ return {
216
+ valid: false,
217
+ error: `Site ${data.site} not found`,
218
+ reason: 'unknown_site',
219
+ field: 'site',
220
+ };
221
+ }
222
+
223
+ const unsupported = checkPageLocales(data.site, site.locales, Object.keys(data.locales));
224
+ if (unsupported) return unsupported;
225
+
226
+ for (const [locale, localeData] of Object.entries(data.locales)) {
227
+ const pathname = localeData.pathname;
228
+
229
+ // Pathname is required for pages
230
+ if (!pathname) {
231
+ return {
232
+ valid: false,
233
+ error: `Pathname is required for pages (locale: ${locale})`,
234
+ reason: 'missing_field',
235
+ field: 'pathname',
236
+ };
237
+ }
238
+
239
+ // The tidied path is written back to localeData so the rest of the
240
+ // create flow (file writes, indexing) sees the cleaned form.
241
+ const checked = checkPagePathname(site, pathname, locale);
242
+ if (!checked.valid) return checked;
243
+ localeData.pathname = checked.pathname;
244
+
245
+ // Check if pathname is in redirects
246
+ if (site.isPathnameInRedirects(checked.pathname, locale)) {
247
+ return {
248
+ valid: false,
249
+ error: `Pathname ${checked.pathname} is already in use as a redirect`,
250
+ reason: 'pathname_in_redirects',
251
+ field: 'pathname',
252
+ };
253
+ }
254
+ }
255
+ }
256
+
257
+ // A fragment or data entry's name
258
+ if (data.kind === 'fragment') {
259
+ const refused = checkEntryName(
260
+ 'fragment',
261
+ data.collection,
262
+ data.name,
263
+ (name) => fragments.isFragmentNameValid(name),
264
+ (name) => fragments.getNameConflict(data.collection, name),
265
+ );
266
+ if (refused) return refused;
267
+ }
268
+ if (data.kind === 'data' && dataApi) {
269
+ const refused = checkEntryName(
270
+ 'data',
271
+ data.collection,
272
+ data.name,
273
+ (name) => dataApi.isDataNameValid(name),
274
+ (name) => dataApi.getNameConflict(data.collection, name),
275
+ );
276
+ if (refused) return refused;
277
+ }
278
+
279
+ return { valid: true };
280
+ }
281
+
282
+ /** Why a new entry's collection or one of its locales cannot be part of its file path. */
283
+ export interface PlaceRefusal {
284
+ valid: false;
285
+ reason: 'invalid_collection' | 'invalid_locale';
286
+ error: string;
287
+ field: 'collection' | 'locales';
288
+ }
289
+
290
+ /**
291
+ * Why a new entry cannot be stored in `collection` under `locales`, or null when it can. Both are
292
+ * part of the entry's file path, so a collection must be a plain folder name and a locale a
293
+ * language code the file layout reads back: '../x' in either would put the file somewhere else.
294
+ */
295
+ export function checkEntryPlace(collection: string | undefined, locales: string[]): PlaceRefusal | null {
296
+ if (!collection) {
297
+ return { valid: false, reason: 'invalid_collection', error: 'Collection name is required', field: 'collection' };
298
+ }
299
+ if (!/^[a-zA-Z0-9_-]+$/.test(collection)) {
300
+ return {
301
+ valid: false,
302
+ reason: 'invalid_collection',
303
+ error: `Invalid collection name: ${collection}. Collection names must contain only alphanumeric characters, hyphens, and underscores.`,
304
+ field: 'collection',
305
+ };
306
+ }
307
+ const malformed = locales.find((locale) => !isContentLocale(locale));
308
+ if (malformed !== undefined) {
309
+ return {
310
+ valid: false,
311
+ reason: 'invalid_locale',
312
+ error: `Invalid locale: ${malformed}. A locale is a two-letter language code, optionally with a region, like en or en-US.`,
313
+ field: 'locales',
314
+ };
315
+ }
316
+ return null;
317
+ }
318
+
319
+ /**
320
+ * Why a new page of `site` cannot be stored under `locales`, or null when it can: each must be a
321
+ * language the site is set up for (`siteLocales`), in the same capitals. The site's pages are read
322
+ * back only in those languages, and a file in another one lands on a page's file: 'EN' on a macOS
323
+ * or Windows disk is the 'en' file, and in an MDX pages folder 'fr' is read as a path folder.
324
+ */
325
+ export function checkPageLocales(site: string, siteLocales: readonly string[], locales: string[]): PlaceRefusal | null {
326
+ const unsupported = locales.find((locale) => !siteLocales.includes(locale));
327
+ if (unsupported === undefined) return null;
328
+ return {
329
+ valid: false,
330
+ reason: 'invalid_locale',
331
+ error: `Invalid locale: ${unsupported}. Site ${site} is set up for ${siteLocales.join(', ')}.`,
332
+ field: 'locales',
333
+ };
334
+ }
335
+
336
+ /** Why a name cannot be a fragment or data entry's name, and the entry that has it when it is taken. */
337
+ export interface NameRefusal {
338
+ valid: false;
339
+ reason: 'invalid_name' | 'name_taken';
340
+ error: string;
341
+ existingId?: string;
342
+ }
343
+
344
+ /**
345
+ * Why `name` cannot be the name of a `kind` entry in `collection`, or null when it can: it is
346
+ * missing or malformed, or `conflictOf` finds another entry of the collection with it. The name is
347
+ * part of the entry's file path, so 'a/b' or '../x' would put the file somewhere else.
348
+ */
349
+ export function checkEntryName(
350
+ kind: 'fragment' | 'data',
351
+ collection: string,
352
+ name: string | undefined,
353
+ isValid: (name: string) => boolean,
354
+ conflictOf: (name: string) => string | null,
355
+ ): NameRefusal | null {
356
+ const label = kind === 'fragment' ? 'Fragment' : 'Data';
357
+ if (!name) {
358
+ return { valid: false, reason: 'invalid_name', error: `${label} name is required` };
359
+ }
360
+ if (!isValid(name)) {
361
+ return {
362
+ valid: false,
363
+ reason: 'invalid_name',
364
+ error: `Invalid ${kind} name: ${name}. ${label} names must contain only alphanumeric characters, hyphens, and underscores.`,
365
+ };
366
+ }
367
+ const existingId = conflictOf(name);
368
+ if (existingId) {
369
+ return {
370
+ valid: false,
371
+ reason: 'name_taken',
372
+ error: `${label} name ${name} already exists in collection ${collection}`,
373
+ existingId,
374
+ };
375
+ }
376
+ return null;
377
+ }
378
+
379
+ /** Why a path cannot be a page's path, and the page that has it when it is taken. */
380
+ export interface PathnameRefusal {
381
+ valid: false;
382
+ reason: 'invalid_pathname' | 'pathname_taken';
383
+ error: string;
384
+ existingId?: string;
385
+ }
386
+
387
+ /**
388
+ * `pathname` tidied as a page's path in `locale`, or why it cannot be one: it is malformed, or
389
+ * another page has it. The path names the page's file, so '/../x' would write it outside the
390
+ * content folder, and '/about/' is the file of the page at '/about'. `excludeId` is a page whose
391
+ * own path is changing; it never blocks itself. Without a `site`, only the path's form is checked.
392
+ */
393
+ export function checkPagePathname(
394
+ site: Site | undefined,
395
+ pathname: string,
396
+ locale: string,
397
+ excludeId?: string,
398
+ ): { valid: true; pathname: string } | PathnameRefusal {
399
+ const validation = normalizeAndValidatePathname(pathname);
400
+ if (!validation.valid) {
401
+ return {
402
+ valid: false,
403
+ reason: 'invalid_pathname',
404
+ error: `Invalid pathname for locale ${locale}: ${validation.message}`,
405
+ };
406
+ }
407
+ const tidied = validation.value;
408
+ const existingId = site?.getPathnameConflict(tidied, locale, excludeId);
409
+ if (existingId) {
410
+ return { valid: false, reason: 'pathname_taken', error: `Pathname ${tidied} already exists`, existingId };
411
+ }
412
+ return { valid: true, pathname: tidied };
413
+ }
414
+
415
+ /**
416
+ * Why a page cannot have `pathname` in `locale` in a site's MDX pages folder (sites.json
417
+ * `mdxPages`), or null when it can. In that folder a page's language is its first folder and a
418
+ * default-language page has none, so '/de/x' in the default language would be written to
419
+ * <folder>/de/x.mdx, the file of the German page at '/x', and read back as that page. `locales`
420
+ * are the languages the folder reads as a first folder. They match in any capitals: macOS and
421
+ * Windows disks treat <folder>/pt-br/x.mdx and <folder>/pt-BR/x.mdx as one file.
422
+ */
423
+ export function checkMdxPagesPathname(
424
+ pathname: string,
425
+ locale: string,
426
+ folder: { defaultLocale: string; locales: Iterable<string> },
427
+ ): PathnameRefusal | null {
428
+ if (locale !== folder.defaultLocale) return null;
429
+ const [first, ...rest] = pathname.slice(1).split('/');
430
+ if (rest.length === 0) return null;
431
+ const kept = [...folder.locales].find((folderLocale) => folderLocale.toLowerCase() === first.toLowerCase());
432
+ if (kept === undefined) return null;
433
+ return {
434
+ valid: false,
435
+ reason: 'invalid_pathname',
436
+ error: `Invalid pathname for locale ${locale}: Path cannot start with \`/${first}/\`: the MDX pages folder keeps the ${kept} pages there`,
437
+ };
438
+ }
439
+
440
+ /**
441
+ * Generate a new content ID
442
+ */
443
+ /**
444
+ * Check if content is published
445
+ */
446
+ export { generateContentId, isContentPublished };
447
+
448
+ /**
449
+ * Update scope bit flags
450
+ * Can be combined with bitwise OR and checked with bitwise AND
451
+ */
452
+ export const UPDATE_SCOPE = {
453
+ NONE: 0, // 00
454
+ META: 1 << 0, // 01
455
+ CONTENT: 1 << 1, // 10
456
+ BOTH: 3, // 11 (META | CONTENT)
457
+ } as const;
458
+
459
+ export type UpdateScope = (typeof UPDATE_SCOPE)[keyof typeof UPDATE_SCOPE];
460
+
461
+ /**
462
+ * Determine what parts of content are being updated
463
+ * Returns bit flags that can be checked with bitwise AND
464
+ *
465
+ * Example usage:
466
+ * ```
467
+ * const scope = analyzeUpdateScope(data);
468
+ * if (scope & UPDATE_SCOPE.META) { // metadata is being updated }
469
+ * if (scope & UPDATE_SCOPE.CONTENT) { // content is being updated }
470
+ * if (scope === UPDATE_SCOPE.BOTH) { // both are being updated }
471
+ * ```
472
+ */
473
+ export function analyzeUpdateScope(data: LocaleUpdateData): UpdateScope {
474
+ let scope = UPDATE_SCOPE.NONE;
475
+
476
+ // Note: pathname is NOT considered a metadata change because:
477
+ // - For pages, pathname is derived from file path, not stored in metadata
478
+ // - Pathname changes don't affect the metadata hash in the ETag
479
+ if (data.meta !== undefined || data.publishAt !== undefined || data.unpublishAt !== undefined) {
480
+ scope |= UPDATE_SCOPE.META;
481
+ }
482
+
483
+ if (data.content !== undefined) {
484
+ scope |= UPDATE_SCOPE.CONTENT;
485
+ }
486
+
487
+ return scope;
488
+ }
489
+
490
+ /**
491
+ * Determine whether the modified timestamp should be updated
492
+ * Updates for ANY change - either metadata or content
493
+ */
494
+ export function shouldUpdateModifiedTimestamp(data: LocaleUpdateData): boolean {
495
+ const scope = analyzeUpdateScope(data);
496
+ return scope !== UPDATE_SCOPE.NONE;
497
+ }
@@ -0,0 +1,113 @@
1
+ /**
2
+ * The ONE locale/path grammar for content files.
3
+ *
4
+ * Every reader that turns a storage path back into content identity parses it
5
+ * here: the pure header parser, the filesystem indexer, and any other reader
6
+ * of content files. Each of those used to carry its own copy of the
7
+ * regex and the copies disagreed — a copy that refused `about.EN.mdx`
8
+ * made content the index had accepted unreadable, and an indexer copy
9
+ * matching `\w+` filed `about.en-US.mdx` under a fallback locale with no
10
+ * pathname at all.
11
+ *
12
+ * Zero imports on purpose: some consumers embed this module where nothing
13
+ * effectful may run at import time.
14
+ */
15
+
16
+ /**
17
+ * A two-letter language subtag, optionally followed by a two-letter upper-case
18
+ * region (`en`, `nl`, `en-US`). Case is PRESERVED, never normalized: writers
19
+ * splice this exact string back into the filename (`getFilePath`, and any other
20
+ * writer of content files), so lower-casing it here would make a repo holding
21
+ * `about.EN.mdx` save its next edit to `about.en.mdx` and orphan the original.
22
+ *
23
+ * Wider BCP-47 forms (three-letter languages, script subtags like `zh-Hans`,
24
+ * lower-case regions) are deliberately NOT accepted: no spec text defines the
25
+ * grammar, and every writer in the system emits this shape today.
26
+ */
27
+ const LOCALE_SUBTAG = '[a-zA-Z]{2}(?:-[A-Z]{2})?';
28
+
29
+ const LOCALE = new RegExp(`^${LOCALE_SUBTAG}$`);
30
+
31
+ /**
32
+ * Whether `locale` is one this grammar reads back. A writer checks a new entry's locale with it:
33
+ * the locale is spliced into the file path, so anything else is either unreadable or, like
34
+ * '../x', a path out of the entry's folder.
35
+ */
36
+ export function isContentLocale(locale: string): boolean {
37
+ return LOCALE.test(locale);
38
+ }
39
+
40
+ /** `<base>.<locale>.<ext>` — the filename half of every content layout. */
41
+ const LOCALE_FILENAME = new RegExp(`^(.+)\\.(${LOCALE_SUBTAG})\\.(mdx|vxjson|json)$`);
42
+
43
+ export type ContentFileExtension = 'mdx' | 'vxjson' | 'json';
44
+
45
+ export interface LocaleFilename {
46
+ /** Filename with the `.<locale>.<ext>` suffix removed. */
47
+ base: string;
48
+ /** Locale exactly as written in the filename. */
49
+ locale: string;
50
+ extension: ContentFileExtension;
51
+ }
52
+
53
+ /** Split `<base>.<locale>.<ext>`. Null when the filename carries no locale suffix. */
54
+ export function parseLocaleFilename(filename: string): LocaleFilename | null {
55
+ const match = LOCALE_FILENAME.exec(filename);
56
+ if (!match) return null;
57
+ return { base: match[1], locale: match[2], extension: match[3] as ContentFileExtension };
58
+ }
59
+
60
+ export interface ContentPathLayout {
61
+ kind: 'fragment' | 'page' | 'data';
62
+ site?: string;
63
+ collection: string;
64
+ locale: string;
65
+ pathname?: string;
66
+ name?: string;
67
+ }
68
+
69
+ /**
70
+ * Pure path → identity mapper. Returns null when the path doesn't match any
71
+ * supported layout or lacks an explicit locale suffix.
72
+ *
73
+ * Layouts honoured here mirror filesystem-content-api.ts:137-144:
74
+ *
75
+ * - `fragments/<collection>/<name>.<locale>.{mdx,vxjson}` (collection required;
76
+ * `fragments/<name>.<locale>.ext` is a legacy form that FileSystemContentAPI
77
+ * repairs by renaming. The pure parser refuses it.)
78
+ * - `data/<collection>/<name>.<locale>.json`
79
+ * - `<site>/<collection>/<path...>/<file>.<locale>.{vxjson,mdx}` (pages; `index`
80
+ * basename maps to the parent path).
81
+ *
82
+ * Reads accept .mdx pages even though the filesystem writer emits main-layout
83
+ * pages as .vxjson only (its mdx pages live under the separate mdxPagesRoot):
84
+ * another writer may derive the extension from the content type, so an
85
+ * mdx-typed page can land here as <site>/pages/<f>.<l>.mdx and must re-parse —
86
+ * dropping it made written pages unreadable.
87
+ */
88
+ export function parseContentPathLayout(path: string): ContentPathLayout | null {
89
+ const parts = path.split('/').filter(Boolean);
90
+ if (parts.length < 3) return null;
91
+
92
+ const suffix = parseLocaleFilename(parts[parts.length - 1]);
93
+ if (!suffix) return null;
94
+
95
+ if (parts[0] === 'fragments') {
96
+ if (parts.length !== 3 || suffix.extension === 'json') return null;
97
+ return { kind: 'fragment', collection: parts[1], name: suffix.base, locale: suffix.locale };
98
+ }
99
+
100
+ if (parts[0] === 'data') {
101
+ if (parts.length !== 3 || suffix.extension !== 'json') return null;
102
+ return { kind: 'data', collection: parts[1], name: suffix.base, locale: suffix.locale };
103
+ }
104
+
105
+ if (suffix.extension === 'json') return null;
106
+
107
+ const pathParts = parts.slice(2);
108
+ pathParts[pathParts.length - 1] = suffix.base;
109
+ const pathname =
110
+ pathParts[pathParts.length - 1] === 'index' ? `/${pathParts.slice(0, -1).join('/')}` : `/${pathParts.join('/')}`;
111
+
112
+ return { kind: 'page', site: parts[0], collection: parts[1], locale: suffix.locale, pathname };
113
+ }
@@ -0,0 +1,18 @@
1
+ import type { ContentAPIOptions } from './types.js';
2
+
3
+ export type ContentReaderOptions = ContentAPIOptions;
4
+
5
+ /**
6
+ * Creates a read-oriented content API instance for build-time consumers.
7
+ *
8
+ * The filesystem implementation is loaded lazily so build tooling does not
9
+ * eagerly traverse native hashing dependencies through the reader entrypoint.
10
+ */
11
+ export async function createContentAPI(options: ContentReaderOptions) {
12
+ const { FileSystemContentAPI: ReaderAPI } = await import('./filesystem-content-api.js');
13
+
14
+ return ReaderAPI.create({
15
+ ...options,
16
+ canvasDir: options.canvasDir || './canvas',
17
+ });
18
+ }