@jonandev/longbeard 0.11.2 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/AGENTS.md +4 -5
  2. package/CHANGELOG.md +72 -0
  3. package/README.md +64 -43
  4. package/beard.json +124 -28
  5. package/bin/long-beard.js +171 -36
  6. package/package.json +1 -1
  7. package/skills/design/SKILL.md +51 -126
  8. package/skills/design-brief/SKILL.md +146 -0
  9. package/skills/design-brief/references/graphic.md +59 -0
  10. package/skills/design-check/SKILL.md +27 -11
  11. package/skills/design-check/references/design-rules.md +21 -7
  12. package/skills/design-check/scripts/capture.mjs +121 -37
  13. package/skills/design-check/scripts/contrast.mjs +23 -9
  14. package/skills/design-check/scripts/rules.mjs +613 -0
  15. package/skills/design-check/scripts/screen-contrast.mjs +428 -0
  16. package/skills/design-check/scripts/tokens.mjs +346 -0
  17. package/skills/design-content/SKILL.md +118 -0
  18. package/skills/design-content/scripts/contact-sheet.mjs +259 -0
  19. package/skills/design-content/scripts/pictures.mjs +197 -0
  20. package/skills/design-direction/SKILL.md +115 -200
  21. package/skills/design-direction/references/controls.md +58 -0
  22. package/skills/{design → design-direction}/references/design-rules.md +21 -7
  23. package/skills/design-direction/references/five-second.md +47 -0
  24. package/skills/design-direction/references/full-build.md +19 -0
  25. package/skills/design-direction/references/graphic.md +35 -0
  26. package/skills/design-direction/references/grid.md +114 -0
  27. package/skills/design-direction/references/library.md +75 -0
  28. package/skills/design-direction/references/screen-template.html +233 -0
  29. package/skills/design-direction/references/subject.md +33 -0
  30. package/skills/design-direction/scripts/capture.mjs +121 -37
  31. package/skills/design-direction/scripts/contact-sheet.mjs +18 -7
  32. package/skills/design-direction/scripts/fonts.mjs +276 -0
  33. package/skills/design-direction/scripts/layout-diff.mjs +118 -0
  34. package/skills/design-direction/scripts/measure.mjs +1728 -0
  35. package/skills/design-direction/scripts/moodboard.mjs +123 -32
  36. package/skills/design-direction/scripts/picture-cast.mjs +270 -0
  37. package/skills/design-direction/scripts/pictures.mjs +197 -0
  38. package/skills/design-direction/scripts/screen-contrast.mjs +428 -0
  39. package/skills/design-direction/scripts/sheet.mjs +67 -0
  40. package/skills/design-direction/scripts/subject-colours.mjs +410 -0
  41. package/skills/design-families/SKILL.md +138 -0
  42. package/skills/design-families/scripts/contact-sheet.mjs +259 -0
  43. package/skills/design-families/scripts/families.mjs +606 -0
  44. package/skills/design-families/scripts/measure.mjs +1728 -0
  45. package/skills/design-families/scripts/moodboard.mjs +467 -0
  46. package/skills/design-families/scripts/tag.mjs +289 -0
  47. package/skills/design-figma/SKILL.md +69 -0
  48. package/skills/design-figma/references/graphic.md +26 -0
  49. package/skills/design-identity/SKILL.md +134 -0
  50. package/skills/design-identity/references/graphic.md +8 -0
  51. package/skills/design-identity/references/identity-board.html +140 -0
  52. package/skills/{design → design-identity}/scripts/capture.mjs +121 -37
  53. package/skills/design-identity/scripts/colours.mjs +213 -0
  54. package/skills/design-identity/scripts/contrast.mjs +41 -0
  55. package/skills/design-identity/scripts/fonts.mjs +276 -0
  56. package/skills/design-identity/scripts/measure.mjs +1728 -0
  57. package/skills/design-identity/scripts/sheet.mjs +67 -0
  58. package/skills/design-identity/scripts/subject-colours.mjs +410 -0
  59. package/skills/design-moodboard/SKILL.md +184 -0
  60. package/skills/design-moodboard/references/graphic.md +25 -0
  61. package/skills/{design-direction → design-moodboard}/references/sources.md +21 -11
  62. package/skills/design-moodboard/scripts/capture.mjs +893 -0
  63. package/skills/design-moodboard/scripts/contact-sheet.mjs +259 -0
  64. package/skills/design-moodboard/scripts/crops.mjs +256 -0
  65. package/skills/design-moodboard/scripts/dribbble-cards.mjs +418 -0
  66. package/skills/design-moodboard/scripts/families.mjs +606 -0
  67. package/skills/design-moodboard/scripts/group.mjs +221 -0
  68. package/skills/design-moodboard/scripts/measure.mjs +1728 -0
  69. package/skills/design-moodboard/scripts/moodboard.mjs +467 -0
  70. package/skills/{design-direction → design-moodboard}/scripts/picker.mjs +121 -20
  71. package/skills/design-moodboard/scripts/pictures.mjs +197 -0
  72. package/skills/design-moodboard/scripts/tag.mjs +289 -0
  73. package/skills/design-prototype/SKILL.md +149 -0
  74. package/skills/design-prototype/references/design-md-template.md +82 -0
  75. package/skills/design-prototype/references/design-rules.md +203 -0
  76. package/skills/design-prototype/references/five-second.md +47 -0
  77. package/skills/design-prototype/references/graphic.md +27 -0
  78. package/skills/design-prototype/scripts/capture.mjs +893 -0
  79. package/skills/design-prototype/scripts/contrast.mjs +41 -0
  80. package/skills/design-prototype/scripts/links.mjs +308 -0
  81. package/skills/design-prototype/scripts/pictures.mjs +197 -0
  82. package/skills/design-prototype/scripts/rules.mjs +613 -0
  83. package/skills/design-prototype/scripts/screen-contrast.mjs +428 -0
  84. package/skills/design-prototype/scripts/sheet.mjs +67 -0
  85. package/skills/design-prototype/scripts/tokens.mjs +346 -0
  86. package/skills/design-wordmark/SKILL.md +65 -0
  87. package/skills/design-wordmark/scripts/capture.mjs +893 -0
  88. package/skills/design-wordmark/scripts/sheet.mjs +67 -0
  89. package/skills/framework-skills/SKILL.md +71 -0
  90. package/skills/idea/SKILL.md +13 -7
  91. package/skills/idea/references/graphic.md +64 -0
  92. package/skills/design/references/design-md-template.md +0 -58
  93. package/skills/design/scripts/contrast.mjs +0 -27
  94. package/skills/design-direction/scripts/dribbble-cards.mjs +0 -356
