@caelo-cms/shared 0.11.7 → 0.11.9
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/dist/ai-tools.d.ts +2 -2
- package/dist/build-page.d.ts +4 -4
- package/dist/change-journal.d.ts.map +1 -1
- package/dist/change-journal.js +2 -0
- package/dist/change-journal.js.map +1 -1
- package/dist/content.d.ts +6 -6
- package/dist/content.d.ts.map +1 -1
- package/dist/content.js +8 -1
- package/dist/content.js.map +1 -1
- package/dist/lists/fields.d.ts +6 -0
- package/dist/lists/fields.d.ts.map +1 -1
- package/dist/lists/fields.js +70 -7
- package/dist/lists/fields.js.map +1 -1
- package/dist/lists/index.d.ts +1 -0
- package/dist/lists/index.d.ts.map +1 -1
- package/dist/lists/index.js +1 -0
- package/dist/lists/index.js.map +1 -1
- package/dist/lists/list-type.d.ts +28 -6
- package/dist/lists/list-type.d.ts.map +1 -1
- package/dist/lists/list-type.js +43 -15
- package/dist/lists/list-type.js.map +1 -1
- package/dist/lists/overlay-scope.d.ts +68 -0
- package/dist/lists/overlay-scope.d.ts.map +1 -0
- package/dist/lists/overlay-scope.js +56 -0
- package/dist/lists/overlay-scope.js.map +1 -0
- package/dist/lists/query-merge.d.ts +27 -0
- package/dist/lists/query-merge.d.ts.map +1 -0
- package/dist/lists/query-merge.js +53 -0
- package/dist/lists/query-merge.js.map +1 -0
- package/dist/lists/resolve.d.ts +1 -0
- package/dist/lists/resolve.d.ts.map +1 -1
- package/dist/lists/resolve.js +11 -25
- package/dist/lists/resolve.js.map +1 -1
- package/dist/lists/scope.d.ts +4 -1
- package/dist/lists/scope.d.ts.map +1 -1
- package/dist/template-engine.d.ts +23 -0
- package/dist/template-engine.d.ts.map +1 -1
- package/dist/template-engine.js +69 -3
- package/dist/template-engine.js.map +1 -1
- package/dist/version.d.ts +2 -2
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/src/change-journal.ts +3 -0
- package/src/content.test.ts +54 -0
- package/src/content.ts +10 -1
- package/src/lists/fields.ts +88 -7
- package/src/lists/index.ts +1 -0
- package/src/lists/list-type.ts +53 -18
- package/src/lists/lists.test.ts +70 -0
- package/src/lists/overlay-scope.test.ts +140 -0
- package/src/lists/overlay-scope.ts +120 -0
- package/src/lists/pagination.test.ts +15 -0
- package/src/lists/query-merge.ts +84 -0
- package/src/lists/resolve.test.ts +24 -4
- package/src/lists/resolve.ts +12 -33
- package/src/lists/scope.ts +4 -2
- package/src/template-engine-lists.test.ts +27 -1
- package/src/template-engine-memory.test.ts +129 -0
- package/src/template-engine.ts +72 -3
- package/src/version.ts +1 -1
|
@@ -104,14 +104,34 @@ describe("query sources", () => {
|
|
|
104
104
|
const q = query({ page_type: "news", order: "published_desc", limit: 3 }, ["limit"]);
|
|
105
105
|
expect(entries(page(PAGE.home), q, { limit: 1 })).toHaveLength(1);
|
|
106
106
|
const r = page(PAGE.home).resolveField(q, { page_type: "page" }, { contentInstanceId: null });
|
|
107
|
-
expect(r).
|
|
107
|
+
expect(r).toMatchObject({ ok: false, marker: "list-malformed:q[query.page_type]" });
|
|
108
|
+
expect(r.ok ? "" : r.detail).toContain(
|
|
109
|
+
'"page_type" cannot be set per entry or placement — the source\'s editable list is limit.',
|
|
110
|
+
);
|
|
108
111
|
});
|
|
109
112
|
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
// #1281 — the marker names the missing knob and `detail` says the fix
|
|
114
|
+
// (preview_list_source used to blame the `editable` list instead).
|
|
115
|
+
it("an incomplete query (no order / limit) is malformed, never guessed — naming the knob", () => {
|
|
116
|
+
const noOrder = page(PAGE.home).resolveField(query({ page_type: "news" }), undefined, {
|
|
112
117
|
contentInstanceId: null,
|
|
113
118
|
});
|
|
114
|
-
expect(
|
|
119
|
+
expect(noOrder).toMatchObject({ ok: false, marker: "list-malformed:q[query.order]" });
|
|
120
|
+
expect(noOrder.ok ? "" : noOrder.detail).toContain('the query has no "order" — add order (');
|
|
121
|
+
const noLimit = page(PAGE.home).resolveField(
|
|
122
|
+
query({ page_type: "news", order: "title_asc" }),
|
|
123
|
+
undefined,
|
|
124
|
+
{ contentInstanceId: null },
|
|
125
|
+
);
|
|
126
|
+
expect(noLimit).toMatchObject({ ok: false, marker: "list-malformed:q[query.limit]" });
|
|
127
|
+
expect(noLimit.ok ? "" : noLimit.detail).toContain('the query has no "limit"');
|
|
128
|
+
const badLimit = page(PAGE.home).resolveField(
|
|
129
|
+
query({ page_type: "news", order: "title_asc", limit: 3 }, ["limit"]),
|
|
130
|
+
{ limit: 500 },
|
|
131
|
+
{ contentInstanceId: null },
|
|
132
|
+
);
|
|
133
|
+
expect(badLimit).toMatchObject({ ok: false, marker: "list-malformed:q[query.limit]" });
|
|
134
|
+
expect(badLimit.ok ? "" : badLimit.detail).toContain('query knob "limit" is invalid');
|
|
115
135
|
});
|
|
116
136
|
|
|
117
137
|
it("returns only pages of the rendering page's variant", () => {
|
package/src/lists/resolve.ts
CHANGED
|
@@ -34,8 +34,6 @@ import {
|
|
|
34
34
|
type ListTypeDef,
|
|
35
35
|
type NodeDef,
|
|
36
36
|
type PageQuery,
|
|
37
|
-
pageQuerySchema,
|
|
38
|
-
type QueryKnob,
|
|
39
37
|
type SourceDef,
|
|
40
38
|
type SystemSourceName,
|
|
41
39
|
} from "./list-type.js";
|
|
@@ -49,6 +47,7 @@ import {
|
|
|
49
47
|
pagedUrl,
|
|
50
48
|
splitPaginationValue,
|
|
51
49
|
} from "./pagination.js";
|
|
50
|
+
import { mergeQuerySource } from "./query-merge.js";
|
|
52
51
|
import { parseListRef } from "./refs.js";
|
|
53
52
|
import type {
|
|
54
53
|
ListPageRecord,
|
|
@@ -107,7 +106,7 @@ export interface ListFieldSpec {
|
|
|
107
106
|
/** Result of resolving one list field for one page. */
|
|
108
107
|
export type ListFieldResolution =
|
|
109
108
|
| { readonly ok: true; readonly entries: readonly ScopeEntry[] }
|
|
110
|
-
| { readonly ok: false; readonly marker: string };
|
|
109
|
+
| { readonly ok: false; readonly marker: string; readonly detail?: string };
|
|
111
110
|
|
|
112
111
|
/**
|
|
113
112
|
* Where a local list is stored: the content instance holding the module
|
|
@@ -1098,29 +1097,6 @@ function createPageContext(
|
|
|
1098
1097
|
);
|
|
1099
1098
|
};
|
|
1100
1099
|
|
|
1101
|
-
/** Merge a source's query defaults with the knobs an entry / placement may set. */
|
|
1102
|
-
const mergedQuery = (
|
|
1103
|
-
source: Extract<SourceDef, { kind: "query" }>,
|
|
1104
|
-
overrides: unknown,
|
|
1105
|
-
field: string,
|
|
1106
|
-
path: string,
|
|
1107
|
-
): PageQuery | { marker: string } => {
|
|
1108
|
-
const merged: Record<string, unknown> = { ...source.defaults };
|
|
1109
|
-
if (overrides !== undefined && overrides !== null) {
|
|
1110
|
-
if (typeof overrides !== "object" || Array.isArray(overrides)) {
|
|
1111
|
-
return { marker: `list-malformed:${field}[${path}]` };
|
|
1112
|
-
}
|
|
1113
|
-
for (const [k, v] of Object.entries(overrides)) {
|
|
1114
|
-
if (!(source.editable as readonly string[]).includes(k as QueryKnob)) {
|
|
1115
|
-
return { marker: `list-malformed:${field}[${path}.${k}]` };
|
|
1116
|
-
}
|
|
1117
|
-
merged[k] = v;
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
const parsed = pageQuerySchema.safeParse(merged);
|
|
1121
|
-
return parsed.success ? parsed.data : { marker: `list-malformed:${field}[${path}]` };
|
|
1122
|
-
};
|
|
1123
|
-
|
|
1124
1100
|
/** Generated entries of a source node (`{{#items}}`). */
|
|
1125
1101
|
const sourceItems = (
|
|
1126
1102
|
source: SourceDef,
|
|
@@ -1130,8 +1106,10 @@ function createPageContext(
|
|
|
1130
1106
|
): ScopeValue => {
|
|
1131
1107
|
switch (source.kind) {
|
|
1132
1108
|
case "query": {
|
|
1133
|
-
const q =
|
|
1134
|
-
return "marker" in q
|
|
1109
|
+
const q = mergeQuerySource(source, item.query, field, `${item.id}.query`);
|
|
1110
|
+
return "marker" in q
|
|
1111
|
+
? { kind: "failed", marker: q.marker, detail: q.detail }
|
|
1112
|
+
: runQuery(q, field);
|
|
1135
1113
|
}
|
|
1136
1114
|
case "system": {
|
|
1137
1115
|
const termFields = node.fields.filter((f) => f.type === "term");
|
|
@@ -1153,7 +1131,7 @@ function createPageContext(
|
|
|
1153
1131
|
|
|
1154
1132
|
const fromValue = (v: ScopeValue): ListFieldResolution =>
|
|
1155
1133
|
v.kind === "failed"
|
|
1156
|
-
? { ok: false, marker: v.marker }
|
|
1134
|
+
? { ok: false, marker: v.marker, ...(v.detail !== undefined ? { detail: v.detail } : {}) }
|
|
1157
1135
|
: { ok: true, entries: v.kind === "list" ? v.entries : [] };
|
|
1158
1136
|
|
|
1159
1137
|
/**
|
|
@@ -1172,13 +1150,14 @@ function createPageContext(
|
|
|
1172
1150
|
if (eff === null) return { ok: false, marker: `list-malformed:${field.name}[paginate]` };
|
|
1173
1151
|
const source = field.source;
|
|
1174
1152
|
if (source?.kind === "query") {
|
|
1175
|
-
const q =
|
|
1153
|
+
const q = mergeQuerySource(
|
|
1176
1154
|
{ ...source, defaults: { ...source.defaults, limit: eff.perPage } },
|
|
1177
1155
|
rest,
|
|
1178
1156
|
field.name,
|
|
1179
1157
|
"query",
|
|
1158
|
+
true,
|
|
1180
1159
|
);
|
|
1181
|
-
if ("marker" in q) return { ok: false, marker: q.marker };
|
|
1160
|
+
if ("marker" in q) return { ok: false, marker: q.marker, detail: q.detail };
|
|
1182
1161
|
const key = JSON.stringify(["query", { ...q, limit: null }]);
|
|
1183
1162
|
return fromValue(paginate(field.name, key, queryHits(q), eff));
|
|
1184
1163
|
}
|
|
@@ -1207,9 +1186,9 @@ function createPageContext(
|
|
|
1207
1186
|
if (!source) return { ok: false, marker: `list-malformed:${field.name}[source]` };
|
|
1208
1187
|
switch (source.kind) {
|
|
1209
1188
|
case "query": {
|
|
1210
|
-
const q =
|
|
1189
|
+
const q = mergeQuerySource(source, value, field.name, "query");
|
|
1211
1190
|
return "marker" in q
|
|
1212
|
-
? { ok: false, marker: q.marker }
|
|
1191
|
+
? { ok: false, marker: q.marker, detail: q.detail }
|
|
1213
1192
|
: fromValue(runQuery(q, field.name));
|
|
1214
1193
|
}
|
|
1215
1194
|
case "system": {
|
package/src/lists/scope.ts
CHANGED
|
@@ -42,9 +42,11 @@ export type ScopeValue =
|
|
|
42
42
|
/**
|
|
43
43
|
* A value that could not resolve (a list source of an inactive plugin,
|
|
44
44
|
* a malformed query). The engine renders the tag raw and reports
|
|
45
|
-
* `marker` — loud, never silently empty (CLAUDE.md §2).
|
|
45
|
+
* `marker` — loud, never silently empty (CLAUDE.md §2). `detail`, when
|
|
46
|
+
* set, says what is wrong and how to fix it (shown to the AI by
|
|
47
|
+
* `preview_list_source`, #1281).
|
|
46
48
|
*/
|
|
47
|
-
| { readonly kind: "failed"; readonly marker: string };
|
|
49
|
+
| { readonly kind: "failed"; readonly marker: string; readonly detail?: string };
|
|
48
50
|
|
|
49
51
|
/** One entry a list section iterates. */
|
|
50
52
|
export interface ScopeEntry {
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
TERM,
|
|
20
20
|
} from "./lists/list-fixtures.test.js";
|
|
21
21
|
import { createListRenderContext, type ListPageContext } from "./lists/resolve.js";
|
|
22
|
-
import { renderTemplate, type TemplateField } from "./template-engine.js";
|
|
22
|
+
import { renderTemplate, type TemplateField, templateFieldsFromJson } from "./template-engine.js";
|
|
23
23
|
|
|
24
24
|
function ctx(
|
|
25
25
|
pageId: string | null = PAGE.home,
|
|
@@ -397,6 +397,32 @@ describe("lists — {{page.*}}", () => {
|
|
|
397
397
|
'<img src="/_caelo/media/hero" alt="Hero">We launched.|4;<img src="" alt="">|5;',
|
|
398
398
|
);
|
|
399
399
|
});
|
|
400
|
+
|
|
401
|
+
// #1304 — a stored `modules.fields` jsonb whose query source omits
|
|
402
|
+
// `editable` renders through the parsed source (editable: []), never a
|
|
403
|
+
// raw one the resolver would read `editable` off.
|
|
404
|
+
it("a stored query source without editable renders (#1304)", () => {
|
|
405
|
+
const fields = templateFieldsFromJson([
|
|
406
|
+
{
|
|
407
|
+
name: "latest",
|
|
408
|
+
kind: "list",
|
|
409
|
+
listType: "links",
|
|
410
|
+
bind: "source",
|
|
411
|
+
source: {
|
|
412
|
+
kind: "query",
|
|
413
|
+
defaults: { page_type: "news", order: "prop_asc:rating", limit: 5 },
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
]);
|
|
417
|
+
expect(fields[0]?.source).toEqual({
|
|
418
|
+
kind: "query",
|
|
419
|
+
editable: [],
|
|
420
|
+
defaults: { page_type: "news", order: "prop_asc:rating", limit: 5 },
|
|
421
|
+
});
|
|
422
|
+
const r = render("{{#latest}}{{props.rating}};{{/latest}}", fields, {}, ctx(PAGE.home));
|
|
423
|
+
expect(r.html).toBe("4;5;");
|
|
424
|
+
expect(r.missingSlots).toEqual([]);
|
|
425
|
+
});
|
|
400
426
|
});
|
|
401
427
|
|
|
402
428
|
describe("lists — terms", () => {
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The template engine keeps nothing of what it renders, and long literal
|
|
5
|
+
* text costs Mustache one token (searchVIU incident, 2026-11-01: the
|
|
6
|
+
* Mustache parse cache held every page the admin or the generator ever
|
|
7
|
+
* rendered, and the parser allocated a token per character of inline SVG).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { heapStats } from "bun:jsc";
|
|
11
|
+
import { describe, expect, it } from "bun:test";
|
|
12
|
+
import Mustache from "mustache";
|
|
13
|
+
import { renderTemplate, shieldLiteralText } from "./template-engine.js";
|
|
14
|
+
|
|
15
|
+
/** Render `html` the way the engine does: shielded, then the runs put back. */
|
|
16
|
+
function renderShielded(html: string, view: Record<string, unknown>): string {
|
|
17
|
+
const runs: string[] = [];
|
|
18
|
+
const shielded = shieldLiteralText(html, (text) => {
|
|
19
|
+
runs.push(text);
|
|
20
|
+
return `__RUN_${runs.length - 1}__`;
|
|
21
|
+
});
|
|
22
|
+
return Mustache.render(shielded, view).replace(
|
|
23
|
+
/__RUN_(\d+)__/g,
|
|
24
|
+
(_, i: string) => runs[Number(i)] ?? "",
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const LONG = "M10 20 C 30 40, 50 60, 70 80 Z ".repeat(20);
|
|
29
|
+
|
|
30
|
+
describe("shieldLiteralText", () => {
|
|
31
|
+
const cases: [string, string, Record<string, unknown>][] = [
|
|
32
|
+
[
|
|
33
|
+
"a placeholder between long runs",
|
|
34
|
+
`<svg><path d="${LONG}"/>{{title}}<p>${LONG}</p></svg>`,
|
|
35
|
+
{ title: "T" },
|
|
36
|
+
],
|
|
37
|
+
[
|
|
38
|
+
"a standalone comment line after a long line",
|
|
39
|
+
`<p>${LONG}</p>\n {{! note }}\n<p>{{title}}</p>`,
|
|
40
|
+
{ title: "T" },
|
|
41
|
+
],
|
|
42
|
+
["a standalone section line", `${LONG}\n{{#flag}}\n${LONG}\n{{/flag}}\nend`, { flag: true }],
|
|
43
|
+
["a long run inside a false section", `a{{#flag}}${LONG}{{/flag}}b`, { flag: false }],
|
|
44
|
+
[
|
|
45
|
+
"a long run inside an iterated section",
|
|
46
|
+
`{{#items}}<li>${LONG}{{name}}</li>{{/items}}`,
|
|
47
|
+
{ items: [{ name: "x" }, { name: "y" }] },
|
|
48
|
+
],
|
|
49
|
+
["a triple mustache next to a long run", `{{{html}}}${LONG}{{&html}}`, { html: "<b>raw</b>" }],
|
|
50
|
+
["a long comment", `x{{! ${LONG} }}y`, {}],
|
|
51
|
+
["a whitespace-only long run", `{{#flag}}${" ".repeat(400)}{{/flag}}`, { flag: true }],
|
|
52
|
+
];
|
|
53
|
+
for (const [name, html, view] of cases) {
|
|
54
|
+
it(`renders exactly what Mustache renders: ${name}`, () => {
|
|
55
|
+
expect(renderShielded(html, view)).toBe(Mustache.render(html, view));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
it("shields long runs and leaves tags, short text and a tag's inside alone", () => {
|
|
60
|
+
const html = `<i>{{a}}</i>${LONG}{{${"b".repeat(300)}}}`;
|
|
61
|
+
const kept: string[] = [];
|
|
62
|
+
const out = shieldLiteralText(html, (t) => {
|
|
63
|
+
kept.push(t);
|
|
64
|
+
return "KEY";
|
|
65
|
+
});
|
|
66
|
+
expect(kept).toEqual([`</i>${LONG}`]);
|
|
67
|
+
expect(out).toBe(`<i>{{a}}KEY{{${"b".repeat(300)}}}`);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
for (const setDelimiters of ["{{=<% %>=}}", "{{ = <% %> = }}"]) {
|
|
71
|
+
it(`leaves a template that changes the delimiters whole: ${setDelimiters}`, () => {
|
|
72
|
+
const html = `${setDelimiters}${LONG}<% name %>${LONG}`;
|
|
73
|
+
expect(shieldLiteralText(html, () => "KEY")).toBe(html);
|
|
74
|
+
expect(renderShielded(html, { name: "N" })).toBe(Mustache.render(html, { name: "N" }));
|
|
75
|
+
expect(Mustache.render(html, { name: "N" })).toContain(`${LONG}N${LONG}`);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("renderTemplate keeps nothing it rendered", () => {
|
|
81
|
+
it("Mustache's parse cache is off", () => {
|
|
82
|
+
expect((Mustache as unknown as { templateCache: unknown }).templateCache).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("puts a nested module back inside a shielded run", () => {
|
|
86
|
+
const r = renderTemplate({
|
|
87
|
+
html: `<section>${LONG}{{>featured}}${LONG}{{title}}</section>`,
|
|
88
|
+
fields: [
|
|
89
|
+
{ name: "featured", kind: "module" },
|
|
90
|
+
{ name: "title", kind: "text" },
|
|
91
|
+
],
|
|
92
|
+
contentValues: { featured: { moduleId: "m", contentInstanceId: "c" }, title: "T" },
|
|
93
|
+
partials: { featured: "<p>nested {{not_a_tag}}</p>" },
|
|
94
|
+
});
|
|
95
|
+
expect(r.html).toBe(`<section>${LONG}<p>nested {{not_a_tag}}</p>${LONG}T</section>`);
|
|
96
|
+
expect(r.missingSlots).toEqual([]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("renders 500 large modules without growing the live heap", () => {
|
|
100
|
+
// One short line per path: nothing to shield, Mustache parses all of
|
|
101
|
+
// it. The nested module is pasted in as a sentinel with a per-call
|
|
102
|
+
// UUID, so Mustache sees a different template text on every call — as
|
|
103
|
+
// it does for every module with a list, a nested module or a link.
|
|
104
|
+
const svg = `<svg>${'<path d="M1 2 C 3 4, 5 6, 7 8 Z"/>\n'.repeat(300)}</svg>`;
|
|
105
|
+
const render = (i: number) =>
|
|
106
|
+
renderTemplate({
|
|
107
|
+
html: `<section><h2>{{title}}</h2>${svg}{{>featured}}</section>`,
|
|
108
|
+
fields: [
|
|
109
|
+
{ name: "title", kind: "text" },
|
|
110
|
+
{ name: "featured", kind: "module" },
|
|
111
|
+
],
|
|
112
|
+
contentValues: {
|
|
113
|
+
title: `Page ${i}`,
|
|
114
|
+
featured: { moduleId: "m", contentInstanceId: "c" },
|
|
115
|
+
},
|
|
116
|
+
partials: { featured: `<p>Card ${i}</p>` },
|
|
117
|
+
});
|
|
118
|
+
render(0);
|
|
119
|
+
Bun.gc(true);
|
|
120
|
+
const before = heapStats();
|
|
121
|
+
for (let i = 1; i <= 500; i += 1) render(i);
|
|
122
|
+
Bun.gc(true);
|
|
123
|
+
const after = heapStats();
|
|
124
|
+
const grownMb =
|
|
125
|
+
(after.heapSize + after.extraMemorySize - before.heapSize - before.extraMemorySize) / 1048576;
|
|
126
|
+
// With the parse cache on, this kept 160 MB.
|
|
127
|
+
expect(grownMb).toBeLessThan(8);
|
|
128
|
+
});
|
|
129
|
+
});
|
package/src/template-engine.ts
CHANGED
|
@@ -96,6 +96,17 @@ import { renderSections } from "./template-sections.js";
|
|
|
96
96
|
// {{href}} test pins this; any leak of the default escape fails.
|
|
97
97
|
Mustache.escape = (s: unknown): string => (s === null || s === undefined ? "" : String(s));
|
|
98
98
|
|
|
99
|
+
// No parse cache. Mustache keeps the parsed tokens of every template it
|
|
100
|
+
// renders, keyed by the full template text, for the life of the process,
|
|
101
|
+
// and never evicts. The text this engine hands Mustache is different on
|
|
102
|
+
// every call — the sentinels carry a per-call UUID, and list entries,
|
|
103
|
+
// nested modules and link references are pasted in before Mustache runs —
|
|
104
|
+
// so the cache never hits and only grows: every module of every page
|
|
105
|
+
// stayed in memory for good, ~30x its own size (a 280 KB page ~2-5 MB).
|
|
106
|
+
// The searchVIU admin and its Stage's generator were OOM-killed on a 2 GiB
|
|
107
|
+
// container by it (2026-11-01). Parsing anew is what every call did anyway.
|
|
108
|
+
Mustache.templateCache = undefined;
|
|
109
|
+
|
|
99
110
|
/**
|
|
100
111
|
* Subset of `modules.fields[]` the engine cares about. The full
|
|
101
112
|
* schema lives in `content.ts`; the engine needs `name`, `kind`, the
|
|
@@ -129,6 +140,9 @@ export function templateFieldsFromJson(raw: unknown): TemplateField[] {
|
|
|
129
140
|
if (!f || typeof f !== "object") continue;
|
|
130
141
|
const o = f as Record<string, unknown>;
|
|
131
142
|
if (typeof o.name !== "string") continue;
|
|
143
|
+
// The parsed source, not the stored one: parsing fills defaults
|
|
144
|
+
// (`editable: []` when the source omits it, #1304).
|
|
145
|
+
const source = sourceDefSchema.safeParse(o.source);
|
|
132
146
|
out.push({
|
|
133
147
|
name: o.name,
|
|
134
148
|
kind: (typeof o.kind === "string" ? o.kind : "text") as ModuleFieldKind,
|
|
@@ -138,7 +152,7 @@ export function templateFieldsFromJson(raw: unknown): TemplateField[] {
|
|
|
138
152
|
...(typeof o.list === "string" ? { list: o.list } : {}),
|
|
139
153
|
// A source that fails its schema stays absent: the resolver then
|
|
140
154
|
// reports `list-malformed:<field>[source]` instead of guessing.
|
|
141
|
-
...(
|
|
155
|
+
...(source.success ? { source: source.data } : {}),
|
|
142
156
|
// Passed as stored: the resolver validates it and reports a
|
|
143
157
|
// malformed one as `list-malformed:<field>[paginate]`.
|
|
144
158
|
...(o.paginate !== undefined ? { paginate: o.paginate as ListPaginationSettings } : {}),
|
|
@@ -448,8 +462,23 @@ export function renderTemplate(input: RenderTemplateInput): RenderTemplateOutput
|
|
|
448
462
|
);
|
|
449
463
|
|
|
450
464
|
// 4. Render. The view holds only lowercase keys; sentinels survive
|
|
451
|
-
// untouched; sections + partials are already substituted.
|
|
452
|
-
|
|
465
|
+
// untouched; sections + partials are already substituted. Long runs
|
|
466
|
+
// of literal text are shielded from Mustache's parser first (see
|
|
467
|
+
// shieldLiteralText) and put back right after, BEFORE the sentinels:
|
|
468
|
+
// a shielded run may hold sentinels, which step 5 then restores.
|
|
469
|
+
const literals: string[] = [];
|
|
470
|
+
const literalPrefix = `__CAELO_TXT_${globalThis.crypto.randomUUID()}_`;
|
|
471
|
+
const shielded = shieldLiteralText(html, (text) => {
|
|
472
|
+
literals.push(text);
|
|
473
|
+
return `${literalPrefix}${literals.length - 1}__`;
|
|
474
|
+
});
|
|
475
|
+
let rendered = Mustache.render(shielded, view);
|
|
476
|
+
if (literals.length > 0) {
|
|
477
|
+
rendered = rendered.replace(
|
|
478
|
+
new RegExp(`${literalPrefix}(\\d+)__`, "g"),
|
|
479
|
+
(key, i: string) => literals[Number(i)] ?? key,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
453
482
|
|
|
454
483
|
// 5. Restore sentinels in one pass. A single replace never re-scans
|
|
455
484
|
// the text it inserts, so restored content cannot be mistaken for a
|
|
@@ -471,6 +500,46 @@ export function renderTemplate(input: RenderTemplateInput): RenderTemplateOutput
|
|
|
471
500
|
return { html: final, missingSlots: missing };
|
|
472
501
|
}
|
|
473
502
|
|
|
503
|
+
/**
|
|
504
|
+
* Shortest literal run worth shielding from Mustache: below this the key
|
|
505
|
+
* that stands in for it is not much shorter than the text.
|
|
506
|
+
*/
|
|
507
|
+
const SHIELD_MIN_LENGTH = 256;
|
|
508
|
+
/** A set-delimiters tag, with the whitespace Mustache allows after `{{`. */
|
|
509
|
+
const SET_DELIMITERS_RE = /\{\{\s*=/;
|
|
510
|
+
const SHIELD_CANDIDATE_RE = new RegExp(`[^{}\\n]{${SHIELD_MIN_LENGTH},}`, "g");
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Replace long runs of literal text with opaque keys before Mustache
|
|
514
|
+
* parses `html`. Mustache's parser allocates a token array per CHARACTER
|
|
515
|
+
* of literal text (mustache.js `parseTemplate`) before it squashes them,
|
|
516
|
+
* so a module's inline SVG or data-URI image cost the composer many times
|
|
517
|
+
* its size in garbage; a key is one short token instead.
|
|
518
|
+
*
|
|
519
|
+
* What Mustache renders is unchanged, because a shielded run:
|
|
520
|
+
* - holds no `{`, `}` or newline, so it never cuts a tag and every line
|
|
521
|
+
* stays where it was;
|
|
522
|
+
* - holds a non-space character and so does the key, so no line's
|
|
523
|
+
* standalone-tag verdict changes (Mustache drops the whitespace of a
|
|
524
|
+
* line that holds only a section or comment tag);
|
|
525
|
+
* - is never the inside of a tag (`{` right before it and `}` right after).
|
|
526
|
+
*
|
|
527
|
+
* A template that sets other delimiters (`{{=<% %>=}}`, or `{{ = … }}`:
|
|
528
|
+
* Mustache allows whitespace after the `{{`) is left whole: there, text
|
|
529
|
+
* without braces can be a tag.
|
|
530
|
+
*
|
|
531
|
+
* @param mkKey stores one run and returns the key standing in for it (no
|
|
532
|
+
* braces, no whitespace).
|
|
533
|
+
*/
|
|
534
|
+
export function shieldLiteralText(html: string, mkKey: (text: string) => string): string {
|
|
535
|
+
if (html.length < SHIELD_MIN_LENGTH || SET_DELIMITERS_RE.test(html)) return html;
|
|
536
|
+
return html.replace(SHIELD_CANDIDATE_RE, (run: string, offset: number) => {
|
|
537
|
+
if (html[offset - 1] === "{" && html[offset + run.length] === "}") return run;
|
|
538
|
+
if (run.trim().length === 0) return run;
|
|
539
|
+
return mkKey(run);
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
|
|
474
543
|
function renderModuleList(
|
|
475
544
|
name: string,
|
|
476
545
|
field: TemplateField,
|