@taprootio/site-authoring 0.62.0 → 0.64.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 +63 -23
- package/package.json +2 -2
- package/src/api.js +149 -21
- package/src/appearance-contract.js +9 -0
- package/src/article-end-settings.js +295 -0
- package/src/changelog.json +39 -0
- package/src/chrome-input.js +1 -1
- package/src/cli.js +110 -26
- package/src/constants.js +7 -0
- package/src/content/components.js +130 -14
- package/src/content/cta-decoration-paint.js +321 -0
- package/src/content/form-placement-presentation.js +91 -0
- package/src/content/form-placement-presentation.json +6 -0
- package/src/content/free-form-section-registry.json +19 -1
- package/src/content/free-form-sections.js +64 -55
- package/src/content/markdown.js +13 -0
- package/src/content/tiptap-prosemirror.ts +72 -59
- package/src/content/validate-document.js +2 -0
- package/src/cta-artwork-warnings.js +164 -0
- package/src/errors.js +6 -0
- package/src/field-schema.json +6 -2
- package/src/field-validation.js +14 -5
- package/src/footer-workspace.js +7 -3
- package/src/form-references.js +71 -9
- package/src/forms-contract.js +3 -5
- package/src/heading-outline.js +7 -0
- package/src/output.js +2 -0
- package/src/page-article-end.js +66 -0
- package/src/page-block-catalog.js +54 -0
- package/src/page-form-presentation.js +104 -0
- package/src/page-structure-checks.js +7 -1
- package/src/placed-image-ids.js +15 -18
- package/src/presentation-reference.js +30 -52
- package/src/reference-help.js +97 -27
- package/src/reusable-blocks.js +429 -0
- package/src/reusable-placements.js +271 -0
- package/src/setting-reference.js +25 -1
- package/src/settings-catalog.js +1 -0
- package/src/site-check-findings.js +30 -2
- package/src/site-check-rules.json +1 -0
- package/src/site-checks.js +26 -2
- package/src/site-health-findings.js +25 -3
- package/src/site-health-image-checks.js +1 -1
- package/src/staging-check.js +108 -21
- package/src/staging-served.js +5 -49
- package/src/status-warning-paints.js +17 -0
- package/src/template-token-reach.json +14 -2
- package/src/theme-resolve.js +27 -0
- package/src/transport.js +41 -5
- package/src/typed-page-reference.js +55 -5
- package/src/typed-pages.js +27 -4
- package/src/verbs/approve.js +2 -1
- package/src/verbs/deploy.js +93 -15
- package/src/verbs/footer-push.js +4 -2
- package/src/verbs/forms-push.js +9 -0
- package/src/verbs/health.js +11 -6
- package/src/verbs/index.js +3 -0
- package/src/verbs/pages-blocks-pull.js +22 -0
- package/src/verbs/pages-push.js +89 -34
- package/src/verbs/plan.js +34 -5
- package/src/verbs/pull.js +213 -124
- package/src/verbs/status.js +20 -2
- package/src/verbs/theme-push.js +164 -14
- package/src/verbs/theme-resolve.js +114 -40
- package/src/verbs/validate.js +49 -18
- package/src/workspace-check.js +42 -5
- package/src/workspace-context-usage.js +54 -1
- package/src/workspace.js +37 -7
- package/test/fixtures/default-site-theme.json +1 -1
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { pageLinkInventory } from "./chrome-input.js";
|
|
2
|
+
import { LIMITS, SURFACE_STANDARD } from "./constants.js";
|
|
3
|
+
import { ctaContrastFindings } from "./cta-artwork-warnings.js";
|
|
4
|
+
import { normalizeCtaDecoration } from "./content/cta-decoration-paint.js";
|
|
5
|
+
import { stripImageDescriptionHydration } from "./content/image-description-document.js";
|
|
6
|
+
import { formPlacementPresentationIssues, readFormPlacementPresentation } from "./content/form-placement-presentation.js";
|
|
7
|
+
import { canonicalizeComponentData, getComponentDefinition, mapComponentImages, validateComponentBlock } from "./content/components.js";
|
|
8
|
+
import { isCanonicalUuid, sanitizeDiagnostic, SiteAuthoringError } from "./errors.js";
|
|
9
|
+
import { stableJson } from "./footer-draft-hash.js";
|
|
10
|
+
import { anyUnstorableString, FORM_LIMITS, isFormKey } from "./forms-contract.js";
|
|
11
|
+
import { boundedByBytes } from "./session.js";
|
|
12
|
+
import { missingPageLinkFindings, normalizedPathSet } from "./site-checks.js";
|
|
13
|
+
import { fitPrecheckToBudget } from "./site-health-precheck.js";
|
|
14
|
+
import { sharedDeclaredThemeContextNames } from "./theme-contexts.js";
|
|
15
|
+
import { readWorkspaceJson, workspaceFileExists, workspaceImageFromReference, WORKSPACE_LIMITS } from "./workspace.js";
|
|
16
|
+
|
|
17
|
+
export const REUSABLE_BLOCKS_FILE = "settings/reusable-blocks.json";
|
|
18
|
+
export const REUSABLE_BLOCK_LIMITS = Object.freeze({ blocks: 64, nameCharacters: FORM_LIMITS.nameCharacters, payloadBytes: 16 * 1024, listBytes: 128 * 1024 });
|
|
19
|
+
|
|
20
|
+
function isRecord(value) {
|
|
21
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function closedRecord(value, fields) {
|
|
25
|
+
return isRecord(value) && Object.keys(value).every((key) => fields.includes(key));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function invalid(field, message) {
|
|
29
|
+
throw new SiteAuthoringError("theme.reusable_blocks_invalid", message, {
|
|
30
|
+
field: field === "" ? REUSABLE_BLOCKS_FILE : `${REUSABLE_BLOCKS_FILE}:${field}`,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function invalidProjection(field) {
|
|
35
|
+
const catalog = field === "forms" || field === "definitions";
|
|
36
|
+
throw new SiteAuthoringError(
|
|
37
|
+
catalog ? "api.page_block_catalog" : "api.reusable_blocks_contract",
|
|
38
|
+
catalog ? "Taproot returned an invalid page block catalog." : "Taproot returned invalid reusable block data.",
|
|
39
|
+
{ field },
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const validName = (name) => typeof name === "string" && name.trim() !== "" && name.length <= REUSABLE_BLOCK_LIMITS.nameCharacters;
|
|
44
|
+
const validLayout = (layout) => layout === undefined || layout === "compact";
|
|
45
|
+
const FORM_PRESENTATION_FIELDS = ["heading", "introduction", "finePrint", "context"];
|
|
46
|
+
|
|
47
|
+
function sectionRows(section, list, field, maximum = Infinity) {
|
|
48
|
+
if (section === undefined) return undefined;
|
|
49
|
+
const rows = section?.[list] === undefined ? [] : section[list];
|
|
50
|
+
if (!isRecord(section) || !Array.isArray(rows) || rows.length > maximum) invalidProjection(field);
|
|
51
|
+
return rows;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function readFormPayload(form, field) {
|
|
55
|
+
if (!isCanonicalUuid(form?.formId) || !validLayout(form.layout) || formPlacementPresentationIssues(form).length > 0) {
|
|
56
|
+
invalidProjection(field);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
formId: form.formId,
|
|
60
|
+
...(form.layout === undefined ? {} : { layout: form.layout }),
|
|
61
|
+
...readFormPlacementPresentation(form),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function reusableDefinitionRows(section, { preview = false } = {}) {
|
|
66
|
+
const field = preview ? "definitions" : "reusableBlockDefinitions";
|
|
67
|
+
const rows = sectionRows(section, preview ? "rows" : "blocks", field, REUSABLE_BLOCK_LIMITS.blocks);
|
|
68
|
+
const keys = new Set();
|
|
69
|
+
return rows?.map((row) => {
|
|
70
|
+
const displayName = row?.[preview ? "label" : "name"];
|
|
71
|
+
if (!isFormKey(row?.key) || keys.has(row.key) || !validName(displayName)
|
|
72
|
+
|| (row.cta === undefined) === (row.form === undefined)) invalidProjection(field);
|
|
73
|
+
keys.add(row.key);
|
|
74
|
+
if (preview && row.draftOnly !== undefined && typeof row.draftOnly !== "boolean") invalidProjection(field);
|
|
75
|
+
const identity = preview
|
|
76
|
+
? { key: row.key, label: displayName, ...(row.draftOnly === undefined ? {} : { draftOnly: row.draftOnly }) }
|
|
77
|
+
: { key: row.key, name: displayName };
|
|
78
|
+
if (row.form !== undefined) return { ...identity, form: readFormPayload(row.form, field) };
|
|
79
|
+
if (preview) {
|
|
80
|
+
if (!isRecord(row.cta)) invalidProjection(field);
|
|
81
|
+
const fields = getComponentDefinition("cta").fields;
|
|
82
|
+
const cta = Object.fromEntries(Object.entries(row.cta)
|
|
83
|
+
.filter(([name]) => name !== "buttonUrl" && Object.hasOwn(fields, name)));
|
|
84
|
+
if (validateComponentBlock("cta", JSON.stringify(cta), "").length > 0) invalidProjection(field);
|
|
85
|
+
return { ...identity, cta };
|
|
86
|
+
}
|
|
87
|
+
const { data, pageResourceId } = row.cta ?? {};
|
|
88
|
+
if (!isRecord(data) || (pageResourceId !== undefined && !isCanonicalUuid(pageResourceId))) invalidProjection(field);
|
|
89
|
+
if (pageResourceId === undefined) return { ...identity, cta: { data: authoredCtaImages(data) } };
|
|
90
|
+
const { buttonUrl, ...trackedPageData } = data;
|
|
91
|
+
if (buttonUrl !== undefined && buttonUrl !== "") invalidProjection(field);
|
|
92
|
+
return { ...identity, cta: { data: authoredCtaImages(trackedPageData), pageResourceId } };
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function reusableFormRows(section, field) {
|
|
97
|
+
return sectionRows(section, "rows", field)?.map((row) => {
|
|
98
|
+
if (!isCanonicalUuid(row?.formId) || !isFormKey(row.key) || typeof row.name !== "string" ||
|
|
99
|
+
!Number.isSafeInteger(row.version) || row.version < 1 ||
|
|
100
|
+
(row.archived !== undefined && typeof row.archived !== "boolean") ||
|
|
101
|
+
(row.compactEligible !== undefined && typeof row.compactEligible !== "boolean")) invalidProjection(field);
|
|
102
|
+
return {
|
|
103
|
+
key: row.key, formId: row.formId, name: row.name, version: row.version,
|
|
104
|
+
archived: row.archived === true, compactEligible: row.compactEligible === true,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function readReusablePresentation(presentation) {
|
|
110
|
+
return {
|
|
111
|
+
blocks: reusableDefinitionRows(presentation?.reusableBlockDefinitions),
|
|
112
|
+
forms: reusableFormRows(presentation?.reusableBlockForms, "reusableBlockForms"),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function authoredCtaImages(data) {
|
|
117
|
+
return mapComponentImages("cta", data, (image) => {
|
|
118
|
+
if (!isRecord(image)) return image;
|
|
119
|
+
const { src, urls, ...authored } = stripImageDescriptionHydration(image);
|
|
120
|
+
return authored;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function ctaWithEmptyDelivery(data) {
|
|
125
|
+
return mapComponentImages("cta", data, (image) => isRecord(image) ? { ...image, src: "", urls: [] } : image);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function reusableCtaForWorkspaceChecks({ data, pageResourceId }, knownPaths) {
|
|
129
|
+
const target = pageResourceId === undefined ? undefined : knownPaths.get(pageResourceId);
|
|
130
|
+
return ctaWithEmptyDelivery(target === undefined ? data : { ...data, buttonUrl: target });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function resolveCtaImageSource(image, mediaManifest, field) {
|
|
134
|
+
if (typeof image !== "string") return image;
|
|
135
|
+
const record = workspaceImageFromReference(mediaManifest, image);
|
|
136
|
+
if (record === undefined) invalid(field, "Record this image with media upload before placing it in a reusable CTA.");
|
|
137
|
+
const { imageId, width, height, alt } = record;
|
|
138
|
+
return {
|
|
139
|
+
imageId,
|
|
140
|
+
...(Number.isSafeInteger(width) && width > 0 ? { width } : {}),
|
|
141
|
+
...(Number.isSafeInteger(height) && height > 0 ? { height } : {}),
|
|
142
|
+
...(typeof alt === "string" ? { alt } : {}),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function ctaImageIds(data) {
|
|
147
|
+
const ids = [];
|
|
148
|
+
mapComponentImages("cta", data, (image) => {
|
|
149
|
+
if (isCanonicalUuid(image?.imageId)) ids.push(image.imageId);
|
|
150
|
+
return image;
|
|
151
|
+
});
|
|
152
|
+
return ids;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function projectReusableBlocks(blocks, forms) {
|
|
156
|
+
if (blocks === undefined) return undefined;
|
|
157
|
+
const formKey = new Map((forms ?? []).map(({ formId, key }) => [formId, key]));
|
|
158
|
+
return { blocks: blocks.map(({ key, name, cta, form }) => {
|
|
159
|
+
if (cta !== undefined) {
|
|
160
|
+
const data = authoredCtaImages(cta.data);
|
|
161
|
+
if (cta.pageResourceId !== undefined) {
|
|
162
|
+
delete data.buttonUrl;
|
|
163
|
+
data.pageResourceId = cta.pageResourceId;
|
|
164
|
+
}
|
|
165
|
+
return { key, name, type: "cta", data };
|
|
166
|
+
}
|
|
167
|
+
const keyOfForm = formKey.get(form.formId);
|
|
168
|
+
if (keyOfForm === undefined) invalidProjection("reusableBlockForms");
|
|
169
|
+
return { key, name, type: "form", data: {
|
|
170
|
+
formKey: keyOfForm, ...(form.layout === undefined ? {} : { layout: form.layout }),
|
|
171
|
+
...readFormPlacementPresentation(form),
|
|
172
|
+
} };
|
|
173
|
+
}) };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function reusableWorkspaceEvidence({ blocks, forms }) {
|
|
177
|
+
if (blocks === undefined) return undefined;
|
|
178
|
+
return {
|
|
179
|
+
...(forms === undefined ? {} : { forms }),
|
|
180
|
+
formReferences: Object.fromEntries(blocks.filter((block) => block.form !== undefined)
|
|
181
|
+
.map(({ key, form }) => [key, form])),
|
|
182
|
+
imageIds: [...new Set(blocks.flatMap(({ cta }) => cta === undefined ? [] : ctaImageIds(cta.data)))].sort(),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function requireRetainedReusableContexts(blocks, style) {
|
|
187
|
+
const contexts = new Set(sharedDeclaredThemeContextNames(style.lightTheme, style.darkTheme));
|
|
188
|
+
for (const [index, block] of (blocks ?? []).entries()) {
|
|
189
|
+
const context = block.cta?.data.context ?? block.form?.context;
|
|
190
|
+
if (context && !contexts.has(context)) {
|
|
191
|
+
const type = block.cta === undefined ? "Form" : "CTA";
|
|
192
|
+
invalid("/blocks/" + index + "/data/context",
|
|
193
|
+
`The site’s reusable ${type} '` + sanitizeDiagnostic(block.name, block.key) +
|
|
194
|
+
"' still uses the removed context '" + sanitizeDiagnostic(context, "") +
|
|
195
|
+
"'. Keep the context in both themes or repoint the block in settings/reusable-blocks.json.");
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function ctaForSave(data, field, knownImageIds, contexts, mediaManifest) {
|
|
201
|
+
if (anyUnstorableString(data)) invalid(field, "Remove unsupported characters from the call to action.");
|
|
202
|
+
const { pageResourceId, ...inlineData } = data;
|
|
203
|
+
if (Object.hasOwn(inlineData, "reusableKey")) invalid(`${field}/reusableKey`, "Write the call-to-action fields on the definition.");
|
|
204
|
+
const source = mapComponentImages("cta", inlineData, (image, path) => resolveCtaImageSource(image, mediaManifest, field + path));
|
|
205
|
+
const normalized = ctaWithEmptyDelivery(source);
|
|
206
|
+
if (pageResourceId !== undefined && (!isCanonicalUuid(pageResourceId) || source.buttonUrl?.trim?.())) {
|
|
207
|
+
invalid(`${field}/pageResourceId`, "Use a site page resource ID in place of buttonUrl.");
|
|
208
|
+
}
|
|
209
|
+
const [finding] = validateComponentBlock("cta", JSON.stringify(normalized), field);
|
|
210
|
+
if (finding !== undefined) invalid(finding.path.replace("/componentData", ""), finding.message);
|
|
211
|
+
if (source.context && !contexts.has(source.context)) {
|
|
212
|
+
invalid(`${field}/context`, "Choose a context defined by both workspace themes.");
|
|
213
|
+
}
|
|
214
|
+
if (knownImageIds !== undefined) {
|
|
215
|
+
for (const imageId of ctaImageIds(source)) {
|
|
216
|
+
if (!knownImageIds.has(imageId)) invalid(field, "Choose an owned image recorded by media upload.");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (pageResourceId !== undefined) delete normalized.buttonUrl;
|
|
220
|
+
return {
|
|
221
|
+
data: authoredCtaImages(JSON.parse(canonicalizeComponentData("cta", normalized))),
|
|
222
|
+
...(pageResourceId === undefined ? {} : { pageResourceId }),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export async function readReusableWorkspace(workspaceDir, { authoringSurface, knownImageIds, mediaManifest, style }) {
|
|
227
|
+
if (!await workspaceFileExists(workspaceDir, REUSABLE_BLOCKS_FILE)) return undefined;
|
|
228
|
+
requireReusableSurface(authoringSurface);
|
|
229
|
+
const document = await readWorkspaceJson(workspaceDir, REUSABLE_BLOCKS_FILE, WORKSPACE_LIMITS.settingsBytes);
|
|
230
|
+
const { blocks: maximum, nameCharacters } = REUSABLE_BLOCK_LIMITS;
|
|
231
|
+
if (!closedRecord(document, ["blocks"]) || !Array.isArray(document.blocks) || document.blocks.length > maximum) {
|
|
232
|
+
invalid("/blocks", `Write a blocks array with at most ${maximum} reusable blocks.`);
|
|
233
|
+
}
|
|
234
|
+
const keys = new Set();
|
|
235
|
+
const contexts = new Set(sharedDeclaredThemeContextNames(style.lightTheme, style.darkTheme));
|
|
236
|
+
return document.blocks.map((block, index) => {
|
|
237
|
+
const field = `/blocks/${index}`;
|
|
238
|
+
if (!closedRecord(block, ["key", "name", "type", "data"]) || !isRecord(block.data)) {
|
|
239
|
+
invalid(field, "Write a reusable block with key, name, type and data.");
|
|
240
|
+
}
|
|
241
|
+
const { key, name, type, data } = block;
|
|
242
|
+
if (!isFormKey(key)) invalid(`${field}/key`, "Use at most 64 lowercase letters and digits, separated by single hyphens.");
|
|
243
|
+
if (keys.has(key)) invalid(`${field}/key`, "Use a different key for each reusable block.");
|
|
244
|
+
keys.add(key);
|
|
245
|
+
if (!validName(name)) invalid(`${field}/name`, `Give the block a display name of 1 to ${nameCharacters} characters.`);
|
|
246
|
+
if (anyUnstorableString(name)) invalid(`${field}/name`, "Remove unsupported characters from the block name.");
|
|
247
|
+
if (type === "cta") return { key, name, field, cta: ctaForSave(data, `${field}/data`, knownImageIds, contexts, mediaManifest) };
|
|
248
|
+
if (type !== "form") invalid(`${field}/type`, "Choose cta or form.");
|
|
249
|
+
if (!closedRecord(data, ["formKey", "layout", ...FORM_PRESENTATION_FIELDS]) || !isFormKey(data.formKey) || !validLayout(data.layout)) {
|
|
250
|
+
invalid(`${field}/data`, "Name a same-site form by its formKey and use only supported layout and presentation fields.");
|
|
251
|
+
}
|
|
252
|
+
const [issue] = formPlacementPresentationIssues(data, contexts);
|
|
253
|
+
if (issue !== undefined) invalid(`${field}/data/${issue.field.replaceAll(".", "/")}`, issue.message);
|
|
254
|
+
const { formKey, layout } = data;
|
|
255
|
+
return { key, name, field, form: { formKey, ...(layout === undefined ? {} : { layout }), ...readFormPlacementPresentation(data) } };
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function requireReusableSurface(surface) {
|
|
260
|
+
if (surface !== undefined && surface !== SURFACE_STANDARD) invalid("", "Reusable blocks are available on Standard sites.");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function resolveReusableWorkspace(blocks, { forms, formReferences } = {}) {
|
|
264
|
+
if (blocks === undefined) return undefined;
|
|
265
|
+
const byKey = Array.isArray(forms) ? new Map(forms.map((row) => [row.key, row])) : undefined;
|
|
266
|
+
const resolved = blocks.map(({ key, name, field, cta, form }) => {
|
|
267
|
+
if (cta !== undefined) return { key, name, cta };
|
|
268
|
+
if (byKey === undefined) invalid(`${field}/data/formKey`, "Run pull --settings-only to read this site's reusable block forms.");
|
|
269
|
+
const row = byKey.get(form.formKey);
|
|
270
|
+
if (row === undefined) invalid(`${field}/data/formKey`, "Choose a form from this site's form options.");
|
|
271
|
+
const layout = form.layout;
|
|
272
|
+
const previous = Object.hasOwn(formReferences ?? {}, key) ? formReferences[key] : undefined;
|
|
273
|
+
const unchanged = previous?.formId === row.formId && previous.layout === layout;
|
|
274
|
+
if (row.archived && !unchanged) invalid(`${field}/data/formKey`, "Choose an active form for a new or changed reference.");
|
|
275
|
+
if (layout === "compact" && !row.compactEligible && !unchanged) {
|
|
276
|
+
invalid(`${field}/data/layout`, "This form cannot use the compact layout. Choose an eligible newsletter form or omit layout.");
|
|
277
|
+
}
|
|
278
|
+
return { key, name, form: { formId: row.formId, ...(layout === undefined ? {} : { layout }), ...readFormPlacementPresentation(form) } };
|
|
279
|
+
});
|
|
280
|
+
for (const [index, block] of resolved.entries()) {
|
|
281
|
+
if (Buffer.byteLength(JSON.stringify(block.cta ?? block.form)) > REUSABLE_BLOCK_LIMITS.payloadBytes) {
|
|
282
|
+
invalid(`${blocks[index].field}/data`, "A reusable block payload can use at most 16 KiB.");
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (Buffer.byteLength(JSON.stringify(resolved)) > REUSABLE_BLOCK_LIMITS.listBytes) invalid("/blocks", "The reusable block list can use at most 128 KiB.");
|
|
286
|
+
return resolved.sort((a, b) => a.key < b.key ? -1 : 1);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function checkReusableWorkspaceTargets(blocks, manifest, plannedPages = []) {
|
|
290
|
+
if (blocks === undefined) return undefined;
|
|
291
|
+
const complete = manifest.pagesTruncated !== true;
|
|
292
|
+
const inventory = pageLinkInventory(manifest.pages, complete);
|
|
293
|
+
const replaced = new Set(plannedPages.map(({ pageId }) => pageId).filter(Boolean));
|
|
294
|
+
const remaining = pageLinkInventory(manifest.pages.filter(({ pageId }) => !replaced.has(pageId)), complete);
|
|
295
|
+
const authoredPaths = normalizedPathSet([
|
|
296
|
+
...remaining.knownPaths.values(),
|
|
297
|
+
...plannedPages.map(({ pagePath }) => pagePath),
|
|
298
|
+
]);
|
|
299
|
+
let missingInventory = false;
|
|
300
|
+
const unresolved = [];
|
|
301
|
+
for (const { key, cta, field, form } of blocks) {
|
|
302
|
+
if (cta?.pageResourceId !== undefined) {
|
|
303
|
+
if (inventory.statuses.has(cta.pageResourceId)) continue;
|
|
304
|
+
if (complete) invalid(`${field}/data/pageResourceId`, "This CTA target is missing from the workspace page inventory.");
|
|
305
|
+
missingInventory = true;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const url = cta?.data.buttonUrl?.trim() ?? form?.finePrint?.linkUrl;
|
|
309
|
+
if (!url?.startsWith("/")) continue;
|
|
310
|
+
const [finding] = missingPageLinkFindings({ linkUrl: url }, "", authoredPaths);
|
|
311
|
+
if (finding === undefined) continue;
|
|
312
|
+
if (complete) unresolved.push({ key, path: sanitizeDiagnostic(finding.subject, "/"), ...(form === undefined ? {} : { type: "form" }) });
|
|
313
|
+
else missingInventory = true;
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
workspace: {
|
|
317
|
+
scope: "workspace",
|
|
318
|
+
links: missingInventory ? "unavailable" : unresolved.length > 0 ? "unresolved" : "checked",
|
|
319
|
+
...(unresolved.length === 0 ? {} : { unresolvedLinks: { count: unresolved.length, ...boundedByBytes(unresolved, 4 * 1024) } }),
|
|
320
|
+
},
|
|
321
|
+
selectedRelease: {
|
|
322
|
+
status: "unavailable",
|
|
323
|
+
reason: "The workspace has no theme and page inventory identified as belonging to the same selected release.",
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function reusableFormPresentationChanged(proposed, current) {
|
|
329
|
+
if (proposed === undefined) return false;
|
|
330
|
+
function presentations(blocks) {
|
|
331
|
+
return Object.fromEntries((blocks ?? []).flatMap(({ key, form }) => {
|
|
332
|
+
const presentation = readFormPlacementPresentation(form);
|
|
333
|
+
return Object.keys(presentation).length === 0 ? [] : [[key, presentation]];
|
|
334
|
+
}));
|
|
335
|
+
}
|
|
336
|
+
return stableJson(presentations(proposed)) !== stableJson(presentations(current));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function requireKnownReusableFormPresentation(proposed, current, recordedForms) {
|
|
340
|
+
const byKey = new Map(proposed.map(({ key, form }) => [key, form]));
|
|
341
|
+
for (const [index, { key, form }] of (current ?? []).entries()) {
|
|
342
|
+
const stored = readFormPlacementPresentation(form);
|
|
343
|
+
if (Object.keys(stored).length === 0) continue;
|
|
344
|
+
const next = readFormPlacementPresentation(byKey.get(key));
|
|
345
|
+
const recorded = readFormPlacementPresentation(Object.hasOwn(recordedForms ?? {}, key) ? recordedForms[key] : undefined);
|
|
346
|
+
if (stableJson(stored) !== stableJson(next) && stableJson(stored) !== stableJson(recorded)) {
|
|
347
|
+
invalid(`/blocks/${index}/data`, "Run pull --settings-only and reconcile the saved Form presentation before replacing or removing it.");
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export function reusableBlockChanges(proposed, current) {
|
|
353
|
+
if (proposed === undefined) return undefined;
|
|
354
|
+
const previous = new Map((current ?? []).map((block) => [block.key, block]));
|
|
355
|
+
const added = [];
|
|
356
|
+
const edited = [];
|
|
357
|
+
for (const block of proposed) {
|
|
358
|
+
const { key, name } = block;
|
|
359
|
+
const old = previous.get(key);
|
|
360
|
+
if (old === undefined) added.push({ key, name });
|
|
361
|
+
else if ((old.cta === undefined) !== (block.cta === undefined))
|
|
362
|
+
invalid("/blocks", "The block '" + key + "' keeps its type. Create a block with a new key for a different type.");
|
|
363
|
+
else if (stableJson(old) !== stableJson(block)) edited.push({ key, name });
|
|
364
|
+
previous.delete(key);
|
|
365
|
+
}
|
|
366
|
+
return { added, edited, removed: [...previous.values()].map(({ key, name }) => ({ key, name })) };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export function reportReusableChanges(changes, onProgress) {
|
|
370
|
+
if (changes === undefined) return;
|
|
371
|
+
for (const [action, rows] of Object.entries(changes)) {
|
|
372
|
+
for (const { key, name } of rows) onProgress(`Reusable block ${action} in the proposed presentation: ${sanitizeDiagnostic(name, key)} (${key}).`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
export async function reusableWorkspaceReport(blocks, style, targetChecks, onProgress, loadEngines) {
|
|
377
|
+
if (blocks === undefined) return undefined;
|
|
378
|
+
const ctas = blocks.filter(({ cta }) => cta !== undefined).map(({ key, field, cta }) => {
|
|
379
|
+
const { data, pageResourceId } = cta;
|
|
380
|
+
return {
|
|
381
|
+
key,
|
|
382
|
+
file: REUSABLE_BLOCKS_FILE,
|
|
383
|
+
path: `${field}/data`,
|
|
384
|
+
context: data.context,
|
|
385
|
+
data: pageResourceId === undefined ? data : { ...data, buttonUrl: "/" },
|
|
386
|
+
decoration: normalizeCtaDecoration(ctaWithEmptyDelivery(data).decoration).decoration,
|
|
387
|
+
};
|
|
388
|
+
});
|
|
389
|
+
const items = (await ctaContrastFindings(ctas, style, loadEngines))
|
|
390
|
+
.map(({ code, ...finding }) => ({ ...finding, code: `cta.workspace_contrast_${code}` }));
|
|
391
|
+
for (const item of items.slice(0, 20)) {
|
|
392
|
+
onProgress(`Warning: Workspace reusable CTA '${item.key}' (${item.scheme}). ${item.message}`);
|
|
393
|
+
}
|
|
394
|
+
if (targetChecks.workspace.links === "unavailable") {
|
|
395
|
+
onProgress("The workspace page inventory is incomplete, so some reusable block links could not be checked.");
|
|
396
|
+
}
|
|
397
|
+
for (const { key, path, type } of targetChecks.workspace.unresolvedLinks?.items.slice(0, 20) ?? []) {
|
|
398
|
+
const label = type === "form" ? "Form fine print" : "CTA";
|
|
399
|
+
onProgress(`Warning: Workspace reusable ${label} '${key}' links to '${path}'. This path is missing from the workspace page inventory and may be another kind of route.`);
|
|
400
|
+
}
|
|
401
|
+
onProgress(`Selected-release reusable block checks are unavailable. ${targetChecks.selectedRelease.reason}`);
|
|
402
|
+
return {
|
|
403
|
+
...targetChecks,
|
|
404
|
+
workspace: {
|
|
405
|
+
...targetChecks.workspace,
|
|
406
|
+
contrast: items.some(({ code }) => code === "cta.workspace_contrast_unavailable") ? "partial" : "checked",
|
|
407
|
+
findings: { count: items.length, ...boundedByBytes(items, 4 * 1024) },
|
|
408
|
+
},
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
export function appendReusableBlockReport(result, report, onProgress, changes) {
|
|
413
|
+
if (report === undefined) return result;
|
|
414
|
+
const listed = changes === undefined ? {} : {
|
|
415
|
+
changes: Object.fromEntries(Object.entries(changes)
|
|
416
|
+
.map(([action, items]) => [action, { count: items.length, ...boundedByBytes(items, 4 * 1024) }])),
|
|
417
|
+
};
|
|
418
|
+
const fitted = fitPrecheckToBudget(result, { reusableBlocks: { ...listed, ...report } }, [
|
|
419
|
+
"reusableBlocks.changes.removed",
|
|
420
|
+
"reusableBlocks.changes.edited",
|
|
421
|
+
"reusableBlocks.changes.added",
|
|
422
|
+
"reusableBlocks.workspace.unresolvedLinks",
|
|
423
|
+
"reusableBlocks.workspace.findings",
|
|
424
|
+
]);
|
|
425
|
+
const merged = { ...result, ...fitted };
|
|
426
|
+
if (Buffer.byteLength(JSON.stringify(merged), "utf8") <= LIMITS.githubOutputBytes) return merged;
|
|
427
|
+
onProgress("The JSON result has no room for the reusable block summary.");
|
|
428
|
+
return result;
|
|
429
|
+
}
|