decklight 0.8.1 → 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.
Files changed (133) hide show
  1. package/PLATFORM.md +666 -0
  2. package/README.md +224 -150
  3. package/SPEC.md +732 -886
  4. package/cli/agent-review.mjs +245 -0
  5. package/cli/agents.mjs +25 -5
  6. package/cli/associate.mjs +12 -12
  7. package/cli/audit.mjs +42 -10
  8. package/cli/banner.mjs +41 -17
  9. package/cli/bundle-audio.mjs +183 -0
  10. package/cli/bundle.mjs +383 -31
  11. package/cli/cast.mjs +7 -7
  12. package/cli/check.mjs +961 -0
  13. package/cli/clone-deck.mjs +179 -0
  14. package/cli/commands.mjs +345 -0
  15. package/cli/comments.mjs +64 -18
  16. package/cli/commit-flow.mjs +44 -19
  17. package/cli/commit-message.mjs +51 -74
  18. package/cli/commit-split.mjs +601 -0
  19. package/cli/deck-refs.mjs +59 -0
  20. package/cli/deck-routes.mjs +67 -0
  21. package/cli/deckfile.mjs +3 -5
  22. package/cli/decklight.mjs +68 -289
  23. package/cli/design-system-deps.mjs +209 -0
  24. package/cli/design-system-edit.mjs +194 -0
  25. package/cli/design-system-refs.mjs +409 -0
  26. package/cli/design-system.mjs +429 -0
  27. package/cli/doctor.mjs +115 -0
  28. package/cli/edit-git.mjs +353 -0
  29. package/cli/edit-handover.mjs +445 -0
  30. package/cli/edit-look.mjs +388 -0
  31. package/cli/edit-slides.mjs +683 -0
  32. package/cli/edit.mjs +2126 -842
  33. package/cli/enhance.mjs +483 -0
  34. package/cli/extension.mjs +10 -10
  35. package/cli/font-refs.mjs +273 -0
  36. package/cli/font.mjs +246 -0
  37. package/cli/git-tree.mjs +1 -1
  38. package/cli/git.mjs +65 -11
  39. package/cli/import.mjs +56 -10
  40. package/cli/init.mjs +400 -234
  41. package/cli/library.mjs +127 -0
  42. package/cli/loader.mjs +3 -1
  43. package/cli/marketplace.mjs +215 -21
  44. package/cli/narrate.mjs +176 -0
  45. package/cli/open-browser.mjs +63 -0
  46. package/cli/open.mjs +943 -0
  47. package/cli/pdf.mjs +41 -13
  48. package/cli/pkg.mjs +44 -2
  49. package/cli/plugin.mjs +43 -41
  50. package/cli/port-conflict.mjs +53 -23
  51. package/cli/pptx-export.mjs +57 -24
  52. package/cli/publish.mjs +8 -4
  53. package/cli/qr.mjs +1 -1
  54. package/cli/record.mjs +14 -12
  55. package/cli/remote.mjs +11 -11
  56. package/cli/restore.mjs +11 -4
  57. package/cli/review-ref.mjs +125 -0
  58. package/cli/review-remote.mjs +92 -48
  59. package/cli/review-routes.mjs +254 -0
  60. package/cli/review-store.mjs +47 -7
  61. package/cli/review-submit.mjs +47 -57
  62. package/cli/runtime-link.mjs +560 -0
  63. package/cli/serve.mjs +235 -44
  64. package/cli/setup.mjs +112 -0
  65. package/cli/sign.mjs +1 -1
  66. package/cli/skill-content.mjs +190 -33
  67. package/cli/skills.mjs +107 -24
  68. package/cli/start.mjs +110 -0
  69. package/cli/supervise.mjs +6 -6
  70. package/cli/theme-refs.mjs +515 -0
  71. package/cli/theme.mjs +181 -137
  72. package/cli/trust.mjs +83 -0
  73. package/cli/units.mjs +39 -42
  74. package/cli/update-check.mjs +1 -5
  75. package/cli/upgrade.mjs +194 -28
  76. package/cli/upstream.mjs +4 -3
  77. package/cli/util.mjs +4 -4
  78. package/cli/wizard.mjs +6 -6
  79. package/dist/build-info.json +1 -1
  80. package/dist/decklight.css +1126 -49
  81. package/dist/decklight.js +212 -115
  82. package/dist/decklight.js.map +4 -4
  83. package/docs/GLOSSARY.md +141 -0
  84. package/docs/architecture.svg +17 -17
  85. package/package.json +4 -2
  86. package/themes/README.md +9 -8
  87. package/themes/reveal-solarized.css +10 -0
  88. package/tools/args.mjs +20 -0
  89. package/tools/atomic-write.mjs +48 -0
  90. package/tools/brand-palette.mjs +717 -0
  91. package/tools/bridge.mjs +1 -1
  92. package/tools/build-groups.mjs +42 -0
  93. package/tools/color.mjs +48 -0
  94. package/tools/deck-html.mjs +396 -11
  95. package/tools/design-system-format.mjs +753 -0
  96. package/tools/elevenlabs-tts.mjs +110 -17
  97. package/tools/exec.mjs +1 -1
  98. package/tools/extension-check.mjs +31 -7
  99. package/tools/font-format.mjs +190 -0
  100. package/tools/font-stacks.mjs +24 -0
  101. package/tools/gemini-tts.mjs +3 -0
  102. package/tools/lipsync-config.mjs +103 -0
  103. package/tools/lipsync-engines.mjs +205 -2
  104. package/tools/lipsync-server.mjs +264 -41
  105. package/tools/lipsync.mjs +37 -18
  106. package/tools/local-voice.mjs +32 -42
  107. package/tools/narration-manifest.mjs +196 -0
  108. package/tools/narration-synth.mjs +416 -0
  109. package/tools/pptx.mjs +776 -19
  110. package/tools/publish-targets.mjs +1 -1
  111. package/tools/publish-voices.mjs +3 -3
  112. package/tools/recording-impact.mjs +7 -3
  113. package/tools/render-theme.mjs +33 -0
  114. package/tools/review-anchor.mjs +28 -12
  115. package/tools/semver.mjs +40 -0
  116. package/tools/sentences.mjs +180 -0
  117. package/tools/shot.mjs +14 -7
  118. package/tools/site-voices.mjs +232 -0
  119. package/tools/template-theme.mjs +1 -1
  120. package/tools/test-impact.mjs +17 -10
  121. package/tools/theme-check.mjs +30 -3
  122. package/tools/tts-cache.mjs +4 -3
  123. package/tools/tts-engines.mjs +38 -20
  124. package/tools/tts-setup.mjs +7 -7
  125. package/tools/video-options.mjs +34 -0
  126. package/tools/video.mjs +461 -48
  127. package/tools/voiceover-server.mjs +185 -104
  128. package/tools/voiceover.mjs +119 -261
  129. package/tools/zip.mjs +1 -1
  130. package/cli/dev.mjs +0 -623
  131. package/cli/present.mjs +0 -853
  132. package/cli/review.mjs +0 -361
  133. package/tools/publish-site-voices.mjs +0 -75