@@ -2,10 +2,10 @@
2
2
  // Full-page capture. Unsticks fixed/sticky chrome first so Chrome does not
3
3
  // repeat the same fold. Node 22+. Uses an installed Chrome/Edge/Chromium.
4
4
  //
5
- // node capture.mjs <url> --out design-exploration/<slug>/refs/name.jpg
6
- // node capture.mjs <url> --out design-exploration/<slug>/refs/name.jpg --review /tmp/name-review.jpg
7
- // node capture.mjs <url> --out design-exploration/<slug>/refs/name.jpg --clip x,y,w,h
8
- // node capture.mjs <url> --out design-exploration/<slug>/refs/name.jpg --clip-selector "main"
5
+ // node capture.mjs <url> --out .longbeard/design/moodboard/refs/name.jpg
6
+ // node capture.mjs <url> --out .longbeard/design/moodboard/refs/name.jpg --review /tmp/name-review.jpg
7
+ // node capture.mjs <url> --out .longbeard/design/moodboard/refs/name.jpg --clip x,y,w,h
8
+ // node capture.mjs <url> --out .longbeard/design/moodboard/refs/name.jpg --clip-selector "main"
9
9
  // node capture.mjs <url> --out /tmp/mobile.png --width 390
10
10
  // node capture.mjs http://localhost:3000/app --out /tmp/app.png --cookie "session=abc; theme=dark" --strict
11
11
  //
@@ -13,7 +13,7 @@
13
13
  // www) is named on stderr. With --strict that is exit 1, so a 0 means the
14
14
  // shot is of the page asked for.
15
15
  // node capture.mjs file:///abs/path/moodboard.html --out /tmp/moodboard.jpg
