@uselazuli/cli 0.0.0-stage → 1.0.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.
- package/CHANGELOG.md +98 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/agents/lazuli-design-reviewer.md +30 -0
- package/agents/lazuli-first-look.md +22 -0
- package/agents/lazuli-task-tester.md +25 -0
- package/bin/lazuli.mjs +699 -0
- package/package.json +11 -4
- package/rules/accessibility/HYPOTHESES.md +8 -0
- package/rules/accessibility/_area.yaml +7 -0
- package/rules/accessibility/operable.yaml +100 -0
- package/rules/accessibility/perceivable.yaml +153 -0
- package/rules/accessibility/robust.yaml +24 -0
- package/rules/accessibility/understandable.yaml +72 -0
- package/rules/color/HYPOTHESES.md +27 -0
- package/rules/color/_area.yaml +10 -0
- package/rules/color/adapt.yaml +183 -0
- package/rules/color/create.yaml +259 -0
- package/rules/color/review.yaml +215 -0
- package/rules/color/signal.yaml +196 -0
- package/rules/content/HYPOTHESES.md +18 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +347 -0
- package/rules/content/voice.yaml +144 -0
- package/rules/content/writing.yaml +202 -0
- package/rules/data/HYPOTHESES.md +11 -0
- package/rules/data/_area.yaml +7 -0
- package/rules/data/charts.yaml +189 -0
- package/rules/data/dashboards.yaml +141 -0
- package/rules/data/review.yaml +159 -0
- package/rules/graphic/HYPOTHESES.md +17 -0
- package/rules/graphic/_area.yaml +8 -0
- package/rules/graphic/identity.yaml +141 -0
- package/rules/graphic/pieces.yaml +164 -0
- package/rules/graphic/review.yaml +242 -0
- package/rules/graphic/symbol.yaml +141 -0
- package/rules/motion/HYPOTHESES.md +19 -0
- package/rules/motion/_area.yaml +7 -0
- package/rules/motion/micro.yaml +143 -0
- package/rules/motion/review.yaml +347 -0
- package/rules/motion/transitions.yaml +169 -0
- package/rules/typography/HYPOTHESES.md +12 -0
- package/rules/typography/_area.yaml +8 -0
- package/rules/typography/adapt.yaml +45 -0
- package/rules/typography/create.yaml +126 -0
- package/rules/typography/review.yaml +127 -0
- package/rules/ui/HYPOTHESES.md +23 -0
- package/rules/ui/_area.yaml +9 -0
- package/rules/ui/compose.yaml +219 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +399 -0
- package/rules/ui/states.yaml +257 -0
- package/rules/ux/HYPOTHESES.md +13 -0
- package/rules/ux/_area.yaml +8 -0
- package/rules/ux/flows.yaml +164 -0
- package/rules/ux/forms.yaml +142 -0
- package/rules/ux/review.yaml +186 -0
- package/skills/lazuli/SKILL.md +180 -0
- package/skills/lazuli/procedures/create.md +29 -0
- package/skills/lazuli/procedures/explain-decisions.md +22 -0
- package/skills/lazuli/procedures/improve.md +30 -0
- package/skills/lazuli/procedures/remember-this.md +41 -0
- package/skills/lazuli/procedures/review-design.md +35 -0
- package/skills/lazuli/procedures/show-options.md +32 -0
- package/skills/lazuli-accessibility-check/SKILL.md +68 -0
- package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
- package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
- package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
- package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
- package/skills/lazuli-color-screen-review/SKILL.md +51 -0
- package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
- package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
- package/skills/lazuli-content-voice/SKILL.md +39 -0
- package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
- package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
- package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
- package/skills/lazuli-graphic-identity/SKILL.md +43 -0
- package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
- package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
- package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
- package/skills/lazuli-ui-composition/SKILL.md +42 -0
- package/skills/lazuli-ui-composition/references/option-template.html +34 -0
- package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
- package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
- package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
- package/skills/lazuli-ux-task-test/SKILL.md +44 -0
- package/src/browser.mjs +161 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +428 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +97 -0
- package/src/commands/draft-memo.mjs +161 -0
- package/src/commands/hooks.mjs +343 -0
- package/src/commands/install-hooks.mjs +120 -0
- package/src/commands/install.mjs +132 -0
- package/src/commands/list-rules.mjs +49 -0
- package/src/commands/map-page.mjs +594 -0
- package/src/commands/record-answers.mjs +79 -0
- package/src/commands/record-log.mjs +296 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +164 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +383 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +549 -0
- package/src/measure/chart-read.mjs +401 -0
- package/src/measure/chart.mjs +264 -0
- package/src/measure/coherence.mjs +145 -0
- package/src/measure/color.mjs +79 -0
- package/src/measure/content.mjs +82 -0
- package/src/measure/cvd.mjs +24 -0
- package/src/measure/extreme.mjs +241 -0
- package/src/measure/forms.mjs +150 -0
- package/src/measure/in-page/a11y.js +489 -0
- package/src/measure/in-page/color.js +346 -0
- package/src/measure/in-page/composition.js +1496 -0
- package/src/measure/in-page/contrast.js +15 -0
- package/src/measure/in-page/cvd.js +173 -0
- package/src/measure/in-page/extreme.js +355 -0
- package/src/measure/in-page/frames.js +781 -0
- package/src/measure/in-page/look.js +28 -0
- package/src/measure/in-page/motion-probe.js +169 -0
- package/src/measure/in-page/style.js +107 -0
- package/src/measure/in-page/svg-strokes.js +112 -0
- package/src/measure/in-page/svg-uses.js +50 -0
- package/src/measure/in-page/symbol.js +123 -0
- package/src/measure/in-page/type.js +266 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +91 -0
- package/src/measure/motion.mjs +508 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +252 -0
- package/src/measure/rhythm.mjs +81 -0
- package/src/measure/signature.mjs +65 -0
- package/src/measure/states.mjs +240 -0
- package/src/measure/style.mjs +13 -0
- package/src/measure/symbol.mjs +268 -0
- package/src/measure/terms.mjs +153 -0
- package/src/measure/tiles.mjs +20 -0
- package/src/measure/timeline.mjs +621 -0
- package/src/measure/type.mjs +167 -0
- package/src/measure/weight.mjs +18 -0
- package/src/private-strings.mjs +81 -0
- package/src/project/blind.mjs +70 -0
- package/src/project/edits.mjs +154 -0
- package/src/project/gate.mjs +159 -0
- package/src/project/lazuli-md.mjs +497 -0
- package/src/project/log.mjs +329 -0
- package/src/project/match.mjs +112 -0
- package/src/project/records.mjs +380 -0
- package/src/project/tasks.mjs +153 -0
- package/src/report/evaluate.mjs +27 -0
- package/src/report/evaluators/a11y.mjs +206 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +86 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +53 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +94 -0
- package/src/report/evaluators/motion.mjs +273 -0
- package/src/report/evaluators/piece.mjs +69 -0
- package/src/report/evaluators/rhythm.mjs +99 -0
- package/src/report/evaluators/states.mjs +39 -0
- package/src/report/evaluators/style.mjs +35 -0
- package/src/report/evaluators/symbol.mjs +110 -0
- package/src/report/evaluators/terms.mjs +49 -0
- package/src/report/evaluators/type.mjs +163 -0
- package/src/report/evaluators/weight.mjs +51 -0
- package/src/report/evidence.mjs +4 -0
- package/src/report/finding.mjs +11 -0
- package/src/report/format.mjs +215 -0
- package/src/rules/hypotheses.mjs +28 -0
- package/src/rules/load.mjs +256 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +212 -0
- package/templates/LAZULI.md +86 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { BULLET, FIELD, HEADING, SECTION_NAMES, knownSection, stripComments } from "./lazuli-md.mjs";
|
|
2
|
+
|
|
3
|
+
// Writes one entry of the Breaks or Decisions log into the text of LAZULI.md and returns the new text.
|
|
4
|
+
// Everything the entry does not own stays byte for byte: other sections, comments, hand-written lines
|
|
5
|
+
// and the line ending (LF or CRLF), including a file that has no final newline.
|
|
6
|
+
|
|
7
|
+
const STATUSES = ["trying", "kept", "reverted"];
|
|
8
|
+
const norm = (s) => String(s ?? "").trim().toLowerCase();
|
|
9
|
+
const label = (key) => key.charAt(0).toUpperCase() + key.slice(1);
|
|
10
|
+
|
|
11
|
+
export class LogInputError extends Error {
|
|
12
|
+
constructor(problems) {
|
|
13
|
+
super(problems.join("\n"));
|
|
14
|
+
this.name = "LogInputError";
|
|
15
|
+
this.code = "LOG_INPUT";
|
|
16
|
+
this.problems = problems;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const BREAKS = { section: "breaks", heading: "Breaks", open: "rule", fields: ["piece", "effect", "check", "status"] };
|
|
21
|
+
const DECISIONS = { section: "decisions", heading: "Decisions", open: "decision", fields: ["piece", "goal", "rule", "measure"] };
|
|
22
|
+
|
|
23
|
+
// Lines keep their own terminator so nothing about them changes unless we edit them.
|
|
24
|
+
function splitLines(text) {
|
|
25
|
+
const lines = [];
|
|
26
|
+
const re = /\r\n|\n|\r/g;
|
|
27
|
+
let at = 0;
|
|
28
|
+
let m;
|
|
29
|
+
while ((m = re.exec(text)) !== null) {
|
|
30
|
+
lines.push({ text: text.slice(at, m.index), eol: m[0] });
|
|
31
|
+
at = m.index + m[0].length;
|
|
32
|
+
}
|
|
33
|
+
if (at < text.length) {
|
|
34
|
+
lines.push({ text: text.slice(at), eol: "" });
|
|
35
|
+
}
|
|
36
|
+
return lines;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// What the parser sees: comments removed, one string per line.
|
|
40
|
+
function visibleLines(lines) {
|
|
41
|
+
const joined = lines.map((l) => l.text).join("\n").replace(new RegExp("^" + String.fromCharCode(0xfeff)), "");
|
|
42
|
+
return stripComments(joined, []).split("\n");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Content ranges { start, end } (end exclusive) of every section with this name, as the parser opens them.
|
|
46
|
+
function sectionRanges(vis, name) {
|
|
47
|
+
const opens = [];
|
|
48
|
+
for (let i = 0; i < vis.length; i++) {
|
|
49
|
+
const line = vis[i];
|
|
50
|
+
const heading = HEADING.exec(line);
|
|
51
|
+
const exact = heading ? SECTION_NAMES.find((n) => n === heading[2].toLowerCase()) : undefined;
|
|
52
|
+
const plain = line.trim().toLowerCase();
|
|
53
|
+
const setext = !heading && SECTION_NAMES.includes(plain) && /^[ \t]*(?:=+|-+)[ \t]*$/.test(vis[i + 1] ?? "");
|
|
54
|
+
if (heading && heading[1] === "##") {
|
|
55
|
+
opens.push({ at: i, start: i + 1, name: knownSection(heading[2]) ?? heading[2].toLowerCase() });
|
|
56
|
+
} else if (exact) {
|
|
57
|
+
opens.push({ at: i, start: i + 1, name: exact });
|
|
58
|
+
} else if (setext) {
|
|
59
|
+
opens.push({ at: i, start: i + 2, name: plain });
|
|
60
|
+
i++;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return opens
|
|
64
|
+
.map((o, n) => ({ ...o, end: n + 1 < opens.length ? opens[n + 1].at : vis.length }))
|
|
65
|
+
.filter((o) => o.name === name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A bullet read as { indent, key, value }, the way the parser reads it.
|
|
69
|
+
function readBullet(line) {
|
|
70
|
+
const bullet = BULLET.exec(line);
|
|
71
|
+
if (!bullet) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const field = FIELD.exec(bullet[2]);
|
|
75
|
+
let value = field ? field[3] : bullet[2];
|
|
76
|
+
if (field && field[1] && value.endsWith(field[1]) && !value.startsWith(field[1])) {
|
|
77
|
+
value = value.slice(0, -field[1].length).trim();
|
|
78
|
+
}
|
|
79
|
+
return { indent: bullet[1].replace(/\t/g, " ").length, key: field ? field[2].toLowerCase() : null, value };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Entries of one section range, grouped exactly as the parser groups them: a head bullet at the base
|
|
83
|
+
// indent opens an entry, field bullets (nested or flat, across blank lines) fill it, a base-indent
|
|
84
|
+
// bullet that is neither resets it. { head, last (line of its last bullet), end (range end), value,
|
|
85
|
+
// fields: { key: { line, value } } }
|
|
86
|
+
function scanEntries(vis, range, spec) {
|
|
87
|
+
const bullets = [];
|
|
88
|
+
for (let i = range.start; i < range.end; i++) {
|
|
89
|
+
const b = readBullet(vis[i]);
|
|
90
|
+
if (b !== null) {
|
|
91
|
+
bullets.push({ ...b, i });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const base = Math.min(...bullets.map((b) => b.indent));
|
|
95
|
+
const found = [];
|
|
96
|
+
let current = null;
|
|
97
|
+
for (const b of bullets) {
|
|
98
|
+
if (b.key === spec.open && b.indent <= base) {
|
|
99
|
+
current = { head: b.i, last: b.i, end: range.end, value: b.value, fields: {} };
|
|
100
|
+
found.push(current);
|
|
101
|
+
} else if (b.key && spec.fields.includes(b.key) && current) {
|
|
102
|
+
current.last = b.i;
|
|
103
|
+
if (!(b.key in current.fields)) {
|
|
104
|
+
current.fields[b.key] = { line: b.i, value: b.value };
|
|
105
|
+
}
|
|
106
|
+
} else if (b.indent > base && current) {
|
|
107
|
+
current.last = b.i;
|
|
108
|
+
} else {
|
|
109
|
+
current = null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return found;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const leading = (line) => /^[ \t]*/.exec(line)[0];
|
|
116
|
+
|
|
117
|
+
// Lines right after a field line that wrap its value: indented, not a bullet, not a Key: line.
|
|
118
|
+
function continuation(lines, vis, line, end) {
|
|
119
|
+
const out = [];
|
|
120
|
+
for (let j = line + 1; j < end; j++) {
|
|
121
|
+
if (lines[j].text.trim() === "" || !/^[ \t]/.test(vis[j]) || readBullet(vis[j]) !== null || FIELD.test(vis[j].trim())) {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
out.push(j);
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// True when the end of line k sits inside a closed comment; the entry must go after the comment ends.
|
|
130
|
+
function commentEndLine(lines, k) {
|
|
131
|
+
const text = lines.map((l) => l.text).join("\n");
|
|
132
|
+
const ends = [];
|
|
133
|
+
let at = 0;
|
|
134
|
+
for (const l of lines) {
|
|
135
|
+
at += l.text.length + 1;
|
|
136
|
+
ends.push(at - 1);
|
|
137
|
+
}
|
|
138
|
+
let from = 0;
|
|
139
|
+
for (;;) {
|
|
140
|
+
const open = text.indexOf("<!--", from);
|
|
141
|
+
if (open === -1) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const close = text.indexOf("-->", open + 4);
|
|
145
|
+
if (close === -1) {
|
|
146
|
+
from = text.indexOf("\n", open) === -1 ? text.length : text.indexOf("\n", open);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (open < ends[k] && close >= ends[k]) {
|
|
150
|
+
return ends.findIndex((e) => e >= close);
|
|
151
|
+
}
|
|
152
|
+
from = close + 3;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function insertAfter(lines, after, texts, fileEol) {
|
|
157
|
+
let k = after;
|
|
158
|
+
for (let again = commentEndLine(lines, k); again !== null; again = commentEndLine(lines, k)) {
|
|
159
|
+
k = again;
|
|
160
|
+
}
|
|
161
|
+
const eol = lines[k].eol || fileEol;
|
|
162
|
+
const added = texts.map((text) => ({ text, eol }));
|
|
163
|
+
if (lines[k].eol === "") {
|
|
164
|
+
// The file had no final newline: it gets one here and the last new line ends the file instead.
|
|
165
|
+
lines[k].eol = fileEol;
|
|
166
|
+
added[added.length - 1].eol = "";
|
|
167
|
+
}
|
|
168
|
+
lines.splice(k + 1, 0, ...added);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function clean(value, name, problems) {
|
|
172
|
+
const text = String(value).replace(/\s+/g, " ").trim();
|
|
173
|
+
if (text.includes("<!--") || text.includes("-->")) {
|
|
174
|
+
problems.push(`${name} cannot contain <!-- or -->, it would hide text in LAZULI.md.`);
|
|
175
|
+
}
|
|
176
|
+
return text;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function fieldLine(key, value) {
|
|
180
|
+
return value === "" ? `- ${label(key)}:` : `- ${label(key)}: ${value}`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Rewrites only the value of a field line: its bullet, name style, closing emphasis and every comment stay.
|
|
184
|
+
// The value goes where the old visible text was; a comment before the value stays before it.
|
|
185
|
+
function withValue(raw, key, value) {
|
|
186
|
+
const emphasis = String.raw`(?:\*\*|__|\*|_)?`;
|
|
187
|
+
const re = new RegExp(String.raw`^([ \t]*(?:[-*+]|\d+[.)])[ \t]+(${emphasis})${key}${emphasis}[ \t]*:${emphasis}[ \t]*)(.*)$`, "i");
|
|
188
|
+
const m = re.exec(raw);
|
|
189
|
+
if (!m) {
|
|
190
|
+
return raw;
|
|
191
|
+
}
|
|
192
|
+
const parts = m[3].split(/(<!--.*?(?:-->|$))/);
|
|
193
|
+
const shown = parts.filter((_, n) => n % 2 === 0).join("").trim();
|
|
194
|
+
const closing = m[2] && shown.endsWith(m[2]) && !shown.startsWith(m[2]) ? m[2] : "";
|
|
195
|
+
let placed = false;
|
|
196
|
+
const body = parts.map((p, n) => {
|
|
197
|
+
if (n % 2 === 1 || p.trim() === "") {
|
|
198
|
+
return p;
|
|
199
|
+
}
|
|
200
|
+
if (placed) {
|
|
201
|
+
return "";
|
|
202
|
+
}
|
|
203
|
+
placed = true;
|
|
204
|
+
return `${leading(p)}${value}${closing}${/\s$/.test(p) ? " " : ""}`;
|
|
205
|
+
});
|
|
206
|
+
return placed ? `${m[1]}${body.join("").trimEnd()}` : `${m[1]}${value}${closing}${m[3].trim() === "" ? "" : ` ${m[3].trim()}`}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function write(text, spec, head, given, problems) {
|
|
210
|
+
const lines = splitLines(text);
|
|
211
|
+
const fileEol = lines.find((l) => l.eol !== "")?.eol ?? "\n";
|
|
212
|
+
const vis = visibleLines(lines);
|
|
213
|
+
const ranges = sectionRanges(vis, spec.section);
|
|
214
|
+
const entries = ranges.flatMap((r) => scanEntries(vis, r, spec));
|
|
215
|
+
const hit = entries.find((e) => norm(e.value) === norm(head) && norm(e.fields.piece?.value) === norm(given.piece));
|
|
216
|
+
const keys = spec.fields.filter((k) => given[k] !== undefined);
|
|
217
|
+
|
|
218
|
+
if (hit) {
|
|
219
|
+
if (problems.length > 0) {
|
|
220
|
+
throw new LogInputError(problems);
|
|
221
|
+
}
|
|
222
|
+
const added = [];
|
|
223
|
+
for (const key of keys) {
|
|
224
|
+
if (key === "piece") {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const have = hit.fields[key];
|
|
228
|
+
if (have === undefined) {
|
|
229
|
+
added.push(key);
|
|
230
|
+
} else if (have.value !== given[key]) {
|
|
231
|
+
lines[have.line].text = withValue(lines[have.line].text, key, given[key]);
|
|
232
|
+
// The parser reads one line of a value, so wrapped value text and a person's note look the same: never delete either.
|
|
233
|
+
if (continuation(lines, vis, have.line, hit.end).length > 0) {
|
|
234
|
+
throw new LogInputError(["Nothing was written: the field continues on lines Lazuli cannot safely replace."]);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (added.length > 0) {
|
|
239
|
+
const last = Math.max(hit.head, ...Object.values(hit.fields).map((f) => f.line));
|
|
240
|
+
const sample = Object.values(hit.fields)[0]?.line;
|
|
241
|
+
const indent = sample === undefined ? `${leading(lines[hit.head].text)} ` : leading(lines[sample].text);
|
|
242
|
+
insertAfter(lines, last, added.map((k) => `${indent}${fieldLine(k, given[k])}`), fileEol);
|
|
243
|
+
}
|
|
244
|
+
return lines.filter((l) => !l.gone).map((l) => l.text + l.eol).join("");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const absent = (k) => given[k] === undefined || given[k] === "";
|
|
248
|
+
if (spec === BREAKS && (absent("effect") || absent("check"))) {
|
|
249
|
+
problems.push("A new break needs an effect and a check.");
|
|
250
|
+
}
|
|
251
|
+
if (spec === BREAKS && absent("status")) {
|
|
252
|
+
problems.push("A new break needs a status.");
|
|
253
|
+
}
|
|
254
|
+
if (spec === DECISIONS && (absent("goal") || absent("rule") || absent("measure"))) {
|
|
255
|
+
problems.push("A new decision needs a goal, a rule and a measure.");
|
|
256
|
+
}
|
|
257
|
+
if (problems.length > 0) {
|
|
258
|
+
throw new LogInputError(problems);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (entries.length > 0) {
|
|
262
|
+
// Same indent as the entries already there, so the base indent the parser reads does not move.
|
|
263
|
+
const prev = entries[entries.length - 1];
|
|
264
|
+
const sample = Object.values(prev.fields)[0]?.line;
|
|
265
|
+
const headIndent = leading(lines[prev.head].text);
|
|
266
|
+
const fieldIndent = sample === undefined ? `${headIndent} ` : leading(lines[sample].text);
|
|
267
|
+
const block = [`${headIndent}${fieldLine(spec.open, head)}`, ...keys.map((k) => `${fieldIndent}${fieldLine(k, given[k])}`)];
|
|
268
|
+
// After the notes the person wrote under the previous entry, not between its last bullet and them.
|
|
269
|
+
let after = prev.last;
|
|
270
|
+
while (after + 1 < prev.end && lines[after + 1].text.trim() !== "" && /^[ \t]/.test(lines[after + 1].text) && readBullet(vis[after + 1]) === null) {
|
|
271
|
+
after++;
|
|
272
|
+
}
|
|
273
|
+
insertAfter(lines, after, block, fileEol);
|
|
274
|
+
return lines.map((l) => l.text + l.eol).join("");
|
|
275
|
+
}
|
|
276
|
+
const block = [fieldLine(spec.open, head), ...keys.map((k) => ` ${fieldLine(k, given[k])}`)];
|
|
277
|
+
if (ranges.length > 0) {
|
|
278
|
+
const range = ranges[ranges.length - 1];
|
|
279
|
+
let k = range.start - 1;
|
|
280
|
+
for (let j = range.end - 1; j >= range.start; j--) {
|
|
281
|
+
if (lines[j].text.trim() !== "") {
|
|
282
|
+
k = j;
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
insertAfter(lines, k, ["", ...block], fileEol);
|
|
287
|
+
} else if (lines.length === 0) {
|
|
288
|
+
lines.push(...[`## ${spec.heading}`, "", ...block].map((t) => ({ text: t, eol: fileEol })));
|
|
289
|
+
} else {
|
|
290
|
+
const spacer = lines[lines.length - 1].text.trim() === "" ? [] : [""];
|
|
291
|
+
insertAfter(lines, lines.length - 1, [...spacer, `## ${spec.heading}`, "", ...block], fileEol);
|
|
292
|
+
}
|
|
293
|
+
return lines.map((l) => l.text + l.eol).join("");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function writeBreak(text, brk) {
|
|
297
|
+
const problems = [];
|
|
298
|
+
const rule = clean(brk.rule ?? "", "Rule", problems);
|
|
299
|
+
const piece = clean(brk.piece ?? "", "Piece", problems);
|
|
300
|
+
if (rule === "" || piece === "") {
|
|
301
|
+
problems.push("A break needs a rule and a piece.");
|
|
302
|
+
}
|
|
303
|
+
const given = { piece };
|
|
304
|
+
for (const key of ["effect", "check", "status"]) {
|
|
305
|
+
if (brk[key] !== undefined) {
|
|
306
|
+
given[key] = clean(brk[key], label(key), problems);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (given.status !== undefined && given.status !== "" && !STATUSES.includes(given.status)) {
|
|
310
|
+
problems.push("Status must be trying, kept or reverted.");
|
|
311
|
+
}
|
|
312
|
+
return write(text, BREAKS, rule, given, problems);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function writeDecision(text, d) {
|
|
316
|
+
const problems = [];
|
|
317
|
+
const decision = clean(d.decision ?? "", "Decision", problems);
|
|
318
|
+
const piece = clean(d.piece ?? "", "Piece", problems);
|
|
319
|
+
if (decision === "" || piece === "") {
|
|
320
|
+
problems.push("A decision needs a decision text and a piece.");
|
|
321
|
+
}
|
|
322
|
+
const given = { piece };
|
|
323
|
+
for (const key of ["goal", "rule", "measure"]) {
|
|
324
|
+
if (d[key] !== undefined) {
|
|
325
|
+
given[key] = clean(d[key], label(key), problems);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return write(text, DECISIONS, decision, given, problems);
|
|
329
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { toUrl } from "../browser.mjs";
|
|
3
|
+
|
|
4
|
+
// A page identity string: file URLs decoded, web URLs by origin + path (no trailing slash) + query.
|
|
5
|
+
export function identity(target, cwd) {
|
|
6
|
+
const url = toUrl(target, cwd);
|
|
7
|
+
if (/^file:/i.test(url)) {
|
|
8
|
+
let decoded = url;
|
|
9
|
+
try {
|
|
10
|
+
decoded = decodeURIComponent(url);
|
|
11
|
+
} catch {
|
|
12
|
+
// keep the raw URL when it holds a stray percent sign
|
|
13
|
+
}
|
|
14
|
+
return process.platform === "win32" ? decoded.toLowerCase() : decoded;
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
const u = new URL(url);
|
|
18
|
+
return `${u.origin.toLowerCase()}${u.pathname.replace(/\/+$/, "")}${u.search}`;
|
|
19
|
+
} catch {
|
|
20
|
+
return url;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function sameTarget(a, b, cwd) {
|
|
25
|
+
if (typeof a !== "string" || typeof b !== "string" || a.trim() === "" || b.trim() === "") {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return identity(a.trim(), cwd) === identity(b.trim(), cwd);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const KEY_BASE_MAX = 40;
|
|
32
|
+
|
|
33
|
+
// A folder-safe key for a page: its last path segment (the host for a URL root) plus a short hash of its
|
|
34
|
+
// identity. Every written form that sameTarget treats as equal gives the same key.
|
|
35
|
+
export function targetKey(target, cwd) {
|
|
36
|
+
const text = String(target).trim();
|
|
37
|
+
const hash = createHash("sha1").update(identity(text, cwd)).digest("hex").slice(0, 10);
|
|
38
|
+
let base = "";
|
|
39
|
+
try {
|
|
40
|
+
const u = new URL(toUrl(text, cwd));
|
|
41
|
+
const last = u.pathname.split("/").filter((p) => p !== "").pop();
|
|
42
|
+
base = last ?? (u.protocol === "file:" ? "" : u.hostname);
|
|
43
|
+
try {
|
|
44
|
+
base = decodeURIComponent(base);
|
|
45
|
+
} catch {
|
|
46
|
+
// keep the raw segment when it holds a stray percent sign
|
|
47
|
+
}
|
|
48
|
+
} catch {
|
|
49
|
+
// an unparseable target keeps the generic base
|
|
50
|
+
}
|
|
51
|
+
const slug = base.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, KEY_BASE_MAX).replace(/-+$/, "");
|
|
52
|
+
return `${slug || "page"}-${hash}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function matchGoals(md, target, cwd) {
|
|
56
|
+
return (md?.goals ?? []).filter((g) => g.target && sameTarget(g.target, target, cwd));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const norm = (s) => String(s ?? "").trim().toLowerCase();
|
|
60
|
+
|
|
61
|
+
// Applies the declared breaks of the goals of this page to the findings. `unapplied` lists the breaks
|
|
62
|
+
// that did nothing and why: any break whose rule id is not in knownIds, and each break of a goal of this
|
|
63
|
+
// page that was not applied. A break of another page is not listed.
|
|
64
|
+
export function applyBreaks(findings, breaks, goals, rules, knownIds) {
|
|
65
|
+
const pieces = new Set(goals.map((g) => norm(g.piece)));
|
|
66
|
+
const known = knownIds ? new Set([...knownIds].map(norm)) : null;
|
|
67
|
+
const live = breaks.filter((b) => (b.status === "kept" || b.status === "trying") && pieces.has(norm(b.piece)));
|
|
68
|
+
const applied = new Set();
|
|
69
|
+
const out = findings.map((f) => {
|
|
70
|
+
if (f.level !== "fail" && f.level !== "hint") {
|
|
71
|
+
return f;
|
|
72
|
+
}
|
|
73
|
+
const brk = live.find((b) => norm(b.rule) === norm(f.ruleId));
|
|
74
|
+
if (!brk) {
|
|
75
|
+
return f;
|
|
76
|
+
}
|
|
77
|
+
const rule = rules.find((r) => norm(r.id) === norm(f.ruleId));
|
|
78
|
+
if (rule?.origin === "norm") {
|
|
79
|
+
return { ...f, evidence: [...(f.evidence ?? []), `A declared break on ${brk.piece} does not waive a norm, so this stays a fail and the break was not applied.`] };
|
|
80
|
+
}
|
|
81
|
+
applied.add(brk);
|
|
82
|
+
return {
|
|
83
|
+
...f,
|
|
84
|
+
level: "break",
|
|
85
|
+
message: [`Declared break (${brk.status}):`, brk.effect, brk.check ? `Check: ${brk.check}` : ""].filter((p) => p !== "").join(" "),
|
|
86
|
+
evidence: [f.message, ...(f.evidence ?? [])],
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
const appliedRules = new Set([...applied].map((b) => norm(b.rule)));
|
|
90
|
+
const unapplied = [];
|
|
91
|
+
for (const b of breaks) {
|
|
92
|
+
let reason = null;
|
|
93
|
+
if (known && !known.has(norm(b.rule))) {
|
|
94
|
+
reason = "unknown rule id";
|
|
95
|
+
} else if (!pieces.has(norm(b.piece))) {
|
|
96
|
+
continue;
|
|
97
|
+
} else if (b.status === "reverted") {
|
|
98
|
+
reason = "status is reverted";
|
|
99
|
+
} else if (b.status !== "kept" && b.status !== "trying") {
|
|
100
|
+
reason = "no status";
|
|
101
|
+
} else if (rules.find((r) => norm(r.id) === norm(b.rule))?.origin === "norm") {
|
|
102
|
+
reason = "a norm is never waived";
|
|
103
|
+
} else if (!applied.has(b) && !appliedRules.has(norm(b.rule))) {
|
|
104
|
+
const rule = rules.find((r) => norm(r.id) === norm(b.rule));
|
|
105
|
+
reason = rule && !rule.measure ? "not measured by check-design" : "no fail or hint for this rule on this page";
|
|
106
|
+
}
|
|
107
|
+
if (reason) {
|
|
108
|
+
unapplied.push({ rule: b.rule, piece: b.piece, reason });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return { findings: out, applied: [...applied], unapplied };
|
|
112
|
+
}
|