@taprootio/site-authoring 0.38.0 → 0.39.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.
@@ -1,6 +1,8 @@
1
1
  import { createHash } from "node:crypto";
2
2
 
3
3
  import { SiteAuthoringError } from "./errors.js";
4
+ import { resolveFooterLogoScale } from "./footer-branding.js";
5
+ import { footerAlignmentProfileValues, normalizeFooterLogoBox } from "./footer-alignment.js";
4
6
  import { FOOTER_COLOR_TOKENS } from "./theme-validation.js";
5
7
 
6
8
  // The ten scheme colors theme push owns — light/dark times the color-token
@@ -88,15 +90,25 @@ export function stableJson(value) {
88
90
  // the canonical form.
89
91
  export function computeFooterDraftHash(settings = {}) {
90
92
  const canonical = [];
91
- append(canonical, "footer-draft-v4");
93
+ append(canonical, "footer-draft-v7");
92
94
  append(canonical, settings.enabled === true);
93
95
  append(canonical, settings.showBrand ?? true);
94
96
  append(canonical, settings.showBrandText ?? true);
97
+ append(canonical, settings.brandSpan ?? 1);
98
+ append(canonical, settings.brandSize || "standard");
99
+ append(canonical, resolveFooterLogoScale(settings.logoScale, settings.brandSize).canonical);
100
+ append(canonical, normalizeFooterLogoBox(settings.logoBox));
101
+ appendAlignment(canonical, settings.alignment);
102
+ appendAlignment(canonical, settings.brandAlignment);
103
+ appendAlignment(canonical, settings.asideAlignment);
104
+ appendAlignment(canonical, settings.bottomAlignment, true);
95
105
 
96
106
  const columns = Array.isArray(settings.linkColumns) ? settings.linkColumns : [];
97
107
  append(canonical, columns.length);
98
108
  for (const column of columns) {
99
109
  append(canonical, normalizeId(column?.id));
110
+ append(canonical, column?.span ?? 1);
111
+ appendAlignment(canonical, column?.alignment);
100
112
  const groups = Array.isArray(column?.groups) ? column.groups : [];
101
113
  append(canonical, groups.length);
102
114
  for (const group of groups) {
@@ -111,6 +123,7 @@ export function computeFooterDraftHash(settings = {}) {
111
123
  const hasAsideCta = settings.asideCta !== undefined && settings.asideCta !== null;
112
124
  append(canonical, hasAsideCta);
113
125
  if (hasAsideCta) appendLink(canonical, settings.asideCta);
126
+ append(canonical, settings.asideSpan ?? 1);
114
127
  appendRichText(canonical, settings.bottomContent);
115
128
  appendLinks(canonical, settings.bottomLinks);
116
129
  appendSocialLinks(canonical, settings.socialLinks);
@@ -127,6 +140,10 @@ export function computeFooterDraftHash(settings = {}) {
127
140
  return createHash("sha256").update(canonical.join(""), "utf8").digest("hex");
128
141
  }
129
142
 
143
+ function appendAlignment(canonical, profile, inlineOnly = false) {
144
+ for (const value of footerAlignmentProfileValues(profile, inlineOnly)) append(canonical, value);
145
+ }
146
+
130
147
  function appendLinks(canonical, rawLinks) {
131
148
  const links = Array.isArray(rawLinks) ? rawLinks : [];
132
149
  append(canonical, links.length);
@@ -186,6 +203,7 @@ function appendScheme(canonical, rawScheme) {
186
203
  append(canonical, scheme.headingColor ?? "");
187
204
  append(canonical, scheme.linkColor ?? "");
188
205
  append(canonical, scheme.linkHoverColor ?? "");
206
+ append(canonical, normalizeId(scheme.logoId));
189
207
  append(canonical, normalizeId(scheme.backgroundImageId));
190
208
  append(canonical, doubleBits(scheme.backgroundImageOpacity ?? 1));
191
209
  append(canonical, presentationValue(scheme.backgroundPresentation));
@@ -0,0 +1,10 @@
1
+ // Generated from shared/footer-fit-widths.ts by scripts/sync-field-validation.mjs. Do not edit.
2
+ export const FOOTER_FIT_WIDTH_LABELS = Object.freeze({
3
+ desktop: "Desktop",
4
+ small_desktop: "Small desktop",
5
+ tablet: "Tablet",
6
+ phone: "Phone",
7
+ small_phone: "Small phone (320px)",
8
+ });
9
+ export const FOOTER_WIDTHS = Object.freeze(Object.keys(FOOTER_FIT_WIDTH_LABELS));
10
+ export const MAX_FOOTER_FIT_FINDINGS = 12;
@@ -0,0 +1,328 @@
1
+ // Generated from shared/footer-fit.ts by scripts/sync-field-validation.mjs. Do not edit.
2
+ import { FOOTER_LOGO_BOX, resolveFooterLogoScale } from "./footer-branding.js";
3
+ import { FOOTER_WIDTHS, MAX_FOOTER_FIT_FINDINGS } from "./footer-fit-widths.js";
4
+ import { FOOTER_FONT_METRICS } from "./footer-font-metrics.js";
5
+ export { footerBrandSizeStyles } from "./footer-branding.js";
6
+ export { FOOTER_FIT_WIDTH_LABELS, FOOTER_WIDTHS, MAX_FOOTER_FIT_FINDINGS, } from "./footer-fit-widths.js";
7
+ const noto = (fontSize, bold = false) => ({
8
+ fontSize,
9
+ fontWeight: bold ? "700" : "400",
10
+ fontFamily: "\"Noto Sans\", sans-serif",
11
+ metricProfile: bold ? "noto700" : "noto400",
12
+ boldMetricProfile: "noto700",
13
+ });
14
+ const inika = (fontSize) => ({
15
+ fontSize,
16
+ fontWeight: "400",
17
+ fontFamily: "\"Inika\", serif",
18
+ metricProfile: "inika400",
19
+ boldMetricProfile: "inika700",
20
+ });
21
+ function assumption(width, viewportWidth, padding, brandGap, type) {
22
+ return {
23
+ width,
24
+ viewportWidth,
25
+ paddingInline: padding,
26
+ columnGap: padding,
27
+ brandGap,
28
+ type,
29
+ roles: {
30
+ link: noto(type.normal, true),
31
+ groupHeading: inika(type.normal),
32
+ brand: inika(type.medium),
33
+ brandLarge: inika(type.big),
34
+ brandLargeCompact: inika(type.large),
35
+ asideHeading: inika(type.large),
36
+ asideBody: noto(type.normal),
37
+ asideCta: noto(type.normal),
38
+ },
39
+ };
40
+ }
41
+ // Espalier 7 default scale, Taproot's default theme: 16px root, 1.25 type,
42
+ // 1.618 space, 320–1200 viewport clamps. Viewport includes both inline pads.
43
+ // Values retain clamp rounding; the production style oracle guards this table.
44
+ export const DEFAULT_FIT_ASSUMPTIONS = {
45
+ desktop: assumption(1152, 1194, 20.862692, 12.920528, {
46
+ tiny: 15.332618,
47
+ small: 19.166172,
48
+ normal: 23.95642,
49
+ medium: 29.946921,
50
+ big: 37.431957,
51
+ large: 46.791241,
52
+ huge: 58.489953,
53
+ }),
54
+ small_desktop: assumption(1025, 1063, 19.198797, 12.441313, {
55
+ tiny: 14.763867,
56
+ small: 18.455234,
57
+ normal: 23.067845,
58
+ medium: 28.836138,
59
+ big: 36.043575,
60
+ large: 45.055666,
61
+ huge: 56.320516,
62
+ }),
63
+ tablet: assumption(769, 801, 15.844804, 11.475336, {
64
+ tiny: 13.617409,
65
+ small: 17.022162,
66
+ normal: 21.276702,
67
+ medium: 26.597078,
68
+ big: 33.244946,
69
+ large: 41.557184,
70
+ huge: 51.947479,
71
+ }),
72
+ phone: assumption(360, 381, 10.486277, 9.932036, {
73
+ tiny: 11.785764,
74
+ small: 14.732605,
75
+ normal: 18.415071,
76
+ medium: 23.019829,
77
+ big: 28.773701,
78
+ large: 35.967811,
79
+ huge: 44.960869,
80
+ }),
81
+ small_phone: assumption(300.5824, 320, 9.7088, 9.7088, {
82
+ tiny: 11.52,
83
+ small: 14.4,
84
+ normal: 18,
85
+ medium: 22.5008,
86
+ big: 28.1248,
87
+ large: 35.1568,
88
+ huge: 43.94592,
89
+ }),
90
+ };
91
+ // The independent Espalier fixture uses bundled Inter v4.001, 16px inherited
92
+ // normal type, bold links/headings, and 24px gaps.
93
+ export const CALIBRATION_FIT_ASSUMPTIONS = Object.fromEntries(FOOTER_WIDTHS.map(width => {
94
+ const base = DEFAULT_FIT_ASSUMPTIONS[width];
95
+ const font = {
96
+ fontSize: 16,
97
+ fontWeight: "700",
98
+ fontFamily: "\"Inter\", sans-serif",
99
+ metricProfile: "inter700",
100
+ boldMetricProfile: "inter700",
101
+ };
102
+ return [width, {
103
+ ...base,
104
+ columnGap: 24,
105
+ brandGap: 24,
106
+ roles: Object.fromEntries(Object.keys(base.roles).map(role => [role, { ...font }])),
107
+ }];
108
+ }));
109
+ export const footerTargetIsSet = (target) => Boolean(target.pageResourceId || target.externalUrl);
110
+ /** The same presence test the generator's renderAside uses, including CTA resolution. */
111
+ export function footerAsideRenders(heading, body, ctaResolves) {
112
+ const text = (paragraph) => paragraph?.runs?.some(run => Boolean(run.text)) ?? false;
113
+ return text(heading?.paragraphs?.[0]) || (body?.paragraphs?.some(text) ?? false) || ctaResolves;
114
+ }
115
+ /** Offline callers treat set targets as present; the deploy supplies its real resolver. */
116
+ export function buildFooterFitInput(settings, brand = {}, resolves = footerTargetIsSet) {
117
+ const value = settings ?? {};
118
+ const brandText = value.showBrandText === false ? "" : brand.brandText ?? "";
119
+ const hasLogo = brand.hasLogo === true;
120
+ const paragraph = (p) => ({
121
+ runs: (p?.runs ?? []).map(run => ({
122
+ text: run.text ?? "",
123
+ bold: run.bold === true,
124
+ link: Boolean(run.link && resolves(run.link)),
125
+ })),
126
+ });
127
+ const ctaResolves = Boolean(value.asideCta && resolves(value.asideCta));
128
+ return {
129
+ enabled: value.enabled === true,
130
+ brand: value.showBrand !== false && (hasLogo || brandText)
131
+ ? {
132
+ text: brandText,
133
+ hasLogo,
134
+ logoAspectRatio: brand.logoAspectRatio,
135
+ span: value.brandSpan ?? 1,
136
+ size: value.brandSize || "standard",
137
+ logoScale: resolveFooterLogoScale(value.logoScale, value.brandSize).canonical,
138
+ }
139
+ : undefined,
140
+ columns: (value.linkColumns ?? []).flatMap(column => {
141
+ const groups = (column.groups ?? []).flatMap(group => {
142
+ const labels = (group.links ?? []).filter(resolves).map(link => link.label ?? "");
143
+ return labels.length ? [{ heading: group.heading, labels }] : [];
144
+ });
145
+ return groups.length ? [{ span: column.span ?? 1, groups }] : [];
146
+ }),
147
+ aside: footerAsideRenders(value.asideHeadingContent, value.asideBodyContent, ctaResolves)
148
+ ? {
149
+ span: value.asideSpan ?? 1,
150
+ heading: paragraph(value.asideHeadingContent?.paragraphs?.[0]),
151
+ body: (value.asideBodyContent?.paragraphs ?? []).map(paragraph),
152
+ ctaLabel: ctaResolves ? value.asideCta?.label : undefined,
153
+ }
154
+ : undefined,
155
+ };
156
+ }
157
+ /** Use the wider known scheme. An unavailable measurement keeps the 4:1 estimate. */
158
+ export function footerLogoAspectRatio(ratios) {
159
+ const known = ratios.flatMap(ratio => {
160
+ const parts = typeof ratio === "string" ? ratio.split("/").map(Number) : [];
161
+ const n = typeof ratio === "number" ? ratio : parts.length === 2 ? parts[0] / parts[1] : Number(ratio);
162
+ return Number.isFinite(n) && n > 0 ? [n] : [];
163
+ });
164
+ return known.length ? Math.max(...known) : undefined;
165
+ }
166
+ const ordinarySpace = (text) => /[^\S\u00a0\u202f\u2007\ufeff]/u.test(text);
167
+ const joiningCharacter = (text = "") => /[\u00a0\u202f\u2007\ufeff\u2060]/u.test(text);
168
+ const zeroAdvance = (text) => /[\u00ad\u200b\ufeff\u2060]/u.test(text);
169
+ const unsupportedBreakingScript = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Thai}]/u;
170
+ function characterAdvance(character) {
171
+ if (zeroAdvance(character.text))
172
+ return 0;
173
+ const metrics = FOOTER_FONT_METRICS[character.profile];
174
+ const codePoint = character.text.codePointAt(0);
175
+ const supported = metrics.advances[codePoint];
176
+ if (supported !== undefined)
177
+ return supported;
178
+ // These joining spaces retain their spacing even when a source cmap omits them.
179
+ if (character.text === "\u00a0" || character.text === "\u202f") {
180
+ return metrics.advances[32] ?? metrics.unsupportedAdvance;
181
+ }
182
+ if (character.text === "\u2007")
183
+ return metrics.advances[48] ?? metrics.unsupportedAdvance;
184
+ return metrics.unsupportedAdvance;
185
+ }
186
+ function measuredCharacters(characters, fontSize) {
187
+ let width = 0;
188
+ let previous;
189
+ for (const character of characters) {
190
+ if (zeroAdvance(character.text))
191
+ continue;
192
+ width += characterAdvance(character);
193
+ const left = previous?.text.codePointAt(0) ?? 0;
194
+ const right = character.text.codePointAt(0);
195
+ if (previous?.profile === character.profile && left >= 32 && left <= 126 && right >= 32 && right <= 126) {
196
+ width += FOOTER_FONT_METRICS[character.profile].positivePairs[left * 128 + right] ?? 0;
197
+ }
198
+ previous = character;
199
+ }
200
+ return {
201
+ text: characters.map(character => character.text).join("").replace(/[\u00ad\u200b]/gu, ""),
202
+ width: width * fontSize,
203
+ };
204
+ }
205
+ /** Bounded Latin-style break fragments, with default-font advances rather than contextual shaping. */
206
+ export function footerFitSegments(paragraph, font, availableWidth = Infinity) {
207
+ const characters = [];
208
+ for (const run of paragraph.runs ?? []) {
209
+ const profile = run.bold || run.link ? font.boldMetricProfile : font.metricProfile;
210
+ for (const text of run.text ?? "") {
211
+ if (ordinarySpace(text)) {
212
+ if (characters[characters.length - 1]?.text === " ")
213
+ continue;
214
+ characters.push({ text: " ", profile });
215
+ }
216
+ else
217
+ characters.push({ text, profile });
218
+ }
219
+ }
220
+ const result = [];
221
+ let current = [];
222
+ const append = (fragment, hyphenProfile) => {
223
+ if (hyphenProfile && fragment.length)
224
+ fragment = [...fragment, { text: "-", profile: hyphenProfile }];
225
+ const measured = measuredCharacters(fragment, font.fontSize);
226
+ if (measured.text)
227
+ result.push(measured);
228
+ };
229
+ const flush = () => {
230
+ // A discretionary hyphen is invisible when the complete word already fits.
231
+ if (measuredCharacters(current, font.fontSize).width > availableWidth) {
232
+ let fragment = [];
233
+ for (const character of current) {
234
+ if (character.softBreak) {
235
+ append(fragment, character.profile);
236
+ fragment = [];
237
+ }
238
+ else
239
+ fragment.push(character);
240
+ }
241
+ append(fragment);
242
+ }
243
+ else
244
+ append(current);
245
+ current = [];
246
+ };
247
+ for (const [index, character] of characters.entries()) {
248
+ const text = character.text;
249
+ // Look at both sides before committing a break, including across rich-text runs.
250
+ const joined = joiningCharacter(characters[index - 1]?.text) || joiningCharacter(characters[index + 1]?.text);
251
+ if ((text === " " || text === "\u200b") && !joined) {
252
+ flush();
253
+ continue;
254
+ }
255
+ if (/[\u2012-\u2014]/u.test(text) && !joined)
256
+ flush();
257
+ current.push({ ...character, softBreak: text === "\u00ad" && !joined });
258
+ if (/[-\u2010\u2012-\u2014]/u.test(text) && !joined)
259
+ flush();
260
+ }
261
+ flush();
262
+ return result;
263
+ }
264
+ const plain = (text = "") => ({ runs: [{ text }] });
265
+ /** Findings are widest first, then brand/column order/aside, once per word and region. */
266
+ export function footerFitFindings(input, assumptions = DEFAULT_FIT_ASSUMPTIONS) {
267
+ if (!input.enabled)
268
+ return { findings: [], total: 0 };
269
+ const found = [];
270
+ const seen = new Set();
271
+ const columns = input.columns.filter(column => column.groups.some(group => group.labels.length));
272
+ const brand = input.brand && (input.brand.hasLogo || input.brand.text) ? input.brand : undefined;
273
+ for (const name of FOOTER_WIDTHS) {
274
+ const a = assumptions[name];
275
+ const stacked = a.width <= 768;
276
+ const middle = !stacked && a.width <= 1024;
277
+ const brandSpan = brand?.span ?? 1;
278
+ const asideSpan = input.aside?.span ?? 1;
279
+ const totalSpan = columns.reduce((sum, column) => sum + (column.span ?? 1), 0)
280
+ + (input.aside ? asideSpan : 0) + (brand && !middle ? brandSpan : 0);
281
+ const regionWidth = (span) => stacked || (middle && totalSpan === 0)
282
+ ? a.width
283
+ : span * (a.width - (totalSpan - 1) * a.columnGap) / totalSpan + (span - 1) * a.columnGap;
284
+ const check = (paragraph, font, width, region) => {
285
+ for (const word of footerFitSegments(paragraph, font, Math.max(0, width))) {
286
+ const key = `${region}\0${word.text}`;
287
+ if (unsupportedBreakingScript.test(word.text) || word.width <= Math.max(0, width) || seen.has(key))
288
+ continue;
289
+ seen.add(key);
290
+ found.push({
291
+ ruleId: "footer.word_breaks",
292
+ severity: "warning",
293
+ subject: word.text,
294
+ detail: `${name}:${region}`,
295
+ });
296
+ }
297
+ };
298
+ if (brand) {
299
+ let width = middle || stacked ? a.width : regionWidth(brandSpan);
300
+ if (brand.hasLogo && brand.text) {
301
+ const scale = resolveFooterLogoScale(brand.logoScale, brand.size).scale;
302
+ const height = FOOTER_LOGO_BOX.heightRem * 16 * scale;
303
+ const max = FOOTER_LOGO_BOX.maxWidthRem * 16 * scale;
304
+ width -= Math.min(height * (brand.logoAspectRatio ?? 4), max) + a.brandGap;
305
+ }
306
+ const font = brand.size === "large" ? (stacked ? a.roles.brandLargeCompact : a.roles.brandLarge) : a.roles.brand;
307
+ check(plain(brand.text), font, width, "brand");
308
+ }
309
+ for (const column of columns) {
310
+ const width = regionWidth(column.span ?? 1);
311
+ for (const group of column.groups) {
312
+ if (!group.labels.length)
313
+ continue;
314
+ check(plain(group.heading), a.roles.groupHeading, width, "column");
315
+ for (const label of group.labels)
316
+ check(plain(label), a.roles.link, width, "column");
317
+ }
318
+ }
319
+ if (input.aside) {
320
+ const width = regionWidth(asideSpan);
321
+ check(input.aside.heading ?? {}, a.roles.asideHeading, width, "aside");
322
+ for (const p of input.aside.body ?? [])
323
+ check(p, a.roles.asideBody, width, "aside");
324
+ check(plain(input.aside.ctaLabel), a.roles.asideCta, width, "aside");
325
+ }
326
+ }
327
+ return { findings: found.slice(0, MAX_FOOTER_FIT_FINDINGS), total: found.length };
328
+ }