16
- // node capture.mjs file:///abs/path/shot.jpg --out design-exploration/<slug>/refs/name.jpg --clip x,y,w,h
16
+ // node capture.mjs file:///abs/path/shot.jpg --out .longbeard/design/moodboard/refs/name.jpg --clip x,y,w,h
17
17
  //
18
18
  // Before the shot it waits out loaders: until no loader covers the screen and
19
19
  // the screen is not one flat colour and the network is quiet, for at most --wait ms (15 s on the web,
@@ -27,19 +27,20 @@
27
27
  // start as the page opens, so the loader and the intro are in them. With
28
28
  // --scroll they start once it has loaded, and each frame scrolls half a
29
29
  // screen by the wheel, so scroll-driven motion plays.
30
- // node capture.mjs <url> --out design-exploration/<slug>/refs/name-intro --frames 12
31
- // node capture.mjs <url> --out design-exploration/<slug>/refs/name-scroll --frames 12 --every 400 --scroll
30
+ // node capture.mjs <url> --out .longbeard/design/moodboard/refs/name-intro --frames 12
31
+ // node capture.mjs <url> --out .longbeard/design/moodboard/refs/name-scroll --frames 12 --every 400 --scroll
32
32
  //
33
33
  // --click "css" clicks that element once the page has loaded, as a person
34
34
  // would. Alone it keeps the screen after the click (an open menu). With
35
35
  // --frames the first frame is before the click and the rest record what the
36
36
  // click does (a menu opening, a page transition).
37
- // node capture.mjs <url> --out design-exploration/<slug>/refs/name-menu.jpg --click "button[aria-label*=menu i]"
38
- // node capture.mjs <url> --out design-exploration/<slug>/refs/name-menu-open --frames 10 --every 150 --click "header button"
39
- import { spawn, execSync } from "node:child_process";
37
+ // node capture.mjs <url> --out .longbeard/design/moodboard/refs/name-menu.jpg --click "button[aria-label*=menu i]"
38
+ // node capture.mjs <url> --out .longbeard/design/moodboard/refs/name-menu-open --frames 10 --every 150 --click "header button"
39
+ import { spawn } from "node:child_process";
40
40
  import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
41
41
  import { tmpdir } from "node:os";
42
- import { dirname, isAbsolute, join, resolve } from "node:path";
42
+ import { dirname, join, resolve } from "node:path";
43
+ import { fileURLToPath } from "node:url";
43
44
  import { inflateSync } from "node:zlib";
44
45
 
45
46
  const args = process.argv.slice(2);
@@ -47,6 +48,11 @@ const flag = (name, def) => {
47
48
  const i = args.indexOf(`--${name}`);
48
49
  if (i === -1) return def;
49
50
  const v = args[i + 1];
51
+ // A flag that needs a value and has none is a usage error, not a plain shot.
52
+ if (v === undefined || /^--[a-z]/.test(v)) {
53
+ console.error(`--${name} needs a value`);
54
+ process.exit(2);
55
+ }
50
56
  args.splice(i, 2);
51
57
  return v;
52
58
  };
@@ -73,7 +79,7 @@ const waitArg = flag("wait", "") || "";
73
79
  const width = Math.round(Number(flag("width", "1440")));
74
80
  if (!url || !/^(https?|file):\/\//.test(url) || !outArg) {
75
81
  console.error(
76
- 'usage: node capture.mjs <https://url | file:///path> --out design-exploration/<slug>/refs/name.jpg [--review /tmp/name-review.jpg] [--clip x,y,w,h | --clip-selector "css"] [--width 390] [--cookie "name=value; …"] [--strict] [--wait ms] [--click "css"] [--frames N [--every ms] [--scroll]]',
82
+ 'usage: node capture.mjs <https://url | file:///path> --out .longbeard/design/moodboard/refs/name.jpg [--review /tmp/name-review.jpg] [--clip x,y,w,h | --clip-selector "css"] [--width 390] [--cookie "name=value; …"] [--strict] [--wait ms] [--click "css"] [--frames N [--every ms] [--scroll]]',
77
83
  );
78
84
  process.exit(2);
79
85
  }
@@ -104,7 +110,8 @@ if ((frames || clickArg) && (reviewArg || clipArg || clipSelector)) {
104
110
  process.exit(2);
105
111
  }
106
112
  // A local page has no loader worth waiting for, so a sparse screen costs little.
