@unbase-app/pages 0.4.0 → 0.5.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.
- package/README.md +8 -4
- package/astro.d.mts +7 -0
- package/astro.mjs +3 -2
- package/binding.mjs +1777 -0
- package/core.mjs +1610 -217
- package/package.json +3 -2
- package/pages-address.mjs +162 -0
package/binding.mjs
ADDED
|
@@ -0,0 +1,1777 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// @unbase-app/pages — binding (docs/plan-cms-release.md R3c, ADR-0030 points 4, 8 and 9)
|
|
3
|
+
//
|
|
4
|
+
// The binding contract's checks: that a site's code reads its collections,
|
|
5
|
+
// copies none of their values, shows them bound to their item and field,
|
|
6
|
+
// shows a new item, and keeps design and content apart. One implementation,
|
|
7
|
+
// with many callers and none required: unbase-projects (the CMS's state and
|
|
8
|
+
// the schema check), the runner (Build's gate), the preview, a site's own
|
|
9
|
+
// `unbase doctor`, and the suite. Exact copy in
|
|
10
|
+
// supabase/functions/_shared/binding.mjs; a test fails when they differ.
|
|
11
|
+
//
|
|
12
|
+
// Pure: files, collections and rendered HTML in, findings out. No file
|
|
13
|
+
// system, no network, no clock; `newItemShows` calls only the render it is
|
|
14
|
+
// given. It knows no framework: an adapter (ASTRO and NEXT below) gives its
|
|
15
|
+
// reader shapes, its marker format and where its content lives.
|
|
16
|
+
//
|
|
17
|
+
// It never guesses a binding. Where no marker says which field an element
|
|
18
|
+
// shows, or more than one could, the answer is plan-editable's reason
|
|
19
|
+
// (no-identity, computed, dynamic, cms, ambiguous, unproven, unsupported),
|
|
20
|
+
// never a pick.
|
|
21
|
+
//
|
|
22
|
+
// reads which files read which collection (the source graph's `reads` edges)
|
|
23
|
+
// readerFunctions the site's own reading functions (`getPage(name)`)
|
|
24
|
+
// readersOf the pages that show a collection, through the import graph
|
|
25
|
+
// noCopiedContent an item's value written as a literal into changed code
|
|
26
|
+
// contentKeptCode a content publish holds no code file
|
|
27
|
+
// designKeptContent a design change changed no content file, and lost or moved no binding
|
|
28
|
+
// binds each element that shows a collection's value → collection, item, field; or its reason
|
|
29
|
+
// newItemShows a test draft laid over a render: does it show on every page that reads the collection
|
|
30
|
+
// tagItem, itemsIn in a preview render only: each item's values carry their item invisibly, and the
|
|
31
|
+
// render's elements are bound to item and field from that and the code (R3e)
|
|
32
|
+
// ============================================================================
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @typedef {{ path: string, text: string }} SiteFile
|
|
36
|
+
* @typedef {{ id: string, file?: string, data: unknown }} Item
|
|
37
|
+
* @typedef {{ key: string, pointer?: string, type?: string, options?: string[], label?: string, blocks?: { key?: string, types?: (string | { value: string, fields?: FieldInput[] })[] }, fields?: FieldInput[] }} FieldInput
|
|
38
|
+
* `blocks`: a blocks field's discriminator and its types; `fields`: a group's or a list's own fields.
|
|
39
|
+
* @typedef {{ name: string, items?: Item[], fields?: FieldInput[], order?: string | null, fixed?: (string | number)[] }} CollectionInput
|
|
40
|
+
* `order`: the field that keeps manual order (`x-cms.order`); `fixed`: the values its schema fixes (`schemaValues`).
|
|
41
|
+
*
|
|
42
|
+
* How code reads a collection, as data an adapter declares:
|
|
43
|
+
* @typedef {{ kind: "call", names: string[], open: string }} CallShape
|
|
44
|
+
* A call or type that names the collection first: `getCollection("blog")`, `CollectionEntry<"blog">`.
|
|
45
|
+
* @typedef {{ kind: "folder", root: string }} FolderShape
|
|
46
|
+
* Code that reads the collection's folder: `join(process.cwd(), "src/content", "blog")`, `"src/content/blog/…"`.
|
|
47
|
+
* @typedef {{ kind: "helper", in: RegExp, not?: RegExp, imports: RegExp, calls: string[], entry: string }} HelperShape
|
|
48
|
+
* The site's own exported reading functions: in files `in` that import `imports`, a function whose body
|
|
49
|
+
* calls one of `calls` with a literal or one of its parameters; `entry` is the call that takes an entry second.
|
|
50
|
+
* @typedef {CallShape | FolderShape | HelperShape} ReaderShape
|
|
51
|
+
*
|
|
52
|
+
* Where an element's binding is written in rendered HTML (plan-editable layers 1 and 2):
|
|
53
|
+
* @typedef {{ field: string[], reason: string, identity: string[] }} MarkerFormat
|
|
54
|
+
*
|
|
55
|
+
* The known transformations a value may go through between its field and the page, as data
|
|
56
|
+
* (plan-editable verification; R3e): a number formatted by a call the code makes with literal
|
|
57
|
+
* arguments (`intl`: `new Intl.NumberFormat("nl-NL", {…})`, `.toLocaleString("nl-NL", {…})`;
|
|
58
|
+
* `fixed`: `.toFixed(2)`), and an image's address as the framework's image service writes it
|
|
59
|
+
* (`query`: the stored path in a parameter, `/_next/image?url=…`; `hashed`: a copy in a folder with
|
|
60
|
+
* a hash in its name, `/_astro/bike.Ab12Cd.webp`).
|
|
61
|
+
* @typedef {"intl" | "fixed"} FormatKind
|
|
62
|
+
* @typedef {{ query: string } | { hashed: string }} ImageTransform
|
|
63
|
+
*
|
|
64
|
+
* @typedef {{
|
|
65
|
+
* id: string,
|
|
66
|
+
* readers: ReaderShape[],
|
|
67
|
+
* markers: MarkerFormat | null,
|
|
68
|
+
* content: string[],
|
|
69
|
+
* assets: string[],
|
|
70
|
+
* formats?: FormatKind[],
|
|
71
|
+
* images?: ImageTransform[],
|
|
72
|
+
* }} BindingAdapter
|
|
73
|
+
*
|
|
74
|
+
* @typedef {{ call: string, from: string, collection: { name: string } | { arg: number }, entry?: { arg: number } }} ReaderFunction
|
|
75
|
+
* @typedef {{ file: string, collection: string, via: "call" | "folder" | "helper", call?: string, entry?: string }} Read
|
|
76
|
+
* @typedef {{ collection: string, item: string, field: string }} Binding
|
|
77
|
+
* @typedef {"no-identity" | "computed" | "dynamic" | "cms" | "ambiguous" | "unproven" | "unsupported"} Reason
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/** plan-editable's reasons, in its order. */
|
|
81
|
+
export const REASONS = ["no-identity", "computed", "dynamic", "cms", "ambiguous", "unproven", "unsupported"];
|
|
82
|
+
|
|
83
|
+
// ── The adapters' part: shapes and formats, no code ───────────────────────
|
|
84
|
+
|
|
85
|
+
/** The marker format of Unbase's Astro hook and the runner's fields answer (proxy/fields.ts). */
|
|
86
|
+
const UNBASE_MARKERS = {
|
|
87
|
+
/** `file#pointer` of the field the element shows: the runner's answer as the bridge applies it. */
|
|
88
|
+
field: ["data-unbase-field", "data-cm-field", "data-cm-veld"],
|
|
89
|
+
reason: "data-unbase-reason",
|
|
90
|
+
/** Layer 1: where the element is written, and the chain of instances it sits in. */
|
|
91
|
+
identity: ["data-unbase-src", "data-unbase-of"],
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Astro: content collections read through `astro:content` (and its types), the site's own reading
|
|
96
|
+
* functions in src/lib and src/utils, and markers from the session's hook.
|
|
97
|
+
* @type {BindingAdapter}
|
|
98
|
+
*/
|
|
99
|
+
export const ASTRO = {
|
|
100
|
+
id: "astro",
|
|
101
|
+
readers: [
|
|
102
|
+
{ kind: "call", names: ["getCollection", "getEntry", "getEntries", "getEntryBySlug", "getDataEntryById", "reference", "CollectionEntry"], open: "(<" },
|
|
103
|
+
{ kind: "helper", in: /^src\/(?:lib|utils)\/.*\.(?:ts|mts|js|mjs)$/, not: /\.test\./, imports: /from\s+["']astro:content["']/, calls: ["getCollection", "getEntry"], entry: "getEntry" },
|
|
104
|
+
],
|
|
105
|
+
markers: UNBASE_MARKERS,
|
|
106
|
+
content: ["src/content/"],
|
|
107
|
+
assets: ["src/assets/images/"],
|
|
108
|
+
formats: ["intl", "fixed"],
|
|
109
|
+
// Astro's image service: `/_image?href=…` on the dev server, a hashed copy in /_astro/ in a build.
|
|
110
|
+
images: [{ query: "href" }, { hashed: "/_astro/" }],
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Next.js: collections kept by convention (src/content/<name>/, _shared/cms/registry.ts), read from
|
|
115
|
+
* their folder at build time as the CMS's generated pages do. No markers in JSX yet
|
|
116
|
+
* (plan-onboarding O5/O6), so no element is bound by a marker: `binds` says `no-identity`.
|
|
117
|
+
* @type {BindingAdapter}
|
|
118
|
+
*/
|
|
119
|
+
export const NEXT = {
|
|
120
|
+
id: "next",
|
|
121
|
+
readers: [{ kind: "folder", root: "src/content" }],
|
|
122
|
+
markers: null,
|
|
123
|
+
content: ["src/content/"],
|
|
124
|
+
assets: ["public/images/"],
|
|
125
|
+
formats: ["intl", "fixed"],
|
|
126
|
+
// next/image: `/_next/image?url=%2Fimages%2Fbike.jpg&w=640&q=75`.
|
|
127
|
+
images: [{ query: "url" }],
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const ADAPTERS = { astro: ASTRO, next: NEXT };
|
|
131
|
+
|
|
132
|
+
// ── Small readers of code ─────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
/** Files whose code can read content. */
|
|
135
|
+
const CODE = /\.(astro|tsx?|jsx?|mts|mjs|vue|svelte|mdx?)$/;
|
|
136
|
+
/** Where collections are defined: naming one there is not reading it. */
|
|
137
|
+
const CONFIG = /content\.config\./;
|
|
138
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\-]/g, "\\$&");
|
|
139
|
+
/** For a pattern with the `u` flag, where `\-` outside a class is an error. */
|
|
140
|
+
const escapeText = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
141
|
+
|
|
142
|
+
/** Comments out, string contents kept (a path in a string is what is looked for). */
|
|
143
|
+
const stripComments = (code) => code.replace(/\/\*[\s\S]*?\*\//g, "").replace(/(^|[^:"'`\\])\/\/.*$/gm, "$1");
|
|
144
|
+
|
|
145
|
+
/** The text between the bracket at `open` and its partner, strings and templates skipped. */
|
|
146
|
+
function balanced(code, open) {
|
|
147
|
+
const pairs = { "(": ")", "{": "}", "[": "]" };
|
|
148
|
+
const stack = [];
|
|
149
|
+
for (let i = open; i < code.length; i++) {
|
|
150
|
+
const c = code[i];
|
|
151
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
152
|
+
const q = c;
|
|
153
|
+
for (i++; i < code.length && code[i] !== q; i++) if (code[i] === "\\") i++;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (pairs[c]) stack.push(pairs[c]);
|
|
157
|
+
else if (c === stack.at(-1)) {
|
|
158
|
+
stack.pop();
|
|
159
|
+
if (!stack.length) return code.slice(open + 1, i);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** A string literal's value, a template literal's with `${name}` kept; null for anything else. */
|
|
166
|
+
const literal = (s) => {
|
|
167
|
+
const t = s.trim();
|
|
168
|
+
const m = t.match(/^(["'`])([\s\S]*)\1$/);
|
|
169
|
+
return m && (m[1] === "`" || !m[2].includes(m[1])) ? m[2] : null;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/** A literal that names one thing: no `${…}` in it. */
|
|
173
|
+
const fixed = (s) => {
|
|
174
|
+
const v = literal(s);
|
|
175
|
+
return v !== null && !v.includes("${") ? v : null;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** A call's arguments at top level, from the text between its brackets. */
|
|
179
|
+
function argumentsOf(inner) {
|
|
180
|
+
const out = [];
|
|
181
|
+
let depth = 0;
|
|
182
|
+
let part = "";
|
|
183
|
+
for (let i = 0; i < inner.length; i++) {
|
|
184
|
+
const c = inner[i];
|
|
185
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
186
|
+
let j = i + 1;
|
|
187
|
+
for (; j < inner.length && inner[j] !== c; j++) if (inner[j] === "\\") j++;
|
|
188
|
+
part += inner.slice(i, j + 1);
|
|
189
|
+
i = j;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if ("([{".includes(c)) depth++;
|
|
193
|
+
if (")]}".includes(c)) depth--;
|
|
194
|
+
if (c === "," && depth === 0) {
|
|
195
|
+
out.push(part);
|
|
196
|
+
part = "";
|
|
197
|
+
} else part += c;
|
|
198
|
+
}
|
|
199
|
+
if (part.trim()) out.push(part);
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const shapes = (adapter) => (Array.isArray(adapter) ? adapter : [adapter]).flatMap((a) => a?.readers ?? []);
|
|
204
|
+
const helperShapes = (adapter) => shapes(adapter).filter((s) => s.kind === "helper");
|
|
205
|
+
|
|
206
|
+
// ── Reads ─────────────────────────────────────────────────────────────────
|
|
207
|
+
|
|
208
|
+
/** The paths a helper shape reads functions from, relative to `root`. */
|
|
209
|
+
export function readerFunctionFiles(adapter, paths, root = "") {
|
|
210
|
+
const helpers = helperShapes(adapter);
|
|
211
|
+
return paths.filter((p) => p.startsWith(root) && helpers.some((h) => h.in.test(p.slice(root.length)) && !h.not?.test(p.slice(root.length))));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The site's own reading functions (the manifest's `reads`, plan-editable F0): an exported function
|
|
216
|
+
* whose body calls the framework's reader with a fixed collection or one of its parameters, in a
|
|
217
|
+
* file the adapter's helper shape names. `files` are the app's (paths with `root` before them).
|
|
218
|
+
* @param {{ files: SiteFile[], adapter: BindingAdapter | BindingAdapter[], root?: string }} input
|
|
219
|
+
* @returns {ReaderFunction[]}
|
|
220
|
+
*/
|
|
221
|
+
export function readerFunctions({ files, adapter, root = "" }) {
|
|
222
|
+
/** @type {ReaderFunction[]} */
|
|
223
|
+
const out = [];
|
|
224
|
+
for (const shape of helperShapes(adapter)) {
|
|
225
|
+
const calls = new RegExp(`\\b(${shape.calls.map(escape).join("|")})\\s*\\(\\s*([^,)]+)\\s*(?:,\\s*([^,)]+))?`);
|
|
226
|
+
for (const { path, text } of files) {
|
|
227
|
+
if (!path.startsWith(root)) continue;
|
|
228
|
+
const rel = path.slice(root.length);
|
|
229
|
+
if (!shape.in.test(rel) || shape.not?.test(rel)) continue;
|
|
230
|
+
if (!text || !shape.imports.test(text)) continue;
|
|
231
|
+
const code = stripComments(text);
|
|
232
|
+
for (const m of code.matchAll(/export\s+(?:async\s+)?function\s+(\w+)\s*(?:<[^>]*>)?\s*\(([^)]*)\)/g)) {
|
|
233
|
+
const params = m[2].split(",").map((p) => p.trim().split(/[\s:=?]/)[0]).filter(Boolean);
|
|
234
|
+
const open = code.indexOf("{", m.index + m[0].length);
|
|
235
|
+
const body = open >= 0 ? balanced(code, open) : null;
|
|
236
|
+
if (!body) continue;
|
|
237
|
+
const call = body.match(calls);
|
|
238
|
+
if (!call) continue;
|
|
239
|
+
const which = (arg) => {
|
|
240
|
+
const lit = literal(arg);
|
|
241
|
+
if (lit !== null) return { name: lit };
|
|
242
|
+
const i = params.indexOf(arg.trim());
|
|
243
|
+
return i >= 0 ? { arg: i } : null;
|
|
244
|
+
};
|
|
245
|
+
const collection = which(call[2]);
|
|
246
|
+
if (!collection) continue;
|
|
247
|
+
const entryArg = call[1] === shape.entry && call[3] ? params.indexOf(call[3].trim()) : -1;
|
|
248
|
+
out.push({ call: m[1], from: path, collection, ...(entryArg >= 0 ? { entry: { arg: entryArg } } : {}) });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return out;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** A shape's matches in a file: the collection each names. */
|
|
256
|
+
function directReads(shape, text) {
|
|
257
|
+
if (shape.kind === "call") {
|
|
258
|
+
const re = new RegExp(`(?:${shape.names.map(escape).join("|")})\\s*[${escape(shape.open)}]\\s*["'\`]([^"'\`\\n]+)["'\`]`, "g");
|
|
259
|
+
return Array.from(text.matchAll(re), (m) => m[1]);
|
|
260
|
+
}
|
|
261
|
+
if (shape.kind === "folder") {
|
|
262
|
+
const root = escape(shape.root);
|
|
263
|
+
const re = new RegExp(`["'\`]${root}["'\`]\\s*,\\s*["'\`]([^"'\`\\n]+)["'\`]|["'\`]${root}/([^"'\`/\\n]+)["'\`/]`, "g");
|
|
264
|
+
return Array.from(text.matchAll(re), (m) => m[1] ?? m[2]);
|
|
265
|
+
}
|
|
266
|
+
return [];
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The regular expression of the adapter's direct shapes for one collection: what `reads` finds for
|
|
271
|
+
* it, for a caller that tests one file at a time (the CMS's page list).
|
|
272
|
+
*/
|
|
273
|
+
export function readsPattern(adapter, collection) {
|
|
274
|
+
const parts = [];
|
|
275
|
+
for (const s of shapes(adapter)) {
|
|
276
|
+
const name = escape(collection);
|
|
277
|
+
if (s.kind === "call") parts.push(`(?:${s.names.map(escape).join("|")})\\s*[${escape(s.open)}]\\s*["'\`]${name}["'\`]`);
|
|
278
|
+
if (s.kind === "folder") parts.push(`["'\`]${escape(s.root)}["'\`]\\s*,\\s*["'\`]${name}["'\`]|["'\`]${escape(s.root)}/${name}["'\`/]`);
|
|
279
|
+
}
|
|
280
|
+
return new RegExp(parts.length ? parts.join("|") : "(?!)");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** A name a collection can have: a word with hyphens; not a template, a glob or a dot folder (`.schemas`). */
|
|
284
|
+
const collectionName = (n) => /^[A-Za-z0-9_][\w-]*$/.test(n ?? "");
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Which files read which collection: the source graph's `reads` edges (ADR-0015). A file reads a
|
|
288
|
+
* collection when it calls the framework's reader with its name (`via: "call"`), reads its folder
|
|
289
|
+
* (`via: "folder"`), or calls one of the site's reading functions with it (`via: "helper"`, with the
|
|
290
|
+
* function's `call` and, when it reads one entry, the `entry`). Where a call's argument is not a
|
|
291
|
+
* literal the file reads something nobody can name from the code: no edge, never a guess.
|
|
292
|
+
* Definitions (`content.config.*`) are not reads. `collections`, when given, keeps only those.
|
|
293
|
+
* @param {{ files: SiteFile[], adapter: BindingAdapter | BindingAdapter[], collections?: string[], functions?: ReaderFunction[] }} input
|
|
294
|
+
* @returns {Read[]}
|
|
295
|
+
*/
|
|
296
|
+
export function reads({ files, adapter, collections, functions }) {
|
|
297
|
+
const keep = collections ? new Set(collections) : null;
|
|
298
|
+
const helpers = functions ?? readerFunctions({ files, adapter });
|
|
299
|
+
/** @type {Map<string, Read>} */
|
|
300
|
+
const out = new Map();
|
|
301
|
+
const add = (r) => {
|
|
302
|
+
if (!collectionName(r.collection) || (keep && !keep.has(r.collection))) return;
|
|
303
|
+
out.set(JSON.stringify([r.file, r.collection, r.via, r.call ?? "", r.entry ?? ""]), r);
|
|
304
|
+
};
|
|
305
|
+
const callOf = helpers.length ? new RegExp(`(?<![\\w$.])(${[...new Set(helpers.map((h) => h.call))].map(escape).join("|")})\\s*\\(`, "g") : null;
|
|
306
|
+
for (const { path, text } of files) {
|
|
307
|
+
if (!CODE.test(path) || CONFIG.test(path) || !text) continue;
|
|
308
|
+
for (const s of shapes(adapter)) for (const name of directReads(s, text)) add({ file: path, collection: name, via: s.kind });
|
|
309
|
+
if (!callOf) continue;
|
|
310
|
+
const code = stripComments(text);
|
|
311
|
+
for (const m of code.matchAll(callOf)) {
|
|
312
|
+
// Its definition is not a call of it.
|
|
313
|
+
if (/(?:function\s*\*?\s*|(?:const|let|var)\s+)$/.test(code.slice(Math.max(0, m.index - 20), m.index))) continue;
|
|
314
|
+
const inner = balanced(code, m.index + m[0].length - 1);
|
|
315
|
+
if (inner === null) continue;
|
|
316
|
+
const args = argumentsOf(inner);
|
|
317
|
+
for (const h of helpers.filter((x) => x.call === m[1])) {
|
|
318
|
+
const collection = "name" in h.collection ? h.collection.name : fixed(args[h.collection.arg] ?? "");
|
|
319
|
+
if (collection === null) continue;
|
|
320
|
+
const entry = h.entry ? fixed(args[h.entry.arg] ?? "") : null;
|
|
321
|
+
add({ file: path, collection, via: "helper", call: h.call, ...(entry !== null ? { entry } : {}) });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return [...out.values()].sort((a, b) => a.file.localeCompare(b.file) || a.collection.localeCompare(b.collection) || a.via.localeCompare(b.via));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* The pages that read a collection: a page whose own file, or any file it imports however deep,
|
|
330
|
+
* reads it (`readsEdges`, from `reads`). `importers` is the import graph as the core makes it
|
|
331
|
+
* (`importGraph`: file → the files that import it); `pages` are the core's (`discover`).
|
|
332
|
+
* @param {{ collection: string, reads: Read[], importers: Map<string, Set<string>>, pages: { path: string, file: string | null }[] }} input
|
|
333
|
+
* @returns {{ path: string, file: string, through: string[] }[]}
|
|
334
|
+
*/
|
|
335
|
+
export function readersOf({ collection, reads: edges, importers, pages }) {
|
|
336
|
+
const readers = new Set(edges.filter((r) => r.collection === collection).map((r) => r.file));
|
|
337
|
+
/** @type {Map<string, string[]>} */
|
|
338
|
+
const imports = new Map();
|
|
339
|
+
for (const [target, by] of importers) for (const f of by) imports.set(f, [...(imports.get(f) ?? []), target]);
|
|
340
|
+
const out = [];
|
|
341
|
+
for (const page of pages) {
|
|
342
|
+
if (!page.file) continue;
|
|
343
|
+
const seen = new Set([page.file]);
|
|
344
|
+
const queue = [page.file];
|
|
345
|
+
while (queue.length) {
|
|
346
|
+
for (const next of imports.get(queue.pop()) ?? []) {
|
|
347
|
+
if (seen.has(next)) continue;
|
|
348
|
+
seen.add(next);
|
|
349
|
+
queue.push(next);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const through = [...seen].filter((f) => readers.has(f)).sort();
|
|
353
|
+
if (through.length) out.push({ path: page.path, file: page.file, through });
|
|
354
|
+
}
|
|
355
|
+
return out.sort((a, b) => a.path.localeCompare(b.path));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// ── What counts as an item's own value ────────────────────────────────────
|
|
359
|
+
|
|
360
|
+
const collapse = (s) => String(s).replace(/\s+/g, " ").trim();
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Whether a value is specific enough that finding it written in code means it was copied there:
|
|
364
|
+
* a string of at least two words, at least 12 characters and at least 8 letters, that is not an
|
|
365
|
+
* address. A word ("Explorer"), a label ("Race bikes"), a number, a price ("€ 1.299,00"), a date,
|
|
366
|
+
* a slug, a path or a URL is too common to tell copying from coincidence, and is never reported.
|
|
367
|
+
*/
|
|
368
|
+
export function significant(value) {
|
|
369
|
+
if (typeof value !== "string") return false;
|
|
370
|
+
const v = collapse(value);
|
|
371
|
+
if (v.length < 12 || !v.includes(" ")) return false;
|
|
372
|
+
if ((v.match(/\p{L}/gu)?.length ?? 0) < 8) return false;
|
|
373
|
+
return !/^(?:[a-z][a-z0-9+.-]*:|\/|\.{1,2}\/|#|www\.)/i.test(v);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* A value too short to call copied alone, that still says which item it is when another of the same
|
|
378
|
+
* item's values stands beside it (R3b's second signal): a text of three characters or more with a
|
|
379
|
+
* letter in it ("City Bike"), or a number of two digits or more (a price, 899). Not an address.
|
|
380
|
+
*/
|
|
381
|
+
export function distinctive(value) {
|
|
382
|
+
if (typeof value === "number") return Number.isFinite(value) && Math.abs(value) >= 10;
|
|
383
|
+
if (typeof value !== "string") return false;
|
|
384
|
+
const v = collapse(value);
|
|
385
|
+
return v.length >= 3 && /\p{L}/u.test(v) && !/^(?:[a-z][a-z0-9+.-]*:|\/|\.{1,2}\/|#|www\.)/i.test(v);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** One text however it is written: "City Bike" and its slug "city-bike" are one value of an item, not two. */
|
|
389
|
+
const sameText = (v) => collapse(String(v)).toLowerCase().replace(/[\s_-]+/g, " ");
|
|
390
|
+
|
|
391
|
+
/** Every leaf of a value with its JSON pointer. */
|
|
392
|
+
function leaves(value, at = "", out = []) {
|
|
393
|
+
if (Array.isArray(value)) value.forEach((v, i) => leaves(v, `${at}/${i}`, out));
|
|
394
|
+
else if (value && typeof value === "object") for (const [k, v] of Object.entries(value)) leaves(v, `${at}/${k.replace(/~/g, "~0").replace(/\//g, "~1")}`, out);
|
|
395
|
+
else out.push([at || "", value]);
|
|
396
|
+
return out;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** The value at a JSON pointer, or undefined. */
|
|
400
|
+
function at(value, pointer) {
|
|
401
|
+
if (pointer === "" || pointer === "/") return pointer === "" ? value : value?.[""];
|
|
402
|
+
let here = value;
|
|
403
|
+
for (const raw of pointer.slice(1).split("/")) {
|
|
404
|
+
const key = raw.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
405
|
+
if (here === null || typeof here !== "object" || !(key in here)) return undefined;
|
|
406
|
+
here = here[key];
|
|
407
|
+
}
|
|
408
|
+
return here;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const under = (pointer, set) => [...set].some((p) => pointer === p || pointer.startsWith(`${p}/`));
|
|
412
|
+
|
|
413
|
+
/** A leaf's pointer with each place in a list as `0`, as a field's pointer names any entry of its list (fields.ts). */
|
|
414
|
+
const anyEntry = (pointer) => pointer.replace(/\/\d+(?=\/|$)/g, "/0");
|
|
415
|
+
|
|
416
|
+
/** The discriminator a block names its type by when no schema says it: Payload's `blockType`, Sanity's `_type`. */
|
|
417
|
+
const BLOCK_KEYS = new Set(["blockType", "_type"]);
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Every value a JSON Schema fixes, anywhere in it: each `const` and each `enum` value (texts and
|
|
421
|
+
* numbers). A block's type, an option, a fixed label: the schema's, not an item's content.
|
|
422
|
+
* @param {unknown} schema
|
|
423
|
+
* @returns {(string | number)[]}
|
|
424
|
+
*/
|
|
425
|
+
export function schemaValues(schema) {
|
|
426
|
+
/** @type {Set<string | number>} */
|
|
427
|
+
const out = new Set();
|
|
428
|
+
const keep = (v) => (typeof v === "string" || (typeof v === "number" && Number.isFinite(v))) && out.add(v);
|
|
429
|
+
const walk = (node, depth) => {
|
|
430
|
+
if (!node || typeof node !== "object" || depth > 64) return;
|
|
431
|
+
if (Array.isArray(node)) return node.forEach((n) => walk(n, depth + 1));
|
|
432
|
+
if ("const" in node) keep(node.const);
|
|
433
|
+
if (Array.isArray(node.enum)) node.enum.forEach(keep);
|
|
434
|
+
for (const [k, v] of Object.entries(node)) if (k !== "const" && k !== "enum" && k !== "default" && k !== "examples") walk(v, depth + 1);
|
|
435
|
+
};
|
|
436
|
+
walk(schema, 0);
|
|
437
|
+
return [...out];
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* What in a collection's items is its schema's, not an item's content (R3b): its choice fields'
|
|
442
|
+
* values, a blocks field's discriminator (`/body/2/blockType` = "quote": a renderer names it to show
|
|
443
|
+
* the block), the field that keeps the manual order (`x-cms.order`), and any value the schema fixes
|
|
444
|
+
* (`fixed`: a const, an enum value, a block type). A boolean is never content (`distinctive`).
|
|
445
|
+
* @param {CollectionInput} c
|
|
446
|
+
*/
|
|
447
|
+
function structure(c) {
|
|
448
|
+
/** @type {Set<string>} */
|
|
449
|
+
const pointers = new Set();
|
|
450
|
+
/** @type {Set<string>} */
|
|
451
|
+
const discriminators = new Set();
|
|
452
|
+
/** @type {Set<string>} */
|
|
453
|
+
const fixed = new Set();
|
|
454
|
+
const keyOf = (v) => `${typeof v}:${typeof v === "string" ? sameText(v) : v}`;
|
|
455
|
+
const walk = (fields) => {
|
|
456
|
+
for (const f of fields ?? []) {
|
|
457
|
+
const pointer = f.pointer ?? `/${f.key}`;
|
|
458
|
+
if (f.type === "option" || f.type === "options" || f.type === "tags" || f.options?.length) pointers.add(pointer);
|
|
459
|
+
for (const o of f.options ?? []) fixed.add(keyOf(o));
|
|
460
|
+
if (f.blocks) {
|
|
461
|
+
discriminators.add(`${pointer}/0/${f.blocks.key ?? "blockType"}`);
|
|
462
|
+
for (const t of f.blocks.types ?? []) {
|
|
463
|
+
fixed.add(keyOf(typeof t === "string" ? t : t.value));
|
|
464
|
+
if (typeof t === "object") walk(t.fields);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
walk(f.fields);
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
walk(c.fields);
|
|
471
|
+
if (c.order) pointers.add(`/${c.order}`);
|
|
472
|
+
for (const v of c.fixed ?? []) fixed.add(keyOf(v));
|
|
473
|
+
/** Whether the leaf at `pointer`, holding `value`, is the schema's. */
|
|
474
|
+
return (pointer, value) => {
|
|
475
|
+
const any = anyEntry(pointer);
|
|
476
|
+
if (under(any, pointers) || discriminators.has(any)) return true;
|
|
477
|
+
const parts = pointer.split("/");
|
|
478
|
+
if (parts.length >= 3 && /^\d+$/.test(parts[parts.length - 2]) && BLOCK_KEYS.has(parts[parts.length - 1])) return true;
|
|
479
|
+
return (typeof value === "string" || typeof value === "number") && fixed.has(keyOf(value));
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// ── No copied content ─────────────────────────────────────────────────────
|
|
484
|
+
|
|
485
|
+
/** Text of code as it shows: escapes and entities a literal is written with read as the text, whitespace as one space. */
|
|
486
|
+
function readable(text) {
|
|
487
|
+
/** @type {number[]} */
|
|
488
|
+
const map = [];
|
|
489
|
+
let out = "";
|
|
490
|
+
const push = (s, i) => {
|
|
491
|
+
for (const c of s) {
|
|
492
|
+
out += c;
|
|
493
|
+
map.push(i);
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
const ENTITIES = { "&": "&", """: '"', "'": "'", "'": "'", "<": "<", ">": ">", " ": " " };
|
|
497
|
+
let space = false;
|
|
498
|
+
for (let i = 0; i < text.length; i++) {
|
|
499
|
+
const c = text[i];
|
|
500
|
+
if (/\s/.test(c)) {
|
|
501
|
+
if (!space) push(" ", i);
|
|
502
|
+
space = true;
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
space = false;
|
|
506
|
+
if (c === "\\" && /["'`]/.test(text[i + 1] ?? "")) {
|
|
507
|
+
push(text[i + 1], i);
|
|
508
|
+
i++;
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
if (c === "&") {
|
|
512
|
+
const e = /^&(?:amp|quot|#39|apos|lt|gt|nbsp);/.exec(text.slice(i, i + 6));
|
|
513
|
+
if (e) {
|
|
514
|
+
const v = ENTITIES[e[0]];
|
|
515
|
+
if (v === " ") {
|
|
516
|
+
if (!space) push(" ", i);
|
|
517
|
+
space = true;
|
|
518
|
+
} else push(v, i);
|
|
519
|
+
i += e[0].length - 1;
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
push(c, i);
|
|
524
|
+
}
|
|
525
|
+
return { text: out, map };
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const lineAt = (text, index) => text.slice(0, index).split("\n").length;
|
|
529
|
+
const isContent = (path, adapter) => contentKind(path, adapter) !== null;
|
|
530
|
+
|
|
531
|
+
/** Where a value is written in a file's text as it shows, or null: a number not inside a longer one, a text not inside a longer word. */
|
|
532
|
+
function written(value, text) {
|
|
533
|
+
const re =
|
|
534
|
+
typeof value === "number"
|
|
535
|
+
? new RegExp(`(?<![\\p{N}.,])${escapeText(String(value))}(?![\\p{N}]|[.,]\\p{N})`, "u")
|
|
536
|
+
: new RegExp(`(?<![\\p{L}\\p{N}])${escapeText(collapse(value))}(?![\\p{L}\\p{N}])`, "u");
|
|
537
|
+
return re.exec(text);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* An item's values written as literals in changed code (ADR-0030 point 6, BUG-03): a component that
|
|
542
|
+
* copies the products into JSX instead of reading the collection. Only an item's own values count:
|
|
543
|
+
* not what is the schema's (`structure`: an option, a block's type, the manual order, a const), not a
|
|
544
|
+
* value two items share (a category), and not one the file had already before the change (`before`).
|
|
545
|
+
* Content files are not code. Two signals:
|
|
546
|
+
*
|
|
547
|
+
* - one `significant` value (a name of two words and 12 characters, a sentence) is enough;
|
|
548
|
+
* - shorter ones count together (R3b): two or more `distinctive` values of one item in one file,
|
|
549
|
+
* one of them a text ("City Bike" with its price 899, `together`). A name alone, or a price alone,
|
|
550
|
+
* is too common to tell copying from coincidence. A text and its slug are one value.
|
|
551
|
+
* @param {{ files: (SiteFile & { before?: string | null })[], collections: CollectionInput[], adapter: BindingAdapter | BindingAdapter[] }} input
|
|
552
|
+
*/
|
|
553
|
+
export function noCopiedContent({ files, collections, adapter }) {
|
|
554
|
+
/** @type {Map<string, { collection: string, item: string, field: string }>} */
|
|
555
|
+
const values = new Map();
|
|
556
|
+
const shared = new Set();
|
|
557
|
+
/** Per item, its shorter values: [collection, item, [[pointer, value]]]. */
|
|
558
|
+
const shorter = [];
|
|
559
|
+
/** How many items hold a shorter value, by its text. */
|
|
560
|
+
const holders = new Map();
|
|
561
|
+
for (const c of collections) {
|
|
562
|
+
const schemaOwn = structure(c);
|
|
563
|
+
const seen = new Map();
|
|
564
|
+
for (const item of c.items ?? []) {
|
|
565
|
+
const own = [];
|
|
566
|
+
for (const [pointer, v] of leaves(item.data)) {
|
|
567
|
+
if (schemaOwn(pointer, v)) continue;
|
|
568
|
+
if (significant(v)) {
|
|
569
|
+
const key = collapse(v);
|
|
570
|
+
if (seen.has(key) && seen.get(key) !== item.id) shared.add(key);
|
|
571
|
+
seen.set(key, item.id);
|
|
572
|
+
if (!values.has(key)) values.set(key, { collection: c.name, item: item.id, field: pointer });
|
|
573
|
+
} else if (distinctive(v)) own.push([pointer, v]);
|
|
574
|
+
}
|
|
575
|
+
for (const k of new Set(own.map(([, v]) => `${typeof v}:${sameText(v)}`))) holders.set(k, (holders.get(k) ?? 0) + 1);
|
|
576
|
+
shorter.push([c.name, item.id, own]);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
for (const k of shared) values.delete(k);
|
|
580
|
+
const findings = [];
|
|
581
|
+
const adapters = Array.isArray(adapter) ? adapter : [adapter];
|
|
582
|
+
for (const f of files) {
|
|
583
|
+
if (!f.text || !/\.(astro|tsx?|jsx?|mts|mjs|cjs|cts|vue|svelte|html)$/.test(f.path) || adapters.some((a) => isContent(f.path, a))) continue;
|
|
584
|
+
const now = readable(f.text);
|
|
585
|
+
const was = f.before ? readable(f.before).text : "";
|
|
586
|
+
const reported = new Set();
|
|
587
|
+
for (const [value, where] of values) {
|
|
588
|
+
const re = new RegExp(`(?<![\\p{L}\\p{N}])${escapeText(value)}(?![\\p{L}\\p{N}])`, "u");
|
|
589
|
+
const m = re.exec(now.text);
|
|
590
|
+
if (!m || re.test(was)) continue;
|
|
591
|
+
reported.add(`${where.collection}\u0000${where.item}`);
|
|
592
|
+
findings.push({ check: "noCopiedContent", file: f.path, line: lineAt(f.text, now.map[m.index]), ...where, value });
|
|
593
|
+
}
|
|
594
|
+
for (const [collection, item, own] of shorter) {
|
|
595
|
+
if (reported.has(`${collection}\u0000${item}`)) continue;
|
|
596
|
+
const found = new Map();
|
|
597
|
+
for (const [pointer, v] of own) {
|
|
598
|
+
const k = `${typeof v}:${sameText(v)}`;
|
|
599
|
+
// One another item holds too is not this item's own; one written before is not the change's.
|
|
600
|
+
if (found.has(k) || holders.get(k) > 1) continue;
|
|
601
|
+
const m = written(v, now.text);
|
|
602
|
+
if (!m || written(v, was)) continue;
|
|
603
|
+
found.set(k, { field: pointer, value: v, at: m.index });
|
|
604
|
+
}
|
|
605
|
+
const all = [...found.values()].sort((a, b) => a.at - b.at);
|
|
606
|
+
if (all.length < 2 || !all.some((x) => typeof x.value === "string")) continue;
|
|
607
|
+
const first = all.find((x) => typeof x.value === "string");
|
|
608
|
+
findings.push({
|
|
609
|
+
check: "noCopiedContent",
|
|
610
|
+
file: f.path,
|
|
611
|
+
line: lineAt(f.text, now.map[all[0].at]),
|
|
612
|
+
collection,
|
|
613
|
+
item,
|
|
614
|
+
field: first.field,
|
|
615
|
+
value: collapse(first.value),
|
|
616
|
+
together: all.map((x) => ({ field: x.field, value: x.value })),
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
return findings.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// ── Content kept code, design kept content ────────────────────────────────
|
|
624
|
+
|
|
625
|
+
const CONTENT_FILE = /\.(json|md|mdx|markdoc|ya?ml)$/;
|
|
626
|
+
const ASSET_FILE = /\.(png|jpe?g|gif|webp|avif|svg|ico|mp4|webm|mov|pdf)$/i;
|
|
627
|
+
|
|
628
|
+
/** `content` for an item or page text file, `asset` for an uploaded image or file, null for code. */
|
|
629
|
+
function contentKind(path, adapter) {
|
|
630
|
+
if (adapter.content.some((r) => path.startsWith(r))) {
|
|
631
|
+
const rest = path.slice(adapter.content.find((r) => path.startsWith(r)).length);
|
|
632
|
+
if (rest.split("/").pop() === ".gitkeep") return "content";
|
|
633
|
+
// A dot folder there is the schemas' (`.schemas/`): a schema is code the CMS writes, not content.
|
|
634
|
+
return !rest.split("/").some((s) => s.startsWith(".")) && CONTENT_FILE.test(rest) ? "content" : null;
|
|
635
|
+
}
|
|
636
|
+
if (adapter.assets.some((r) => path.startsWith(r)) && ASSET_FILE.test(path)) return "asset";
|
|
637
|
+
return null;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* A content change holds no code (ADR-0030 point 9): every file a CMS publish writes or deletes is
|
|
642
|
+
* an item, a page's text or an uploaded asset, in the adapter's content and asset folders.
|
|
643
|
+
* @param {{ files: { path: string, deleted?: boolean }[], adapter: BindingAdapter }} input
|
|
644
|
+
*/
|
|
645
|
+
export function contentKeptCode({ files, adapter }) {
|
|
646
|
+
return files
|
|
647
|
+
.filter((f) => contentKind(f.path, adapter) === null)
|
|
648
|
+
.map((f) => ({ check: "contentKeptCode", file: f.path, ...(f.deleted ? { deleted: true } : {}) }))
|
|
649
|
+
.sort((a, b) => a.file.localeCompare(b.file));
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const asMap = (files) => new Map((Array.isArray(files) ? files : Object.entries(files ?? {}).map(([path, text]) => ({ path, text }))).map((f) => [f.path, f.text]));
|
|
653
|
+
const bindingKey = (page, b) => JSON.stringify([page, b.collection, b.item, b.field]);
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* A design change kept the content (ADR-0030 point 9, R3b's separation test): no content file
|
|
657
|
+
* changed, came or went, and every binding before is still there after. Bindings are `binds`'
|
|
658
|
+
* answers per page; one lost is one fewer element bound to that item's field on that page, one
|
|
659
|
+
* moved is the same element (by its marker key) bound to another field or now a reason.
|
|
660
|
+
* @param {{ before: { files: SiteFile[] | Record<string, string>, bindings?: ReturnType<typeof binds>[] }, after: { files: SiteFile[] | Record<string, string>, bindings?: ReturnType<typeof binds>[] }, adapter: BindingAdapter }} input
|
|
661
|
+
*/
|
|
662
|
+
export function designKeptContent({ before, after, adapter }) {
|
|
663
|
+
const findings = [];
|
|
664
|
+
const was = asMap(before.files);
|
|
665
|
+
const now = asMap(after.files);
|
|
666
|
+
for (const path of [...new Set([...was.keys(), ...now.keys()])].sort()) {
|
|
667
|
+
if (contentKind(path, adapter) === null) continue;
|
|
668
|
+
const a = was.get(path);
|
|
669
|
+
const b = now.get(path);
|
|
670
|
+
if (a === b) continue;
|
|
671
|
+
findings.push({ check: "designKeptContent", kind: "content-changed", file: path, change: a === undefined ? "added" : b === undefined ? "removed" : "changed" });
|
|
672
|
+
}
|
|
673
|
+
const count = (all) => {
|
|
674
|
+
const n = new Map();
|
|
675
|
+
for (const p of all ?? []) for (const e of p.elements) if (e.binding) n.set(bindingKey(p.page, e.binding), (n.get(bindingKey(p.page, e.binding)) ?? 0) + 1);
|
|
676
|
+
return n;
|
|
677
|
+
};
|
|
678
|
+
const n0 = count(before.bindings);
|
|
679
|
+
const n1 = count(after.bindings);
|
|
680
|
+
for (const [k, n] of n0) {
|
|
681
|
+
const m = n1.get(k) ?? 0;
|
|
682
|
+
if (m >= n) continue;
|
|
683
|
+
const [page, collection, item, field] = JSON.parse(k);
|
|
684
|
+
findings.push({ check: "designKeptContent", kind: "binding-lost", page, collection, item, field, before: n, after: m });
|
|
685
|
+
}
|
|
686
|
+
const afterByKey = new Map();
|
|
687
|
+
for (const p of after.bindings ?? []) for (const e of p.elements) if (e.key) afterByKey.set(JSON.stringify([p.page, e.key]), e);
|
|
688
|
+
for (const p of before.bindings ?? []) {
|
|
689
|
+
for (const e of p.elements) {
|
|
690
|
+
if (!e.binding || !e.key) continue;
|
|
691
|
+
const later = afterByKey.get(JSON.stringify([p.page, e.key]));
|
|
692
|
+
if (!later || (later.binding && bindingKey(p.page, later.binding) === bindingKey(p.page, e.binding))) continue;
|
|
693
|
+
findings.push({ check: "designKeptContent", kind: "binding-moved", page: p.page, key: e.key, from: e.binding, to: later.binding ?? { reason: later.reason } });
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return findings;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// ── Rendered HTML ─────────────────────────────────────────────────────────
|
|
700
|
+
|
|
701
|
+
const VOID = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"]);
|
|
702
|
+
const RAW = new Set(["script", "style", "textarea", "title", "template", "noscript", "xmp", "iframe"]);
|
|
703
|
+
/** Never a place on the page: as the bridge leaves them out (proxy/fields.ts `outside`). */
|
|
704
|
+
const OUT = /^(script|style|link|meta|template|noscript|astro-dev-toolbar|vite-error-overlay)$/;
|
|
705
|
+
/** No text of the page: as the runner reads an element's whole text. */
|
|
706
|
+
const NO_TEXT = /^(script|style|template|noscript|svg|title|head|iframe|canvas)$/;
|
|
707
|
+
|
|
708
|
+
function decode(s) {
|
|
709
|
+
return s.replace(/&(#x[0-9a-f]+|#\d+|amp|lt|gt|quot|apos|nbsp|#39);/gi, (_, e) => {
|
|
710
|
+
const k = e.toLowerCase();
|
|
711
|
+
if (k[0] === "#") {
|
|
712
|
+
const n = k[1] === "x" ? parseInt(k.slice(2), 16) : parseInt(k.slice(1), 10);
|
|
713
|
+
return Number.isFinite(n) ? String.fromCodePoint(n) : _;
|
|
714
|
+
}
|
|
715
|
+
return { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " " }[k] ?? _;
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* @typedef {{ tag: string, attrs: Record<string, string>, children: (HtmlNode | string)[], parent: HtmlNode | null, end?: number }} HtmlNode
|
|
721
|
+
* `end`: where its open tag's `>` is in the HTML, so attributes can be written into it (`itemsIn`).
|
|
722
|
+
*/
|
|
723
|
+
|
|
724
|
+
/** A forgiving HTML parser: enough for a framework's rendered page. */
|
|
725
|
+
export function parseHtml(html) {
|
|
726
|
+
/** @type {HtmlNode} */
|
|
727
|
+
const root = { tag: "#root", attrs: {}, children: [], parent: null };
|
|
728
|
+
let here = root;
|
|
729
|
+
let i = 0;
|
|
730
|
+
while (i < html.length) {
|
|
731
|
+
const lt = html.indexOf("<", i);
|
|
732
|
+
if (lt < 0) {
|
|
733
|
+
here.children.push(decode(html.slice(i)));
|
|
734
|
+
break;
|
|
735
|
+
}
|
|
736
|
+
if (lt > i) here.children.push(decode(html.slice(i, lt)));
|
|
737
|
+
if (html.startsWith("<!--", lt)) {
|
|
738
|
+
const end = html.indexOf("-->", lt + 4);
|
|
739
|
+
i = end < 0 ? html.length : end + 3;
|
|
740
|
+
continue;
|
|
741
|
+
}
|
|
742
|
+
if (html[lt + 1] === "!" || html[lt + 1] === "?") {
|
|
743
|
+
const end = html.indexOf(">", lt);
|
|
744
|
+
i = end < 0 ? html.length : end + 1;
|
|
745
|
+
continue;
|
|
746
|
+
}
|
|
747
|
+
const close = /^<\/([A-Za-z][\w:-]*)\s*>/.exec(html.slice(lt, lt + 200));
|
|
748
|
+
if (close) {
|
|
749
|
+
const tag = close[1].toLowerCase();
|
|
750
|
+
let n = here;
|
|
751
|
+
while (n && n.tag !== tag) n = n.parent;
|
|
752
|
+
if (n && n.parent) here = n.parent;
|
|
753
|
+
i = lt + close[0].length;
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
const open = /^<([A-Za-z][\w:-]*)/.exec(html.slice(lt, lt + 100));
|
|
757
|
+
if (!open) {
|
|
758
|
+
here.children.push("<");
|
|
759
|
+
i = lt + 1;
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
// The tag's end, quotes respected.
|
|
763
|
+
let j = lt + open[0].length;
|
|
764
|
+
let quote = null;
|
|
765
|
+
for (; j < html.length; j++) {
|
|
766
|
+
const c = html[j];
|
|
767
|
+
if (quote) {
|
|
768
|
+
if (c === quote) quote = null;
|
|
769
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
770
|
+
else if (c === ">") break;
|
|
771
|
+
}
|
|
772
|
+
const body = html.slice(lt + open[0].length, j);
|
|
773
|
+
/** @type {Record<string, string>} */
|
|
774
|
+
const attrs = {};
|
|
775
|
+
for (const a of body.matchAll(/([^\s"'>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g)) attrs[a[1].toLowerCase()] = decode(a[2] ?? a[3] ?? a[4] ?? "");
|
|
776
|
+
const tag = open[1].toLowerCase();
|
|
777
|
+
/** @type {HtmlNode} */
|
|
778
|
+
const node = { tag, attrs, children: [], parent: here, end: j };
|
|
779
|
+
here.children.push(node);
|
|
780
|
+
i = j + 1;
|
|
781
|
+
if (VOID.has(tag) || body.trimEnd().endsWith("/")) continue;
|
|
782
|
+
if (RAW.has(tag)) {
|
|
783
|
+
const end = html.toLowerCase().indexOf(`</${tag}`, i);
|
|
784
|
+
const inner = html.slice(i, end < 0 ? html.length : end);
|
|
785
|
+
if (tag !== "template") node.children.push(tag === "title" || tag === "textarea" ? decode(inner) : inner);
|
|
786
|
+
const gt = end < 0 ? -1 : html.indexOf(">", end);
|
|
787
|
+
i = gt < 0 ? html.length : gt + 1;
|
|
788
|
+
continue;
|
|
789
|
+
}
|
|
790
|
+
here = node;
|
|
791
|
+
}
|
|
792
|
+
return root;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/** An element's whole text, as the runner reads it: a line break a space, no script or style. */
|
|
796
|
+
function textOf(node) {
|
|
797
|
+
if (typeof node === "string") return node;
|
|
798
|
+
if (node.tag === "br") return " ";
|
|
799
|
+
if (NO_TEXT.test(node.tag)) return "";
|
|
800
|
+
return node.children.map(textOf).join("");
|
|
801
|
+
}
|
|
802
|
+
export const wholeText = (node) => collapse(textOf(node));
|
|
803
|
+
|
|
804
|
+
/** The page's elements under its body, each with its place (`tag:n/…`, as page_fields keeps it) and whole text. */
|
|
805
|
+
export function elementsOf(html) {
|
|
806
|
+
const root = typeof html === "string" ? parseHtml(html) : html;
|
|
807
|
+
const find = (n, tag) => {
|
|
808
|
+
for (const c of n.children) {
|
|
809
|
+
if (typeof c === "string") continue;
|
|
810
|
+
if (c.tag === tag) return c;
|
|
811
|
+
const d = find(c, tag);
|
|
812
|
+
if (d) return d;
|
|
813
|
+
}
|
|
814
|
+
return null;
|
|
815
|
+
};
|
|
816
|
+
const body = find(root, "body") ?? root;
|
|
817
|
+
const out = [];
|
|
818
|
+
const outside = (e) => OUT.test(e.tag) || e.attrs.id === "comment-modus" || e.attrs.id === "comment-modus-schil" || "data-cm-ignore" in e.attrs || "data-cm-negeer" in e.attrs || "data-unbase-layer" in e.attrs;
|
|
819
|
+
const walk = (n, place, depth) => {
|
|
820
|
+
const seen = new Map();
|
|
821
|
+
for (const c of n.children) {
|
|
822
|
+
if (typeof c === "string" || outside(c)) continue;
|
|
823
|
+
const k = seen.get(c.tag) ?? 0;
|
|
824
|
+
seen.set(c.tag, k + 1);
|
|
825
|
+
const at = `${place ? `${place}/` : ""}${c.tag}:${k}`;
|
|
826
|
+
out.push({ node: c, at, depth, text: wholeText(c) });
|
|
827
|
+
walk(c, at, depth + 1);
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
walk(body, "", 0);
|
|
831
|
+
return out;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// ── Binds ─────────────────────────────────────────────────────────────────
|
|
835
|
+
|
|
836
|
+
/** A value worth looking for on a page: a string with three characters or more, a letter or digit among them. */
|
|
837
|
+
const shown = (v) => typeof v === "string" && collapse(v).length >= 3 && /[\p{L}\p{N}]/u.test(v);
|
|
838
|
+
|
|
839
|
+
/** `file#pointer` → its parts, or null. */
|
|
840
|
+
function fieldRef(ref) {
|
|
841
|
+
const i = ref.indexOf("#");
|
|
842
|
+
if (i <= 0) return null;
|
|
843
|
+
return { file: ref.slice(0, i), pointer: ref.slice(i + 1) };
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* Which item and field each element that shows a collection's value is bound to, or why it is not
|
|
848
|
+
* (ADR-0030 point 4, plan-editable). An element's binding comes from a marker only: the field it
|
|
849
|
+
* shows as `file#pointer` (the adapter's marker attributes, or the runner's fields answer for its
|
|
850
|
+
* identity), and that field's value must be the element's whole text (verification). An element
|
|
851
|
+
* that shows a collection's value without such a marker is listed with its reason and how many
|
|
852
|
+
* fields it could be (`candidates`), and is never given one of them:
|
|
853
|
+
*
|
|
854
|
+
* - no identity marker (the adapter has none, as Next.js today, or the element lacks it): `no-identity`;
|
|
855
|
+
* - an identity but no answer: `ambiguous` when more than one field has its text, else `unproven`;
|
|
856
|
+
* - a reason marker: that reason;
|
|
857
|
+
* - a field marker whose value is not the text shown (`{title}!`, a formatted price): `unproven`.
|
|
858
|
+
*
|
|
859
|
+
* Elements bound to a field outside the collections (a page's own texts) are not listed. Of nested
|
|
860
|
+
* elements with the same text, the innermost is listed, or the one with the marker.
|
|
861
|
+
* @param {{ html: string, page?: string, collections: CollectionInput[], adapter: BindingAdapter, answer?: { elements?: Record<string, { field?: string, reason?: string, at?: string | null }>, fields?: Record<string, { kind: string, file?: string, pointer?: string }> } }} input
|
|
862
|
+
*/
|
|
863
|
+
export function binds({ html, page = "", collections, adapter, answer }) {
|
|
864
|
+
const byFile = new Map();
|
|
865
|
+
/** @type {Map<string, Binding[]>} */
|
|
866
|
+
const byValue = new Map();
|
|
867
|
+
for (const c of collections) {
|
|
868
|
+
for (const item of c.items ?? []) {
|
|
869
|
+
if (item.file) byFile.set(item.file, { collection: c.name, item });
|
|
870
|
+
for (const [pointer, v] of leaves(item.data)) {
|
|
871
|
+
if (!shown(v)) continue;
|
|
872
|
+
const k = collapse(v);
|
|
873
|
+
byValue.set(k, [...(byValue.get(k) ?? []), { collection: c.name, item: item.id, field: pointer }]);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
const markers = adapter.markers;
|
|
878
|
+
const answeredAt = new Map(Object.values(answer?.elements ?? {}).filter((x) => x.at).map((x) => [x.at, x]));
|
|
879
|
+
const identityOf = (e) => {
|
|
880
|
+
if (!markers) return null;
|
|
881
|
+
const [src, ...rest] = markers.identity;
|
|
882
|
+
const s = e.node.attrs[src];
|
|
883
|
+
return s ? [s, ...rest.map((r) => e.node.attrs[r] ?? "")].join("|") : null;
|
|
884
|
+
};
|
|
885
|
+
/** The marker's answer for an element: a field reference, a reason, or nothing. */
|
|
886
|
+
const markedOf = (e, identity) => {
|
|
887
|
+
if (!markers) return null;
|
|
888
|
+
for (const a of markers.field) if (e.node.attrs[a]) return { field: e.node.attrs[a] };
|
|
889
|
+
if (e.node.attrs[markers.reason]) return { reason: e.node.attrs[markers.reason] };
|
|
890
|
+
const answered = (identity && answer?.elements?.[identity]) || answeredAt.get(e.at);
|
|
891
|
+
if (answered?.reason) return { reason: answered.reason };
|
|
892
|
+
if (answered?.field) {
|
|
893
|
+
const f = answer?.fields?.[answered.field];
|
|
894
|
+
if (f?.kind === "content" && f.file && f.pointer !== undefined) return { field: `${f.file}#${f.pointer}` };
|
|
895
|
+
return f ? { outside: true } : fieldRef(answered.field) ? { field: answered.field } : null;
|
|
896
|
+
}
|
|
897
|
+
return null;
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
const all = elementsOf(html);
|
|
901
|
+
/** @type {{ key: string, at: string, tag: string, text: string, binding?: { collection: string, item: string, field: string }, reason?: string, marked?: { collection: string, item: string, field: string }, candidates?: number }[]} */
|
|
902
|
+
const elements = [];
|
|
903
|
+
/** Elements whose text a marked ancestor already answers for. */
|
|
904
|
+
const covered = new Set();
|
|
905
|
+
for (let i = 0; i < all.length; i++) {
|
|
906
|
+
const e = all[i];
|
|
907
|
+
const parentCovered = covered.has(e.node.parent) && e.node.parent && wholeText(e.node.parent) === e.text;
|
|
908
|
+
if (parentCovered) {
|
|
909
|
+
covered.add(e.node);
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
const identity = identityOf(e);
|
|
913
|
+
const marked = markedOf(e, identity);
|
|
914
|
+
const candidates = byValue.get(e.text) ?? [];
|
|
915
|
+
const key = identity ?? e.at;
|
|
916
|
+
const base = { key, at: e.at, tag: e.node.tag, text: e.text };
|
|
917
|
+
if (marked?.field) {
|
|
918
|
+
const ref = fieldRef(marked.field);
|
|
919
|
+
const owner = ref && byFile.get(ref.file);
|
|
920
|
+
covered.add(e.node);
|
|
921
|
+
if (!owner) continue;
|
|
922
|
+
const value = at(owner.item.data, ref.pointer);
|
|
923
|
+
const binding = { collection: owner.collection, item: owner.item.id, field: ref.pointer };
|
|
924
|
+
if (value !== undefined && value !== null && typeof value !== "object" && collapse(value) === e.text) elements.push({ ...base, binding });
|
|
925
|
+
else elements.push({ ...base, reason: "unproven", marked: binding });
|
|
926
|
+
continue;
|
|
927
|
+
}
|
|
928
|
+
if (marked?.outside) {
|
|
929
|
+
covered.add(e.node);
|
|
930
|
+
continue;
|
|
931
|
+
}
|
|
932
|
+
if (!candidates.length) continue;
|
|
933
|
+
if (marked?.reason) {
|
|
934
|
+
covered.add(e.node);
|
|
935
|
+
elements.push({ ...base, reason: marked.reason, candidates: candidates.length });
|
|
936
|
+
continue;
|
|
937
|
+
}
|
|
938
|
+
// Unmarked: listed at the innermost element with this text.
|
|
939
|
+
const inner = e.node.children.some((c) => typeof c !== "string" && !NO_TEXT.test(c.tag) && wholeText(c) === e.text);
|
|
940
|
+
if (inner) continue;
|
|
941
|
+
const reason = !identity ? "no-identity" : candidates.length > 1 ? "ambiguous" : "unproven";
|
|
942
|
+
elements.push({ ...base, reason, candidates: candidates.length });
|
|
943
|
+
}
|
|
944
|
+
const bound = elements.filter((e) => e.binding).length;
|
|
945
|
+
return { page, elements, bound, unbound: elements.length - bound };
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// ── New item shows ────────────────────────────────────────────────────────
|
|
949
|
+
|
|
950
|
+
/**
|
|
951
|
+
* A test item for a collection: every text field holds the token, so the page shows it where it shows
|
|
952
|
+
* that field. `base` (an existing item's data, so the item passes its schema) keeps its other values;
|
|
953
|
+
* a text of its own that is `significant` holds the token too, as it is already on the page.
|
|
954
|
+
*/
|
|
955
|
+
export function testDraft({ collection, fields = [], id = "unbase-test-item", token = "Unbase test item", base = {} }) {
|
|
956
|
+
const data = Object.fromEntries(Object.entries(base ?? {}).map(([k, v]) => [k, significant(v) ? `${k} ${token}` : v]));
|
|
957
|
+
for (const f of fields) {
|
|
958
|
+
if (f.pointer && f.pointer.split("/").length > 2) continue;
|
|
959
|
+
if (f.type === "text" || f.type === "inline" || f.type === "longtext") data[f.key] = `${f.label ?? f.key} ${token}`;
|
|
960
|
+
else if (f.type === "slug") data[f.key] = id;
|
|
961
|
+
}
|
|
962
|
+
return { collection, id, data };
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/** A page's address for one item: its one parameter filled with the item's slug; null where that cannot be told. */
|
|
966
|
+
export function addressFor(path, slug) {
|
|
967
|
+
const params = path.match(/\[[^\]]+\]/g) ?? [];
|
|
968
|
+
if (!params.length) return path;
|
|
969
|
+
// An overview's later pages (/blog/page/[n]) list items; which one holds a new item depends on its order.
|
|
970
|
+
if (params.length > 1 || /\/page\/\[[^\]]+\]$/.test(path)) return null;
|
|
971
|
+
return path.replace(/\[[^\]]+\]/, slug);
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* How a render answered, for the gate (ADR-0030, never guess): true, a page; false, a page that broke
|
|
976
|
+
* (a server error: the build's, or the code's on this content); null, nothing the gate can judge (no
|
|
977
|
+
* answer, a 4xx, not rendered). A dev server that has not taken a new item yet answers its own page
|
|
978
|
+
* with a 404, and one that is gone answers nothing: the gate's measuring limits, never a failure.
|
|
979
|
+
* @param {{ html?: string, status?: number, error?: string }} page
|
|
980
|
+
* @returns {boolean | null}
|
|
981
|
+
*/
|
|
982
|
+
export function loaded(page) {
|
|
983
|
+
const status = page.status ?? (page.html && !page.error ? 200 : 0);
|
|
984
|
+
if (status >= 500) return false;
|
|
985
|
+
return status >= 200 && status < 400 && !!page.html && !page.error ? true : null;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/** Why a render could not be judged, in a few words: its status, or its error. */
|
|
989
|
+
const unanswered = (page) => (page.status ? `the page answered ${page.status}` : page.error ? `the page did not render: ${String(page.error).slice(0, 200)}` : "the page did not answer");
|
|
990
|
+
|
|
991
|
+
/**
|
|
992
|
+
* Whether a new item shows on every page that reads its collection (R3b gate step 4, R3d): each page
|
|
993
|
+
* is rendered with the test draft laid over the version (`render({ path, overlay })`; the caller
|
|
994
|
+
* lays it over a checkout and renders, nothing is committed), and the item must be on it. It is
|
|
995
|
+
* seen by binding where the adapter has markers (`by: "binding"`), else by its values in the page's
|
|
996
|
+
* text (`by: "text"`): that proves it shows, not which element binds it. With `baseline` the page is
|
|
997
|
+
* rendered without the draft first; a value already there proves nothing (`unproven`). A page that
|
|
998
|
+
* breaks with the item (`loaded` false) fails; one that cannot be judged with it (a 404 on the item's
|
|
999
|
+
* own page, no answer) is `unproven` with why, never a failure. `route` is the page's address as asked
|
|
1000
|
+
* (`/blog/[slug]`) where the item's own address differs.
|
|
1001
|
+
* @param {{
|
|
1002
|
+
* collection: string,
|
|
1003
|
+
* draft: { id: string, data: Record<string, unknown>, file?: string, slug?: string },
|
|
1004
|
+
* pages: { path: string, file?: string | null }[],
|
|
1005
|
+
* render: (req: { path: string, overlay: { collection: string, op: "create", id: string, data: unknown, file?: string } | null }) => Promise<string | { html?: string, status?: number, error?: string }>,
|
|
1006
|
+
* adapter: BindingAdapter,
|
|
1007
|
+
* collections?: CollectionInput[],
|
|
1008
|
+
* baseline?: boolean,
|
|
1009
|
+
* }} input
|
|
1010
|
+
*/
|
|
1011
|
+
export async function newItemShows({ collection, draft, pages, render, adapter, collections = [], baseline = true }) {
|
|
1012
|
+
const tokens = [...new Set(leaves(draft.data).map(([, v]) => v).filter(significant).map(collapse))];
|
|
1013
|
+
if (!tokens.length) return { ok: false, reason: "unproven", why: "the test item has no value specific enough to find on a page", pages: [] };
|
|
1014
|
+
const overlay = { collection, op: /** @type {const} */ ("create"), id: draft.id, data: draft.data, ...(draft.file ? { file: draft.file } : {}) };
|
|
1015
|
+
const withItem = collections.map((c) => (c.name === collection ? { ...c, items: [...(c.items ?? []), { id: draft.id, file: draft.file, data: draft.data }] } : c));
|
|
1016
|
+
const asPage = (r) => (typeof r === "string" ? { html: r } : r ?? {});
|
|
1017
|
+
const textIn = (html) => wholeText(parseHtml(html));
|
|
1018
|
+
const results = [];
|
|
1019
|
+
for (const p of pages) {
|
|
1020
|
+
const path = addressFor(p.path, draft.slug ?? draft.id);
|
|
1021
|
+
if (path === null) {
|
|
1022
|
+
results.push({ path: p.path, shows: null, reason: "unsupported" });
|
|
1023
|
+
continue;
|
|
1024
|
+
}
|
|
1025
|
+
const route = path !== p.path ? { route: p.path } : {};
|
|
1026
|
+
try {
|
|
1027
|
+
if (baseline) {
|
|
1028
|
+
const before = asPage(await render({ path, overlay: null }));
|
|
1029
|
+
const seen = loaded(before) ? textIn(before.html) : "";
|
|
1030
|
+
const there = tokens.filter((t) => seen.includes(t));
|
|
1031
|
+
if (there.length) {
|
|
1032
|
+
results.push({ path, ...route, shows: null, reason: "unproven", why: `already on the page without the item: ${there[0]}` });
|
|
1033
|
+
continue;
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
const after = asPage(await render({ path, overlay }));
|
|
1037
|
+
const answer = loaded(after);
|
|
1038
|
+
if (answer !== true) {
|
|
1039
|
+
const said = { ...(after.status ? { status: after.status } : {}), ...(after.error ? { error: after.error } : {}) };
|
|
1040
|
+
results.push(answer === false ? { path, ...route, shows: false, ...said } : { path, ...route, shows: null, reason: "unproven", why: unanswered(after), ...said });
|
|
1041
|
+
continue;
|
|
1042
|
+
}
|
|
1043
|
+
const bound = adapter.markers ? binds({ html: after.html, page: path, collections: withItem, adapter }).elements.some((e) => e.binding?.collection === collection && e.binding.item === draft.id) : false;
|
|
1044
|
+
const text = textIn(after.html);
|
|
1045
|
+
const shows = bound || tokens.some((t) => text.includes(t));
|
|
1046
|
+
results.push({ path, ...route, shows, ...(shows ? { by: bound ? "binding" : "text" } : {}) });
|
|
1047
|
+
} catch (err) {
|
|
1048
|
+
// The render itself failed (the machine, not the page): nothing to judge.
|
|
1049
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
1050
|
+
results.push({ path, ...route, shows: null, reason: "unproven", why: unanswered({ error }), error });
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
return { ok: results.length > 0 && results.every((r) => r.shows === true), pages: results };
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// ── Components on a collection: Build's gate (R3b) ────────────────────────
|
|
1057
|
+
|
|
1058
|
+
/** A name a field can be read by in code: `item.price`, `item["price"]`, `{ price }`. */
|
|
1059
|
+
const FIELD_NAME = /^[A-Za-z_$][\w$]*$/;
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* Which of a collection's fields a code file uses: read as a property (`bike.price`, `bike?.price`,
|
|
1063
|
+
* `bike.data.price`, `bike["price"]`) or taken apart (`{ title, price }`, `{ price: amount }`). Its
|
|
1064
|
+
* top-level fields only. How a component shows a collection, for the gate's answer and for the
|
|
1065
|
+
* schema check, which names the components that show a field before it is renamed or removed.
|
|
1066
|
+
* @param {{ text: string, fields: { key: string }[] }} input
|
|
1067
|
+
* @returns {string[]} the keys, in the fields' order
|
|
1068
|
+
*/
|
|
1069
|
+
export function fieldUse({ text, fields }) {
|
|
1070
|
+
const code = stripComments(text ?? "");
|
|
1071
|
+
const taken = new Set();
|
|
1072
|
+
for (const m of code.matchAll(/\{([^{}()]*)\}\s*(?::[^=;{}()]*)?\s*(?:=(?!=)|\))/g)) {
|
|
1073
|
+
for (const part of m[1].split(",")) {
|
|
1074
|
+
const name = part.trim().split(/\s*[:=]/)[0].trim();
|
|
1075
|
+
if (FIELD_NAME.test(name)) taken.add(name);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
return (fields ?? [])
|
|
1079
|
+
.map((f) => f.key)
|
|
1080
|
+
.filter((k, i, all) => FIELD_NAME.test(k) && all.indexOf(k) === i)
|
|
1081
|
+
.filter((k) => taken.has(k) || new RegExp(`\\??\\.\\s*${escape(k)}(?![\\w$])|\\[\\s*["'\`]${escape(k)}["'\`]\\s*\\]`).test(code));
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
const isCode = (path) => CODE.test(path) && !CONFIG.test(path);
|
|
1085
|
+
|
|
1086
|
+
/** Every file `from` imports, however deep, from the core's import graph (target → its importers). */
|
|
1087
|
+
function importedBy(importers) {
|
|
1088
|
+
/** @type {Map<string, string[]>} */
|
|
1089
|
+
const imports = new Map();
|
|
1090
|
+
for (const [target, by] of importers ?? []) for (const f of by) imports.set(f, [...(imports.get(f) ?? []), target]);
|
|
1091
|
+
return (from) => {
|
|
1092
|
+
const seen = new Set([from]);
|
|
1093
|
+
const queue = [from];
|
|
1094
|
+
while (queue.length) {
|
|
1095
|
+
for (const next of imports.get(queue.pop()) ?? []) {
|
|
1096
|
+
if (seen.has(next)) continue;
|
|
1097
|
+
seen.add(next);
|
|
1098
|
+
queue.push(next);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
seen.delete(from);
|
|
1102
|
+
return seen;
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
/**
|
|
1107
|
+
* The components a change wrote on a collection (R3b): the changed code files that show one of its
|
|
1108
|
+
* items. A file is on a collection when it reads it (`reads`), imports a file that does (`through`:
|
|
1109
|
+
* a slider calling the site's reader module), or is imported by one and uses its fields (`props`: the
|
|
1110
|
+
* card a page passes each item to). With the fields it uses and the pages it is on.
|
|
1111
|
+
* @param {{ files: SiteFile[], changed: { path: string, deleted?: boolean }[], collections: CollectionInput[], adapter: BindingAdapter | BindingAdapter[], pages: { path: string, file: string | null }[], importers: Map<string, Set<string>>, functions?: ReaderFunction[] }} input
|
|
1112
|
+
* @returns {{ file: string, collection: string, via: "reads" | "through" | "props", fields: string[], pages: string[] }[]}
|
|
1113
|
+
*/
|
|
1114
|
+
export function componentsOn({ files, changed, collections, adapter, pages, importers, functions }) {
|
|
1115
|
+
const text = new Map(files.map((f) => [f.path, f.text]));
|
|
1116
|
+
const names = collections.map((c) => c.name);
|
|
1117
|
+
const edges = reads({ files, adapter, collections: names, ...(functions ? { functions } : {}) });
|
|
1118
|
+
const below = importedBy(importers);
|
|
1119
|
+
const wrote = changed.filter((f) => !f.deleted && isCode(f.path) && text.has(f.path) && !(Array.isArray(adapter) ? adapter : [adapter]).some((a) => isContent(f.path, a))).map((f) => f.path);
|
|
1120
|
+
const out = [];
|
|
1121
|
+
for (const c of collections) {
|
|
1122
|
+
const readers = new Set(edges.filter((r) => r.collection === c.name).map((r) => r.file));
|
|
1123
|
+
if (!readers.size) continue;
|
|
1124
|
+
const showing = readersOf({ collection: c.name, reads: edges, importers: importers ?? new Map(), pages });
|
|
1125
|
+
// Every file that reads it, itself or through a file it imports: the page that calls the reader module.
|
|
1126
|
+
const reading = new Set([...text.keys()].filter((f) => readers.has(f) || [...below(f)].some((x) => readers.has(x))));
|
|
1127
|
+
const uses = (file) => fieldUse({ text: text.get(file) ?? "", fields: c.fields ?? [] });
|
|
1128
|
+
for (const file of wrote) {
|
|
1129
|
+
const fields = uses(file);
|
|
1130
|
+
// Passed the items: imported by a reading file, and it or a file it imports shows their fields (the card, the slider of cards).
|
|
1131
|
+
const passed = [...reading].some((r) => r !== file && below(r).has(file)) && (fields.length > 0 || [...below(file)].some((x) => uses(x).length > 0));
|
|
1132
|
+
const via = /** @type {"reads" | "props" | "through" | null} */ (readers.has(file) ? "reads" : passed ? "props" : reading.has(file) ? "through" : null);
|
|
1133
|
+
if (!via) continue;
|
|
1134
|
+
const on = showing.filter((p) => p.file === file || below(p.file).has(file)).map((p) => p.path);
|
|
1135
|
+
out.push({ file, collection: c.name, via, fields, pages: on });
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
return out.sort((a, b) => a.file.localeCompare(b.file) || a.collection.localeCompare(b.collection));
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/** At most this many collections and pages a gate renders, each without and with its test item. */
|
|
1142
|
+
export const GATE_COLLECTIONS = 2;
|
|
1143
|
+
export const GATE_PAGES = 4;
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* Build's gate for a component on a collection (R3b; ADR-0030 points 4, 6 and 8), before a change is
|
|
1147
|
+
* offered. The checks are this module's; who calls it renders (the runner, in its machine's dev
|
|
1148
|
+
* server), lays the test item over its checkout and removes it again. Nothing is committed.
|
|
1149
|
+
*
|
|
1150
|
+
* noCopiedContent no item's values in the changed code, with the second signal for short ones
|
|
1151
|
+
* reads a changed file that shows a collection's values reads it (else it copies them)
|
|
1152
|
+
* build each page the components are on renders, without the test item (an item's own
|
|
1153
|
+
* page with it: without, its address has no item)
|
|
1154
|
+
* newItemShows with a test item laid over the version, it shows on each of those pages
|
|
1155
|
+
* binds where the adapter has markers: the shown values bound to item and field (R8;
|
|
1156
|
+
* reported, never blocking: on Next.js every value is `no-identity` until JSX markers)
|
|
1157
|
+
*
|
|
1158
|
+
* Without `render` it answers the checks that need none, and what to render (`render`: per collection
|
|
1159
|
+
* the test item and the addresses). With it, the rest. `ok` is false when a blocking check failed, null
|
|
1160
|
+
* when nothing was rendered yet, true otherwise. A new item that cannot be told on a page (a value
|
|
1161
|
+
* already there, an address with two parameters, a page that answered a 404 or nothing) is `unproven`,
|
|
1162
|
+
* never a pass and never a failure: the gate never refuses a change over its own measuring limit. A
|
|
1163
|
+
* build's page fails only when it broke (a 5xx); an item's own page is built with the test item on it.
|
|
1164
|
+
* @param {{
|
|
1165
|
+
* files: SiteFile[],
|
|
1166
|
+
* changed: (SiteFile & { before?: string | null, deleted?: boolean })[],
|
|
1167
|
+
* collections: (CollectionInput & { folder?: string })[],
|
|
1168
|
+
* adapter: BindingAdapter,
|
|
1169
|
+
* pages: { path: string, file: string | null }[],
|
|
1170
|
+
* importers: Map<string, Set<string>>,
|
|
1171
|
+
* functions?: ReaderFunction[],
|
|
1172
|
+
* render?: (req: { path: string, overlay: { collection: string, op: "create", id: string, data: unknown, file?: string } | null }) => Promise<string | { html?: string, status?: number, error?: string }>,
|
|
1173
|
+
* }} input
|
|
1174
|
+
*/
|
|
1175
|
+
export async function componentGate({ files, changed, collections, adapter, pages, importers, functions, render }) {
|
|
1176
|
+
const components = componentsOn({ files, changed, collections, adapter, pages, importers, functions });
|
|
1177
|
+
const code = changed.filter((f) => !f.deleted && typeof f.text === "string");
|
|
1178
|
+
const copied = noCopiedContent({ files: code, collections, adapter });
|
|
1179
|
+
const on = new Set(components.map((c) => `${c.file}\u0000${c.collection}`));
|
|
1180
|
+
/** A file that shows a collection's values without reading it. */
|
|
1181
|
+
const unread = [];
|
|
1182
|
+
for (const f of copied) {
|
|
1183
|
+
const key = `${f.file}\u0000${f.collection}`;
|
|
1184
|
+
if (on.has(key) || unread.some((u) => `${u.file}\u0000${u.collection}` === key)) continue;
|
|
1185
|
+
unread.push({ check: "reads", file: f.file, collection: f.collection });
|
|
1186
|
+
}
|
|
1187
|
+
const applies = components.length > 0 || copied.length > 0;
|
|
1188
|
+
// What to render: the collections with the most components first, each with a test item.
|
|
1189
|
+
const counts = new Map();
|
|
1190
|
+
for (const c of components) counts.set(c.collection, (counts.get(c.collection) ?? 0) + 1);
|
|
1191
|
+
const plans = [...counts]
|
|
1192
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
|
1193
|
+
.slice(0, GATE_COLLECTIONS)
|
|
1194
|
+
.map(([name]) => {
|
|
1195
|
+
const c = collections.find((x) => x.name === name);
|
|
1196
|
+
const draft = testDraft({ collection: name, fields: c.fields ?? [], base: c.items?.[0]?.data ?? {} });
|
|
1197
|
+
const routes = [...new Set(components.filter((x) => x.collection === name).flatMap((x) => x.pages))].slice(0, GATE_PAGES);
|
|
1198
|
+
return {
|
|
1199
|
+
collection: name,
|
|
1200
|
+
draft: { id: draft.id, data: draft.data, ...(c.folder ? { file: `${c.folder}/${draft.id}.json` } : {}) },
|
|
1201
|
+
routes,
|
|
1202
|
+
paths: routes.map((r) => addressFor(r, draft.id)).filter((p) => p !== null),
|
|
1203
|
+
};
|
|
1204
|
+
});
|
|
1205
|
+
const findings = [...copied, ...unread];
|
|
1206
|
+
const checks = {
|
|
1207
|
+
noCopiedContent: { ok: copied.length === 0, findings: copied },
|
|
1208
|
+
reads: { ok: unread.length === 0, findings: unread },
|
|
1209
|
+
};
|
|
1210
|
+
if (!render || !applies) return { applies, ok: !applies ? true : findings.length ? false : null, components, findings, checks, render: applies ? plans : [] };
|
|
1211
|
+
|
|
1212
|
+
// One answer per page and overlay: the caller renders each once.
|
|
1213
|
+
const kept = new Map();
|
|
1214
|
+
const once = (req) => {
|
|
1215
|
+
const k = JSON.stringify([req.path, !!req.overlay]);
|
|
1216
|
+
if (!kept.has(k)) kept.set(k, Promise.resolve().then(() => render(req)));
|
|
1217
|
+
return kept.get(k);
|
|
1218
|
+
};
|
|
1219
|
+
const asPage = (r) => (typeof r === "string" ? { html: r } : r ?? {});
|
|
1220
|
+
const build = [];
|
|
1221
|
+
const shows = [];
|
|
1222
|
+
const bindings = { bound: 0, unbound: 0, reasons: {} };
|
|
1223
|
+
for (const plan of plans) {
|
|
1224
|
+
const draft = plan.draft;
|
|
1225
|
+
const overlay = { collection: plan.collection, op: /** @type {const} */ ("create"), id: draft.id, data: draft.data, ...(draft.file ? { file: draft.file } : {}) };
|
|
1226
|
+
// An item's own page is asked at the test item's address: without the item there it is a 404 by
|
|
1227
|
+
// design (QA 12-11, /nieuws/unbase-test-item), so its build is judged with the item laid over.
|
|
1228
|
+
const own = new Set(plan.routes.filter((r) => r.includes("[")).map((r) => addressFor(r, draft.id)).filter((p) => p !== null));
|
|
1229
|
+
for (const path of plan.paths) {
|
|
1230
|
+
const page = asPage(await once({ path, overlay: own.has(path) ? overlay : null }).catch((e) => ({ error: e instanceof Error ? e.message : String(e) })));
|
|
1231
|
+
// Only a page that broke fails the build; one that answered nothing to judge is not checked (null).
|
|
1232
|
+
const ok = loaded(page);
|
|
1233
|
+
build.push({ path, ok, ...(page.status ? { status: page.status } : {}), ...(page.error ? { error: String(page.error).slice(0, 500) } : {}) });
|
|
1234
|
+
}
|
|
1235
|
+
const shown = await newItemShows({ collection: plan.collection, draft, pages: plan.routes.map((path) => ({ path })), render: once, adapter, collections, baseline: true });
|
|
1236
|
+
shows.push({ collection: plan.collection, ...shown });
|
|
1237
|
+
if (adapter.markers) {
|
|
1238
|
+
const withItem = collections.map((c) => (c.name === plan.collection ? { ...c, items: [...(c.items ?? []), { id: draft.id, file: draft.file, data: draft.data }] } : c));
|
|
1239
|
+
for (const path of plan.paths) {
|
|
1240
|
+
const page = asPage(await once({ path, overlay }).catch(() => ({})));
|
|
1241
|
+
if (!page.html) continue;
|
|
1242
|
+
for (const e of binds({ html: page.html, page: path, collections: withItem, adapter }).elements) {
|
|
1243
|
+
if (e.binding) {
|
|
1244
|
+
if (e.binding.collection === plan.collection) bindings.bound++;
|
|
1245
|
+
} else {
|
|
1246
|
+
bindings.unbound++;
|
|
1247
|
+
bindings.reasons[e.reason] = (bindings.reasons[e.reason] ?? 0) + 1;
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
const broken = build.filter((p) => p.ok === false).map((p) => ({ check: "build", page: p.path, ...(p.status ? { status: p.status } : {}), ...(p.error ? { error: p.error } : {}) }));
|
|
1254
|
+
const missing = shows.flatMap((s) => s.pages.filter((p) => p.shows === false && !broken.some((x) => x.page === p.path)).map((p) => ({ check: "newItemShows", collection: s.collection, page: p.path, ...(p.status ? { status: p.status } : {}), ...(p.error ? { error: String(p.error).slice(0, 500) } : {}) })));
|
|
1255
|
+
const all = [...findings, ...broken, ...missing];
|
|
1256
|
+
const proven = shows.length > 0 && shows.every((s) => s.ok);
|
|
1257
|
+
return {
|
|
1258
|
+
applies,
|
|
1259
|
+
ok: all.length === 0,
|
|
1260
|
+
components,
|
|
1261
|
+
findings: all,
|
|
1262
|
+
checks: {
|
|
1263
|
+
...checks,
|
|
1264
|
+
build: { ok: broken.length === 0, pages: build },
|
|
1265
|
+
newItemShows: { ok: missing.length ? false : proven ? true : null, collections: shows },
|
|
1266
|
+
binds: adapter.markers ? bindings : { bound: 0, unbound: null, reason: "no-identity" },
|
|
1267
|
+
},
|
|
1268
|
+
render: plans,
|
|
1269
|
+
};
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
// ── Item identity in a preview render (R3e) ───────────────────────────────
|
|
1273
|
+
//
|
|
1274
|
+
// Clicking a price on a card in the CMS's preview selects that item's price field; selecting the
|
|
1275
|
+
// field shows where it is on the page. Two sources of identity, neither trusted alone:
|
|
1276
|
+
//
|
|
1277
|
+
// the item from the data: in a preview render only (never a build, never Live), every item's
|
|
1278
|
+
// texts carry their collection, id and place, written into the value itself with
|
|
1279
|
+
// Unicode tag characters, which no font draws. The site's own reader reads the item as
|
|
1280
|
+
// it is, so the value reaches the page through whatever component and framework, and a
|
|
1281
|
+
// related item read from another collection carries its own.
|
|
1282
|
+
// the field from the code: the fields the page's components read (`fieldUse`, `componentsOn`),
|
|
1283
|
+
// and the formatting they declare (`formatsIn`).
|
|
1284
|
+
//
|
|
1285
|
+
// A value with no tag of its own (a number, an image's address) gets its item from the nearest
|
|
1286
|
+
// element around it that shows one item's tagged values, and no other item's: the card. What cannot
|
|
1287
|
+
// be resolved gets plan-editable's reason, never a pick: `computed` (fields joined into one text),
|
|
1288
|
+
// `ambiguous` (more than one field could be it), `unproven` (traced, but what the page shows is not
|
|
1289
|
+
// the field's value as a known transformation gives it). An element whose item is certain carries it
|
|
1290
|
+
// even without a field: a click on it opens the item.
|
|
1291
|
+
|
|
1292
|
+
const TAG_OPEN = "\u{E0001}";
|
|
1293
|
+
const TAG_CLOSE = "\u{E007F}";
|
|
1294
|
+
const TAG = /\u{E0001}([\u{E0020}-\u{E007E}]*)\u{E007F}/gu;
|
|
1295
|
+
/** Any tag character, also one a value cut in two left behind. */
|
|
1296
|
+
const TAG_CHAR = /[\u{E0000}-\u{E007F}]/gu;
|
|
1297
|
+
|
|
1298
|
+
/** The field types whose values carry a tag: free text. A slug, an option, an address, a date or a number is what code links, compares, sorts or formats by, and is never changed. */
|
|
1299
|
+
export const TAGGED_TYPES = ["text", "inline", "longtext"];
|
|
1300
|
+
|
|
1301
|
+
/**
|
|
1302
|
+
* A value's tag: its collection, item and place, as tag characters (ASCII only; null otherwise).
|
|
1303
|
+
* @param {{ collection: string, id: string, pointer: string }} ref
|
|
1304
|
+
*/
|
|
1305
|
+
export function tagOf({ collection, id, pointer }) {
|
|
1306
|
+
const plain = `${collection}|${id}|${pointer}`;
|
|
1307
|
+
if (!/^[\x20-\x7e]+$/.test(plain) || collection.includes("|") || id.includes("|")) return null;
|
|
1308
|
+
return TAG_OPEN + [...plain].map((c) => String.fromCodePoint(0xe0000 + c.charCodeAt(0))).join("") + TAG_CLOSE;
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
/** The tags in a text, in order. @returns {{ collection: string, id: string, pointer: string }[]} */
|
|
1312
|
+
export function tagsIn(text) {
|
|
1313
|
+
const out = [];
|
|
1314
|
+
for (const m of String(text ?? "").matchAll(TAG)) {
|
|
1315
|
+
const plain = [...m[1]].map((c) => String.fromCharCode(c.codePointAt(0) - 0xe0000)).join("");
|
|
1316
|
+
const [collection, id, ...rest] = plain.split("|");
|
|
1317
|
+
const pointer = rest.join("|");
|
|
1318
|
+
if (collection && id && pointer.startsWith("/")) out.push({ collection, id, pointer });
|
|
1319
|
+
}
|
|
1320
|
+
return out;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
/** A text without its tags (and any tag character left over, or half of one a cut left). */
|
|
1324
|
+
export const untag = (text) => String(text ?? "").replace(TAG, "").replace(TAG_CHAR, "").replace(/\uDB40(?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC01-\uDC7F]/g, "");
|
|
1325
|
+
|
|
1326
|
+
/**
|
|
1327
|
+
* An item's data with each free-text value tagged (`TAGGED_TYPES`, by the collection's fields, in
|
|
1328
|
+
* groups and lists too): what a preview render lays over the version, never committed. A text that
|
|
1329
|
+
* is an address, or already holds a tag character, is left as it is.
|
|
1330
|
+
* @param {{ collection: string, id: string, data: unknown, fields?: FieldInput[] }} input
|
|
1331
|
+
*/
|
|
1332
|
+
export function tagItem({ collection, id, data, fields = [] }) {
|
|
1333
|
+
const esc = (k) => k.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
1334
|
+
const walk = (value, pointer, field, picture = false) => {
|
|
1335
|
+
if (typeof value === "string") {
|
|
1336
|
+
const v = value.trim();
|
|
1337
|
+
if (!field || !TAGGED_TYPES.includes(field.type) || !v || /[\u{E0000}-\u{E007F}]/u.test(value) || /^(?:[a-z][a-z0-9+.-]*:|\/|#|www\.)/i.test(v)) return value;
|
|
1338
|
+
// A picture's own texts (its alt text, its focal point "20% 80%") and a value with no letter in
|
|
1339
|
+
// it (a number, a percentage, a coordinate) are what code reads, parses or puts in an attribute,
|
|
1340
|
+
// never prose: a tag would break them (a focal point parsed strictly fell back to the middle, 16-11).
|
|
1341
|
+
if (picture || !/\p{L}/u.test(v)) return value;
|
|
1342
|
+
const tag = tagOf({ collection, id, pointer });
|
|
1343
|
+
return tag ? value + tag : value;
|
|
1344
|
+
}
|
|
1345
|
+
if (Array.isArray(value)) return field?.type === "list" ? value.map((v, i) => walk(v, `${pointer}/${i}`, { type: "group", fields: field.fields ?? [] })) : value;
|
|
1346
|
+
if (value && typeof value === "object" && field && (field.type === "group" || field.type === "item")) {
|
|
1347
|
+
// A group beside a picture is the picture's: { src, alt, positie } (as entries.ts typedText reads it).
|
|
1348
|
+
const pictureGroup = field.type === "group" && (field.fields ?? []).some((f) => f.type === "image");
|
|
1349
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, walk(v, `${pointer}/${esc(k)}`, (field.fields ?? []).find((f) => f.key === k) ?? null, pictureGroup)]));
|
|
1350
|
+
}
|
|
1351
|
+
return value;
|
|
1352
|
+
};
|
|
1353
|
+
return walk(data, "", { type: "item", fields });
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
/** Literal arguments only: a string, a number, true or false; an object of those; a list of strings. */
|
|
1357
|
+
function literalValue(src) {
|
|
1358
|
+
const t = src.trim();
|
|
1359
|
+
if (t === "undefined") return { value: undefined };
|
|
1360
|
+
const s = fixed(t);
|
|
1361
|
+
if (s !== null) return { value: s };
|
|
1362
|
+
if (/^-?\d+(?:\.\d+)?$/.test(t)) return { value: Number(t) };
|
|
1363
|
+
if (t === "true" || t === "false") return { value: t === "true" };
|
|
1364
|
+
if (t.startsWith("[")) {
|
|
1365
|
+
const inner = balanced(t, 0);
|
|
1366
|
+
if (inner === null) return null;
|
|
1367
|
+
const parts = argumentsOf(inner).map((p) => fixed(p));
|
|
1368
|
+
return parts.every((p) => p !== null) ? { value: parts } : null;
|
|
1369
|
+
}
|
|
1370
|
+
if (t.startsWith("{")) {
|
|
1371
|
+
const inner = balanced(t, 0);
|
|
1372
|
+
if (inner === null) return null;
|
|
1373
|
+
const out = {};
|
|
1374
|
+
for (const part of argumentsOf(inner)) {
|
|
1375
|
+
const m = /^\s*(?:([A-Za-z_$][\w$]*)|"([^"]*)"|'([^']*)')\s*:\s*([\s\S]+)$/.exec(part);
|
|
1376
|
+
if (!m) return null;
|
|
1377
|
+
const v = literalValue(m[4]);
|
|
1378
|
+
if (!v || (v.value && typeof v.value === "object")) return null;
|
|
1379
|
+
out[m[1] ?? m[2] ?? m[3]] = v.value;
|
|
1380
|
+
}
|
|
1381
|
+
return { value: out };
|
|
1382
|
+
}
|
|
1383
|
+
return null;
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
/**
|
|
1387
|
+
* The number formatting the code declares, as the adapter knows it (`formats`): a formatter made or
|
|
1388
|
+
* called with literal arguments only, which can be run again here to verify what the page shows
|
|
1389
|
+
* (`new Intl.NumberFormat("nl-NL", { style: "currency", currency: "EUR" })`, `.toLocaleString("en-US")`,
|
|
1390
|
+
* `.toFixed(2)`). A locale or option from a variable is not declared, and a value shown through it
|
|
1391
|
+
* stays `unproven`.
|
|
1392
|
+
* @param {{ files: SiteFile[], adapter: BindingAdapter }} input
|
|
1393
|
+
* @returns {({ kind: "intl", locale?: string | string[], options?: Record<string, unknown>, file: string } | { kind: "fixed", digits: number, file: string })[]}
|
|
1394
|
+
*/
|
|
1395
|
+
export function formatsIn({ files, adapter }) {
|
|
1396
|
+
const allowed = new Set(adapter?.formats ?? []);
|
|
1397
|
+
const out = [];
|
|
1398
|
+
const seen = new Set();
|
|
1399
|
+
const add = (f) => {
|
|
1400
|
+
const { file: _file, ...rest } = f;
|
|
1401
|
+
const key = JSON.stringify(rest);
|
|
1402
|
+
if (seen.has(key)) return;
|
|
1403
|
+
try {
|
|
1404
|
+
if (f.kind === "intl") new Intl.NumberFormat(f.locale, f.options).format(1);
|
|
1405
|
+
else (1).toFixed(f.digits);
|
|
1406
|
+
} catch {
|
|
1407
|
+
return;
|
|
1408
|
+
}
|
|
1409
|
+
seen.add(key);
|
|
1410
|
+
out.push(f);
|
|
1411
|
+
};
|
|
1412
|
+
for (const file of files ?? []) {
|
|
1413
|
+
if (!isCode(file.path)) continue;
|
|
1414
|
+
const code = stripComments(file.text ?? "");
|
|
1415
|
+
if (allowed.has("intl")) {
|
|
1416
|
+
for (const m of code.matchAll(/(?:\bnew\s+Intl\.NumberFormat|\.toLocaleString)\s*\(/g)) {
|
|
1417
|
+
const inner = balanced(code, m.index + m[0].length - 1);
|
|
1418
|
+
if (inner === null) continue;
|
|
1419
|
+
const args = argumentsOf(inner);
|
|
1420
|
+
if (args.length > 2) continue;
|
|
1421
|
+
const locale = args[0] === undefined ? { value: undefined } : literalValue(args[0]);
|
|
1422
|
+
const options = args[1] === undefined ? { value: undefined } : literalValue(args[1]);
|
|
1423
|
+
if (!locale || !options) continue;
|
|
1424
|
+
if (locale.value !== undefined && typeof locale.value !== "string" && !Array.isArray(locale.value)) continue;
|
|
1425
|
+
if (options.value !== undefined && (typeof options.value !== "object" || Array.isArray(options.value))) continue;
|
|
1426
|
+
add({ kind: "intl", ...(locale.value !== undefined ? { locale: locale.value } : {}), ...(options.value !== undefined ? { options: options.value } : {}), file: file.path });
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
if (allowed.has("fixed")) for (const m of code.matchAll(/\.toFixed\(\s*(\d{1,2})\s*\)/g)) add({ kind: "fixed", digits: Number(m[1]), file: file.path });
|
|
1430
|
+
}
|
|
1431
|
+
return out;
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
/** A number as each declared format shows it, and as it is. */
|
|
1435
|
+
function renderings(value, formats) {
|
|
1436
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
1437
|
+
const out = [{ text: String(value), format: null }];
|
|
1438
|
+
for (const f of formats) {
|
|
1439
|
+
try {
|
|
1440
|
+
out.push({ text: f.kind === "intl" ? new Intl.NumberFormat(f.locale, f.options).format(value) : value.toFixed(f.digits), format: f });
|
|
1441
|
+
} catch {}
|
|
1442
|
+
}
|
|
1443
|
+
return out;
|
|
1444
|
+
}
|
|
1445
|
+
if (typeof value === "string" && value.trim()) return [{ text: value, format: null }];
|
|
1446
|
+
return [];
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
/**
|
|
1450
|
+
* Whether an image's address shows a stored image path: as it is, or through one of the adapter's
|
|
1451
|
+
* image transforms (`images`). Compared by the path's end, never by a guess at the file's contents.
|
|
1452
|
+
*/
|
|
1453
|
+
export function imageShows(src, value, adapter) {
|
|
1454
|
+
if (typeof src !== "string" || typeof value !== "string" || !src || !value.trim()) return false;
|
|
1455
|
+
const pathOf = (p) => {
|
|
1456
|
+
try {
|
|
1457
|
+
return new URL(p, "http://site.invalid/").pathname;
|
|
1458
|
+
} catch {
|
|
1459
|
+
return p.split(/[?#]/)[0];
|
|
1460
|
+
}
|
|
1461
|
+
};
|
|
1462
|
+
const tail = (p) => {
|
|
1463
|
+
let d = p;
|
|
1464
|
+
try {
|
|
1465
|
+
d = decodeURIComponent(p);
|
|
1466
|
+
} catch {}
|
|
1467
|
+
return d.split(/[?#]/)[0].replace(/^(?:\.{1,2}\/)+/, "").replace(/^\/+/, "").replace(/^(?:public|src)\//, "");
|
|
1468
|
+
};
|
|
1469
|
+
const want = tail(value);
|
|
1470
|
+
if (!want) return false;
|
|
1471
|
+
const ends = (p) => {
|
|
1472
|
+
const t = tail(p);
|
|
1473
|
+
return t === want || t.endsWith(`/${want}`);
|
|
1474
|
+
};
|
|
1475
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(value) && src === value) return true;
|
|
1476
|
+
if (ends(pathOf(src))) return true;
|
|
1477
|
+
for (const t of adapter?.images ?? []) {
|
|
1478
|
+
if ("query" in t) {
|
|
1479
|
+
let param = null;
|
|
1480
|
+
try {
|
|
1481
|
+
param = new URL(src, "http://site.invalid/").searchParams.get(t.query);
|
|
1482
|
+
} catch {}
|
|
1483
|
+
if (param && ends(pathOf(param))) return true;
|
|
1484
|
+
} else if ("hashed" in t) {
|
|
1485
|
+
const path = pathOf(src);
|
|
1486
|
+
const name = want.split("/").pop() ?? "";
|
|
1487
|
+
const stem = name.replace(/\.[^.]+$/, "");
|
|
1488
|
+
if (stem && path.startsWith(t.hashed) && new RegExp(`^${escapeText(stem)}\\.[\\w-]{6,}\\.\\w+$`).test(path.split("/").pop() ?? "")) return true;
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
return false;
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
const escapeAttr = (v) => String(v).replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
1495
|
+
/** Never an item's card: the whole page. */
|
|
1496
|
+
const WHOLE_PAGE = /^(#root|html|body|main)$/;
|
|
1497
|
+
|
|
1498
|
+
/**
|
|
1499
|
+
* The elements of a preview render bound to item and field (R3e), from the tags its items carry
|
|
1500
|
+
* (`tagItem`) and the code: `uses` the fields each collection's components on the page read
|
|
1501
|
+
* (`fieldUse`), `formats` the formatting they declare (`formatsIn`). Answers the render without its
|
|
1502
|
+
* tags, each element marked for the preview (`data-unbase-cms` its item, `data-unbase-cms-field` its
|
|
1503
|
+
* field as `file#pointer`, `data-unbase-cms-reason` why not; a text bound to its field also
|
|
1504
|
+
* `data-cm-veld`, so what is typed shows there), and what it found:
|
|
1505
|
+
*
|
|
1506
|
+
* - a text with one tag that is the element's whole text, on a field the code reads: bound. Its tag
|
|
1507
|
+
* on a field the code does not read, or a text that is not the value (a template around it, a
|
|
1508
|
+
* transformation in code): `unproven`, or `computed` when the value is inside it;
|
|
1509
|
+
* - two tags in one text (a title joined from two fields), or two items': `computed`;
|
|
1510
|
+
* - a value with no tag (a number, an option, a date) in a card: bound when exactly one field the
|
|
1511
|
+
* code reads shows it, as it is or through a declared format; `ambiguous` when more do; `unproven`
|
|
1512
|
+
* when a number of the item is in it but no declared format gives the text (a formatted price
|
|
1513
|
+
* the adapter does not know). Outside a card no item is certain: listed with its reason only;
|
|
1514
|
+
* - an image: through the image fields the code reads and the adapter's image transforms.
|
|
1515
|
+
*
|
|
1516
|
+
* The card is the nearest element around a value that shows one item's tagged values and no other
|
|
1517
|
+
* item's, never the whole page; the largest such element is marked with its item, so a click on the
|
|
1518
|
+
* card's background opens it.
|
|
1519
|
+
* @param {{ html: string, collections: (CollectionInput & { items?: (Item & { file?: string })[] })[], adapter: BindingAdapter, uses?: Record<string, string[]>, formats?: ReturnType<typeof formatsIn> }} input
|
|
1520
|
+
*/
|
|
1521
|
+
export function itemsIn({ html, collections, adapter, uses = {}, formats = [] }) {
|
|
1522
|
+
/** @type {Map<string, { collection: string, id: string, file: string | null, data: any, fields: FieldInput[] }>} */
|
|
1523
|
+
const items = new Map();
|
|
1524
|
+
for (const c of collections) for (const it of c.items ?? []) items.set(`${c.name}/${it.id}`, { collection: c.name, id: it.id, file: it.file ?? null, data: it.data, fields: c.fields ?? [] });
|
|
1525
|
+
const reads = (collection, key) => (uses[collection] ?? []).includes(key);
|
|
1526
|
+
const top = (pointer) => (pointer.split("/")[1] ?? "").replace(/~1/g, "/").replace(/~0/g, "~");
|
|
1527
|
+
const root = parseHtml(html);
|
|
1528
|
+
|
|
1529
|
+
// Which items each element shows, in its text and its attributes (an image's alt), however deep.
|
|
1530
|
+
const under = new Map();
|
|
1531
|
+
const collect = (n) => {
|
|
1532
|
+
const set = new Set();
|
|
1533
|
+
for (const v of Object.values(n.attrs)) for (const r of tagsIn(v)) set.add(`${r.collection}/${r.id}`);
|
|
1534
|
+
for (const c of n.children) {
|
|
1535
|
+
if (typeof c === "string") {
|
|
1536
|
+
if (!NO_TEXT.test(n.tag)) for (const r of tagsIn(c)) set.add(`${r.collection}/${r.id}`);
|
|
1537
|
+
} else for (const k of collect(c)) set.add(k);
|
|
1538
|
+
}
|
|
1539
|
+
under.set(n, set);
|
|
1540
|
+
return set;
|
|
1541
|
+
};
|
|
1542
|
+
collect(root);
|
|
1543
|
+
/**
|
|
1544
|
+
* Whose card an element is: the one item it shows, leaving out another item's block inside it (a
|
|
1545
|
+
* related product in its own element, the only place that item shows here). Null for none, or
|
|
1546
|
+
* for more than one.
|
|
1547
|
+
*/
|
|
1548
|
+
const ownerOf = (n) => {
|
|
1549
|
+
const s = under.get(n);
|
|
1550
|
+
if (!s?.size) return null;
|
|
1551
|
+
if (s.size === 1) return [...s][0];
|
|
1552
|
+
const parts = n.children.map((c) => (typeof c === "string" ? new Set(NO_TEXT.test(n.tag) ? [] : tagsIn(c).map((r) => `${r.collection}/${r.id}`)) : under.get(c)));
|
|
1553
|
+
const nested = [...s].filter((k) => {
|
|
1554
|
+
const holding = parts.filter((p) => p?.has(k));
|
|
1555
|
+
return holding.length === 1 && holding[0].size === 1;
|
|
1556
|
+
});
|
|
1557
|
+
const left = [...s].filter((k) => !nested.includes(k));
|
|
1558
|
+
return left.length === 1 ? left[0] : null;
|
|
1559
|
+
};
|
|
1560
|
+
/** The item a value with no tag of its own is in: the nearest element around it that shows tagged values, when it is one item's card and not the whole page. */
|
|
1561
|
+
const cardItem = (node) => {
|
|
1562
|
+
for (let n = node; n; n = n.parent) {
|
|
1563
|
+
if (under.get(n)?.size) return WHOLE_PAGE.test(n.tag) ? null : ownerOf(n);
|
|
1564
|
+
}
|
|
1565
|
+
return null;
|
|
1566
|
+
};
|
|
1567
|
+
|
|
1568
|
+
const stamps = new Map();
|
|
1569
|
+
const stamp = (node, attrs) => stamps.set(node, { ...(stamps.get(node) ?? {}), ...attrs });
|
|
1570
|
+
const elements = [];
|
|
1571
|
+
const keyOf = (it, pointer) => (it.file ? `${it.file}#${pointer}` : null);
|
|
1572
|
+
const list = (e, found, it) => {
|
|
1573
|
+
elements.push({ at: e.at, tag: e.node.tag, text: found.text ?? e.text, ...found, ...(it ? { item: { collection: it.collection, id: it.id } } : {}) });
|
|
1574
|
+
if (!it) return;
|
|
1575
|
+
const attrs = { "data-unbase-cms": `${it.collection}/${it.id}` };
|
|
1576
|
+
if (found.binding && found.key) attrs["data-unbase-cms-field"] = found.key;
|
|
1577
|
+
// comment-modus 1.11 reads data-cm-field; LEGACY-NL: older layers and Unbase's bridge before it read data-cm-veld.
|
|
1578
|
+
if (found.binding && found.key && found.exact) attrs["data-cm-field"] = attrs["data-cm-veld"] = found.key;
|
|
1579
|
+
if (found.reason) attrs["data-unbase-cms-reason"] = found.reason;
|
|
1580
|
+
stamp(e.node, attrs);
|
|
1581
|
+
};
|
|
1582
|
+
const bind = (it, pointer, extra = {}) => ({ binding: { collection: it.collection, item: it.id, field: pointer }, key: keyOf(it, pointer), ...extra });
|
|
1583
|
+
const ownText = (node) => node.children.filter((c) => typeof c === "string").join("");
|
|
1584
|
+
const escKey = (k) => k.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
1585
|
+
/**
|
|
1586
|
+
* An item's fields of one type with their values, by pointer: a top-level field, or one in a group
|
|
1587
|
+
* at the top (a picture kept as `{ src, alt, positie }`: its address at `/image/src`). Only fields the
|
|
1588
|
+
* code reads (by their top-level key). A picture's value may also be an object with its `src`.
|
|
1589
|
+
*/
|
|
1590
|
+
const slots = (it, type) => {
|
|
1591
|
+
const out = [];
|
|
1592
|
+
for (const f of it.fields) {
|
|
1593
|
+
if (!reads(it.collection, f.key)) continue;
|
|
1594
|
+
if (f.type === type) out.push({ pointer: `/${escKey(f.key)}`, value: it.data?.[f.key] });
|
|
1595
|
+
else if (f.type === "group")
|
|
1596
|
+
for (const g of f.fields ?? []) if (g.type === type) out.push({ pointer: `/${escKey(f.key)}/${escKey(g.key)}`, value: it.data?.[f.key]?.[g.key] });
|
|
1597
|
+
}
|
|
1598
|
+
return out.map((s) => ({ ...s, value: s.value && typeof s.value === "object" && typeof s.value.src === "string" ? s.value.src : s.value }));
|
|
1599
|
+
};
|
|
1600
|
+
/** The item an element with no tag of its own belongs to: one item's tag in its attributes, else its card's. */
|
|
1601
|
+
const itemOfNode = (node) => {
|
|
1602
|
+
const own = new Set(Object.values(node.attrs).flatMap((v) => tagsIn(v).map((r) => `${r.collection}/${r.id}`)));
|
|
1603
|
+
return own.size === 1 ? [...own][0] : own.size ? null : cardItem(node.parent);
|
|
1604
|
+
};
|
|
1605
|
+
|
|
1606
|
+
for (const e of elementsOf(root)) {
|
|
1607
|
+
const node = e.node;
|
|
1608
|
+
// A link's address from a link field (docs/plan-live-preview.md LP2): marked on the link itself,
|
|
1609
|
+
// apart from the text in it, which binds as any text does.
|
|
1610
|
+
if (node.tag === "a" && typeof node.attrs.href === "string" && node.attrs.href.trim()) {
|
|
1611
|
+
const href = untag(node.attrs.href).trim();
|
|
1612
|
+
const itemKey = itemOfNode(node);
|
|
1613
|
+
const it = itemKey ? items.get(itemKey) : null;
|
|
1614
|
+
const fit = it ? slots(it, "link").filter((s) => typeof s.value === "string" && s.value.trim() === href) : [];
|
|
1615
|
+
if (it && fit.length === 1 && it.file) stamp(node, { "data-unbase-cms": `${it.collection}/${it.id}`, "data-unbase-cms-href": keyOf(it, fit[0].pointer) });
|
|
1616
|
+
}
|
|
1617
|
+
if (node.tag === "img") {
|
|
1618
|
+
const src = node.attrs.src ?? "";
|
|
1619
|
+
if (!src) continue;
|
|
1620
|
+
const itemKey = itemOfNode(node);
|
|
1621
|
+
const showing = (it) => slots(it, "image").filter((s) => imageShows(src, s.value, adapter)).map((s) => s.pointer);
|
|
1622
|
+
const it = itemKey ? items.get(itemKey) : null;
|
|
1623
|
+
if (it) {
|
|
1624
|
+
const fit = showing(it);
|
|
1625
|
+
if (fit.length === 1) list(e, { text: "", ...bind(it, fit[0], { image: true }) }, it);
|
|
1626
|
+
else if (fit.length > 1) list(e, { text: "", reason: "ambiguous", candidates: fit.length }, it);
|
|
1627
|
+
else if (slots(it, "image").length) list(e, { text: "", reason: "unproven" }, it);
|
|
1628
|
+
} else {
|
|
1629
|
+
const anywhere = [...items.values()].flatMap((x) => showing(x));
|
|
1630
|
+
if (anywhere.length) list(e, { text: "", reason: anywhere.length > 1 ? "ambiguous" : "unproven", candidates: anywhere.length }, null);
|
|
1631
|
+
}
|
|
1632
|
+
continue;
|
|
1633
|
+
}
|
|
1634
|
+
const visible = collapse(untag(e.text));
|
|
1635
|
+
if (!visible) continue;
|
|
1636
|
+
// Of nested elements with one text, the innermost; and only an element with text of its own.
|
|
1637
|
+
if (node.children.some((c) => typeof c !== "string" && !NO_TEXT.test(c.tag) && wholeText(c) === e.text)) continue;
|
|
1638
|
+
const own = ownText(node);
|
|
1639
|
+
if (!collapse(untag(own))) continue;
|
|
1640
|
+
const refs = tagsIn(own);
|
|
1641
|
+
if (refs.length) {
|
|
1642
|
+
const distinct = [...new Map(refs.map((r) => [`${r.collection}/${r.id}#${r.pointer}`, r])).values()];
|
|
1643
|
+
const itemKeys = new Set(distinct.map((r) => `${r.collection}/${r.id}`));
|
|
1644
|
+
if (itemKeys.size > 1) {
|
|
1645
|
+
list(e, { text: visible, reason: "computed", candidates: distinct.length }, null);
|
|
1646
|
+
continue;
|
|
1647
|
+
}
|
|
1648
|
+
const r = distinct[0];
|
|
1649
|
+
const it = items.get(`${r.collection}/${r.id}`);
|
|
1650
|
+
if (!it) continue;
|
|
1651
|
+
if (distinct.length > 1) {
|
|
1652
|
+
list(e, { text: visible, reason: "computed", fields: distinct.map((x) => x.pointer) }, it);
|
|
1653
|
+
continue;
|
|
1654
|
+
}
|
|
1655
|
+
const value = at(it.data, r.pointer);
|
|
1656
|
+
const shown = typeof value === "string" ? collapse(untag(value)) : null;
|
|
1657
|
+
const marked = { collection: it.collection, item: it.id, field: r.pointer };
|
|
1658
|
+
if (shown !== null && shown === visible && reads(it.collection, top(r.pointer))) list(e, { text: visible, ...bind(it, r.pointer, { exact: true }) }, it);
|
|
1659
|
+
else list(e, { text: visible, reason: shown && shown !== visible && visible.includes(shown) ? "computed" : "unproven", marked }, it);
|
|
1660
|
+
continue;
|
|
1661
|
+
}
|
|
1662
|
+
// A value with no tag of its own: a number, an option, a date.
|
|
1663
|
+
const plain = (it) =>
|
|
1664
|
+
it.fields
|
|
1665
|
+
.filter((f) => !TAGGED_TYPES.includes(f.type) && f.type !== "image" && reads(it.collection, f.key))
|
|
1666
|
+
.flatMap((f) => {
|
|
1667
|
+
const r = renderings(it.data?.[f.key], formats).find((x) => collapse(x.text) === visible);
|
|
1668
|
+
return r ? [{ key: f.key, format: r.format }] : [];
|
|
1669
|
+
});
|
|
1670
|
+
const itemKey = cardItem(node);
|
|
1671
|
+
const it = itemKey ? items.get(itemKey) : null;
|
|
1672
|
+
if (it) {
|
|
1673
|
+
const fit = plain(it);
|
|
1674
|
+
if (fit.length === 1) {
|
|
1675
|
+
const f = fit[0];
|
|
1676
|
+
list(e, { text: visible, ...bind(it, `/${f.key}`, f.format ? { format: f.format.kind === "intl" ? `intl ${JSON.stringify([f.format.locale ?? null, f.format.options ?? null])}` : `fixed ${f.format.digits}` } : { exact: true }) }, it);
|
|
1677
|
+
} else if (fit.length > 1) list(e, { text: visible, reason: "ambiguous", candidates: fit.length }, it);
|
|
1678
|
+
else {
|
|
1679
|
+
// A number of the item inside the text, shown through a formatting no adapter declares: never guessed.
|
|
1680
|
+
const digits = visible.replace(/\D/g, "");
|
|
1681
|
+
const inside = it.fields.some((f) => {
|
|
1682
|
+
const v = it.data?.[f.key];
|
|
1683
|
+
return f.type !== "image" && reads(it.collection, f.key) && typeof v === "number" && Math.abs(v) >= 10 && digits.includes(String(Math.trunc(Math.abs(v))));
|
|
1684
|
+
});
|
|
1685
|
+
if (inside) list(e, { text: visible, reason: "unproven" }, it);
|
|
1686
|
+
}
|
|
1687
|
+
} else {
|
|
1688
|
+
const anywhere = [...items.values()].flatMap((x) => plain(x));
|
|
1689
|
+
if (anywhere.length) list(e, { text: visible, reason: anywhere.length > 1 ? "ambiguous" : "unproven", candidates: anywhere.length }, null);
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
// Each item's card: the largest element around its values that shows no other item's, never the whole page.
|
|
1694
|
+
let cards = 0;
|
|
1695
|
+
for (const [node, attrs] of [...stamps]) {
|
|
1696
|
+
const key = attrs["data-unbase-cms"];
|
|
1697
|
+
let card = null;
|
|
1698
|
+
for (let n = node; n && !WHOLE_PAGE.test(n.tag); n = n.parent) {
|
|
1699
|
+
if (!under.get(n)?.size) continue;
|
|
1700
|
+
if (ownerOf(n) === key) card = n;
|
|
1701
|
+
else break;
|
|
1702
|
+
}
|
|
1703
|
+
if (card && card !== node && !stamps.has(card)) {
|
|
1704
|
+
stamp(card, { "data-unbase-cms": key });
|
|
1705
|
+
cards++;
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
// The marks written into the open tags, from the end so each place stays where it was; then every tag goes.
|
|
1710
|
+
const edits = [...stamps]
|
|
1711
|
+
.filter(([n]) => typeof n.end === "number")
|
|
1712
|
+
.map(([n, attrs]) => ({ at: n.end, text: Object.entries(attrs).filter(([k]) => !(k in n.attrs)).map(([k, v]) => ` ${k}="${escapeAttr(v)}"`).join("") }))
|
|
1713
|
+
.filter((x) => x.text)
|
|
1714
|
+
.sort((a, b) => b.at - a.at);
|
|
1715
|
+
let out = html;
|
|
1716
|
+
for (const x of edits) {
|
|
1717
|
+
const i = out[x.at - 1] === "/" ? x.at - 1 : x.at;
|
|
1718
|
+
out = out.slice(0, i) + x.text + out.slice(i);
|
|
1719
|
+
}
|
|
1720
|
+
const bound = elements.filter((e) => e.binding).length;
|
|
1721
|
+
return { html: untag(out), elements, bound, unbound: elements.length - bound, cards };
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
// ── A field a change adds: does it show, and bind? (docs/plan-live-preview.md LP4) ──
|
|
1725
|
+
|
|
1726
|
+
/** The kind of a field as the check below proves it: free text by its token, a picture by its file. */
|
|
1727
|
+
const addedKind = (f) =>
|
|
1728
|
+
TAGGED_TYPES.includes(f.type) ? "text" : f.type === "image" || (f.type === "group" && (f.fields ?? []).some((g) => g.type === "image")) ? "image" : null;
|
|
1729
|
+
|
|
1730
|
+
/**
|
|
1731
|
+
* Whether the fields a change adds to a collection show on the pages its components are on, and
|
|
1732
|
+
* bind there to their field (Build's gate, LP4; ADR-0030 points 4 and 6). The pages are rendered with
|
|
1733
|
+
* the gate's test item laid over (`renders`: `{ path, html }` with the item, its texts tagged, never
|
|
1734
|
+
* committed). A text field shows when its test value is on a page, a picture field when the test
|
|
1735
|
+
* picture is; either binds when `itemsIn` gives that element the field. `live`: a text bound to its
|
|
1736
|
+
* field exactly, so what is typed in the CMS shows on the page at once. Other types are not proven
|
|
1737
|
+
* here (`shows: null`, `reason: "unchecked"`); a page that did not render proves nothing.
|
|
1738
|
+
* @param {{ collection: string, added: FieldInput[], draft: { id: string, file?: string, data: any }, renders: { path: string, html?: string | null }[], collections: (CollectionInput & { items?: Item[] })[], adapter: BindingAdapter, uses?: Record<string, string[]>, formats?: ReturnType<typeof formatsIn> }} input
|
|
1739
|
+
* @returns {{ key: string, type: string, shows: boolean | null, bound: boolean, live: boolean, pages: string[], reason?: string }[]}
|
|
1740
|
+
*/
|
|
1741
|
+
export function newFieldShows({ collection, added, draft, renders, collections, adapter, uses = {}, formats = [] }) {
|
|
1742
|
+
const withItem = collections.map((c) => (c.name === collection ? { ...c, items: [...(c.items ?? []).filter((i) => i.id !== draft.id), { id: draft.id, file: draft.file, data: draft.data }] } : c));
|
|
1743
|
+
const pages = renders.filter((r) => typeof r.html === "string" && r.html.length > 0);
|
|
1744
|
+
const bound = new Map(pages.map((p) => [p.path, itemsIn({ html: p.html, collections: withItem, adapter, uses, formats }).elements]));
|
|
1745
|
+
const textOfPage = new Map(pages.map((p) => [p.path, untag(wholeText(parseHtml(p.html)))]));
|
|
1746
|
+
const stemOfPath = (p) => (String(p ?? "").split(/[?#]/)[0].split("/").pop() ?? "").replace(/\.[^.]+$/, "").toLowerCase();
|
|
1747
|
+
const pictureAt = (f) => {
|
|
1748
|
+
const v = draft.data?.[f.key];
|
|
1749
|
+
if (f.type === "image") return typeof v === "string" ? v : typeof v?.src === "string" ? v.src : null;
|
|
1750
|
+
const sub = (f.fields ?? []).find((g) => g.type === "image");
|
|
1751
|
+
return sub && typeof v?.[sub.key] === "string" ? v[sub.key] : null;
|
|
1752
|
+
};
|
|
1753
|
+
return added.map((f) => {
|
|
1754
|
+
const kind = addedKind(f);
|
|
1755
|
+
const base = { key: f.key, type: f.type, bound: false, live: false, pages: [] };
|
|
1756
|
+
if (!kind) return { ...base, shows: null, reason: "unchecked" };
|
|
1757
|
+
const value = kind === "text" ? draft.data?.[f.key] : pictureAt(f);
|
|
1758
|
+
if (typeof value !== "string" || !value.trim()) return { ...base, shows: null, reason: "no-test-value" };
|
|
1759
|
+
if (!pages.length) return { ...base, shows: null, reason: "not-rendered" };
|
|
1760
|
+
const shown = [];
|
|
1761
|
+
let isBound = false;
|
|
1762
|
+
let live = false;
|
|
1763
|
+
for (const p of pages) {
|
|
1764
|
+
const mine = (bound.get(p.path) ?? []).filter((e) => e.binding && e.binding.collection === collection && e.binding.item === draft.id && (e.binding.field === `/${f.key}` || e.binding.field.startsWith(`/${f.key}/`)));
|
|
1765
|
+
const seen =
|
|
1766
|
+
mine.length > 0 ||
|
|
1767
|
+
(kind === "text"
|
|
1768
|
+
? (textOfPage.get(p.path) ?? "").includes(collapse(untag(value)))
|
|
1769
|
+
: [...p.html.matchAll(/<(?:img|source)\b[^>]*>/gi)].some((m) => m[0].toLowerCase().includes(stemOfPath(value))));
|
|
1770
|
+
if (!seen) continue;
|
|
1771
|
+
shown.push(p.path);
|
|
1772
|
+
if (mine.length) isBound = true;
|
|
1773
|
+
if (mine.some((e) => e.exact)) live = true;
|
|
1774
|
+
}
|
|
1775
|
+
return { ...base, shows: shown.length > 0, bound: isBound, live, pages: shown, ...(shown.length && !isBound ? { reason: "unbound" } : {}) };
|
|
1776
|
+
});
|
|
1777
|
+
}
|