@cagov/ca-ds 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +600 -2
- package/bin/ca-ds.js +5 -0
- package/package.json +62 -4
- package/src/cli/add.js +134 -0
- package/src/cli/build.js +15 -0
- package/src/cli/diff.js +39 -0
- package/src/cli/eject.js +112 -0
- package/src/cli/exit.js +22 -0
- package/src/cli/format.js +52 -0
- package/src/cli/init.js +148 -0
- package/src/cli/install.js +178 -0
- package/src/cli/items.js +10 -0
- package/src/cli/login.js +331 -0
- package/src/cli/main.js +108 -0
- package/src/cli/new.js +230 -0
- package/src/cli/outdated.js +74 -0
- package/src/cli/pack.js +75 -0
- package/src/cli/preview.js +31 -0
- package/src/cli/publish.js +295 -0
- package/src/cli/pull.js +208 -0
- package/src/cli/remote.js +52 -0
- package/src/cli/remove.js +69 -0
- package/src/cli/update.js +223 -0
- package/src/cli/validate.js +131 -0
- package/src/lib/browser.js +12 -0
- package/src/lib/build.js +249 -0
- package/src/lib/bundle.js +148 -0
- package/src/lib/catalog.js +44 -0
- package/src/lib/checks.js +31 -0
- package/src/lib/collection.js +65 -0
- package/src/lib/emblem.js +29 -0
- package/src/lib/fixtures.js +59 -0
- package/src/lib/floor.js +79 -0
- package/src/lib/glyph.js +148 -0
- package/src/lib/image.js +138 -0
- package/src/lib/json-text.js +213 -0
- package/src/lib/keys.js +322 -0
- package/src/lib/links.js +321 -0
- package/src/lib/markdown.js +408 -0
- package/src/lib/merge.js +93 -0
- package/src/lib/pack.js +30 -0
- package/src/lib/page-schema.js +368 -0
- package/src/lib/page.js +372 -0
- package/src/lib/patch.js +9 -0
- package/src/lib/regions.js +173 -0
- package/src/lib/registry.js +371 -0
- package/src/lib/release.js +119 -0
- package/src/lib/repoints.js +120 -0
- package/src/lib/resolve.js +32 -0
- package/src/lib/schema.js +28 -0
- package/src/lib/serial.js +4 -0
- package/src/lib/serialize.js +444 -0
- package/src/lib/structured.js +72 -0
- package/src/lib/system.js +13 -0
- package/src/lib/targets.js +85 -0
- package/src/lib/terms.js +11 -0
- package/src/lib/token-view.js +289 -0
- package/src/lib/tokens.js +247 -0
- package/src/lib/version.js +34 -0
- package/src/preview/frame.js +75 -0
- package/src/preview/pages.js +128 -0
- package/src/preview/preview.css +58 -0
- package/src/preview/screenshots.js +72 -0
- package/src/preview/server.js +378 -0
- package/src/preview/shell.js +153 -0
- package/src/schema/a11y.schema.json +56 -0
- package/src/schema/collection.lock.schema.json +58 -0
- package/src/schema/collection.schema.json +65 -0
- package/src/schema/composition.schema.json +98 -0
- package/src/schema/contract.schema.json +116 -0
- package/src/schema/interface.schema.json +257 -0
- package/src/schema/item.schema.json +362 -0
- package/src/schema/release.schema.json +39 -0
- package/src/schema/system.schema.json +75 -0
- package/src/schema/variants.schema.json +60 -0
- package/src/validate/collection.js +19 -0
- package/src/validate/collisions.js +124 -0
- package/src/validate/context.js +357 -0
- package/src/validate/facts.js +253 -0
- package/src/validate/files.js +103 -0
- package/src/validate/findings.js +19 -0
- package/src/validate/glyphs.js +126 -0
- package/src/validate/html.js +215 -0
- package/src/validate/imports.js +29 -0
- package/src/validate/index.js +75 -0
- package/src/validate/item.js +47 -0
- package/src/validate/links.js +113 -0
- package/src/validate/manifest.js +147 -0
- package/src/validate/media.js +65 -0
- package/src/validate/page.js +74 -0
- package/src/validate/prefixes.js +58 -0
- package/src/validate/relations.js +310 -0
- package/src/validate/release.js +109 -0
- package/src/validate/skill.js +122 -0
- package/src/validate/structured.js +335 -0
- package/src/validate/tasc.js +80 -0
- package/src/validate/template.js +129 -0
- package/src/validate/tokens.js +154 -0
- package/src/validate/zip.js +40 -0
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
// Spends tokens on names and prose alone: an empty column drops, and the accessibility section cites a11y rules by id.
|
|
2
|
+
|
|
3
|
+
import { TOPICS, a11yRules } from './structured.js';
|
|
4
|
+
import { parsePage, resolve, contentOf, formatElement, isElement } from './page.js';
|
|
5
|
+
import { NODES, SERVER_VALUE } from './page-schema.js';
|
|
6
|
+
import { formatValue, SCHEMES } from './token-view.js';
|
|
7
|
+
import { splitFrontmatter } from './markdown.js';
|
|
8
|
+
|
|
9
|
+
const code = (s) => (String(s).includes('`') ? `\`\` ${s} \`\`` : `\`${s}\``);
|
|
10
|
+
|
|
11
|
+
const cell = (s) => String(s ?? '').replace(/\s*\n\s*/g, ' ').replace(/\|/g, '\\|');
|
|
12
|
+
|
|
13
|
+
function table(columns, rows) {
|
|
14
|
+
const kept = columns.filter((c) => c.always || rows.some((r) => c.value(r) !== ''));
|
|
15
|
+
const line = (cells) => `| ${cells.join(' | ')} |`;
|
|
16
|
+
return [line(kept.map((c) => c.head)), `|${kept.map(() => '---').join('|')}|`, ...rows.map((r) => line(kept.map((c) => cell(c.value(r)))))].join('\n');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function recipeText(recipe) {
|
|
20
|
+
return Object.entries(recipe)
|
|
21
|
+
.map(([key, attrs]) => {
|
|
22
|
+
const pairs = Object.entries(attrs ?? {});
|
|
23
|
+
if (key.startsWith('.')) return pairs.reduce((s, [a, v]) => `${s}[${v === true ? a : `${a}="${v}"`}]`, key);
|
|
24
|
+
return `<${key}${pairs.map(([a, v]) => (v === true ? ` ${a}` : ` ${a}="${v}"`)).join('')}>`;
|
|
25
|
+
})
|
|
26
|
+
.join(', ');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const axisTitle = (axis) => (axis ? `${axis[0].toUpperCase()}${axis.slice(1).replace(/-/g, ' ')}` : 'Combinations');
|
|
30
|
+
|
|
31
|
+
export function variantGroups(variants) {
|
|
32
|
+
const groups = new Map();
|
|
33
|
+
for (const v of variants) {
|
|
34
|
+
const key = v.axis ?? null;
|
|
35
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
36
|
+
groups.get(key).push(v);
|
|
37
|
+
}
|
|
38
|
+
const ordered = [...groups.entries()].filter(([axis]) => axis !== null);
|
|
39
|
+
if (groups.has(null)) ordered.push([null, groups.get(null)]);
|
|
40
|
+
return ordered.map(([axis, list]) => ({ axis, variants: list }));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function serializeVariants(data) {
|
|
44
|
+
const groups = variantGroups(data.variants);
|
|
45
|
+
const titled = groups.some((g) => g.axis !== null);
|
|
46
|
+
const variant = (v) =>
|
|
47
|
+
[
|
|
48
|
+
`- **${v.title}** ${code(v.id)}: ${code(recipeText(v.recipe))}`,
|
|
49
|
+
` - Use: ${v.use}`,
|
|
50
|
+
v.avoid ? ` - Avoid: ${v.avoid}` : null,
|
|
51
|
+
` - Examples: ${v.examples.map(code).join(', ')}`,
|
|
52
|
+
]
|
|
53
|
+
.filter(Boolean)
|
|
54
|
+
.join('\n');
|
|
55
|
+
const body = groups.map((g) => [titled ? `### ${axisTitle(g.axis)}\n` : null, g.variants.map(variant).join('\n')].filter(Boolean).join('\n'));
|
|
56
|
+
return `## Variants\n\n${body.join('\n\n')}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// An open set reads in italics, the interface's not-a-literal signal.
|
|
60
|
+
export function valueText(v) {
|
|
61
|
+
if (typeof v === 'string') return code(v);
|
|
62
|
+
if (v.boolean) return 'boolean';
|
|
63
|
+
if (v.range) return `${code(v.range[0])} to ${code(v.range[1])}`;
|
|
64
|
+
return `*${v.open}*`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const absentText = (a) => (a === 'required' || a === 'script-set' ? a : a.value !== undefined ? code(a.value) : a.behavior);
|
|
68
|
+
|
|
69
|
+
export const requiredText = (r) => (r === true ? 'yes' : r === false ? 'no' : `when ${r.when}`);
|
|
70
|
+
|
|
71
|
+
const childElement = (e) => (Array.isArray(e) ? e.map(code).join(', ') : e);
|
|
72
|
+
|
|
73
|
+
const attributesTable = (attributes) =>
|
|
74
|
+
table(
|
|
75
|
+
[
|
|
76
|
+
{ head: 'Attribute', value: (a) => code(a.name), always: true },
|
|
77
|
+
{ head: 'Values', value: (a) => a.values.map(valueText).join(', '), always: true },
|
|
78
|
+
{ head: 'Absent', value: (a) => absentText(a.absent), always: true },
|
|
79
|
+
{ head: 'Meaning', value: (a) => a.meaning, always: true },
|
|
80
|
+
],
|
|
81
|
+
attributes,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const childrenTable = (children) =>
|
|
85
|
+
table(
|
|
86
|
+
[
|
|
87
|
+
{ head: 'Slot', value: (c) => (c.slot === null ? 'none' : code(c.slot)), always: true },
|
|
88
|
+
{ head: 'Element', value: (c) => childElement(c.element), always: true },
|
|
89
|
+
{ head: 'Required', value: (c) => requiredText(c.required), always: true },
|
|
90
|
+
{ head: 'Meaning', value: (c) => c.meaning, always: true },
|
|
91
|
+
],
|
|
92
|
+
children,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const propertiesTable = (properties) =>
|
|
96
|
+
table(
|
|
97
|
+
[
|
|
98
|
+
{ head: 'Property', value: (p) => code(p.name), always: true },
|
|
99
|
+
{ head: 'Default', value: (p) => (p.default === null ? 'none' : code(p.default)), always: true },
|
|
100
|
+
{ head: 'Pairs with', value: (p) => (p.pairs ?? []).map(code).join(', ') },
|
|
101
|
+
{ head: 'Meaning', value: (p) => p.meaning, always: true },
|
|
102
|
+
],
|
|
103
|
+
properties,
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const markersTable = (markers) =>
|
|
107
|
+
table(
|
|
108
|
+
[
|
|
109
|
+
{ head: 'Marker', value: (k) => code(k.attribute), always: true },
|
|
110
|
+
{ head: 'On', value: (k) => k.on.map(code).join(', '), always: true },
|
|
111
|
+
{ head: 'Required', value: (k) => requiredText(k.required), always: true },
|
|
112
|
+
{ head: 'Meaning', value: (k) => k.meaning, always: true },
|
|
113
|
+
],
|
|
114
|
+
markers,
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const eventsTable = (events) =>
|
|
118
|
+
table(
|
|
119
|
+
[
|
|
120
|
+
{ head: 'Event', value: (e) => code(e.name), always: true },
|
|
121
|
+
{ head: 'Detail', value: (e) => (e.detail === null ? 'none' : e.detail), always: true },
|
|
122
|
+
{ head: 'Cancelable', value: (e) => (e.cancelable ? 'yes' : 'no'), always: true },
|
|
123
|
+
{ head: 'Meaning', value: (e) => e.meaning, always: true },
|
|
124
|
+
],
|
|
125
|
+
events,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const classesTable = (classes) =>
|
|
129
|
+
table(
|
|
130
|
+
[
|
|
131
|
+
{ head: 'Class', value: (c) => code(c.name), always: true },
|
|
132
|
+
{ head: 'On', value: (c) => (c.on ?? []).map(code).join(', ') },
|
|
133
|
+
{ head: 'Modifies', value: (c) => (c.modifies ? code(c.modifies) : '') },
|
|
134
|
+
{ head: 'Meaning', value: (c) => c.meaning, always: true },
|
|
135
|
+
],
|
|
136
|
+
classes,
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const tables = (owner, level) =>
|
|
140
|
+
[
|
|
141
|
+
['Attributes', owner.attributes, attributesTable],
|
|
142
|
+
['Children', owner.children, childrenTable],
|
|
143
|
+
['Classes', owner.classes, classesTable],
|
|
144
|
+
['Properties', owner.properties, propertiesTable],
|
|
145
|
+
['Markers', owner.markers, markersTable],
|
|
146
|
+
['Events', owner.events, eventsTable],
|
|
147
|
+
]
|
|
148
|
+
.filter(([, list]) => list?.length)
|
|
149
|
+
.map(([label, list, render]) => `${'#'.repeat(level)} ${label}\n\n${render(list)}`);
|
|
150
|
+
|
|
151
|
+
function chainText(head, chain) {
|
|
152
|
+
const steps = [...(head ? [head] : []), ...chain.steps.map((s) => s.path)];
|
|
153
|
+
const end = chain.missing ? `${chain.missing} (missing)` : formatValue(chain.type, chain.value).text;
|
|
154
|
+
return [...steps, end].join(' → ');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// e.g. alert.title-weight → font.weight.semibold → 600, or light: …, dark: … for a schemed record.
|
|
158
|
+
export function tokenValueText(record) {
|
|
159
|
+
const type = record.type === 'colorScheme' ? 'color' : record.type;
|
|
160
|
+
const under = (scheme) => {
|
|
161
|
+
const chains = record.chains.filter((c) => c.scheme === scheme);
|
|
162
|
+
const final = formatValue(scheme ? type : record.type, scheme ? record.resolved?.[scheme] : record.resolved).text;
|
|
163
|
+
if (chains.length === 1 && chains[0].branch === null) return chainText(record.path, chains[0]);
|
|
164
|
+
if (chains.length === 0) return final;
|
|
165
|
+
return [...chains.map((c) => `${c.branch}: ${chainText(null, c)}`), final].filter(Boolean).join('; ');
|
|
166
|
+
};
|
|
167
|
+
return record.schemed ? SCHEMES.map((s) => `${s}: ${under(s)}`).join(', ') : under(null);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const repointedText = (r) => (r.repointedBy?.length ? r.repointedBy.map(code).join(', ') : '—');
|
|
171
|
+
|
|
172
|
+
// A component's records carry repointedBy, and the tokens item's don't.
|
|
173
|
+
const tokensTable = (records) =>
|
|
174
|
+
table(
|
|
175
|
+
[
|
|
176
|
+
{ head: 'Token', value: (r) => code(r.path), always: true },
|
|
177
|
+
{ head: 'Property', value: (r) => code(r.cssName), always: true },
|
|
178
|
+
{ head: 'Type', value: (r) => r.type ?? '', always: true },
|
|
179
|
+
{ head: 'Value', value: tokenValueText, always: true },
|
|
180
|
+
...(records.some((r) => Array.isArray(r.repointedBy)) ? [{ head: 'Re-pointed by', value: repointedText, always: true }] : []),
|
|
181
|
+
{ head: 'Meaning', value: (r) => r.description ?? '—', always: true },
|
|
182
|
+
],
|
|
183
|
+
records,
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
// An implicit group after a labeled one takes its path as a heading, so its rows never read as the group above's.
|
|
187
|
+
function groupTables(views, level) {
|
|
188
|
+
const parts = [];
|
|
189
|
+
for (const view of views) {
|
|
190
|
+
let headed = false;
|
|
191
|
+
for (const g of view.groups) {
|
|
192
|
+
const title = g.label ?? (headed ? g.path : null);
|
|
193
|
+
if (title !== null) {
|
|
194
|
+
parts.push(`${'#'.repeat(level)} ${code(title)}`);
|
|
195
|
+
headed = true;
|
|
196
|
+
}
|
|
197
|
+
if (g.description) parts.push(g.description);
|
|
198
|
+
parts.push(tokensTable(g.records));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return parts;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function serializeTokens(views, { remixOf = null } = {}) {
|
|
205
|
+
if (!views?.some((v) => v.groups.length)) return null;
|
|
206
|
+
const base = remixOf ? [`A remix's own tokens. Its base's are in ${code(remixOf)}'s interface.`] : [];
|
|
207
|
+
return ['### Tokens', ...base, ...groupTables(views, 4)].join('\n\n');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const layerTitle = (layer) => `${layer[0].toUpperCase()}${layer.slice(1)}`;
|
|
211
|
+
|
|
212
|
+
export function serializeTokenLayers(layers) {
|
|
213
|
+
const parts = layers.map(({ layer, files }) => [`## ${layerTitle(layer)}`, ...files.flatMap((v) => [`### ${code(v.file)}`, ...groupTables([v], 4)])].join('\n\n'));
|
|
214
|
+
return parts.join('\n\n');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function serializeInterface(data, { tokens = null, remixOf = null } = {}) {
|
|
218
|
+
const parts = data ? tables({ classes: data.classes, properties: data.properties, markers: data.markers }, 3) : [];
|
|
219
|
+
for (const [tag, el] of Object.entries(data?.elements ?? {})) {
|
|
220
|
+
parts.push([`### ${code(`<${tag}>`)}\n\n${el.meaning}`, ...tables(el, 4)].join('\n\n'));
|
|
221
|
+
}
|
|
222
|
+
const section = serializeTokens(tokens, { remixOf });
|
|
223
|
+
if (section) parts.push(section);
|
|
224
|
+
if (!data && !section) return null;
|
|
225
|
+
return `## Interface\n\n${parts.join('\n\n')}`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const wcagText = (list) => (list?.length ? ` (WCAG ${list.join(', ')})` : '');
|
|
229
|
+
|
|
230
|
+
export function serializeContract(data, { item = null } = {}) {
|
|
231
|
+
const cite = item ? ` Cite a rule as ${code(`${item}/<id>`)}.` : '';
|
|
232
|
+
const rule = (r) => `- [ ] ${code(r.id)}${r.severity === 'should' ? ' (should)' : ''}: ${r.rule}${r.why ? ` Why: ${r.why}` : ''}${wcagText(r.wcag)}`;
|
|
233
|
+
return `## Contract\n\nEvery rule is a must unless marked should. A should rule may break with a noted reason.${cite}\n\n${data.rules.map(rule).join('\n')}`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function serializeAccessibility(data, contract = null) {
|
|
237
|
+
const parts = [];
|
|
238
|
+
for (const [topic, title] of Object.entries(TOPICS)) {
|
|
239
|
+
const notes = data?.[topic];
|
|
240
|
+
if (notes?.length) parts.push(`### ${title}\n\n${notes.map((n) => `- ${n.note}${wcagText(n.wcag)}`).join('\n')}`);
|
|
241
|
+
}
|
|
242
|
+
const rules = a11yRules(contract);
|
|
243
|
+
if (rules.length) parts.push(`The contract's accessibility rules: ${rules.map((r) => code(r.id)).join(', ')}.`);
|
|
244
|
+
return parts.length ? `## Accessibility\n\n${parts.join('\n\n')}` : null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function serializeSet(set, { item = null, tokens = null, remixOf = null } = {}) {
|
|
248
|
+
const parts = [
|
|
249
|
+
set.variants ? serializeVariants(set.variants) : null,
|
|
250
|
+
serializeInterface(set.interface ?? null, { tokens, remixOf }),
|
|
251
|
+
set.contract ? serializeContract(set.contract, { item }) : null,
|
|
252
|
+
set.accessibility || set.contract ? serializeAccessibility(set.accessibility, set.contract) : null,
|
|
253
|
+
];
|
|
254
|
+
return parts.filter(Boolean).join('\n\n');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function serializeExample({ entry, text }, { page = false } = {}) {
|
|
258
|
+
const fence = text.includes('```') ? '````' : '```';
|
|
259
|
+
return `## ${page ? 'Page' : 'Default example'}: ${entry.title}\n\n${code(entry.path)}: ${entry.description}\n\n${fence}html\n${text.trimEnd()}\n${fence}\n`;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const names = (list) => list.map(code).join(', ');
|
|
263
|
+
|
|
264
|
+
// The composition records one region per derived region, in document order, so the two pair by position.
|
|
265
|
+
export function serializeComposition(data, derived = null) {
|
|
266
|
+
const regions = data.regions.map((r, i) => ({ ...r, items: derived?.regions[i]?.items ?? [] }));
|
|
267
|
+
const parts = [
|
|
268
|
+
'### Regions',
|
|
269
|
+
`The page's regions, in document order. A region's components are derived from the page, each an item to load.`,
|
|
270
|
+
table(
|
|
271
|
+
[
|
|
272
|
+
{ head: 'Id', value: (r) => code(r.id), always: true },
|
|
273
|
+
{ head: 'Region', value: (r) => r.title, always: true },
|
|
274
|
+
{ head: 'Contents', value: (r) => r.contents, always: true },
|
|
275
|
+
{ head: 'Components', value: (r) => names(r.items), always: true },
|
|
276
|
+
],
|
|
277
|
+
regions,
|
|
278
|
+
),
|
|
279
|
+
];
|
|
280
|
+
if (derived?.frame.items.length) parts.push(`The page's frame, outside every region, uses ${names(derived.frame.items)}.`);
|
|
281
|
+
if (derived?.unmapped.length) parts.push(`The page uses surface no dependency's interface holds: ${names(derived.unmapped)}.`);
|
|
282
|
+
if (data.variations?.length) {
|
|
283
|
+
const variation = (v) => [`- ${code(v.id)}, in ${code(v.region)}: ${code(v.see)}`, ` - Use: ${v.use}`, v.avoid ? ` - Avoid: ${v.avoid}` : null].filter(Boolean).join('\n');
|
|
284
|
+
parts.push('### Variations', data.variations.map(variation).join('\n'));
|
|
285
|
+
}
|
|
286
|
+
if (data.gaps?.length) parts.push('### Gaps', data.gaps.map((g) => `- ${g.need}${g.see ? ` See ${code(g.see)}.` : ''}`).join('\n'));
|
|
287
|
+
return `## Composition\n\n${parts.join('\n\n')}\n`;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export const SCRIPTS_WARNING = 'This skill ships scripts an agent may run. Review them before you run them.';
|
|
291
|
+
|
|
292
|
+
export const shipsScripts = (files) => files.some((f) => f.role === 'script');
|
|
293
|
+
|
|
294
|
+
// A skill's SKILL.md keeps its frontmatter as written, a block of its own, since the manifest never mirrors it.
|
|
295
|
+
export function serializeSkill(prose, files, { doc = 'SKILL.md' } = {}) {
|
|
296
|
+
const blocks = [];
|
|
297
|
+
if (shipsScripts(files)) blocks.push(`> ${SCRIPTS_WARNING}\n`);
|
|
298
|
+
const { frontmatter, body } = splitFrontmatter(prose);
|
|
299
|
+
if (frontmatter !== null) {
|
|
300
|
+
const fence = frontmatter.includes('```') ? '````' : '```';
|
|
301
|
+
blocks.push(`## Frontmatter\n\n${fence}yaml\n${frontmatter}\n${fence}\n`, body);
|
|
302
|
+
} else {
|
|
303
|
+
blocks.push(prose);
|
|
304
|
+
}
|
|
305
|
+
const others = files.filter((f) => f.path !== doc && f.path !== 'item.json');
|
|
306
|
+
if (others.length) {
|
|
307
|
+
const rows = table(
|
|
308
|
+
[
|
|
309
|
+
{ head: 'Path', value: (f) => code(f.path), always: true },
|
|
310
|
+
{ head: 'Role', value: (f) => f.role ?? '', always: true },
|
|
311
|
+
{ head: 'Bytes', value: (f) => String(f.size), always: true },
|
|
312
|
+
],
|
|
313
|
+
others,
|
|
314
|
+
);
|
|
315
|
+
blocks.push(`## Files\n\nThe skill's other files, each answered by \`get_file\`.\n\n${rows}\n`);
|
|
316
|
+
}
|
|
317
|
+
return blocks;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function serializeBlocks([fields, ...rest]) {
|
|
321
|
+
const fence = fields.includes('```') ? '````' : '```';
|
|
322
|
+
return `${[`${fence}json\n${fields}\n${fence}`, ...rest.map((b) => b.trimEnd())].join('\n\n')}\n`;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// A page item's doc as markdown. Components keep their HTML; a server value drops, since the site renders it per request.
|
|
326
|
+
|
|
327
|
+
const HEADINGS = { h1: 1, h2: 2, h3: 3, h4: 4, h5: 5, h6: 6 };
|
|
328
|
+
const WRAPPERS = new Set(['div', 'section', 'article', 'aside']);
|
|
329
|
+
const LISTS = new Set(['bullet-list', 'ordered-list']);
|
|
330
|
+
|
|
331
|
+
const attr = (el, name) => el.attrs?.find((a) => a.name === name)?.value;
|
|
332
|
+
|
|
333
|
+
// An underscore inside a word stays bare, since markdown reads it as text.
|
|
334
|
+
const escapeText = (s) => s.replace(/[\\`*[\]<]/g, '\\$&').replace(/(^|[^A-Za-z0-9])_|_(?=[^A-Za-z0-9]|$)/g, (m) => m.replace('_', '\\_'));
|
|
335
|
+
|
|
336
|
+
const escapeStart = (s) => s.replace(/^(#{1,6}\s|[-+>]\s|\d+[.)]\s)/, '\\$1');
|
|
337
|
+
|
|
338
|
+
// Edge spaces move outside the markers.
|
|
339
|
+
function wrap(inner, marker) {
|
|
340
|
+
const [, lead, body, tail] = inner.match(/^(\s*)([\s\S]*?)(\s*)$/);
|
|
341
|
+
return body ? `${lead}${marker}${body}${marker}${tail}` : inner;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function codeSpan(text) {
|
|
345
|
+
const longest = Math.max(0, ...[...text.matchAll(/`+/g)].map((m) => m[0].length));
|
|
346
|
+
const fence = '`'.repeat(longest + 1);
|
|
347
|
+
return text.startsWith('`') || text.endsWith('`') ? `${fence} ${text} ${fence}` : `${fence}${text}${fence}`;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const textOf = (node) => (node.nodeName === '#text' ? node.value : (node.childNodes ?? []).map(textOf).join(''));
|
|
351
|
+
|
|
352
|
+
function inline(nodes, content) {
|
|
353
|
+
let out = '';
|
|
354
|
+
for (const node of nodes) {
|
|
355
|
+
if (node.nodeName === '#text') {
|
|
356
|
+
out += escapeText(node.value.replace(/\s+/g, ' '));
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (!isElement(node)) continue;
|
|
360
|
+
const name = content ? resolve(node, content) : null;
|
|
361
|
+
const inner = () => inline(node.childNodes ?? [], name === null ? null : contentOf(name));
|
|
362
|
+
if (name === 'link') out += `[${inner().trim()}](${attr(node, 'href')})`;
|
|
363
|
+
else if (name === 'strong') out += wrap(inner(), '**');
|
|
364
|
+
else if (name === 'em') out += wrap(inner(), '*');
|
|
365
|
+
else if (name === 'code') out += codeSpan(textOf(node));
|
|
366
|
+
else if (name === 'break') out += '\\\n';
|
|
367
|
+
else out += formatElement(node, name).replace(/\n\s*/g, ' ');
|
|
368
|
+
}
|
|
369
|
+
return out;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const indent = (text, by) => text.replace(/\n(?!\n)/g, `\n${by}`);
|
|
373
|
+
|
|
374
|
+
function list(el, name) {
|
|
375
|
+
const ordered = name === 'ordered-list';
|
|
376
|
+
let n = Number(attr(el, 'start') ?? 1);
|
|
377
|
+
const content = contentOf('list-item');
|
|
378
|
+
const items = [];
|
|
379
|
+
for (const li of el.childNodes ?? []) {
|
|
380
|
+
if (!isElement(li)) continue;
|
|
381
|
+
const kids = li.childNodes ?? [];
|
|
382
|
+
let at = kids.findIndex((k) => isElement(k) && NODES[resolve(k, content)]?.group === 'block');
|
|
383
|
+
if (at === -1) at = kids.length;
|
|
384
|
+
let lead = inline(kids.slice(0, at), content).trim();
|
|
385
|
+
let rest = kids.slice(at);
|
|
386
|
+
const first = rest.find(isElement);
|
|
387
|
+
if (!lead && first && resolve(first, content) === 'paragraph') {
|
|
388
|
+
lead = inline(first.childNodes ?? [], contentOf('paragraph')).trim();
|
|
389
|
+
rest = rest.slice(rest.indexOf(first) + 1);
|
|
390
|
+
}
|
|
391
|
+
const marker = ordered ? `${n++}.` : '-';
|
|
392
|
+
const pad = ' '.repeat(marker.length + 1);
|
|
393
|
+
let item = `${marker} ${indent(escapeStart(lead), pad)}`;
|
|
394
|
+
for (const block of blocks(rest, content)) item += `${block.list ? '\n' : '\n\n'}${pad}${indent(block.text, pad)}`;
|
|
395
|
+
items.push(item.trimEnd());
|
|
396
|
+
}
|
|
397
|
+
const loose = items.some((i) => i.includes('\n\n'));
|
|
398
|
+
return items.join(loose ? '\n\n' : '\n');
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// list: true for a list, which follows a list item's first line with no blank line.
|
|
402
|
+
function blocks(nodes, content) {
|
|
403
|
+
const out = [];
|
|
404
|
+
for (const node of nodes) {
|
|
405
|
+
if (node.nodeName === '#text') {
|
|
406
|
+
if (/\S/.test(node.value)) out.push({ text: escapeStart(escapeText(node.value.trim().replace(/\s+/g, ' '))) });
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (!isElement(node)) continue;
|
|
410
|
+
const name = content ? resolve(node, content) : null;
|
|
411
|
+
if (name === 'paragraph') {
|
|
412
|
+
const text = inline(node.childNodes ?? [], contentOf(name)).trim();
|
|
413
|
+
if (text) out.push({ text: escapeStart(text) });
|
|
414
|
+
} else if (Object.hasOwn(HEADINGS, name)) {
|
|
415
|
+
out.push({ text: `${'#'.repeat(HEADINGS[name])} ${inline(node.childNodes ?? [], contentOf(name)).trim()}` });
|
|
416
|
+
} else if (WRAPPERS.has(name)) {
|
|
417
|
+
out.push(...blocks(node.childNodes ?? [], contentOf(name)));
|
|
418
|
+
} else if (LISTS.has(name)) {
|
|
419
|
+
out.push({ text: list(node, name), list: true });
|
|
420
|
+
} else if (name === 'blockquote') {
|
|
421
|
+
const inner = blocks(node.childNodes ?? [], contentOf(name)).map((b) => b.text).join('\n\n');
|
|
422
|
+
out.push({ text: inner.split('\n').map((line) => (line ? `> ${line}` : '>')).join('\n') });
|
|
423
|
+
} else if (name === 'divider') {
|
|
424
|
+
out.push({ text: '---' });
|
|
425
|
+
} else if (name === 'image') {
|
|
426
|
+
out.push({ text: `})` });
|
|
427
|
+
} else {
|
|
428
|
+
out.push({ text: formatElement(node, name) });
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return out;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function dropValues(parent) {
|
|
435
|
+
parent.childNodes = (parent.childNodes ?? []).filter((kid) => kid.tagName !== SERVER_VALUE);
|
|
436
|
+
for (const kid of parent.childNodes) dropValues(kid);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export function serializePage(html) {
|
|
440
|
+
const tree = parsePage(html);
|
|
441
|
+
dropValues(tree);
|
|
442
|
+
const out = blocks(tree.childNodes, contentOf(null));
|
|
443
|
+
return out.length ? `${out.map((b) => b.text).join('\n\n')}\n` : '';
|
|
444
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export const SUFFIX = { contract: '.contract.json', interface: '.interface.json', variants: '.variants.json', accessibility: '.a11y.json', composition: '.composition.json' };
|
|
2
|
+
|
|
3
|
+
export const TOPICS = { semantics: 'Semantics', keyboard: 'Keyboard', 'screen-reader': 'Screen reader', motion: 'Motion', contrast: 'Contrast' };
|
|
4
|
+
|
|
5
|
+
export const folderOf = (path) => (path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '');
|
|
6
|
+
|
|
7
|
+
export const roleOf = (path) => Object.keys(SUFFIX).find((role) => path.endsWith(SUFFIX[role])) ?? null;
|
|
8
|
+
|
|
9
|
+
// A structured file in a subfolder is none of the item's.
|
|
10
|
+
export function structuredFiles(manifest) {
|
|
11
|
+
const files = {};
|
|
12
|
+
for (const f of manifest.files ?? []) {
|
|
13
|
+
if (Object.hasOwn(SUFFIX, f.role) && folderOf(f.path) === '' && !files[f.role]) files[f.role] = f.path;
|
|
14
|
+
}
|
|
15
|
+
return files;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const a11yRules = (contract) => (contract?.rules ?? []).filter((r) => r.tags.includes('a11y'));
|
|
19
|
+
|
|
20
|
+
// id is the form a link names the record by; an attribute or marker also answers to its bare name (bareName).
|
|
21
|
+
export function interfaceRecords(iface) {
|
|
22
|
+
const out = [];
|
|
23
|
+
for (const c of iface?.classes ?? []) out.push({ id: c.name, kind: 'class', owner: null, entry: c });
|
|
24
|
+
for (const p of iface?.properties ?? []) out.push({ id: p.name, kind: 'property', owner: null, entry: p });
|
|
25
|
+
for (const k of iface?.markers ?? []) out.push({ id: k.attribute, kind: 'marker', owner: null, entry: k });
|
|
26
|
+
for (const [tag, el] of Object.entries(iface?.elements ?? {})) {
|
|
27
|
+
out.push({ id: tag, kind: 'element', owner: tag, entry: el });
|
|
28
|
+
for (const a of el.attributes ?? []) out.push({ id: `${tag}[${a.name}]`, kind: 'attribute', owner: tag, entry: a });
|
|
29
|
+
for (const p of el.properties ?? []) out.push({ id: p.name, kind: 'property', owner: tag, entry: p });
|
|
30
|
+
for (const k of el.markers ?? []) out.push({ id: `${tag}[${k.attribute}]`, kind: 'marker', owner: tag, entry: k });
|
|
31
|
+
for (const e of el.events ?? []) out.push({ id: `${tag}@${e.name}`, kind: 'event', owner: tag, entry: e });
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const bareName = (record) => (record.kind === 'attribute' ? record.entry.name : record.kind === 'marker' && record.owner !== null ? record.entry.attribute : null);
|
|
37
|
+
|
|
38
|
+
export function recordIds(role, data) {
|
|
39
|
+
if (!data) return new Set();
|
|
40
|
+
if (role === 'contract') return new Set(data.rules.map((r) => r.id));
|
|
41
|
+
if (role === 'variants') return new Set(data.variants.map((v) => v.id));
|
|
42
|
+
if (role === 'accessibility') return new Set(Object.keys(TOPICS).filter((t) => data[t]));
|
|
43
|
+
if (role === 'composition') return new Set([...data.regions, ...(data.variations ?? [])].map((r) => r.id));
|
|
44
|
+
const ids = new Set();
|
|
45
|
+
for (const r of interfaceRecords(data)) {
|
|
46
|
+
ids.add(r.id);
|
|
47
|
+
const bare = bareName(r);
|
|
48
|
+
if (bare) ids.add(bare);
|
|
49
|
+
}
|
|
50
|
+
return ids;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function proseStrings(role, data) {
|
|
54
|
+
if (!data) return [];
|
|
55
|
+
if (role === 'contract') return data.rules.flatMap((r) => [r.rule, r.why].filter(Boolean));
|
|
56
|
+
if (role === 'variants') return data.variants.flatMap((v) => [v.use, v.avoid].filter(Boolean));
|
|
57
|
+
if (role === 'accessibility') return Object.keys(TOPICS).flatMap((t) => (data[t] ?? []).map((n) => n.note));
|
|
58
|
+
if (role === 'composition') return [...data.regions.map((r) => r.contents), ...(data.variations ?? []).flatMap((v) => [v.use, v.avoid]), ...(data.gaps ?? []).map((g) => g.need)].filter(Boolean);
|
|
59
|
+
const out = [];
|
|
60
|
+
for (const { kind, entry } of interfaceRecords(data)) {
|
|
61
|
+
out.push(entry.meaning);
|
|
62
|
+
if (kind === 'attribute') {
|
|
63
|
+
for (const v of entry.values) if (typeof v === 'object' && v.open) out.push(v.open);
|
|
64
|
+
if (entry.absent?.behavior) out.push(entry.absent.behavior);
|
|
65
|
+
}
|
|
66
|
+
if (kind === 'marker' && entry.required?.when) out.push(entry.required.when);
|
|
67
|
+
if (kind === 'element') {
|
|
68
|
+
for (const c of entry.children ?? []) out.push(c.meaning, ...(c.required?.when ? [c.required.when] : []));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return out.filter(Boolean);
|
|
72
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const SYSTEM_NAMESPACE = 'standard';
|
|
2
|
+
|
|
3
|
+
export const systemItem = (name) => `${SYSTEM_NAMESPACE}/${name}`;
|
|
4
|
+
|
|
5
|
+
export const isSystemItem = (name) => typeof name === 'string' && name.startsWith(`${SYSTEM_NAMESPACE}/`);
|
|
6
|
+
|
|
7
|
+
// The docs site's own namespace: no release or preset holds it, and nothing keyed on SYSTEM_NAMESPACE keys on it.
|
|
8
|
+
export const SITE_NAMESPACE = 'site';
|
|
9
|
+
|
|
10
|
+
const REPO_FOLDERS = new Map([SYSTEM_NAMESPACE, SITE_NAMESPACE].map((ns) => [ns, `${ns}/`]));
|
|
11
|
+
|
|
12
|
+
// null for a namespace the repo doesn't author.
|
|
13
|
+
export const repoFolder = (scope) => REPO_FOLDERS.get(scope) ?? null;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Imports nothing, so the editors' element bundles take it without a parser.
|
|
2
|
+
|
|
3
|
+
const SEGMENT = '[a-z0-9][a-z0-9-]*';
|
|
4
|
+
const TARGET = new RegExp(`^(${SEGMENT})/(${SEGMENT})(?:/([^#\\s]+))?(?:#([^#\\s]+))?$`);
|
|
5
|
+
|
|
6
|
+
// markdown-it percent-encodes an href, so a heading id such as café parses back as caf%C3%A9.
|
|
7
|
+
export const decodeFragment = (fragment) => {
|
|
8
|
+
try {
|
|
9
|
+
return decodeURIComponent(fragment);
|
|
10
|
+
} catch {
|
|
11
|
+
return fragment;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export function parseTarget(href) {
|
|
16
|
+
if (typeof href !== 'string') return null;
|
|
17
|
+
const m = TARGET.exec(href);
|
|
18
|
+
if (!m) return null;
|
|
19
|
+
const [, scope, item, file = null, fragment = null] = m;
|
|
20
|
+
if (file !== null && file.split('/').some((s) => s === '' || s === '.' || s === '..')) return null;
|
|
21
|
+
return { name: `${scope}/${item}`, scope, file, fragment: fragment === null ? null : decodeFragment(fragment) };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const ITEM_PATH = new RegExp(`^/items/(${SEGMENT})/(${SEGMENT})(\\.md)?(?:/(.*))?$`);
|
|
25
|
+
const VERSION = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
26
|
+
|
|
27
|
+
const decode = (text) => {
|
|
28
|
+
try {
|
|
29
|
+
return decodeURIComponent(text);
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// The version and .md suffix drop, since a target never carries a version.
|
|
36
|
+
export function siteTarget(href, origin = null) {
|
|
37
|
+
if (typeof href !== 'string') return null;
|
|
38
|
+
let text = href.trim();
|
|
39
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(text)) {
|
|
40
|
+
let url;
|
|
41
|
+
try {
|
|
42
|
+
url = new URL(text);
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
if (origin === null || url.origin !== origin) return null;
|
|
47
|
+
text = `${url.pathname}${url.hash}`;
|
|
48
|
+
} else if (!text.startsWith('/') || text.startsWith('//')) return null;
|
|
49
|
+
const hash = text.indexOf('#');
|
|
50
|
+
const fragment = hash === -1 ? '' : decode(text.slice(hash + 1));
|
|
51
|
+
const path = (hash === -1 ? text : text.slice(0, hash)).replace(/\?.*$/, '');
|
|
52
|
+
const m = ITEM_PATH.exec(path);
|
|
53
|
+
if (!m || fragment === null) return null;
|
|
54
|
+
const [, scope, name, md, rest = ''] = m;
|
|
55
|
+
let file = null;
|
|
56
|
+
if (md && rest !== '') return null;
|
|
57
|
+
if (rest !== '' && rest !== 'versions' && rest !== 'edit') {
|
|
58
|
+
const [version, ...parts] = rest.split('/');
|
|
59
|
+
if (!VERSION.test(version.replace(/\.md$/, ''))) return null;
|
|
60
|
+
if (parts.length > 0) {
|
|
61
|
+
if (version.endsWith('.md')) return null;
|
|
62
|
+
file = decode(parts.join('/'));
|
|
63
|
+
if (file === null || file === '') return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const target = `${scope}/${name}${file ? `/${file}` : ''}${fragment ? `#${fragment}` : ''}`;
|
|
67
|
+
return parseTarget(target) ? target : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A file's URL keeps its version, which it needs, and a URL off origin stays as written.
|
|
71
|
+
export function sitePath(href, origin = null) {
|
|
72
|
+
if (typeof href !== 'string') return href;
|
|
73
|
+
const text = href.trim();
|
|
74
|
+
const target = parseTarget(siteTarget(text, origin));
|
|
75
|
+
if (target && target.file === null) return `/items/${target.name}${target.fragment ? `#${target.fragment}` : ''}`;
|
|
76
|
+
if (origin !== null && /^[a-z][a-z0-9+.-]*:/i.test(text)) {
|
|
77
|
+
try {
|
|
78
|
+
const url = new URL(text);
|
|
79
|
+
if (url.origin === origin) return `${url.pathname}${url.search}${url.hash}`;
|
|
80
|
+
} catch {
|
|
81
|
+
return text;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return text;
|
|
85
|
+
}
|
package/src/lib/terms.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The in-memory stand-in for Postgres's english search config, which also stems -ing and -ed; this folds only -s.
|
|
2
|
+
|
|
3
|
+
const STOP = new Set(
|
|
4
|
+
'a an and are as at be but by for from has have i if in into is it its no not of on or so such than that the their then there these they this to was were when where which while will with you your'.split(' '),
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
const fold = (word) => (word.length > 3 && word.endsWith('s') && !word.endsWith('ss') ? word.slice(0, -1) : word);
|
|
8
|
+
|
|
9
|
+
export function terms(text) {
|
|
10
|
+
return (String(text ?? '').toLowerCase().match(/[a-z0-9]+/g) ?? []).filter((w) => !STOP.has(w)).map(fold);
|
|
11
|
+
}
|