@@ -0,0 +1,209 @@
1
+ // Copyright 2026 Gilles Philippart
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // A design system's DEPENDENCIES (SPEC DESIGN_SYSTEMS, #642): the themes and
5
+ // fonts it recommends. A design system leaves the page, the type and the
6
+ // colours to a theme — `paints-the-page` — so `recommendedThemes` and
7
+ // `recommendedFonts` are how it names the ones it was drawn for. Adding it
8
+ // to a deck brings them along (unless `--no-recommended`); applying them —
9
+ // making the deck's theme and font the recommended ones — is offered, never
10
+ // automatic, because the page and the type stay the author's choice.
11
+ //
12
+ // Shared by `design-system add|apply` and the edit server's
13
+ // /deck/edit/design-system/mark|apply, so the command line and the UI resolve,
14
+ // gate and refuse by the same code:
15
+ //
16
+ // planRecommended(html, ds) → what each recommendation is: added, already
17
+ // there, a stack (applied, never referenced),
18
+ // or skipped — with the reason and the command
19
+ // writePlan(html, plan) → the deck with every addable one written, in
20
+ // the same string the design system is written
21
+ // to, so the whole thing is ONE edit
22
+ // lookOf(html, ds, plan) → the theme and font the deck would apply, and
23
+ // whether that differs from what it has
24
+ // applyLook(html, look) → the deck's `theme` and `font` set to them
25
+ //
26
+ // Nothing here fetches: registry, catalog cache and checkouts only.
27
+
28
+ import { configHome, loadRegistry, loadCatalog, loadLedger, MarketplaceError } from './marketplace.mjs';
29
+ import { configBlock } from './runtime-link.mjs';
30
+ import {
31
+ parseRef, resolveThemeRef, stillValid, refForDeck, setMarked, markedEntries, shippedThemes, withKey,
32
+ } from './theme-refs.mjs';
33
+ import { resolveFontRef, setFont, fontRefs, FONT_DEFAULT_KEY } from './font-refs.mjs';
34
+ import { checkFontDir } from './font.mjs';
35
+ import { STACK_LABELS } from '../tools/font-stacks.mjs';
36
+
37
+ /** The registered catalogs this machine has fetched. */
38
+ function catalogs(home) {
39
+ const out = {};
40
+ for (const name of Object.keys(loadRegistry(home).marketplaces ?? {})) {
41
+ const c = loadCatalog(name, home);
42
+ if (c?.ok) out[name] = c.manifest;
43
+ }
44
+ return out;
45
+ }
46
+
47
+ /**
48
+ * A recommendation's qualified reference, by the resolution order the spec
49
+ * sets: `name@marketplace` as written; a bare name is the design system's own
50
+ * marketplace's entry of that kind, else the one registered catalog that has
51
+ * it. Returns `{ qualified }`, or `{ why }` — ambiguous (with the qualified
52
+ * forms to write instead) or nowhere.
53
+ */
54
+ function qualify(rec, type, dsLocal, cats) {
55
+ if (rec.includes('@')) return parseRef(rec) ? { qualified: rec } : { why: `"${rec}" is not a reference (name@marketplace)` };
56
+ const has = (m) => (cats[m]?.entries ?? []).some((e) => e.name === rec && e.type === type);
57
+ if (dsLocal && has(dsLocal)) return { qualified: `${rec}@${dsLocal}` };
58
+ const everywhere = Object.keys(cats).filter(has);
59
+ if (everywhere.length === 1) return { qualified: `${rec}@${everywhere[0]}` };
60
+ if (everywhere.length > 1) {
61
+ return { why: `"${rec}" is a ${type} in ${everywhere.length} marketplaces — say which: ${everywhere.map((m) => `${rec}@${m}`).join(', ')}` };
62
+ }
63
+ return { why: `no registered marketplace offers a ${type} called "${rec}"` };
64
+ }
65
+
66
+ /**
67
+ * What every recommendation of a design system would do to this deck.
68
+ * `ds` is `{ manifest, local }` — the package's manifest and what this
69
+ * machine calls its marketplace. Each item: `{ kind: 'theme'|'font', rec,
70
+ * status: 'add'|'already'|'stack'|'skip', ref, name, version, why, cmd }` —
71
+ * an `already` one carries `same` when the deck has THIS entry, not another
72
+ * marketplace's of the same name.
73
+ */
74
+ export function planRecommended(html, ds, home = configHome()) {
75
+ const cats = catalogs(home);
76
+ const marked = markedEntries(html);
77
+ const fonts = fontRefs(html);
78
+ const items = [];
79
+ for (const rec of (ds.manifest?.recommendedThemes ?? []).filter((t) => typeof t === 'string')) {
80
+ // a theme decklight ships is marked by its bare name — nothing to resolve or check
81
+ if (!rec.includes('@') && shippedThemes().includes(rec)) {
82
+ items.push({ kind: 'theme', rec, name: rec, ref: rec, status: marked.some((m) => m.ref === rec) ? 'already' : 'add', shipped: true });
83
+ continue;
84
+ }
85
+ const q = qualify(rec, 'theme', ds.local, cats);
86
+ if (q.why) { items.push({ kind: 'theme', rec, status: 'skip', why: q.why, cmd: `decklight theme add <name@marketplace> <deck>` }); continue; }
87
+ const r = resolveThemeRef(q.qualified, home);
88
+ const cmd = `decklight theme add ${q.qualified} <deck>`;
89
+ if (!r.file) {
90
+ items.push({ kind: 'theme', rec, status: 'skip', why: r.remote ? `${q.qualified} lives at a URL — reading it is its own explicit act` : r.missing, cmd });
91
+ continue;
92
+ }
93
+ const valid = stillValid(r.file);
94
+ if (!valid.ok) { items.push({ kind: 'theme', rec, status: 'skip', why: valid.why, cmd }); continue; }
95
+ const ref = refForDeck(html, r.name, r.local, r.source ?? null);
96
+ const same = marked.some((m) => m.ref === ref);
97
+ items.push({
98
+ kind: 'theme', rec, name: r.name, ref, source: r.source ?? null, local: r.local, version: r.entry?.version ?? null,
99
+ status: same || marked.some((m) => m.name === r.name && !m.shipped) ? 'already' : 'add', same,
100
+ });
101
+ }
102
+ for (const rec of (ds.manifest?.recommendedFonts ?? []).filter((t) => typeof t === 'string')) {
103
+ // a picker stack is APPLIED, never referenced: there is nothing to carry
104
+ if (STACK_LABELS.includes(rec)) { items.push({ kind: 'font', rec, name: rec, status: 'stack' }); continue; }
105
+ const q = qualify(rec, 'font', ds.local, cats);
106
+ if (q.why) { items.push({ kind: 'font', rec, status: 'skip', why: q.why, cmd: `decklight font add <name@marketplace> <deck>` }); continue; }
107
+ const r = resolveFontRef(q.qualified, home);
108
+ const cmd = `decklight font add ${q.qualified} <deck>`;
109
+ if (!r.dir) { items.push({ kind: 'font', rec, status: 'skip', why: r.missing, cmd }); continue; }
110
+ const verdict = checkFontDir(r.dir);
111
+ if (!verdict.ok) {
112
+ const p = verdict.problems[0];
113
+ items.push({ kind: 'font', rec, status: 'skip', why: `it fails the font check — ${p.file}: ${p.msg}`, cmd });
114
+ continue;
115
+ }
116
+ const ref = refForDeck(html, r.name, r.local, r.source ?? null);
117
+ const same = fonts.some((f) => f.ref === ref);
118
+ items.push({
119
+ kind: 'font', rec, name: r.name, ref, source: r.source ?? null, local: r.local, version: r.entry?.version ?? null,
120
+ family: verdict.manifest.family, status: same || fonts.some((f) => f.name === r.name) ? 'already' : 'add', same,
121
+ });
122
+ }
123
+ // a design system that ships its own themes (SPEC BRAND_PALETTE) is worn in the first of them
124
+ const own = Array.isArray(ds.manifest?.themes) ? ds.manifest.themes.find((t) => t && typeof t.name === 'string')?.name ?? null : null;
125
+ return { items, title: ds.manifest?.title || ds.manifest?.name || '', brandTheme: own };
126
+ }
127
+
128
+ /** The deck with every addable recommendation written — the caller writes it with the design system, as one edit. */
129
+ export function writePlan(html, plan) {
130
+ let out = html;
131
+ for (const it of plan.items) {
132
+ if (it.status !== 'add') continue;
133
+ // a refusal here (a name another theme or font already holds) skips THAT
134
+ // one and keeps the rest: a dependency never blocks the design system
135
+ try {
136
+ out = it.kind === 'theme'
137
+ ? setMarked(out, it.ref, true, { source: it.source ?? null }).html
138
+ : setFont(out, it.ref, true, { source: it.source ?? null }).html;
139
+ } catch (e) {
140
+ if (!(e instanceof MarketplaceError)) throw e;
141
+ Object.assign(it, { status: 'skip', why: e.message, cmd: `decklight ${it.kind} add ${it.ref} <deck>` });
142
+ }
143
+ }
144
+ return out;
145
+ }
146
+
147
+ /**
148
+ * What `installed.json` remembers for each recommendation, as a separate add
149
+ * would: every one added, AND every one the deck already has — re-running
150
+ * `theme add` / `font add` on a marked one records the catalog's version now
151
+ * (#616), so bringing it again does the same, or `marketplace list` keeps
152
+ * calling it out of date (#653). Never a shipped theme or a stack, and never
153
+ * one the deck has from another marketplace's entry of that name. An
154
+ * `already` item whose record this changes gets `recorded`, for its line.
155
+ */
156
+ export function plannedInstalls(plan, home = configHome()) {
157
+ const reg = loadRegistry(home).marketplaces ?? {};
158
+ const had = loadLedger(home).installs;
159
+ const taken = plan.items.filter((it) => !it.shipped && (it.status === 'add' || (it.status === 'already' && it.same)));
160
+ for (const it of taken) {
161
+ if (it.status === 'already' && it.version && had[`${it.kind}:${it.name}@${it.local}`]?.version !== it.version) it.recorded = it.version;
162
+ }
163
+ return taken.map((it) => ({ type: it.kind, name: it.name, marketplace: it.local, version: it.version ?? null, commit: reg[it.local]?.commit ?? null }));
164
+ }
165
+
166
+ /**
167
+ * The look the design system was drawn for, as this deck could wear it: the
168
+ * first recommended theme that is (or is being) marked, and the first
169
+ * recommended font that is a stack or a (being) referenced package — and
170
+ * whether either differs from the deck's `theme` / `font` now.
171
+ */
172
+ export function lookOf(html, plan) {
173
+ const config = configBlock(html)?.config ?? {};
174
+ const usable = (it) => it.status === 'add' || it.status === 'already' || it.status === 'stack';
175
+ // its own theme first: it comes with the design system, nothing to mark
176
+ const theme = plan.brandTheme ?? plan.items.find((it) => it.kind === 'theme' && usable(it))?.name ?? null;
177
+ const font = plan.items.find((it) => it.kind === 'font' && usable(it))?.name ?? null;
178
+ const themeChanges = !!theme && config.theme !== theme;
179
+ const fontChanges = !!font && config[FONT_DEFAULT_KEY] !== font;
180
+ return { title: plan.title, theme, font, themeChanges, fontChanges, differs: themeChanges || fontChanges };
181
+ }
182
+
183
+ /** The deck's `theme` and `font` set to the look — only what changes. */
184
+ export function applyLook(html, look) {
185
+ const block = configBlock(html);
186
+ if (!block?.config) return html;
187
+ let inner = block.inner;
188
+ if (look.themeChanges) inner = withKey(inner, 'theme', look.theme);
189
+ if (look.fontChanges) inner = withKey(inner, FONT_DEFAULT_KEY, look.font);
190
+ return html.slice(0, block.innerStart) + inner + html.slice(block.innerEnd);
191
+ }
192
+
193
+ /** The question, naming only what would change: "theme verdant-light, font Fira Sans". */
194
+ export function lookPhrase(look, plan) {
195
+ const fontLabel = plan.items.find((it) => it.kind === 'font' && it.name === look.font)?.family ?? look.font;
196
+ return [look.themeChanges ? `theme ${look.theme}` : null, look.fontChanges ? `font ${fontLabel}` : null].filter(Boolean).join(', ');
197
+ }
198
+
199
+ /** One line per recommendation, for the command line's summary. */
200
+ export function planLines(plan) {
201
+ return plan.items.map((it) => {
202
+ const what = it.kind === 'theme' ? 'theme' : 'font';
203
+ const v = it.version ? ` ${it.version}` : '';
204
+ if (it.status === 'add') return ` + ${what} ${it.ref}${v}${it.shipped ? ' (shipped)' : ''}`;
205
+ if (it.status === 'already') return ` = ${what} ${it.ref ?? it.rec} — already there${it.recorded ? `, recorded ${it.recorded}` : ''}`;
206
+ if (it.status === 'stack') return ` · font ${it.rec} — a picker stack: applied, nothing to carry`;
207
+ return ` ⚠ ${what} ${it.rec} — ${it.why}\n ${it.cmd}`;
208
+ });
209
+ }
@@ -0,0 +1,194 @@
1
+ // Copyright 2026 Gilles Philippart
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Editing a slide that uses a slide template (SPEC DESIGN_SYSTEMS).
5
+ //
6
+ // Two halves, both pure string surgery on the deck's FILE — the engine's
7
+ // expansion lives only in the DOM, so nothing here ever writes a template's
8
+ // structure into a slide:
9
+ //
10
+ // - the guard: an element edit on such a slide may change an element's
11
+ // content, never drop the `data-slot` that puts it in its place;
12
+ // - the template picker's writes (POST /deck/edit/slide/template): put a
13
+ // slide INTO a template (convert), move it to another (switch), take it out
14
+ // (remove), or insert a new slide built from one — each one edit, so one
15
+ // undo — and a report of what went where, in words.
16
+ //
17
+ // Template definitions (`{ id, slots: [{ name, hint, required, default }] }`)
18
+ // come from the deck's design systems as the server resolves them, never
19
+ // from the page; they are handed in, so every transform is testable alone.
20
+
21
+ import { locateSlide, sectionChildRanges, splitOpenTag, readAttrs, writeAttrs, insertSectionsAfter } from '../tools/deck-html.mjs';
22
+
23
+ /** Elements a slide carries rather than shows — never assigned to a slot. */
24
+ const CARRIED = new Set(['aside', 'script', 'style']);
25
+
26
+ /** The `data-template` slide `n` names in the file, or ''. */
27
+ export function slideTemplateOf(html, slide) {
28
+ const { parts, idx } = locateSlide(html, slide);
29
+ const { attrs } = splitOpenTag(parts[idx]);
30
+ return readAttrs(attrs)['data-template'] ?? '';
31
+ }
32
+
33
+ /** An element's open-tag attributes, and the pieces to put it back together. */
34
+ function openTag(el) {
35
+ const m = /^<([A-Za-z][\w:-]*)/.exec(el);
36
+ const tag = m?.[1] ?? '';
37
+ const { attrs, close, rest } = splitOpenTag(el.slice(m ? m[0].length : 0));
38
+ return { tag, attrs: readAttrs(attrs), close, rest };
39
+ }
40
+ const withAttrs = (el, attrs) => {
41
+ const t = openTag(el);
42
+ return `<${t.tag}${writeAttrs(attrs)}${t.close}${t.rest}`;
43
+ };
44
+
45
+ /**
46
+ * Why an element write on a design-system slide is refused, or null. The
47
+ * slot an element fills is its address in the template; an edit that drops it
48
+ * would silently move the content to the unslotted box.
49
+ */
50
+ export function slotWriteProblem(html, slide, index, outerHtml) {
51
+ if (!slideTemplateOf(html, slide)) return null;
52
+ const { parts, idx } = locateSlide(html, slide);
53
+ const r = sectionChildRanges(parts[idx])[index];
54
+ if (!r) return null;
55
+ const before = openTag(parts[idx].slice(r.start, r.end)).attrs['data-slot'];
56
+ if (before === undefined) return null;
57
+ const after = openTag(String(outerHtml ?? '').trim()).attrs['data-slot'];
58
+ return after === undefined
59
+ ? `this element fills the "${before}" slot of the slide's template — keep data-slot="${before}" on it (an edit may change what it says, not where it goes)`
60
+ : null;
61
+ }
62
+
63
+ /** A hint (`"h1,h2"`) as the tags it names. */
64
+ const hintTags = (hint) => String(hint ?? '').toLowerCase().split(/[\s,]+/).filter(Boolean);
65
+
66
+ /**
67
+ * Put slide `n` into `template` (`{ ref: "acme/divider", id, slots }`), take it
68
+ * out (`template` null), or move it from one template to another. Returns
69
+ * `{ html, mode, moved, lacking }`:
70
+ * - convert (from a plain slide): children that already carry a data-slot
71
+ * the template has keep it; the rest are given one by the slots' hints, in
72
+ * the template's slot order (the first h2 → a slot hinted h1,h2 …);
73
+ * leftovers carry none and land in the template's default slot (or the
74
+ * unslotted box) when the engine expands it;
75
+ * - switch (from another slide template): only the attribute moves;
76
+ * content stays by slot name, and slots the new template lacks are listed;
77
+ * - remove: the attribute goes; data-slot attributes stay — harmless on a
78
+ * plain slide, and what a later convert would read.
79
+ */
80
+ export function applySlideTemplate(html, slide, template) {
81
+ const { parts, idx } = locateSlide(html, slide);
82
+ const seg = parts[idx];
83
+ const head = splitOpenTag(seg);
84
+ const sectionAttrs = readAttrs(head.attrs);
85
+ const current = sectionAttrs['data-template'] ?? '';
86
+ const mode = !template ? 'remove' : current ? 'switch' : 'convert';
87
+ const moved = [];
88
+ const lacking = [];
89
+ if (template) sectionAttrs['data-template'] = template.ref;
90
+ else delete sectionAttrs['data-template'];
91
+ let body = head.rest;
92
+ if (mode === 'convert' || mode === 'switch') {
93
+ const names = new Set(template.slots.map((s) => s.name));
94
+ const ranges = sectionChildRanges(seg).map((r) => ({ ...r, start: r.start - (seg.length - head.rest.length), end: r.end - (seg.length - head.rest.length) }));
95
+ const kids = ranges.map((r) => ({ r, tag: r.tag.toLowerCase(), el: body.slice(r.start, r.end) }))
96
+ .filter((k) => !CARRIED.has(k.tag) && !/\bclass\s*=\s*["'][^"']*\bslide-bg\b/.test(k.el));
97
+ const assigned = new Map(); // child → slot
98
+ for (const k of kids) {
99
+ const slot = openTag(k.el).attrs['data-slot'];
100
+ if (slot === undefined) continue;
101
+ if (names.has(slot)) { assigned.set(k, slot); continue; }
102
+ lacking.push(slot);
103
+ }
104
+ if (mode === 'convert') {
105
+ const taken = new Set(assigned.values());
106
+ for (const s of template.slots) {
107
+ if (taken.has(s.name) || s.default) continue;
108
+ const tags = hintTags(s.hint);
109
+ if (!tags.length) continue;
110
+ const k = kids.find((x) => !assigned.has(x) && openTag(x.el).attrs['data-slot'] === undefined && tags.includes(x.tag));
111
+ if (!k) continue;
112
+ assigned.set(k, s.name);
113
+ taken.add(s.name);
114
+ moved.push({ tag: k.tag, slot: s.name, by: `hint ${s.hint}` });
115
+ }
116
+ const fallback = template.slots.find((s) => s.default)?.name ?? null;
117
+ for (const k of kids) {
118
+ if (assigned.has(k) || openTag(k.el).attrs['data-slot'] !== undefined) continue;
119
+ moved.push({ tag: k.tag, slot: fallback, by: fallback ? 'the default slot' : 'the unslotted box' });
120
+ }
121
+ // write the new data-slot attributes back to front, so earlier offsets hold
122
+ const writes = [...assigned].filter(([k, slot]) => openTag(k.el).attrs['data-slot'] !== slot)
123
+ .sort((a, b) => b[0].r.start - a[0].r.start);
124
+ for (const [k, slot] of writes) {
125
+ const attrs = { ...openTag(k.el).attrs, 'data-slot': slot };
126
+ body = body.slice(0, k.r.start) + withAttrs(k.el, attrs) + body.slice(k.r.end);
127
+ }
128
+ }
129
+ }
130
+ // `seg` is what follows the `<section` token: its attributes, then the body
131
+ parts[idx] = writeAttrs(sectionAttrs) + head.close + body;
132
+ return { html: parts.join(''), mode, moved, lacking: [...new Set(lacking)] };
133
+ }
134
+
135
+ /** A tag a hint suggests for a new slide's slot, or `p`. */
136
+ const tagFor = (hint) => hintTags(hint).find((t) => /^(h[1-6]|p|ul|ol|blockquote|figure|div)$/.test(t)) ?? 'p';
137
+ const titleCase = (name) => name.replace(/-/g, ' ').replace(/^./, (c) => c.toUpperCase());
138
+
139
+ /**
140
+ * A new slide's elements in `template`, one per REQUIRED slot, by its hint,
141
+ * saying the slot's name: what *Use slide template… → insert* writes. `fill`
142
+ * gives a slot its own markup instead (already escaped), in the tag `tag`
143
+ * when one is named — how `init` puts the deck's title in a heading slot.
144
+ */
145
+ export function slotElements(template, fill = {}) {
146
+ return template.slots.filter((s) => s.required || fill[s.name]).map((s) => {
147
+ const tag = fill[s.name]?.tag ?? tagFor(s.hint);
148
+ return `<${tag} data-slot="${s.name}">${fill[s.name]?.html ?? titleCase(s.name)}</${tag}>`;
149
+ });
150
+ }
151
+
152
+ /** The heading tag a slot's hint names (`h1,h2` → `h1`), or null. */
153
+ export const headingOf = (slot) => (slot.default ? null : hintTags(slot.hint).find((t) => /^h[1-6]$/.test(t)) ?? null);
154
+
155
+ /**
156
+ * The template a new deck's title slide is built on: the FIRST template with
157
+ * a slot hinted for a heading, its title in that slot and every other
158
+ * required slot filled as insert fills one. `{ ref, template, slot, lines }`,
159
+ * or null when no template has a heading-hinted slot.
160
+ */
161
+ export function titleSlideTemplate(dsName, templates, titleHtml) {
162
+ for (const template of templates ?? []) {
163
+ const slot = (template.slots ?? []).find((s) => headingOf(s));
164
+ if (!slot) continue;
165
+ return {
166
+ ref: `${dsName}/${template.id}`, template, slot: slot.name,
167
+ lines: slotElements(template, { [slot.name]: { tag: headingOf(slot), html: titleHtml } }),
168
+ };
169
+ }
170
+ return null;
171
+ }
172
+
173
+ /**
174
+ * A deck with a new slide after slide `after`, in `template`: one element per
175
+ * REQUIRED slot, by its hint, saying the slot's name — every other slot is
176
+ * left to the template's own default content, which the engine shows until
177
+ * the author fills it.
178
+ */
179
+ export function insertSlideTemplateSlide(html, after, template) {
180
+ const lines = slotElements(template).map((l) => ` ${l}`);
181
+ const section = `<section data-template="${template.ref}">\n${lines.join('\n')}${lines.length ? '\n' : ''} <aside class="notes"></aside>\n</section>`;
182
+ return insertSectionsAfter(String(html ?? ''), after, [section]);
183
+ }
184
+
185
+ /** What happened, said for a toast: "title ← h2 (hint h1,h2) · p → the default slot". */
186
+ export function describeTemplateChange({ mode, moved, lacking }, ref) {
187
+ const out = [];
188
+ if (mode === 'remove') out.push('back to a plain slide — data-slot attributes stay');
189
+ if (mode === 'switch') out.push(`now ${ref} — content stays by slot name${lacking.length ? '' : ', nothing left over'}`);
190
+ if (mode === 'convert') out.push(`now ${ref}`);
191
+ for (const m of moved) out.push(m.slot ? `${m.tag} → ${m.slot}${m.by.startsWith('hint') ? ` (${m.by})` : ' (the default slot)'}` : `${m.tag} → ${m.by}`);
192
+ if (lacking.length) out.push(`${ref} has no ${lacking.map((s) => `"${s}"`).join(', ')} slot — that content shows in the unslotted box`);
193
+ return out.join(' · ');
194
+ }