107
- // A full page stops at 14000 px; our own files (a picker, a moodboard) can run longer.
113
+ // A full page stops at 14000 px; our own files (a picker, a moodboard) run to 30000 px a part,
114
+ // and a longer one is kept in parts (see below).
108
115
  const tallest = /^file:/.test(url) ? 30000 : 14000;
109
116
  const local = /^file:/.test(url) || /^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:|\/|$)/.test(url);
110
117
  const maxWait = waitArg ? Number(waitArg) : local ? 2000 : 15000;
@@ -119,23 +126,26 @@ if (clipArg && clipSelector) {
119
126
  process.exit(2);
120
127
  }
121
128
 
122
- function projectRoot() {
123
- try {
124
- return execSync("git rev-parse --show-toplevel", { encoding: "utf8" }).trim();
125
- } catch {
126
- return process.cwd();
127
- }
128
- }
129
-
129
+ // A relative path is relative to the folder the command runs in.
130
130
  function resolveOut(path) {
131
- if (!path) return "";
132
- if (isAbsolute(path)) return path;
133
- return resolve(projectRoot(), path);
131
+ return path ? resolve(path) : "";
134
132
  }
135
133
 
136
134
  const out = resolveOut(outArg);
137
135
  const reviewOut = resolveOut(reviewArg);
138
136
 
