decklight 0.8.0 → 0.9.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/PLATFORM.md +666 -0
- package/README.md +224 -150
- package/SPEC.md +732 -886
- package/cli/agent-review.mjs +245 -0
- package/cli/agents.mjs +25 -5
- package/cli/associate.mjs +12 -12
- package/cli/audit.mjs +42 -10
- package/cli/banner.mjs +41 -17
- package/cli/bundle-audio.mjs +183 -0
- package/cli/bundle.mjs +383 -31
- package/cli/cast.mjs +7 -7
- package/cli/check.mjs +961 -0
- package/cli/clone-deck.mjs +179 -0
- package/cli/commands.mjs +345 -0
- package/cli/comments.mjs +64 -18
- package/cli/commit-flow.mjs +44 -19
- package/cli/commit-message.mjs +51 -74
- package/cli/commit-split.mjs +601 -0
- package/cli/deck-refs.mjs +59 -0
- package/cli/deck-routes.mjs +67 -0
- package/cli/deckfile.mjs +3 -5
- package/cli/decklight.mjs +68 -289
- package/cli/design-system-deps.mjs +209 -0
- package/cli/design-system-edit.mjs +194 -0
- package/cli/design-system-refs.mjs +409 -0
- package/cli/design-system.mjs +429 -0
- package/cli/doctor.mjs +115 -0
- package/cli/edit-git.mjs +353 -0
- package/cli/edit-handover.mjs +445 -0
- package/cli/edit-look.mjs +388 -0
- package/cli/edit-slides.mjs +683 -0
- package/cli/edit.mjs +2126 -842
- package/cli/enhance.mjs +483 -0
- package/cli/extension.mjs +10 -10
- package/cli/font-refs.mjs +273 -0
- package/cli/font.mjs +246 -0
- package/cli/git-tree.mjs +1 -1
- package/cli/git.mjs +65 -11
- package/cli/import.mjs +56 -10
- package/cli/init.mjs +400 -234
- package/cli/library.mjs +127 -0
- package/cli/loader.mjs +3 -1
- package/cli/marketplace.mjs +215 -21
- package/cli/narrate.mjs +176 -0
- package/cli/open-browser.mjs +63 -0
- package/cli/open.mjs +943 -0
- package/cli/pdf.mjs +41 -13
- package/cli/pkg.mjs +44 -2
- package/cli/plugin.mjs +43 -41
- package/cli/port-conflict.mjs +53 -23
- package/cli/pptx-export.mjs +71 -26
- package/cli/publish.mjs +8 -4
- package/cli/qr.mjs +1 -1
- package/cli/record.mjs +14 -12
- package/cli/remote.mjs +11 -11
- package/cli/restore.mjs +11 -4
- package/cli/review-ref.mjs +125 -0
- package/cli/review-remote.mjs +92 -48
- package/cli/review-routes.mjs +254 -0
- package/cli/review-store.mjs +47 -7
- package/cli/review-submit.mjs +47 -57
- package/cli/runtime-link.mjs +560 -0
- package/cli/serve.mjs +235 -44
- package/cli/setup.mjs +112 -0
- package/cli/sign.mjs +1 -1
- package/cli/skill-content.mjs +190 -33
- package/cli/skills.mjs +107 -24
- package/cli/start.mjs +110 -0
- package/cli/supervise.mjs +6 -6
- package/cli/theme-refs.mjs +515 -0
- package/cli/theme.mjs +181 -137
- package/cli/trust.mjs +83 -0
- package/cli/units.mjs +39 -42
- package/cli/update-check.mjs +1 -5
- package/cli/upgrade.mjs +194 -28
- package/cli/upstream.mjs +4 -3
- package/cli/util.mjs +4 -4
- package/cli/wizard.mjs +6 -6
- package/dist/build-info.json +1 -1
- package/dist/decklight.css +1126 -49
- package/dist/decklight.js +212 -115
- package/dist/decklight.js.map +4 -4
- package/docs/GLOSSARY.md +141 -0
- package/docs/architecture.svg +17 -17
- package/package.json +4 -2
- package/themes/README.md +9 -8
- package/themes/reveal-solarized.css +10 -0
- package/tools/args.mjs +20 -0
- package/tools/atomic-write.mjs +48 -0
- package/tools/brand-palette.mjs +717 -0
- package/tools/bridge.mjs +1 -1
- package/tools/build-groups.mjs +42 -0
- package/tools/color.mjs +48 -0
- package/tools/deck-html.mjs +396 -11
- package/tools/design-system-format.mjs +753 -0
- package/tools/elevenlabs-tts.mjs +110 -17
- package/tools/exec.mjs +35 -2
- package/tools/extension-check.mjs +31 -7
- package/tools/font-format.mjs +190 -0
- package/tools/font-stacks.mjs +24 -0
- package/tools/gemini-tts.mjs +3 -0
- package/tools/lipsync-config.mjs +103 -0
- package/tools/lipsync-engines.mjs +205 -2
- package/tools/lipsync-server.mjs +264 -41
- package/tools/lipsync.mjs +37 -18
- package/tools/local-voice.mjs +32 -42
- package/tools/narration-manifest.mjs +196 -0
- package/tools/narration-synth.mjs +416 -0
- package/tools/pptx.mjs +776 -19
- package/tools/publish-targets.mjs +1 -1
- package/tools/publish-voices.mjs +3 -3
- package/tools/recording-impact.mjs +7 -3
- package/tools/render-theme.mjs +33 -0
- package/tools/review-anchor.mjs +28 -12
- package/tools/semver.mjs +40 -0
- package/tools/sentences.mjs +180 -0
- package/tools/shot.mjs +14 -7
- package/tools/site-voices.mjs +232 -0
- package/tools/template-theme.mjs +1 -1
- package/tools/test-impact.mjs +19 -10
- package/tools/theme-check.mjs +30 -3
- package/tools/tts-cache.mjs +4 -3
- package/tools/tts-engines.mjs +38 -20
- package/tools/tts-setup.mjs +7 -7
- package/tools/video-options.mjs +34 -0
- package/tools/video.mjs +461 -48
- package/tools/voiceover-server.mjs +185 -104
- package/tools/voiceover.mjs +119 -261
- package/tools/zip.mjs +1 -1
- package/cli/dev.mjs +0 -623
- package/cli/present.mjs +0 -853
- package/cli/review.mjs +0 -361
- package/tools/publish-site-voices.mjs +0 -75
|
@@ -0,0 +1,753 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// The design-system package format, and the rules it is held to (SPEC
|
|
5
|
+
// DESIGN_SYSTEMS). A design system is what a company publishes once so every
|
|
6
|
+
// deck built on it inherits tokens beyond the theme contract, background art,
|
|
7
|
+
// and slide templates with slots — by reference, the way a marked theme is.
|
|
8
|
+
//
|
|
9
|
+
// <name>/
|
|
10
|
+
// design-system.json apiVersion, name, version, title, styles, templates
|
|
11
|
+
// (+ description, tokenPrefix, palette, recommendedThemes)
|
|
12
|
+
// design-system.css prefixed tokens, component and per-template rules;
|
|
13
|
+
// url()s relative to this file
|
|
14
|
+
// templates.html inert <template data-template="…"> blocks with data-slot containers
|
|
15
|
+
// assets/… svg · png · jpg · jpeg · webp · woff2 · woff
|
|
16
|
+
//
|
|
17
|
+
// PURE: every function here takes texts and a file list, never a path to read,
|
|
18
|
+
// and imports nothing from Node. `decklight design-system gate` (the
|
|
19
|
+
// admission gate a catalog's CI runs) is a thin main over it, and the same
|
|
20
|
+
// rules run again where a package is served and where the engine clones a
|
|
21
|
+
// template — one module, so the three can never disagree about what is safe.
|
|
22
|
+
//
|
|
23
|
+
// THE THREAT this exists for: template HTML and SVG from a third-party catalog
|
|
24
|
+
// end up inside a page whose policy allows inline script. So templates are
|
|
25
|
+
// inert markup and nothing else, SVGs carry no script, handlers or outside
|
|
26
|
+
// references, and the CSS reaches nothing outside its own package.
|
|
27
|
+
|
|
28
|
+
import { SEMVER_RE } from './semver.mjs';
|
|
29
|
+
import { readScales, expandThemes, paletteEntries, offPaletteTokens, offRoleColours, nearestRole, roleToken, COLOUR_TOKENS, WEIGHTS } from './brand-palette.mjs';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The design-system package contract's own version (SPEC DESIGN_SYSTEMS) —
|
|
33
|
+
* additive only, like TRANSFORM_API_VERSION and its siblings: bumped only
|
|
34
|
+
* when the format would break an existing package, never for an internal
|
|
35
|
+
* reorganisation. A package declares the lowest version it needs; this
|
|
36
|
+
* decklight reads anything at or below it.
|
|
37
|
+
*/
|
|
38
|
+
export const DESIGN_SYSTEM_API_VERSION = 2;
|
|
39
|
+
|
|
40
|
+
/** The format version a scale palette and brand themes need (SPEC BRAND_PALETTE). */
|
|
41
|
+
export const BRAND_API_VERSION = 2;
|
|
42
|
+
|
|
43
|
+
/** What an asset may be. SVG is the executable one, and gets the deep scan. */
|
|
44
|
+
export const ASSET_EXTENSIONS = ['svg', 'png', 'jpg', 'jpeg', 'webp', 'woff2', 'woff'];
|
|
45
|
+
|
|
46
|
+
/** Past this, an asset is a warning — a deck that ships it pays for it on every load. */
|
|
47
|
+
export const ASSET_WARN_BYTES = 2 * 1024 * 1024;
|
|
48
|
+
|
|
49
|
+
/** A package's, a template's and a slot's name: what a slide writes, so it stays plain. */
|
|
50
|
+
export const NAME_RE = /^[a-z][a-z0-9-]*$/;
|
|
51
|
+
|
|
52
|
+
/** Is this `data-template` value a slide template (`name/template`)? The built-in ring never has a slash. */
|
|
53
|
+
export const isTemplateRef = (value) => typeof value === 'string' && value.includes('/');
|
|
54
|
+
|
|
55
|
+
/** `acme/section-divider` → `{ system, template }`, or null for anything else. */
|
|
56
|
+
export function parseTemplateRef(value) {
|
|
57
|
+
const m = /^([^/\s]+)\/([^/\s]+)$/.exec(String(value ?? ''));
|
|
58
|
+
return m && NAME_RE.test(m[1]) && NAME_RE.test(m[2]) ? { system: m[1], template: m[2] } : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The theme contract (SPEC THEMING): the tokens a THEME owns. A design system
|
|
63
|
+
* sits beside any theme, so setting one of these is overriding the theme —
|
|
64
|
+
* allowed only with a `ds-exception: --token reason` comment, which every run
|
|
65
|
+
* prints, the way a theme's `rule-exception:` is.
|
|
66
|
+
*/
|
|
67
|
+
export const THEME_CONTRACT = [
|
|
68
|
+
'bg', 'bg-accent', 'fg', 'muted',
|
|
69
|
+
'font-body', 'font-heading', 'font-mono', 'heading-color', 'heading-weight', 'link',
|
|
70
|
+
'accent', 'accent-contrast',
|
|
71
|
+
'block-bg', 'block-border', 'block-radius', 'shadow',
|
|
72
|
+
'code-bg', 'code-fg', 'hl-keyword', 'hl-string', 'hl-number', 'hl-comment', 'hl-function', 'hl-type', 'hl-punct',
|
|
73
|
+
'd-stroke', 'd-text', 'd-muted', 'd-accent', 'd-fill-1', 'd-fill-2', 'd-fill-3', 'd-fill-4', 'd-fill-5', 'd-fill-6',
|
|
74
|
+
'term-bg', 'term-fg', 'term-prompt', 'term-cursor', 'term-selection',
|
|
75
|
+
'ansi-black', 'ansi-red', 'ansi-green', 'ansi-yellow', 'ansi-blue', 'ansi-magenta', 'ansi-cyan', 'ansi-white',
|
|
76
|
+
'ansi-bright-black', 'ansi-bright-red', 'ansi-bright-green', 'ansi-bright-yellow',
|
|
77
|
+
'ansi-bright-blue', 'ansi-bright-magenta', 'ansi-bright-cyan', 'ansi-bright-white',
|
|
78
|
+
].map((t) => `--${t}`);
|
|
79
|
+
const CONTRACT = new Set(THEME_CONTRACT);
|
|
80
|
+
|
|
81
|
+
const lineAt = (text, index) => text.slice(0, index).split('\n').length;
|
|
82
|
+
const ext = (path) => (/\.([A-Za-z0-9]+)$/.exec(path)?.[1] ?? '').toLowerCase();
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Is `p` a plain path inside the package? No scheme, not absolute, no `..`,
|
|
86
|
+
* no backslashes — the containment every serving path relies on, decided on
|
|
87
|
+
* the text so it means the same on every platform.
|
|
88
|
+
*/
|
|
89
|
+
export function packagePathProblem(p) {
|
|
90
|
+
const s = String(p ?? '');
|
|
91
|
+
if (!s) return 'is empty';
|
|
92
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(s)) return 'names a URL — a design system reaches only its own files';
|
|
93
|
+
if (s.startsWith('/') || s.startsWith('\\') || /^[A-Za-z]:/.test(s)) return 'is absolute — paths are relative to the package';
|
|
94
|
+
if (s.includes('\\')) return 'uses a backslash — paths use /';
|
|
95
|
+
if (s.split('/').some((seg) => seg === '..')) return 'climbs out with .. — a package reaches only its own files';
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** `a/b/../c` style joining, for a url() relative to the stylesheet. Assumes no `..` (refused first). */
|
|
100
|
+
const joinRel = (base, rel) => {
|
|
101
|
+
const dir = base.includes('/') ? base.slice(0, base.lastIndexOf('/') + 1) : '';
|
|
102
|
+
return (dir + rel).split('/').filter((seg) => seg && seg !== '.').join('/');
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// ── the manifest ───────────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
/** The line a JSON key first appears on — good enough to point a person at it. */
|
|
108
|
+
const keyLine = (raw, key) => {
|
|
109
|
+
const i = raw.indexOf(`"${key}"`);
|
|
110
|
+
return i < 0 ? 1 : lineAt(raw, i);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* `design-system.json`, parsed and shaped. Returns `{ manifest, problems }`;
|
|
115
|
+
* `manifest` is null when the JSON itself does not parse. `prefix` is the
|
|
116
|
+
* token prefix the CSS is held to (`tokenPrefix`, default `--<name>-`).
|
|
117
|
+
*/
|
|
118
|
+
export function readManifest(raw) {
|
|
119
|
+
const problems = [];
|
|
120
|
+
const at = (rule, key, msg) => problems.push({ file: 'design-system.json', line: keyLine(raw, key), rule, msg });
|
|
121
|
+
let m;
|
|
122
|
+
try { m = JSON.parse(raw); } catch (e) {
|
|
123
|
+
const pos = /position (\d+)/.exec(e.message)?.[1];
|
|
124
|
+
problems.push({ file: 'design-system.json', line: pos ? lineAt(raw, Number(pos)) : 1, rule: 'manifest-json', msg: `not valid JSON — ${e.message}` });
|
|
125
|
+
return { manifest: null, problems };
|
|
126
|
+
}
|
|
127
|
+
if (!m || typeof m !== 'object' || Array.isArray(m)) {
|
|
128
|
+
problems.push({ file: 'design-system.json', line: 1, rule: 'manifest-json', msg: 'must be a JSON object' });
|
|
129
|
+
return { manifest: null, problems };
|
|
130
|
+
}
|
|
131
|
+
if (!Number.isInteger(m.apiVersion) || m.apiVersion < 1) at('manifest-field', 'apiVersion', 'apiVersion must be a positive integer — the design-system format version this package needs');
|
|
132
|
+
else if (m.apiVersion > DESIGN_SYSTEM_API_VERSION) {
|
|
133
|
+
at('api-too-new', 'apiVersion', `apiVersion ${m.apiVersion} needs a newer decklight — this one reads design systems up to ${DESIGN_SYSTEM_API_VERSION}`);
|
|
134
|
+
}
|
|
135
|
+
if (typeof m.name !== 'string' || !NAME_RE.test(m.name)) at('manifest-field', 'name', `name ${JSON.stringify(m.name)} — a lowercase word: letters, digits and -, starting with a letter (a slide writes it: data-template="<name>/<template>")`);
|
|
136
|
+
if (typeof m.version !== 'string' || !SEMVER_RE.test(m.version)) at('manifest-field', 'version', `version ${JSON.stringify(m.version)} — a semver version, major.minor.patch (e.g. "1.2.0"), no leading v`);
|
|
137
|
+
if (typeof m.title !== 'string' || !m.title.trim()) at('manifest-field', 'title', 'title must be the design system\'s human name, e.g. "Acme Brand"');
|
|
138
|
+
for (const key of ['styles', 'templates']) {
|
|
139
|
+
if (typeof m[key] !== 'string') at('manifest-field', key, `${key} must name the package's ${key === 'styles' ? 'stylesheet ("design-system.css")' : 'templates file ("templates.html")'}`);
|
|
140
|
+
else {
|
|
141
|
+
const why = packagePathProblem(m[key]);
|
|
142
|
+
if (why) at('file-outside', key, `${key} "${m[key]}" ${why}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (m.description !== undefined && typeof m.description !== 'string') at('manifest-field', 'description', 'description must be a string');
|
|
146
|
+
if (m.tokenPrefix !== undefined && (typeof m.tokenPrefix !== 'string' || !/^--[a-z][a-z0-9-]*-$/.test(m.tokenPrefix))) {
|
|
147
|
+
at('manifest-field', 'tokenPrefix', `tokenPrefix ${JSON.stringify(m.tokenPrefix)} — a custom-property prefix ending in -, e.g. "--acme-"`);
|
|
148
|
+
}
|
|
149
|
+
if (m.palette !== undefined) {
|
|
150
|
+
const scales = m.palette && typeof m.palette === 'object' && !Array.isArray(m.palette);
|
|
151
|
+
if (scales) {
|
|
152
|
+
// a palette of scales (SPEC BRAND_PALETTE): shaped by readScales in checkPackage
|
|
153
|
+
if (Number.isInteger(m.apiVersion) && m.apiVersion < BRAND_API_VERSION) at('manifest-field', 'palette', `a palette of scales needs apiVersion ${BRAND_API_VERSION} — an older decklight reads palette as a list and would refuse it`);
|
|
154
|
+
} else if (!Array.isArray(m.palette)) at('manifest-field', 'palette', 'palette must be { colors: { <name>: { label, weights } } } (a palette of scales) or an array of { group, label, token }');
|
|
155
|
+
else {
|
|
156
|
+
m.palette.forEach((p, i) => {
|
|
157
|
+
if (!p || typeof p.label !== 'string' || typeof p.token !== 'string' || !/^--[A-Za-z0-9_-]+$/.test(p.token)
|
|
158
|
+
|| (p.group !== undefined && typeof p.group !== 'string')) {
|
|
159
|
+
at('manifest-field', 'palette', `palette[${i}] must be { group, label, token: "--…" }`);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (m.themes !== undefined) {
|
|
165
|
+
if (!Array.isArray(m.themes)) at('manifest-field', 'themes', 'themes must be an array of { name, title, mode, tokens }');
|
|
166
|
+
else if (!(m.palette && typeof m.palette === 'object' && !Array.isArray(m.palette))) at('manifest-field', 'themes', 'themes are drawn from a palette of scales — palette must be { colors: { … } } (SPEC BRAND_PALETTE)');
|
|
167
|
+
else if (Number.isInteger(m.apiVersion) && m.apiVersion < BRAND_API_VERSION) at('manifest-field', 'themes', `themes need apiVersion ${BRAND_API_VERSION}`);
|
|
168
|
+
}
|
|
169
|
+
// roles (SPEC DESIGN_SYSTEM_ROLES): what each colour is for, set by every theme
|
|
170
|
+
const isObj = (v) => v && typeof v === 'object' && !Array.isArray(v);
|
|
171
|
+
if (m.roles !== undefined) {
|
|
172
|
+
if (!isObj(m.roles) || !Object.keys(m.roles).length) at('manifest-field', 'roles', 'roles must be { "<role>": { "label": "…", "group": "…" } }, one entry per role');
|
|
173
|
+
else if (!Array.isArray(m.themes) || !m.themes.length) at('manifest-field', 'roles', 'roles are set by the themes — a design system with roles has themes, and every theme gives every role a swatch');
|
|
174
|
+
else {
|
|
175
|
+
for (const [id, r] of Object.entries(m.roles)) {
|
|
176
|
+
const token = roleToken(typeof m.tokenPrefix === 'string' ? m.tokenPrefix : `--${m.name ?? ''}-`, id);
|
|
177
|
+
if (!NAME_RE.test(id)) at('manifest-field', 'roles', `roles: ${JSON.stringify(id)} — a role's name is a lowercase word (letters, digits and -), e.g. "text-heading-main"`);
|
|
178
|
+
// the longest token a slide's style may name (the edit server's own limit)
|
|
179
|
+
else if (token.length > 43) at('manifest-field', 'roles', `roles: ${id} makes the token ${token}, ${token.length} characters — a slide's style names at most 43; shorten the role's name`);
|
|
180
|
+
else if (!isObj(r) || typeof r.label !== 'string' || !r.label.trim() || (r.group !== undefined && (typeof r.group !== 'string' || !r.group.trim()))) {
|
|
181
|
+
at('manifest-field', 'roles', `roles: ${id} must be { "label": "<what a person reads>", "group": "<Text, Surface, Border…>" }`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (m.contract !== undefined) {
|
|
187
|
+
if (!isObj(m.roles)) at('manifest-field', 'contract', 'contract maps theme-contract tokens to roles — it needs "roles"');
|
|
188
|
+
else if (!isObj(m.contract)) at('manifest-field', 'contract', 'contract must be { "--<contract token>": "<role>" }, e.g. { "--heading-color": "text-heading-main" }');
|
|
189
|
+
else {
|
|
190
|
+
for (const [token, id] of Object.entries(m.contract)) {
|
|
191
|
+
if (!COLOUR_TOKENS.has(token)) at('manifest-field', 'contract', `contract: ${token} is not a colour token of the theme contract (SPEC THEMING)`);
|
|
192
|
+
else if (typeof id !== 'string' || !Object.hasOwn(m.roles, id)) at('manifest-field', 'contract', `contract: ${token} names ${JSON.stringify(id)}, which "roles" does not declare`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (m.recommendedThemes !== undefined && (!Array.isArray(m.recommendedThemes) || !m.recommendedThemes.every((t) => typeof t === 'string'))) {
|
|
197
|
+
at('manifest-field', 'recommendedThemes', 'recommendedThemes must be an array of theme names');
|
|
198
|
+
}
|
|
199
|
+
if (m.recommendedFonts !== undefined && (!Array.isArray(m.recommendedFonts) || !m.recommendedFonts.every((t) => typeof t === 'string'))) {
|
|
200
|
+
at('manifest-field', 'recommendedFonts', 'recommendedFonts must be an array of font names — a font package (name or name@marketplace) or a font-picker stack');
|
|
201
|
+
}
|
|
202
|
+
return { manifest: m, problems };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** A theme file's custom properties, `{ '--name': value }` — for holding a recommended theme to the palette. */
|
|
206
|
+
function parseThemeTokens(css) {
|
|
207
|
+
const out = {};
|
|
208
|
+
for (const m of String(css ?? '').replace(/\/\*[\s\S]*?\*\//g, '').matchAll(/(--[a-z0-9-]+)\s*:\s*([^;]+);/gi)) out[m[1].toLowerCase()] = m[2].trim();
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The token prefix a manifest holds its CSS to. */
|
|
213
|
+
export const tokenPrefixOf = (manifest) => manifest?.tokenPrefix ?? `--${manifest?.name ?? ''}-`;
|
|
214
|
+
|
|
215
|
+
// ── the stylesheet ─────────────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* What `design-system.css` declares and references: `{ tokens, exceptions,
|
|
219
|
+
* urls, imports }` — custom properties DECLARED (not merely read through
|
|
220
|
+
* var()), `ds-exception:` comments, every url() with its line, every @import.
|
|
221
|
+
*/
|
|
222
|
+
export function parseStyles(css) {
|
|
223
|
+
const text = String(css ?? '');
|
|
224
|
+
// comments blanked to spaces, so indexes (and lines) stay true
|
|
225
|
+
const bare = text.replace(/\/\*[\s\S]*?\*\//g, (c) => c.replace(/[^\n]/g, ' '));
|
|
226
|
+
const tokens = [];
|
|
227
|
+
for (const m of bare.matchAll(/(^|[{;\s])(--[A-Za-z0-9_-]+)\s*:([^;{}]*)/g)) {
|
|
228
|
+
tokens.push({ name: m[2], value: m[3].trim(), line: lineAt(text, m.index + m[1].length) });
|
|
229
|
+
}
|
|
230
|
+
const exceptions = [];
|
|
231
|
+
for (const m of text.matchAll(/ds-exception:\s*(--[A-Za-z0-9_-]+)\s+([^\n*]+)/g)) {
|
|
232
|
+
exceptions.push({ token: m[1], reason: m[2].trim(), line: lineAt(text, m.index) });
|
|
233
|
+
}
|
|
234
|
+
const urls = [];
|
|
235
|
+
for (const m of bare.matchAll(/url\(\s*(['"]?)(.*?)\1\s*\)/gi)) urls.push({ value: m[2].trim(), line: lineAt(text, m.index) });
|
|
236
|
+
const imports = [];
|
|
237
|
+
for (const m of bare.matchAll(/@import\b/gi)) imports.push({ line: lineAt(text, m.index) });
|
|
238
|
+
return { tokens, exceptions, urls, imports };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function styleProblems(css, { file, prefix, files }) {
|
|
242
|
+
const problems = [];
|
|
243
|
+
const { tokens, exceptions, urls, imports } = parseStyles(css);
|
|
244
|
+
const push = (line, rule, msg) => problems.push({ file, line, rule, msg });
|
|
245
|
+
for (const i of imports) push(i.line, 'css-import', '@import is refused — a design system reaches nothing outside its own package; put the rules in this file');
|
|
246
|
+
for (const u of urls) {
|
|
247
|
+
const v = u.value;
|
|
248
|
+
if (!v || v.startsWith('#')) continue; // a fragment points into the page itself
|
|
249
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(v)) { push(u.line, 'css-url-scheme', `url(${v}) names a URL — a design system reaches only its own files (url(assets/…))`); continue; }
|
|
250
|
+
if (v.startsWith('/') || v.startsWith('\\')) { push(u.line, 'css-url-absolute', `url(${v}) is absolute — url()s are relative to ${file}`); continue; }
|
|
251
|
+
if (v.split(/[\\/]/).includes('..')) { push(u.line, 'css-url-dotdot', `url(${v}) climbs out with .. — a design system reaches only its own files`); continue; }
|
|
252
|
+
const target = joinRel(file, v.replace(/[?#].*$/, ''));
|
|
253
|
+
if (!files.has(target)) push(u.line, 'css-url-missing', `url(${v}) — no such file in the package (${target})`);
|
|
254
|
+
}
|
|
255
|
+
const excepted = new Map(exceptions.map((e) => [e.token, e]));
|
|
256
|
+
for (const t of tokens) {
|
|
257
|
+
if (CONTRACT.has(t.name)) {
|
|
258
|
+
if (!excepted.has(t.name)) {
|
|
259
|
+
push(t.line, 'contract-token', `${t.name} is a theme-contract token — a design system sits beside any theme; set it only with a /* ds-exception: ${t.name} <reason> */ comment`);
|
|
260
|
+
}
|
|
261
|
+
} else if (prefix && !t.name.startsWith(prefix)) {
|
|
262
|
+
push(t.line, 'unprefixed-token', `${t.name} does not start with this package's tokenPrefix ${prefix}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const warnings = pageRules(css).map((r) => ({
|
|
266
|
+
file, line: r.line, rule: 'paints-the-page',
|
|
267
|
+
msg: `${r.selector} sets ${r.prop}: ${r.value} — the slide's page, text and type are the theme's (--bg, --fg, --font-body…); painting them here decides every theme's look on these slides, and the chrome on top keeps the theme's. Use the theme's tokens, or ship a theme beside the design system and name it in recommendedThemes`,
|
|
268
|
+
}));
|
|
269
|
+
return { problems, warnings, tokens, exceptions };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ── the page is the theme's ────────────────────────────────────────────────
|
|
273
|
+
|
|
274
|
+
/** Selectors whose subject is the slide itself, or the deck around it. */
|
|
275
|
+
const PAGE_SUBJECT = /^(?:section(?=$|[.[:#])|\.decklight(?=$|[[:])|\.decklight-stage(?=$|[[:]))/i;
|
|
276
|
+
/** A colour that is not a theme token: a literal, a function, a name, or another custom property. */
|
|
277
|
+
const OWN_COLOUR = /#[0-9a-f]{3,8}\b|\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|color-mix)\(|\bvar\(--|\b(?:white|black|red|green|blue|gray|grey|silver|navy|teal|olive|maroon|purple|orange|yellow|ivory|beige|whitesmoke|snow|gainsboro)\b/i;
|
|
278
|
+
const CONTRACT_REF = /var\(\s*(--[a-z0-9-]+)\s*(?:,[^()]*)?\)/gi;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Does a declaration paint the page, text or type with something other than
|
|
282
|
+
* the theme's own tokens? A background IMAGE alone is art — a design system's
|
|
283
|
+
* to bring — so only a colour counts there.
|
|
284
|
+
*/
|
|
285
|
+
function ownsThePage(prop, value) {
|
|
286
|
+
const v = value.replace(/url\([^)]*\)/gi, ' ')
|
|
287
|
+
.replace(CONTRACT_REF, (whole, token) => (CONTRACT.has(token) ? ' ' : whole));
|
|
288
|
+
if (/^(?:background|background-color|color)$/.test(prop)) return OWN_COLOUR.test(v);
|
|
289
|
+
if (/^(?:font|font-family)$/.test(prop)) return !/^\s*(?:inherit|initial|unset)?\s*$/i.test(v.replace(/!important/i, ''));
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* The stylesheet's rules that paint the slide itself — its background, its
|
|
295
|
+
* text colour, its type — with the design system's own values rather than
|
|
296
|
+
* the theme's tokens. Not refused: a brand page may be the point. But the
|
|
297
|
+
* page is what a THEME owns (THEMING), and a design system that paints it
|
|
298
|
+
* decides every theme's look on its slides, chrome left clashing on top; so
|
|
299
|
+
* it is said, with the way out.
|
|
300
|
+
*/
|
|
301
|
+
export function pageRules(css) {
|
|
302
|
+
const text = String(css ?? '');
|
|
303
|
+
const bare = text.replace(/\/\*[\s\S]*?\*\//g, (c) => c.replace(/[^\n]/g, ' '));
|
|
304
|
+
const out = [];
|
|
305
|
+
for (const rule of bare.matchAll(/([^{}]*)\{([^{}]*)\}/g)) {
|
|
306
|
+
const selector = rule[1].trim().replace(/\s+/g, ' ');
|
|
307
|
+
const onPage = selector.split(',').some((part) => PAGE_SUBJECT.test(part.trim().split(/[\s>+~]+/).filter(Boolean).pop() ?? ''));
|
|
308
|
+
if (!onPage) continue;
|
|
309
|
+
let at = rule.index + rule[1].length + 1;
|
|
310
|
+
for (const decl of rule[2].split(';')) {
|
|
311
|
+
const m = /^\s*([a-z-]+)\s*:\s*([\s\S]*?)\s*$/i.exec(decl);
|
|
312
|
+
if (m && ownsThePage(m[1].toLowerCase(), m[2])) {
|
|
313
|
+
out.push({ selector, prop: m[1].toLowerCase(), value: m[2], line: lineAt(text, at + decl.search(/\S/)) });
|
|
314
|
+
}
|
|
315
|
+
at += decl.length + 1;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return out;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// ── plain markup and conventional parts (SPEC DESIGN_SYSTEMS) ───────────────
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The conventional slot names, each with its usual hint: a design system uses
|
|
325
|
+
* them where its part IS that part, so a slide switched between two design
|
|
326
|
+
* systems keeps its content by name.
|
|
327
|
+
*/
|
|
328
|
+
export const CONVENTIONAL_SLOTS = [
|
|
329
|
+
{ name: 'eyebrow', hint: 'p' }, { name: 'title', hint: 'h1,h2' }, { name: 'subtitle', hint: 'p' },
|
|
330
|
+
{ name: 'number', hint: 'p' }, { name: 'points', hint: 'ul,ol' }, { name: 'body', hint: 'p,div' },
|
|
331
|
+
{ name: 'diagram', hint: 'div,svg,img' }, { name: 'table', hint: 'table' }, { name: 'quote', hint: 'blockquote,p' },
|
|
332
|
+
{ name: 'code', hint: 'pre' }, { name: 'image', hint: 'img,figure' }, { name: 'caption', hint: 'p,figcaption' },
|
|
333
|
+
];
|
|
334
|
+
const CONVENTIONAL_NAMES = new Set(CONVENTIONAL_SLOTS.map((c) => c.name));
|
|
335
|
+
/** A tag that belongs to one part only: a slot hinted only with tags of one part is that part. */
|
|
336
|
+
const PART_OF_TAG = {
|
|
337
|
+
h1: 'title', h2: 'title', h3: 'title', ul: 'points', ol: 'points', table: 'table', blockquote: 'quote',
|
|
338
|
+
pre: 'code', figure: 'image', img: 'image', svg: 'diagram', figcaption: 'caption',
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
/** The conventional part a slot is, by its hint, when its name is not that part's: `{ part }` or null. */
|
|
342
|
+
export function conventionalMiss(slot) {
|
|
343
|
+
if (!slot || slot.default || CONVENTIONAL_NAMES.has(slot.name)) return null;
|
|
344
|
+
const tags = String(slot.hint ?? '').toLowerCase().split(/[\s,]+/).filter(Boolean);
|
|
345
|
+
const parts = new Set(tags.map((t) => PART_OF_TAG[t] ?? null));
|
|
346
|
+
if (parts.size !== 1 || parts.has(null)) return null;
|
|
347
|
+
return { part: [...parts][0] };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** The plain content elements a design system may style inside a slide. */
|
|
351
|
+
export const BASE_ELEMENTS = ['h1', 'h2', 'h3', 'p', 'ul', 'ol', 'li', 'table', 'blockquote', 'pre', 'code', 'a', 'figcaption'];
|
|
352
|
+
const BASE_SET = new Set(BASE_ELEMENTS);
|
|
353
|
+
|
|
354
|
+
/** A selector list split at its top-level commas (a comma inside :is( … ) stays). */
|
|
355
|
+
function splitSelectors(text) {
|
|
356
|
+
const out = [];
|
|
357
|
+
let depth = 0; let at = 0;
|
|
358
|
+
for (let i = 0; i < text.length; i++) {
|
|
359
|
+
const c = text[i];
|
|
360
|
+
if (c === '(') depth++;
|
|
361
|
+
else if (c === ')') depth = Math.max(0, depth - 1);
|
|
362
|
+
else if (c === ',' && depth === 0) { out.push(text.slice(at, i)); at = i + 1; }
|
|
363
|
+
}
|
|
364
|
+
out.push(text.slice(at));
|
|
365
|
+
return out.map((x) => x.trim()).filter(Boolean);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** A selector's subject: its last compound (combinators outside parentheses split it). */
|
|
369
|
+
function subjectOf(selector) {
|
|
370
|
+
let depth = 0; let at = 0;
|
|
371
|
+
for (let i = 0; i < selector.length; i++) {
|
|
372
|
+
const c = selector[i];
|
|
373
|
+
if (c === '(') depth++;
|
|
374
|
+
else if (c === ')') depth = Math.max(0, depth - 1);
|
|
375
|
+
else if (depth === 0 && /[\s>+~]/.test(c)) at = i + 1;
|
|
376
|
+
}
|
|
377
|
+
return selector.slice(at).trim();
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* The stylesheet's base styles: the plain content elements it styles inside a
|
|
382
|
+
* slide (`.decklight section h2`, `.decklight section :is(h2, ul)`), and, for
|
|
383
|
+
* a rule that styles a component AND a plain element (`.decklight .cf-title,
|
|
384
|
+
* .decklight section:not([data-template]) > h2`), which elements give each
|
|
385
|
+
* component class. `{ elements: ['h2', 'ul'], components: { 'cf-title': ['h2'] } }`.
|
|
386
|
+
*/
|
|
387
|
+
export function baseStyles(css) {
|
|
388
|
+
const bare = String(css ?? '').replace(/\/\*[\s\S]*?\*\//g, ' ');
|
|
389
|
+
const elements = new Set();
|
|
390
|
+
const components = {};
|
|
391
|
+
for (const rule of bare.matchAll(/([^{}]*)\{[^{}]*\}/g)) {
|
|
392
|
+
const prelude = rule[1].trim();
|
|
393
|
+
if (!prelude || prelude.startsWith('@')) continue;
|
|
394
|
+
const plain = new Set();
|
|
395
|
+
const classes = new Set();
|
|
396
|
+
for (const sel of splitSelectors(prelude.replace(/^@[^{]*$/, ''))) {
|
|
397
|
+
const subject = subjectOf(sel);
|
|
398
|
+
const cls = [...subject.matchAll(/\.([A-Za-z][\w-]*)/g)].map((m) => m[1]);
|
|
399
|
+
if (cls.length) { classes.add(cls[0]); continue; }
|
|
400
|
+
if (!/\bsection\b/.test(sel)) continue; // a base style is inside a slide
|
|
401
|
+
const bareTag = /^([a-z][a-z0-9]*)/i.exec(subject)?.[1]?.toLowerCase();
|
|
402
|
+
const isTags = /^:is\(([^()]*)\)/i.exec(subject)?.[1]?.split(',').map((t) => t.trim().toLowerCase()) ?? [];
|
|
403
|
+
for (const t of bareTag ? [bareTag] : isTags) if (BASE_SET.has(t)) plain.add(t);
|
|
404
|
+
}
|
|
405
|
+
for (const t of plain) elements.add(t);
|
|
406
|
+
if (plain.size) for (const c of classes) components[c] = [...new Set([...(components[c] ?? []), ...plain])];
|
|
407
|
+
}
|
|
408
|
+
return { elements: BASE_ELEMENTS.filter((t) => elements.has(t)), components };
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// ── the templates ────────────────────────────────────────────────────────────
|
|
412
|
+
|
|
413
|
+
/** Tags a template may never contain: anything that runs, loads, or frames. */
|
|
414
|
+
const FORBIDDEN_TAGS = new Set(['script', 'style', 'iframe', 'object', 'embed', 'foreignobject', 'link', 'meta', 'base', 'frame', 'frameset', 'template-in-template']);
|
|
415
|
+
/** Attributes that point somewhere — a template carries structure; its art lives in the CSS. */
|
|
416
|
+
const REF_ATTRS = new Set(['src', 'srcset', 'href', 'xlink:href', 'poster', 'action', 'formaction', 'data', 'background']);
|
|
417
|
+
|
|
418
|
+
/** Numeric and the common named entities decoded, controls and whitespace stripped — what a browser sees in a URL. */
|
|
419
|
+
const urlSeen = (v) => String(v).replace(/&#x([0-9a-f]+);?/gi, (_, h) => String.fromCodePoint(parseInt(h, 16)))
|
|
420
|
+
.replace(/&#(\d+);?/g, (_, d) => String.fromCodePoint(Number(d)))
|
|
421
|
+
.replace(/:/gi, ':').replace(/&tab;|&newline;/gi, '')
|
|
422
|
+
.replace(/[\u0000- ]/g, '');
|
|
423
|
+
|
|
424
|
+
const ATTR_RE = /([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
425
|
+
function attrsOf(src) {
|
|
426
|
+
const out = [];
|
|
427
|
+
for (const m of src.matchAll(ATTR_RE)) out.push({ name: m[1].toLowerCase(), value: m[2] ?? m[3] ?? m[4] ?? '' });
|
|
428
|
+
return out;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* `templates.html` as data: `[{ id, title, slots: [{ name, hint, required,
|
|
433
|
+
* default }], line }]`, in file order. The structure a slide fills, read
|
|
434
|
+
* without a DOM — what the deck's meta block, `slide templates` and
|
|
435
|
+
* `check` all print.
|
|
436
|
+
*/
|
|
437
|
+
export function parseTemplates(html) {
|
|
438
|
+
return walkTemplates(html).templates;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** The tokenizer every template question goes through — tags, comments and text, with lines. */
|
|
442
|
+
function walkTemplates(html) {
|
|
443
|
+
const text = String(html ?? '');
|
|
444
|
+
const templates = [];
|
|
445
|
+
const problems = [];
|
|
446
|
+
const push = (line, rule, msg) => problems.push({ line, rule, msg });
|
|
447
|
+
let current = null;
|
|
448
|
+
let depth = 0;
|
|
449
|
+
// element nesting inside the current template — where data-ds-bleed may sit
|
|
450
|
+
let el = 0;
|
|
451
|
+
const VOID = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr']);
|
|
452
|
+
// one finding per stray run outside the templates, not one per tag and text in it
|
|
453
|
+
let strayed = false;
|
|
454
|
+
const stray = (line, msg) => { if (!strayed) { strayed = true; push(line, 'template-outside-block', msg); } };
|
|
455
|
+
const TAG = /<!--[\s\S]*?-->|<(\/?)([A-Za-z][\w:-]*)((?:[^>"']|"[^"]*"|'[^']*')*)>/g;
|
|
456
|
+
let last = 0;
|
|
457
|
+
for (const m of text.matchAll(TAG)) {
|
|
458
|
+
const between = text.slice(last, m.index);
|
|
459
|
+
last = m.index + m[0].length;
|
|
460
|
+
if (!current && between.trim()) stray(lineAt(text, m.index - between.length + between.search(/\S/)), 'text outside a <template data-template> block — everything in templates.html sits inside one');
|
|
461
|
+
if (m[0].startsWith('<!--')) continue;
|
|
462
|
+
const closing = m[1] === '/';
|
|
463
|
+
const tag = m[2].toLowerCase();
|
|
464
|
+
const line = lineAt(text, m.index);
|
|
465
|
+
const selfClosing = /\/\s*$/.test(m[3]);
|
|
466
|
+
if (tag === 'template') {
|
|
467
|
+
if (closing) {
|
|
468
|
+
if (current && depth === 0) { templates.push(current); current = null; }
|
|
469
|
+
else if (current) depth--;
|
|
470
|
+
// A </template> with nothing open is not harmless: wherever these
|
|
471
|
+
// templates are inlined into a page's own <template>, it would close
|
|
472
|
+
// that one early and let what follows out as live markup.
|
|
473
|
+
else push(line, 'template-outside-block', '</template> with no <template data-template> open — it would close whatever template the templates are carried in');
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (current) { push(line, 'template-forbidden-tag', '<template> inside a template — one level of templates, one per template'); depth++; continue; }
|
|
477
|
+
const attrs = attrsOf(m[3]);
|
|
478
|
+
const id = attrs.find((a) => a.name === 'data-template')?.value;
|
|
479
|
+
if (id === undefined) { push(line, 'template-outside-block', '<template> without data-template — every template is a template, named by data-template'); }
|
|
480
|
+
else if (!NAME_RE.test(id)) push(line, 'template-id', `data-template="${id}" — a lowercase word: letters, digits and -, starting with a letter`);
|
|
481
|
+
current = { id: id ?? '', title: attrs.find((a) => a.name === 'data-title')?.value || id || '', slots: [], line, bleed: false };
|
|
482
|
+
depth = 0;
|
|
483
|
+
el = 0;
|
|
484
|
+
strayed = false;
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (!current) {
|
|
488
|
+
// the opening tag is the finding; its closing tag is the same one
|
|
489
|
+
if (!closing) stray(line, `<${tag}> outside a <template data-template> block — everything in templates.html sits inside one`);
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
if (closing) { el = Math.max(0, el - 1); continue; }
|
|
493
|
+
if (FORBIDDEN_TAGS.has(tag)) push(line, 'template-forbidden-tag', `<${tag}> is refused in a template — a template is inert structure: no script, style, frames or embedded documents`);
|
|
494
|
+
for (const a of attrsOf(m[3].replace(/\/\s*$/, ''))) {
|
|
495
|
+
if (/^on/.test(a.name)) push(line, 'template-forbidden-attr', `${a.name}= on <${tag}> is refused — a template runs nothing`);
|
|
496
|
+
else if (a.name === 'srcdoc') push(line, 'template-forbidden-attr', `srcdoc on <${tag}> is refused — a template embeds no document`);
|
|
497
|
+
else if (/^javascript:/i.test(urlSeen(a.value))) push(line, 'template-javascript-url', `${a.name}="${a.value}" on <${tag}> is a javascript: URL — refused`);
|
|
498
|
+
else if (REF_ATTRS.has(a.name) && a.value && !a.value.startsWith('#')) {
|
|
499
|
+
push(line, 'template-reference', `${a.name}="${a.value}" on <${tag}> — a template carries structure only; put art in design-system.css (url(assets/…)), where it resolves inside the package`);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
const attrs = attrsOf(m[3]);
|
|
503
|
+
const slot = attrs.find((a) => a.name === 'data-slot');
|
|
504
|
+
if (slot) {
|
|
505
|
+
if (!NAME_RE.test(slot.value)) push(line, 'slot-name', `data-slot="${slot.value}" in ${current.id} — a lowercase word: letters, digits and -, starting with a letter (a slide writes it)`);
|
|
506
|
+
else if (current.slots.some((s) => s.name === slot.value)) push(line, 'slot-repeated', `slot "${slot.value}" appears twice in ${current.id} — a slot name is one place in a template`);
|
|
507
|
+
const isDefault = attrs.some((a) => a.name === 'data-slot-default');
|
|
508
|
+
if (isDefault && current.slots.some((s) => s.default)) push(line, 'slot-default-twice', `${current.id} has more than one data-slot-default — unslotted content can land in one place only`);
|
|
509
|
+
current.slots.push({
|
|
510
|
+
name: slot.value,
|
|
511
|
+
hint: attrs.find((a) => a.name === 'data-slot-hint')?.value ?? '',
|
|
512
|
+
required: attrs.some((a) => a.name === 'data-slot-required'),
|
|
513
|
+
default: isDefault,
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
// data-ds-bleed: the element whose background art fills the screen, not
|
|
517
|
+
// just the stage (SPEC DESIGN_SYSTEMS) — the template's own root, and one
|
|
518
|
+
if (attrs.some((a) => a.name === 'data-ds-bleed')) {
|
|
519
|
+
if (el !== 0) push(line, 'bleed-placement', `data-ds-bleed on <${tag}> in ${current.id} — it marks the template's top-level element, whose background is the slide's art`);
|
|
520
|
+
else if (current.bleed) push(line, 'bleed-twice', `${current.id} marks data-ds-bleed twice — one element's art bleeds`);
|
|
521
|
+
current.bleed = true;
|
|
522
|
+
}
|
|
523
|
+
if (selfClosing || VOID.has(tag)) continue;
|
|
524
|
+
el++;
|
|
525
|
+
}
|
|
526
|
+
const tail = text.slice(last);
|
|
527
|
+
if (current) push(current.line, 'template-outside-block', `<template data-template="${current.id}"> is never closed`);
|
|
528
|
+
else if (tail.trim()) stray(lineAt(text, last + tail.search(/\S/)), 'text outside a <template data-template> block — everything in templates.html sits inside one');
|
|
529
|
+
const seen = new Map();
|
|
530
|
+
for (const l of templates) {
|
|
531
|
+
if (!l.id) continue;
|
|
532
|
+
if (seen.has(l.id)) push(l.line, 'template-duplicate', `data-template="${l.id}" is defined twice (lines ${seen.get(l.id)} and ${l.line}) — a template id names one template`);
|
|
533
|
+
else seen.set(l.id, l.line);
|
|
534
|
+
}
|
|
535
|
+
return { templates, problems };
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** Everything wrong with a templates file, each `{ line, rule, msg }`. */
|
|
539
|
+
export function templateProblems(html) {
|
|
540
|
+
return walkTemplates(html).problems;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// ── SVG assets ─────────────────────────────────────────────────────────────
|
|
544
|
+
|
|
545
|
+
/** An SVG is a document that can run script: none, no handlers, nothing outside it. */
|
|
546
|
+
export function svgProblems(svg) {
|
|
547
|
+
const text = String(svg ?? '');
|
|
548
|
+
const problems = [];
|
|
549
|
+
for (const m of text.matchAll(/<\s*script\b/gi)) problems.push({ line: lineAt(text, m.index), rule: 'svg-script', msg: '<script> in an SVG asset is refused — an asset is a picture, not a program' });
|
|
550
|
+
for (const m of text.matchAll(/<\s*foreignObject\b/gi)) problems.push({ line: lineAt(text, m.index), rule: 'svg-script', msg: '<foreignObject> in an SVG asset is refused — it embeds a document' });
|
|
551
|
+
for (const m of text.matchAll(/<[A-Za-z][^>]*?\s(on[a-z]+)\s*=/gi)) problems.push({ line: lineAt(text, m.index), rule: 'svg-handler', msg: `${m[1]}= in an SVG asset is refused — an asset runs nothing` });
|
|
552
|
+
for (const m of text.matchAll(/\s((?:xlink:)?href)\s*=\s*(["'])(.*?)\2/gi)) {
|
|
553
|
+
const v = m[3].trim();
|
|
554
|
+
if (v && !v.startsWith('#')) problems.push({ line: lineAt(text, m.index), rule: 'svg-external-href', msg: `${m[1]}="${v}" reaches outside the SVG — only #fragment references are allowed` });
|
|
555
|
+
}
|
|
556
|
+
return problems;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// ── the package ────────────────────────────────────────────────────────────
|
|
560
|
+
|
|
561
|
+
/** Files a package may hold that are neither its manifest, CSS, templates nor assets. */
|
|
562
|
+
const isPapers = (p) => /^(readme|license|licence|changelog|notice)(\.(md|txt))?$/i.test(p);
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Check a whole package. `pkg` is plain data:
|
|
566
|
+
* { manifest: <design-system.json text>,
|
|
567
|
+
* files: Map<relative path, { size, text? }> } — every file in the package
|
|
568
|
+
* (the CSS, templates and every .svg must carry `text`).
|
|
569
|
+
* Returns `{ ok, problems, warnings, summary }`. `problems` refuse the
|
|
570
|
+
* package; `warnings` do not. Each is `{ file, line?, rule, msg }`.
|
|
571
|
+
*/
|
|
572
|
+
export function checkPackage(pkg) {
|
|
573
|
+
const problems = [];
|
|
574
|
+
const warnings = [];
|
|
575
|
+
const files = pkg.files instanceof Map ? pkg.files : new Map(Object.entries(pkg.files ?? {}));
|
|
576
|
+
const { manifest, problems: mp } = readManifest(pkg.manifest ?? '');
|
|
577
|
+
problems.push(...mp);
|
|
578
|
+
const summary = { name: manifest?.name ?? null, version: manifest?.version ?? null, apiVersion: manifest?.apiVersion ?? null, title: manifest?.title ?? null };
|
|
579
|
+
if (!manifest) return { ok: false, problems, warnings, summary };
|
|
580
|
+
|
|
581
|
+
// the prefix the CSS is held to — unknowable when neither a tokenPrefix nor
|
|
582
|
+
// a valid name says it, and then that rule stands aside rather than holding
|
|
583
|
+
// every token to a nonsense prefix
|
|
584
|
+
const prefixKnown = (typeof manifest.tokenPrefix === 'string' && /^--[a-z][a-z0-9-]*-$/.test(manifest.tokenPrefix))
|
|
585
|
+
|| (typeof manifest.name === 'string' && NAME_RE.test(manifest.name));
|
|
586
|
+
const prefix = prefixKnown ? tokenPrefixOf(manifest) : null;
|
|
587
|
+
const stylesPath = typeof manifest.styles === 'string' && !packagePathProblem(manifest.styles) ? manifest.styles : null;
|
|
588
|
+
const templatesPath = typeof manifest.templates === 'string' && !packagePathProblem(manifest.templates) ? manifest.templates : null;
|
|
589
|
+
|
|
590
|
+
let tokens = [];
|
|
591
|
+
let stylesRead = false;
|
|
592
|
+
const conventionNotes = [];
|
|
593
|
+
if (stylesPath) {
|
|
594
|
+
const f = files.get(stylesPath);
|
|
595
|
+
if (!f) problems.push({ file: 'design-system.json', line: keyLine(pkg.manifest, 'styles'), rule: 'file-missing', msg: `styles names ${stylesPath}, which is not in the package` });
|
|
596
|
+
else {
|
|
597
|
+
stylesRead = true;
|
|
598
|
+
const r = styleProblems(f.text ?? '', { file: stylesPath, prefix, files });
|
|
599
|
+
problems.push(...r.problems);
|
|
600
|
+
warnings.push(...r.warnings);
|
|
601
|
+
tokens = r.tokens;
|
|
602
|
+
summary.exceptions = r.exceptions;
|
|
603
|
+
// the plain elements it styles, and which component each also is (SPEC DESIGN_SYSTEMS)
|
|
604
|
+
summary.baseStyles = baseStyles(f.text ?? '');
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
summary.tokens = [...new Set(tokens.filter((t) => prefix && t.name.startsWith(prefix)).map((t) => t.name))];
|
|
608
|
+
// what each of them is set to (its first declaration): how check resolves an alias a slide reads to a colour
|
|
609
|
+
summary.tokenValues = {};
|
|
610
|
+
for (const t of tokens) if (prefix && t.name.startsWith(prefix) && !(t.name in summary.tokenValues)) summary.tokenValues[t.name] = t.value;
|
|
611
|
+
summary.prefix = prefix;
|
|
612
|
+
|
|
613
|
+
if (templatesPath) {
|
|
614
|
+
const f = files.get(templatesPath);
|
|
615
|
+
if (!f) problems.push({ file: 'design-system.json', line: keyLine(pkg.manifest, 'templates'), rule: 'file-missing', msg: `templates names ${templatesPath}, which is not in the package` });
|
|
616
|
+
else {
|
|
617
|
+
const { templates, problems: lp } = walkTemplates(f.text ?? '');
|
|
618
|
+
problems.push(...lp.map((p) => ({ file: templatesPath, ...p })));
|
|
619
|
+
summary.templates = templates.map(({ id, title, slots }) => ({ id, title, slots }));
|
|
620
|
+
// a part named off the convention: said, never refused
|
|
621
|
+
for (const t of templates) {
|
|
622
|
+
for (const slot of t.slots) {
|
|
623
|
+
const miss = conventionalMiss(slot);
|
|
624
|
+
if (miss) conventionNotes.push({ file: templatesPath, line: t.line, rule: 'slot-name-conventional', msg: `${t.id}: the slot "${slot.name}" (hint ${slot.hint}) is a ${miss.part}; the conventional name for this part is "${miss.part}", which keeps its content when a slide switches between design systems` });
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
const declared = new Set(tokens.map((t) => t.name));
|
|
631
|
+
// a palette is held to the stylesheet only when there was one to read
|
|
632
|
+
(stylesRead && Array.isArray(manifest.palette) ? manifest.palette : []).forEach((p, i) => {
|
|
633
|
+
if (p && typeof p.token === 'string' && !declared.has(p.token)) {
|
|
634
|
+
problems.push({ file: 'design-system.json', line: keyLine(pkg.manifest, 'palette'), rule: 'palette-undefined', msg: `palette[${i}] "${p.label ?? ''}" names ${p.token}, which ${stylesPath ?? 'the stylesheet'} never defines` });
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
// ── a palette of scales, and the themes drawn from it (SPEC BRAND_PALETTE)
|
|
639
|
+
const notes = [];
|
|
640
|
+
const scaled = manifest.palette && typeof manifest.palette === 'object' && !Array.isArray(manifest.palette);
|
|
641
|
+
if (scaled && prefix && !mp.some((p) => p.rule === 'api-too-new')) {
|
|
642
|
+
const read = readScales(manifest.palette);
|
|
643
|
+
const atKey = (key) => (p) => ({ file: 'design-system.json', line: keyLine(pkg.manifest, key), ...p });
|
|
644
|
+
problems.push(...read.problems.map(atKey('palette')));
|
|
645
|
+
notes.push(...read.notes.map(atKey('palette')));
|
|
646
|
+
if (!read.problems.length) {
|
|
647
|
+
// the manifest is the one source of a swatch: the stylesheet reads it, never redefines it
|
|
648
|
+
const swatch = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(${Object.keys(read.scales).join('|')})-(${WEIGHTS.join('|')})$`);
|
|
649
|
+
for (const t of tokens) {
|
|
650
|
+
if (swatch.test(t.name)) problems.push({ file: stylesPath, line: t.line, rule: 'palette-redefined', msg: `${t.name} is a swatch of the palette — the server defines it from design-system.json; read it with var(), never redefine it here` });
|
|
651
|
+
}
|
|
652
|
+
// roles too: the theme sets them, the stylesheet only reads them (SPEC DESIGN_SYSTEM_ROLES)
|
|
653
|
+
// false: declared but malformed (already said), so the themes are not also told they have none
|
|
654
|
+
const rolesBroken = mp.some((p) => p.rule === 'manifest-field' && /^(?:roles|contract)\b/.test(p.msg));
|
|
655
|
+
const roles = rolesBroken ? false
|
|
656
|
+
: manifest.roles && typeof manifest.roles === 'object' && !Array.isArray(manifest.roles) ? manifest.roles : null;
|
|
657
|
+
const contract = roles && manifest.contract && typeof manifest.contract === 'object' ? manifest.contract : null;
|
|
658
|
+
if (roles) {
|
|
659
|
+
const named = new Set(Object.keys(roles).map((id) => roleToken(prefix, id)));
|
|
660
|
+
for (const t of tokens) {
|
|
661
|
+
if (named.has(t.name)) problems.push({ file: stylesPath, line: t.line, rule: 'role-redefined', msg: `${t.name} is a role — each theme sets it from design-system.json; read it with var(), never define it here` });
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
const shipped = new Set(pkg.shippedThemes ?? []);
|
|
665
|
+
const ex = expandThemes(read, Array.isArray(manifest.themes) ? manifest.themes : [], prefix, { roles, contract });
|
|
666
|
+
problems.push(...ex.problems.map(atKey('themes')));
|
|
667
|
+
notes.push(...ex.notes.map(atKey('themes')));
|
|
668
|
+
for (const t of ex.themes) {
|
|
669
|
+
if (shipped.has(t.name)) problems.push({ file: 'design-system.json', line: keyLine(pkg.manifest, 'themes'), rule: 'theme-name', msg: `themes: "${t.name}" is a theme decklight ships — a brand theme takes a name of its own` });
|
|
670
|
+
}
|
|
671
|
+
// a recommended theme from outside, held to the palette it is recommended for
|
|
672
|
+
for (const [name, css] of Object.entries(pkg.recommendedThemeCss ?? {})) {
|
|
673
|
+
const off = offPaletteTokens(read.scales, parseThemeTokens(css));
|
|
674
|
+
if (off.length) {
|
|
675
|
+
warnings.push({ file: 'design-system.json', line: keyLine(pkg.manifest, 'recommendedThemes'), rule: 'recommended-off-palette',
|
|
676
|
+
msg: `recommends ${name}, whose ${off.slice(0, 4).map((o) => `${o.token} ${o.value} (nearest ${o.nearest})`).join(', ')}${off.length > 4 ? ` and ${off.length - 4} more` : ''} ${off.length === 1 ? 'is' : 'are'} not in the palette` });
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
summary.brand = {
|
|
680
|
+
neutral: read.neutral,
|
|
681
|
+
palette: paletteEntries(read.scales, prefix),
|
|
682
|
+
scales: Object.values(read.scales).map((sc) => ({ name: sc.name, label: sc.label, role: sc.role, derived: sc.derived })),
|
|
683
|
+
themes: ex.themes.map((t) => ({ name: t.name, title: t.title ?? t.name, mode: t.mode, css: t.css, refs: t.refs, stated: t.stated, chosen: t.chosen,
|
|
684
|
+
...(roles ? { roleRefs: t.roleRefs, roleValues: t.roleValues, tokens: t.tokens, viaRole: t.viaRole } : {}) })),
|
|
685
|
+
...(roles ? {
|
|
686
|
+
roles: Object.entries(roles).map(([id, r]) => ({ id, label: r.label, group: r.group ?? 'Roles', token: roleToken(prefix, id) })),
|
|
687
|
+
contract: contract ?? {},
|
|
688
|
+
} : {}),
|
|
689
|
+
};
|
|
690
|
+
// a package with roles colours its components through them, and only them
|
|
691
|
+
if (roles && ex.themes.length) {
|
|
692
|
+
const first = ex.themes[0];
|
|
693
|
+
const ctx = { prefix, scales: read.scales, roles, contractHex: first.tokens };
|
|
694
|
+
const say = (file, line, off) => warnings.push({ file, line, rule: 'component-off-role',
|
|
695
|
+
msg: `${off.map((o) => `${o.name}${o.kind === 'contract' ? ' (a theme-contract token)' : o.kind === 'swatch' ? ' (a swatch)' : ''}`
|
|
696
|
+
+ ` (nearest role ${(o.kind === 'contract' && contract && Object.entries(contract).find(([t]) => t === o.name)?.[1]) || nearestRole(first.roleValues, o.hex) || '?'})`).join(', ')}`
|
|
697
|
+
+ ` — a component colours through the design system's roles, so every theme recolours it; read a role, var(${roleToken(prefix, '<role>')})` });
|
|
698
|
+
if (stylesRead) {
|
|
699
|
+
const css = files.get(stylesPath).text ?? '';
|
|
700
|
+
const bare = css.replace(/\/\*[\s\S]*?\*\//g, (c) => c.replace(/[^\n]/g, ' '));
|
|
701
|
+
for (const rule of bare.matchAll(/([^{}]*)\{([^{}]*)\}/g)) {
|
|
702
|
+
let at = rule.index + rule[1].length + 1;
|
|
703
|
+
for (const decl of rule[2].split(';')) {
|
|
704
|
+
const m = /^\s*(--[A-Za-z0-9_-]+|[a-z-]+)\s*:\s*([\s\S]*?)\s*$/i.exec(decl);
|
|
705
|
+
if (m) {
|
|
706
|
+
const off = offRoleColours(m[2], ctx);
|
|
707
|
+
if (off.length) say(stylesPath, lineAt(css, at + decl.search(/\S/)), off);
|
|
708
|
+
}
|
|
709
|
+
at += decl.length + 1;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
const tf = templatesPath && files.get(templatesPath);
|
|
714
|
+
if (tf) {
|
|
715
|
+
const html = tf.text ?? '';
|
|
716
|
+
for (const a of html.matchAll(/\s(style|fill|stroke|color|stop-color|bgcolor)\s*=\s*(?:"([^"]*)"|'([^']*)')/gi)) {
|
|
717
|
+
const off = offRoleColours(a[2] ?? a[3], ctx);
|
|
718
|
+
if (off.length) say(templatesPath, lineAt(html, a.index + 1), off);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
notes.push(...conventionNotes);
|
|
725
|
+
summary.notes = notes;
|
|
726
|
+
|
|
727
|
+
const assets = [];
|
|
728
|
+
for (const [path, f] of files) {
|
|
729
|
+
// the stylesheet and templates the manifest names — or, when it names them
|
|
730
|
+
// wrongly, the conventional files; either way not assets, and already said
|
|
731
|
+
if (path === 'design-system.json' || path === stylesPath || path === templatesPath) continue;
|
|
732
|
+
if (((!stylesPath || !files.has(stylesPath)) && path === 'design-system.css')
|
|
733
|
+
|| ((!templatesPath || !files.has(templatesPath)) && path === 'templates.html')) continue;
|
|
734
|
+
if (path.split('/').some((seg) => seg.startsWith('.'))) continue; // never served — dotfiles are not the package
|
|
735
|
+
if (!path.includes('/') && isPapers(path)) continue;
|
|
736
|
+
const e = ext(path);
|
|
737
|
+
if (!ASSET_EXTENSIONS.includes(e)) {
|
|
738
|
+
problems.push({ file: path, rule: 'asset-extension', msg: `.${e || '(none)'} is not an asset a design system may carry — ${ASSET_EXTENSIONS.join(', ')}` });
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
assets.push({ path, ext: e, size: f.size ?? 0 });
|
|
742
|
+
if ((f.size ?? 0) > ASSET_WARN_BYTES) {
|
|
743
|
+
warnings.push({ file: path, rule: 'asset-size', msg: `${(f.size / 1024 / 1024).toFixed(1)} MB — over the ${ASSET_WARN_BYTES / 1024 / 1024} MB a deck should carry per asset; every deck using it pays for it on load` });
|
|
744
|
+
}
|
|
745
|
+
if (e === 'svg') problems.push(...svgProblems(f.text ?? '').map((p) => ({ file: path, ...p })));
|
|
746
|
+
}
|
|
747
|
+
summary.assets = assets;
|
|
748
|
+
summary.palette = Array.isArray(manifest.palette) ? manifest.palette.length
|
|
749
|
+
: summary.brand ? `${summary.brand.scales.length} colours, ${summary.brand.palette.length} swatches${summary.brand.palette.some((e) => e.derived) ? ` (${summary.brand.palette.filter((e) => e.derived).length} derived)` : ''}` : 0;
|
|
750
|
+
summary.recommendedThemes = manifest.recommendedThemes ?? [];
|
|
751
|
+
summary.recommendedFonts = manifest.recommendedFonts ?? [];
|
|
752
|
+
return { ok: problems.length === 0, problems, warnings, summary };
|
|
753
|
+
}
|