137
+ // A page that is not there would otherwise be a black shot that looks like a result.
138
+ if (/^file:/.test(url)) {
139
+ let file = "";
140
+ try {
141
+ file = fileURLToPath(url.replace(/[?#].*$/, ""));
142
+ } catch {}
143
+ if (!file || !existsSync(file)) {
144
+ console.error(`no such file: ${file || url}`);
145
+ process.exit(1);
146
+ }
147
+ }
148
+
139
149
  function parseClip(s) {
140
150
  if (!s) return null;
141
151
  const parts = s.split(",").map((n) => Number(n.trim()));
@@ -155,6 +165,8 @@ const clip = parseClip(clipArg);
155
165
 
156
166
  function findChrome() {
157
167
  const env = process.env.CHROME_PATH;
168
+ // CHROME_PATH=none: run as if no browser were installed (a quick test run).
169
+ if (env === "none") return null;
158
170
  if (env && existsSync(env)) return env;
159
171
  const home = process.env.HOME || process.env.USERPROFILE || "";
160
172
  const local = process.env.LOCALAPPDATA || "";
@@ -200,6 +212,13 @@ const drop = () => {
200
212
  } catch {}
201
213
  };
202
214
  process.once("exit", drop);
215
+ // An interrupt or a harness timeout must not leave Chrome or its profile behind.
216
+ for (const [sig, code] of [["SIGINT", 130], ["SIGTERM", 143]]) {
217
+ process.once(sig, () => {
218
+ drop();
219
+ process.exit(code);
220
+ });
221
+ }
203
222
 
204
223
  proc = spawn(
205
224
  exe,
@@ -218,6 +237,10 @@ proc = spawn(
218
237
  ],
219
238
  { stdio: ["ignore", "ignore", "pipe"] },
220
239
  );
240
+ proc.on("error", (e) => {
241
+ console.error(`could not start ${exe}: ${e.message}`);
242
+ process.exit(1);
243
+ });
221
244
 
222
245
  let buf = "";
223
246
  const wsUrl = await new Promise((resolveWs, reject) => {
@@ -238,8 +261,15 @@ const wsUrl = await new Promise((resolveWs, reject) => {
238
261
 
239
262
  const ws = new WebSocket(wsUrl);
240
263
  await new Promise((res, rej) => {
241
- ws.onopen = res;
242
- ws.onerror = () => rej(new Error("CDP socket failed"));
264
+ const t = setTimeout(() => rej(new Error("CDP socket did not open")), 10000);
265
+ ws.onopen = () => {
266
+ clearTimeout(t);
267
+ res();
268
+ };
269
+ ws.onerror = () => {
270
+ clearTimeout(t);
271
+ rej(new Error("CDP socket failed"));
272
+ };
243
273
  });
244
274
  let nextId = 1;
245
275
  const pending = new Map();
@@ -257,6 +287,10 @@ ws.onmessage = (ev) => {
257
287
  inflight.delete(msg.params.requestId);
258
288
  lastNetwork = Date.now();
259
289
  }
290
+ // An alert, confirm or beforeunload prompt holds the page until it is answered.
291
+ if (msg.method === "Page.javascriptDialogOpening") {
292
+ send("Page.handleJavaScriptDialog", { accept: false }, msg.sessionId).catch(() => {});
293
+ }
260
294
  if (msg.id && pending.has(msg.id)) {
261
295
  const { resolve: ok, reject: bad } = pending.get(msg.id);
262
296
  pending.delete(msg.id);
@@ -264,6 +298,10 @@ ws.onmessage = (ev) => {
264
298
  else ok(msg.result);
265
299
  }
266
300
  };
301
+ // If Chrome goes away, every call waiting on it fails now, not at its time limit.
302
+ ws.onclose = () => {
303
+ for (const { reject: bad } of [...pending.values()]) bad(new Error("Chrome closed the connection"));
304
+ };
267
305
  const send = (method, params = {}, sessionId, timeoutMs = 30000) => {
268
306
  const id = nextId++;
269
307
  return new Promise((resolveSend, reject) => {
@@ -455,18 +493,41 @@ async function settle() {
455
493
  // Clicks a consent button, which can be any element, or else strips a short
456
494
  // privacy notice. Rejecting comes first: a script has no use for tracking,
457
495
  // and accepting can start scripts that redraw the page.
458
- // A local page is a state someone set up on purpose, so it is left as it is.
496
+ // A local page is a state someone set up on purpose, so it is left as it is, and
497
+ // so is a record of motion (--frames): nothing there is cleaned up.
459
498
  const dismissConsent = () =>
460
499
  local ? Promise.resolve(false) : evalValue(`(() => {
461
- const accept = /^(accept( all)?( cookies)?|accept (and|&) close|agree|i agree|ok|okay|got it|allow( all)?( cookies)?|i understand|acepto|aceptar)$/i;
462
- const reject = /^(reject( all)?( cookies)?|decline( all)?|deny|only (essential|necessary)( cookies)?)$/i;
500
+ // The common consent tools first, by their own ids: reject, then accept.
501
+ const known = [
502
+ "#onetrust-reject-all-handler", "#CybotCookiebotDialogBodyButtonDecline", "#didomi-notice-disagree-button",
503
+ ".cky-btn-reject", "#cmplz-deny, .cmplz-deny", "[data-cookiefirst-action=reject]", ".iubenda-cs-reject-btn",
504
+ "#onetrust-accept-btn-handler", "#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll", "#didomi-notice-agree-button",
505
+ ".cky-btn-accept", ".cmplz-accept", "[data-cookiefirst-action=accept]", ".iubenda-cs-accept-btn",
506
+ ];
507
+ for (const sel of known) {
508
+ const el = document.querySelector(sel);
509
+ if (el && el.getClientRects().length) {
510
+ el.click();
511
+ return true;
512
+ }
513
+ }
514
+ const accept = /^(accept( all)?( cookies)?|accept (and|&) (close|continue)|agree|i agree|ok|okay|got it|allow( all)?( cookies)?|i understand|acepto|aceptar( todas?| todo| cookies)?|aceptar y (cerrar|continuar)|onartu( guztiak)?|accepter( tout)?|tout accepter|j'accepte|alle akzeptieren|akzeptieren|accetta( tutti)?|accetto|aceitar( todos)?)$/i;
515
+ const reject = /^(reject( all)?( cookies)?|decline( all)?|deny|only (essential|necessary)( cookies)?|rechazar( todas?| todo| cookies)?|denegar|ukatu( guztiak)?|refuser( tout)?|tout refuser|continuer sans accepter|alle ablehnen|ablehnen|rifiuta( tutti)?|rejeitar( todos)?)$/i;
463
516
  const clickable = [...document.querySelectorAll("button, [role=button], a, input[type=button], input[type=submit], [onclick], div, span")]
464
517
  .filter((el) => el.childElementCount <= 2 && el.getClientRects().length);
465
- // Only inside a notice about cookies or privacy, so a menu or a gate is never clicked.
518
+ // Only inside something that looks like a notice about cookies or privacy: a
519
+ // dialog, a layer fixed or sticky over the page, or one named like a banner.
520
+ // Never the page itself, so a short page that mentions privacy is not a notice.
521
+ const about = /cookie|consent|privacy|gdpr|datenschutz|privacidad|consentimiento|confidentialit|privacit/i;
522
+ const named = /cookie|consent|gdpr|privacy|banner|notice|popup|modal|dialog/i;
466
523
  const notice = (el) => {
467
- for (let up = el, i = 0; up && i < 8; up = up.parentElement, i++) {
524
+ for (let up = el, i = 0; up && up !== document.body && up !== document.documentElement && i < 8; up = up.parentElement, i++) {
468
525
  const t = up.innerText || "";
469
- if (t.length < 1500 && /cookie|consent|privacy|gdpr|datenschutz/i.test(t)) return true;
526
+ if (t.length >= 1500 || !about.test(t)) continue;
527
+ const pos = getComputedStyle(up).position;
528
+ const label = (typeof up.className === "string" ? up.className : "") + " " + up.id;
529
+ if (up.tagName === "DIALOG" || /^(dialog|alertdialog)$/.test(up.getAttribute("role") || "") ||
530
+ up.getAttribute("aria-modal") === "true" || pos === "fixed" || pos === "sticky" || named.test(label)) return true;
470
531
  }
471
532
  return false;
472
533
  };
@@ -513,7 +574,14 @@ async function clickOn(selector) {
513
574
  }
514
575
 
515
576
  const navigateStarted = Date.now();
516
- await page("Page.navigate", { url: target }, 40000);
577
+ // A page that cannot be reached comes back as Chrome's own error page, which would be saved as if
578
+ // it were the site: say so and stop.
579
+ const nav = await page("Page.navigate", { url: target }, 40000);
580
+ if (nav?.errorText && nav.errorText !== "net::ERR_ABORTED") {
581
+ console.error(`could not reach ${target}: ${nav.errorText}`);
582
+ drop();
583
+ process.exit(1);
584
+ }
517
585
 
518
586
  if (frames) {
519
587
  // The frames are the record of motion, so nothing is paused, unstuck or cleaned up.
@@ -523,10 +591,6 @@ if (frames) {
523
591
  if (scrollFrames || clickArg) {
524
592
  const why = await settle();
525
593
  if (why) console.error(`waited ${maxWait} ms: ${why}`);
526
- if (await dismissConsent()) {
527
- await sleep(800);
528
- await settle();
529
- }
530
594
  await sleep(600);
531
595
  t0 = Date.now();
532
596
  }
@@ -745,6 +809,26 @@ mkdirSync(dirname(out), { recursive: true });
745
809
  writeFileSync(out, Buffer.from(data, "base64"));
746
810
  console.log(out);
747
811
 
812
+ // A local page longer than one shot (a families board of hundreds) is kept whole, in parts:
813
+ // <out>-2, <out>-3 … each up to the same height, so nothing below the first part is lost.
814
+ const realHeight = Math.ceil(size.height || height);
815
+ if (clipBox && (shotClip.height < clipBox.height || shotClip.width < clipBox.width)) {
816
+ console.error(`the clip reaches past the page (${fullWidth}x${realHeight} px): kept ${shotClip.width}x${shotClip.height} px of ${clipBox.width}x${clipBox.height}`);
817
+ } else if (!clipBox && !/^file:/.test(url) && realHeight > fullHeight) {
818
+ console.error(`the page is ${realHeight} px, over the ${tallest} px limit: kept the first ${fullHeight} px`);
819
+ }
820
+ if (!clipBox && /^file:/.test(url) && realHeight > fullHeight) {
821
+ const parts = Math.min(10, Math.ceil(realHeight / tallest));
822
+ for (let i = 1; i < parts; i++) {
823
+ const y = i * tallest;
824
+ const { data: part } = await page("Page.captureScreenshot", { format, ...(format === "png" ? {} : { quality: 70 }), captureBeyondViewport: true, clip: { x: 0, y, width: fullWidth, height: Math.min(tallest, realHeight - y), scale: 1 } }, 60000);
825
+ const partOut = out.replace(/(\.[a-z]+)?$/i, (ext) => `-${i + 1}${ext || ".jpg"}`);
826
+ writeFileSync(partOut, Buffer.from(part, "base64"));
827
+ console.log(partOut);
828
+ }
829
+ if (realHeight > parts * tallest) console.error(`the page is ${realHeight} px: kept the first ${parts * tallest} px in ${parts} parts`);
830
+ }
831
+
748
832
  if (reviewOut) {
749
833
  // Side-by-side columns ~2800 px tall so a tall capture stays readable.
750
834
  // Real capture stays the single tall file; review is scratch only.
@@ -1,27 +1,41 @@
1
1
  #!/usr/bin/env node
2
2
  // WCAG 2.x contrast for colour pairs.
3
3
  // node contrast.mjs "#2c2824 on #f6f1e7" "#b0563a on #f6f1e7:mark"
4
- // Suffix :large or :mark checks against 3:1 instead of 4.5:1.
4
+ // Suffix :large or :mark checks against 3:1 instead of 4.5:1. --level AAA (the design brief's
5
+ // accessibility line) asks 7:1 for text and 4.5:1 for large text; a mark stays 3:1. Short hex (#abc) is read as #aabbcc.
6
+ // An argument it cannot read is an error: it exits 1 rather than pass unchecked.
7
+ // The ratio is cut, not rounded, to two places: 4.496 prints as 4.49 and fails, never as 4.50 FAIL.
8
+ const hex6 = (h) => (h.length === 4 ? `#${[...h.slice(1)].map((c) => c + c).join("")}` : h).toLowerCase();
5
9
  const lum = (hex) => {
6
10
  const [r, g, b] = [1, 3, 5]
7
11
  .map((i) => parseInt(hex.slice(i, i + 2), 16) / 255)
8
12
  .map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
9
13
  return 0.2126 * r + 0.7152 * g + 0.0722 * b;
10
14
  };
15
+ const args = process.argv.slice(2);
16
+ const at = args.indexOf("--level");
17
+ const level = at === -1 ? "AA" : String(args.splice(at, 2)[1] || "").toUpperCase();
18
+ if (level !== "AA" && level !== "AAA") {
19
+ console.error("contrast: --level is AA or AAA");
20
+ process.exit(2);
21
+ }
11
22
  let failed = false;
12
- for (const arg of process.argv.slice(2)) {
13
- const m = arg.match(/^(#[0-9a-f]{6})\s+on\s+(#[0-9a-f]{6})(?::(large|mark))?$/i);
23
+ for (const arg of args) {
24
+ const m = arg.trim().match(/^(#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s+on\s+(#[0-9a-f]{3}(?:[0-9a-f]{3})?)(?::(large|mark))?$/i);
14
25
  if (!m) {
15
- console.error(`skip: ${arg}`);
26
+ console.error(`cannot read: ${arg} (write "#rrggbb on #rrggbb", optionally :large or :mark)`);
27
+ failed = true;
16
28
  continue;
17
29
  }
30
+ m[1] = hex6(m[1]);
31
+ m[2] = hex6(m[2]);
18
32
  const [a, b] = [lum(m[1]), lum(m[2])];
19
33
  const ratio = (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
20
- const need = m[3] ? 3 : 4.5;
21
- const verdict = ratio >= 7 && !m[3] ? "AAA" : ratio >= need ? "AA" : "FAIL";
34
+ const kind = (m[3] || "").toLowerCase();
35
+ const need = kind === "mark" ? 3 : kind === "large" ? (level === "AAA" ? 4.5 : 3) : level === "AAA" ? 7 : 4.5;
36
+ const best = kind === "mark" ? "" : ratio >= (kind ? 4.5 : 7) ? "AAA" : "";
37
+ const verdict = ratio < need ? "FAIL" : best || "AA";
22
38
  if (verdict === "FAIL") failed = true;
23
- console.log(
24
- `${m[1]} on ${m[2]} ${ratio.toFixed(2)}:1 ${verdict}${m[3] ? ` (${m[3]}, needs 3:1)` : ""}`,
25
- );
39
+ console.log(`${m[1]} on ${m[2]} ${(Math.floor(ratio * 100) / 100).toFixed(2)}:1 ${verdict}${kind || level === "AAA" ? ` (${[kind, `needs ${need}:1`].filter(Boolean).join(", ")})` : ""}`);
26
40
  }
27
41
  process.exit(failed ? 1 : 0);