@netnodeag/kraftwerk 0.82.0 → 0.84.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 (77) hide show
  1. package/README.md +212 -11
  2. package/dist/cli/init.js +1 -0
  3. package/dist/cli/kraftwerk.js +3 -0
  4. package/dist/cli/presentations.d.ts +2 -0
  5. package/dist/cli/presentations.js +261 -0
  6. package/dist/client/routes.d.ts +18 -0
  7. package/dist/client/routes.js +18 -0
  8. package/dist/config.d.ts +16 -0
  9. package/dist/config.js +10 -2
  10. package/dist/core/boards.d.ts +14 -1
  11. package/dist/core/boards.js +34 -1
  12. package/dist/core/chat/context.js +11 -2
  13. package/dist/core/git.js +3 -1
  14. package/dist/core/presentations/browser.d.ts +43 -0
  15. package/dist/core/presentations/browser.js +53 -0
  16. package/dist/core/presentations/builtin.d.ts +3 -0
  17. package/dist/core/presentations/builtin.js +142 -0
  18. package/dist/core/presentations/chart.d.ts +34 -0
  19. package/dist/core/presentations/chart.js +159 -0
  20. package/dist/core/presentations/deck.d.ts +63 -0
  21. package/dist/core/presentations/deck.js +198 -0
  22. package/dist/core/presentations/export.d.ts +21 -0
  23. package/dist/core/presentations/export.js +75 -0
  24. package/dist/core/presentations/html.d.ts +135 -0
  25. package/dist/core/presentations/html.js +389 -0
  26. package/dist/core/presentations/index.d.ts +252 -0
  27. package/dist/core/presentations/index.js +753 -0
  28. package/dist/core/presentations/layout.d.ts +47 -0
  29. package/dist/core/presentations/layout.js +147 -0
  30. package/dist/core/presentations/markdown.d.ts +49 -0
  31. package/dist/core/presentations/markdown.js +158 -0
  32. package/dist/core/presentations/merge.d.ts +26 -0
  33. package/dist/core/presentations/merge.js +28 -0
  34. package/dist/core/presentations/presence.d.ts +31 -0
  35. package/dist/core/presentations/presence.js +131 -0
  36. package/dist/core/presentations/render.d.ts +33 -0
  37. package/dist/core/presentations/render.js +318 -0
  38. package/dist/core/presentations/slots.d.ts +33 -0
  39. package/dist/core/presentations/slots.js +114 -0
  40. package/dist/core/presentations/template.d.ts +253 -0
  41. package/dist/core/presentations/template.js +141 -0
  42. package/dist/core/presentations/versions.d.ts +30 -0
  43. package/dist/core/presentations/versions.js +75 -0
  44. package/dist/core/refs.d.ts +1 -1
  45. package/dist/core/refs.js +9 -1
  46. package/dist/core/search.d.ts +10 -0
  47. package/dist/core/search.js +6 -0
  48. package/dist/core/settings.d.ts +2 -0
  49. package/dist/core/settings.js +12 -1
  50. package/dist/core/trash.d.ts +1 -1
  51. package/dist/server/api/index.d.ts +62 -1
  52. package/dist/server/api/index.js +2 -0
  53. package/dist/server/api/presentations.d.ts +66 -0
  54. package/dist/server/api/presentations.js +185 -0
  55. package/dist/server/api/socket.js +24 -1
  56. package/dist/server/api/workspace.d.ts +5 -0
  57. package/dist/server/api/workspace.js +2 -0
  58. package/package.json +6 -2
  59. package/schema/kraftwerk.schema.json +18 -0
  60. package/web/dist/assets/{dist-BBFUIftm.js → dist-3Yr8mX0X.js} +1 -1
  61. package/web/dist/assets/{dist-DtUB2Uu2.js → dist-BVzRayNB.js} +1 -1
  62. package/web/dist/assets/{dist-BuwdjX6z.js → dist-BdYm_4II.js} +1 -1
  63. package/web/dist/assets/{dist-C16bXf8c.js → dist-Bf3GuzEk.js} +1 -1
  64. package/web/dist/assets/{dist-Bie1ebP9.js → dist-DGymUpLv.js} +1 -1
  65. package/web/dist/assets/{dist-CH73jsgO.js → dist-DIvdTMOT.js} +1 -1
  66. package/web/dist/assets/{dist-DvkrCSGL.js → dist-DchG_zAX.js} +1 -1
  67. package/web/dist/assets/{dist-C0ve4Tka.js → dist-Dt9RqYvy.js} +1 -1
  68. package/web/dist/assets/dist-m1b2E3aJ.js +1 -0
  69. package/web/dist/assets/{dist-C_rJjRj7.js → dist-mxFa618U.js} +1 -1
  70. package/web/dist/assets/{dist-CDUZdAAS.js → dist-xXM6FJLm.js} +1 -1
  71. package/web/dist/assets/{editor-3z92i0tV.js → editor-DYDCHRgT.js} +3 -3
  72. package/web/dist/assets/index-CvvZzwHR.js +473 -0
  73. package/web/dist/assets/index-JhhhmD7T.css +2 -0
  74. package/web/dist/index.html +2 -2
  75. package/web/dist/assets/dist-DYICNp09.js +0 -1
  76. package/web/dist/assets/index-DRhNELK_.js +0 -232
  77. package/web/dist/assets/index-unGEux3Y.css +0 -2
@@ -0,0 +1,318 @@
1
+ import * as ok from "@office-kit/pptx";
2
+ import { parse } from "yaml";
3
+ import { chartPalette } from "./chart.js";
4
+ import { DEFAULT_SLOT, slotsOf, splitDefault } from "./slots.js";
5
+ import { inlineRuns, parseMarkdown, plain } from "./markdown.js";
6
+ import { CANVAS, PPI, defaultSlotOf, layoutFor, slideSlots, resolveColor, resolveFont } from "./template.js";
7
+ /** A slot whose markdown carries raw HTML: PowerPoint gets it as a picture when there is one. */
8
+ export const hasHtml = (md) => /<(div|span|section|svg|table|img|figure|ol|ul|p|h[1-6]|br|strong|em|a)\b[^>]*>/i.test(md);
9
+ /** resolveColor gives #RRGGBB, which is a Color; the type is branded, so one cast here. */
10
+ const col = (hex) => hex;
11
+ const CHART_KINDS = ["column", "bar", "line", "pie", "doughnut", "area"];
12
+ export async function renderDeck(deck, template, assets, options = {}) {
13
+ const size = deck.size ?? template.size;
14
+ const pres = ok.createPresentation({ size });
15
+ // office-kit's 16:9 is 13.333in wide; kraftwerk lays out on a 10in canvas (the HTML stage), so the PPTX gets that size.
16
+ ok.setSlideSize(pres, { width: ok.inches(CANVAS[size].w), height: ok.inches(CANVAS[size].h), type: size === "16:9" ? "screen16x9" : "screen4x3" });
17
+ ok.setPresentationFonts(pres, { majorLatin: template.fonts.heading, minorLatin: template.fonts.body });
18
+ const warnings = [];
19
+ const slides = deck.slides.length ? deck.slides : [{ extra: {}, content: "" }];
20
+ for (const [i, slide] of slides.entries()) {
21
+ const n = i + 1;
22
+ const warn = (message, slot, level = "warning") => warnings.push({ slide: n, ...(slot ? { slot } : {}), level, message });
23
+ const s = ok.addBlankSlide(pres);
24
+ // An unknown layout falls back to the template's default, with a warning naming both.
25
+ const found = layoutFor(template, slide.layout) ?? layoutFor(template, undefined);
26
+ if (!found) {
27
+ warn(`template "${template.slug}" has no layouts`, undefined, "error");
28
+ continue;
29
+ }
30
+ if (slide.layout && found.name !== slide.layout)
31
+ warn(`unknown layout "${slide.layout}", using "${found.name}"`, undefined, "error");
32
+ // The slide's own slot changes and additions (`slots:` in its head) on top of the layout's; the default slot stays the layout's.
33
+ const own = slideSlots(found.layout, slide.extra.slots);
34
+ for (const problem of own.problems)
35
+ warn(problem, undefined, "error");
36
+ const dflt = defaultSlotOf(found.layout);
37
+ const layout = { ...found.layout, slots: own.slots, ...(dflt ? { default: dflt } : {}) };
38
+ if (slide.extra?.hidden === true)
39
+ ok.setSlideHidden(s, true);
40
+ // A transition set on the slide carries over: fade, slide (PowerPoint's push), zoom; none is a cut.
41
+ if (slide.transition === "fade" || slide.transition === "slide" || slide.transition === "zoom")
42
+ ok.setSlideTransition(s, { effect: slide.transition === "slide" ? "push" : slide.transition, speed: "med" });
43
+ const bgPicture = slide.background && /\.(png|jpe?g|gif)$/i.test(slide.background.trim()) ? slide.background.trim() : null;
44
+ const background = resolveColor(template, bgPicture ? (layout.background ?? "background") : (slide.background ?? layout.background ?? "background"));
45
+ if (slide.background && !bgPicture && !/\.(webp|svg|avif)$/i.test(slide.background) && !resolveColor(template, slide.background))
46
+ warn(`unknown background color "${slide.background}"`);
47
+ if (background)
48
+ ok.setSlideBackground(s, col(background));
49
+ if (bgPicture) {
50
+ const bytes = await assets.read(bgPicture);
51
+ if (bytes)
52
+ ok.setSlideBackgroundImage(s, bytes);
53
+ else
54
+ warn(`background picture not found: ${bgPicture}`);
55
+ }
56
+ drawShapes(s, template, layout, size);
57
+ const content = contentOf(slide, layout, warn);
58
+ for (const [name, slot] of Object.entries(layout.slots)) {
59
+ const md = content[name];
60
+ if (md === undefined || md.trim() === "") {
61
+ if (slot.required)
62
+ warn(`slot "${name}" is empty (${slot.kind}${slot.placeholder ? `, e.g. ${slot.placeholder.split("\n")[0]}` : ""})`, name);
63
+ continue;
64
+ }
65
+ const picture = options.images?.get(`${n}:${name}`);
66
+ if (picture && (slot.kind === "html" || slot.kind === "embed" || hasHtml(md))) {
67
+ const b = clamp(slot, size);
68
+ ok.addSlideImage(s, picture, { x: ok.inches(b.x), y: ok.inches(b.y), w: ok.inches(b.w), h: ok.inches(b.h), fit: "contain", name });
69
+ continue;
70
+ }
71
+ if (slot.kind === "html" || slot.kind === "embed") {
72
+ const b = clamp(slot, size);
73
+ const ph = ok.addSlideShape(s, { preset: "rect", name, x: ok.inches(b.x), y: ok.inches(b.y), w: ok.inches(b.w), h: ok.inches(b.h), text: slot.kind === "embed" ? "Live content: open the deck as HTML" : "HTML element: export with a browser to see it here", textAnchor: "ctr" });
74
+ ok.setShapeFill(ph, col(resolveColor(template, "surface") ?? "#F3F4F6"));
75
+ ok.setShapeTextFormat(ph, { size: 11, color: col(resolveColor(template, "muted") ?? "#6B6B70") });
76
+ continue;
77
+ }
78
+ try {
79
+ await drawSlot(s, template, name, slot, md, assets, (m, level) => warn(m, name, level));
80
+ // What the slide's own box settings carry over: rotation and a border.
81
+ for (const sh of ok.getSlideShapes(s)) {
82
+ if (ok.getShapeName(sh) !== name)
83
+ continue;
84
+ if (slot.rotate)
85
+ ok.setShapeRotation(sh, slot.rotate);
86
+ if (slot.border)
87
+ ok.setShapeStroke(sh, { color: col(resolveColor(template, slot.borderColor ?? "text") ?? "#000000"), widthEmu: ok.pt(slot.border) });
88
+ }
89
+ }
90
+ catch (err) {
91
+ warn(`could not draw: ${err.message}`, name, "error");
92
+ }
93
+ }
94
+ if (deck.numbers !== false && slide.extra.number !== false && !NO_NUMBER_LAYOUTS.has(found.name))
95
+ drawNumber(s, template, size, n);
96
+ if (slide.notes?.trim())
97
+ ok.setSlideNotes(s, slide.notes.trim());
98
+ }
99
+ return { pres, warnings };
100
+ }
101
+ /** The slide's content by slot name: named `::slot::` blocks, plus the unmarked content split into title and the default slot. */
102
+ function contentOf(slide, layout, warn) {
103
+ const slots = slotsOf(slide.content);
104
+ const out = {};
105
+ for (const [name, text] of Object.entries(slots)) {
106
+ if (name === DEFAULT_SLOT)
107
+ continue;
108
+ if (!layout.slots[name])
109
+ warn(`layout has no slot "${name}" (it has: ${Object.keys(layout.slots).join(", ")})`, name, "error");
110
+ else
111
+ out[name] = text;
112
+ }
113
+ const dflt = slots[DEFAULT_SLOT];
114
+ if (dflt?.trim()) {
115
+ const { heading, rest } = splitDefault(dflt);
116
+ if (heading && layout.slots.title && out.title === undefined)
117
+ out.title = heading;
118
+ const leftover = heading && layout.slots.title && out.title === heading ? rest : dflt;
119
+ if (leftover.trim()) {
120
+ const target = defaultSlotOf(layout);
121
+ if (target && out[target] === undefined)
122
+ out[target] = leftover;
123
+ else if (target)
124
+ out[target] = `${out[target]}\n\n${leftover}`;
125
+ else
126
+ warn(`unmarked content has no slot to go to (mark it ::name:: with one of: ${Object.keys(layout.slots).join(", ")})`, undefined, "error");
127
+ }
128
+ }
129
+ return out;
130
+ }
131
+ /** Where the built-in theme leaves the number off (theme.css: `:not(.layout-title):not(.layout-section)`). */
132
+ const NO_NUMBER_LAYOUTS = new Set(["title", "section"]);
133
+ /** The slide number bottom right, where the theme draws it (72px from the right, 30px up, 18px), as a field PowerPoint and Keynote keep counting. */
134
+ function drawNumber(s, template, size, n) {
135
+ const c = CANVAS[size];
136
+ const w = 1;
137
+ const h = 0.3;
138
+ const box = ok.addSlideTextBox(s, { name: "Slide number", x: ok.inches(c.w - 72 / PPI - w), y: ok.inches(c.h - 30 / PPI - h), w: ok.inches(w), h: ok.inches(h), text: String(n).padStart(2, "0") });
139
+ ok.setShapeTextFormat(box, { size: Math.round(((18 / PPI) * 72) * 10) / 10, color: col(resolveColor(template, "muted") ?? "#6F6D67"), font: resolveFont(template, undefined) });
140
+ ok.setParagraphAlignment(box, 0, "right");
141
+ ok.setShapeTextAnchor(box, "bottom");
142
+ ok.setShapeTextField(box, "slidenum", { text: String(n).padStart(2, "0") });
143
+ }
144
+ function drawShapes(s, template, layout, size) {
145
+ for (const shape of layout.shapes) {
146
+ const box = clamp(shape, size, 1 / PPI);
147
+ if (shape.kind === "line") {
148
+ ok.addSlideLine(s, { name: "Decoration", from: { x: ok.inches(box.x), y: ok.inches(box.y) }, to: { x: ok.inches(box.x + box.w), y: ok.inches(box.y + box.h) }, color: col(resolveColor(template, shape.color ?? shape.fill ?? "line") ?? "#D1D5DB"), widthEmu: ok.pt(shape.width ?? 1) });
149
+ continue;
150
+ }
151
+ const r = ok.addSlideShape(s, { preset: "rect", name: "Decoration", x: ok.inches(box.x), y: ok.inches(box.y), w: ok.inches(box.w), h: ok.inches(box.h) });
152
+ const fill = resolveColor(template, shape.fill ?? "accent");
153
+ if (fill)
154
+ ok.setShapeFill(r, col(fill));
155
+ if (shape.color)
156
+ ok.setShapeStroke(r, { color: col(resolveColor(template, shape.color) ?? "#000000"), widthEmu: ok.pt(shape.width ?? 1) });
157
+ else
158
+ ok.setShapeNoStroke(r);
159
+ }
160
+ }
161
+ async function drawSlot(s, template, name, slot, md, assets, warn) {
162
+ const box = clamp(slot, template.size);
163
+ const blocks = parseMarkdown(md);
164
+ const geometry = { x: ok.inches(box.x), y: ok.inches(box.y), w: ok.inches(box.w), h: ok.inches(box.h), name };
165
+ switch (slot.kind) {
166
+ case "image": {
167
+ const img = blocks.find((b) => b.kind === "image");
168
+ const src = img?.kind === "image" ? img.src : blocks.length === 1 && blocks[0].kind === "paragraph" ? plain(blocks[0].runs).trim() : "";
169
+ if (!src)
170
+ return warn(`expects a picture: ![caption](assets/file.png)`, "error");
171
+ const bytes = await assets.read(src);
172
+ if (!bytes) {
173
+ const ph = ok.addSlideShape(s, { preset: "rect", ...geometry, text: `missing image: ${src}`, textAnchor: "ctr" });
174
+ ok.setShapeFill(ph, col(resolveColor(template, "surface") ?? "#F3F4F6"));
175
+ ok.setShapeTextFormat(ph, { size: 12, color: col(resolveColor(template, "muted") ?? "#6B6B70") });
176
+ return warn(`image not found: ${src}`, "error");
177
+ }
178
+ ok.addSlideImage(s, bytes, { ...geometry, fit: "contain" });
179
+ return;
180
+ }
181
+ case "chart": {
182
+ const chart = blocks.find((b) => b.kind === "chart");
183
+ if (chart?.kind !== "chart")
184
+ return warn("expects a ```chart block (kind, categories, series)", "error");
185
+ const spec = chartSpec(chart.source, chartPalette(resolveColor(template, "accent") ?? undefined));
186
+ ok.addSlideChart(s, { ...geometry, spec });
187
+ return;
188
+ }
189
+ case "table": {
190
+ const table = blocks.find((b) => b.kind === "table");
191
+ if (table?.kind !== "table")
192
+ return warn("expects a markdown table", "error");
193
+ drawTable(s, template, slot, geometry, table.rows);
194
+ return;
195
+ }
196
+ case "text":
197
+ case "markdown": {
198
+ // One picture, table or chart alone in a markdown slot is drawn as such; mixed with text it is dropped with a warning.
199
+ if (slot.kind === "markdown" && blocks.length === 1 && (blocks[0].kind === "image" || blocks[0].kind === "table" || blocks[0].kind === "chart")) {
200
+ return drawSlot(s, template, name, { ...slot, kind: blocks[0].kind }, md, assets, warn);
201
+ }
202
+ const dropped = blocks.filter((b) => b.kind === "image" || b.kind === "table" || b.kind === "chart");
203
+ for (const d of dropped)
204
+ warn(`${d.kind} dropped: a ${slot.kind} slot shows text only (put it in its own slot or slide)`);
205
+ const paragraphs = slot.kind === "text" ? [{ runs: inlineRuns(md), level: 0, bullet: "none" }] : paragraphsOf(blocks);
206
+ drawText(s, template, slot, geometry, paragraphs);
207
+ return;
208
+ }
209
+ }
210
+ }
211
+ function paragraphsOf(blocks) {
212
+ const out = [];
213
+ for (const b of blocks) {
214
+ if (b.kind === "heading")
215
+ out.push({ runs: b.runs, level: 0, bullet: "none", heading: true });
216
+ else if (b.kind === "paragraph")
217
+ out.push({ runs: b.runs, level: 0, bullet: "none" });
218
+ else if (b.kind === "list")
219
+ for (const item of b.items)
220
+ out.push({ runs: item.runs, level: item.level, bullet: item.ordered ? "number" : "bullet" });
221
+ else if (b.kind === "code")
222
+ for (const line of b.text.split("\n"))
223
+ out.push({ runs: [{ text: line || " ", code: true }], level: 0, bullet: "none", code: true });
224
+ }
225
+ return out;
226
+ }
227
+ function drawText(s, template, slot, geometry, paragraphs) {
228
+ const shape = ok.addSlideTextBox(s, { ...geometry, text: "" });
229
+ const size = slot.size ?? 18;
230
+ const color = col(resolveColor(template, slot.color ?? "text") ?? "#1B1B1F");
231
+ const font = resolveFont(template, slot.font);
232
+ const accent = col(resolveColor(template, "accent") ?? "#1D4ED8");
233
+ const specs = paragraphs.map((p) => ({
234
+ ...(slot.align ? { align: slot.align } : {}),
235
+ runs: (p.runs.length ? p.runs : [{ text: " " }]).map((r) => ({
236
+ text: r.text,
237
+ format: {
238
+ size: p.heading ? size * 1.15 : p.code ? Math.max(8, size * 0.8) : size,
239
+ bold: !!(slot.bold || r.bold || p.heading),
240
+ italic: !!(slot.italic || r.italic),
241
+ // Inline code keeps the body font: the preview renderer loses a wrapped line that mixes font families
242
+ // (office-kit pptx-preview 0.21), so code is told apart by colour only; a code block is one family throughout.
243
+ color: r.href || r.code ? accent : color,
244
+ font: p.code ? "Courier New" : font,
245
+ ...(r.href ? { underline: true } : {}),
246
+ },
247
+ })),
248
+ endFormat: { size },
249
+ }));
250
+ ok.setShapeParagraphs(shape, specs);
251
+ paragraphs.forEach((p, i) => {
252
+ if (p.bullet !== "none") {
253
+ ok.setParagraphBullet(shape, i, p.bullet);
254
+ if (p.level > 0)
255
+ ok.setParagraphLevel(shape, i, p.level);
256
+ }
257
+ if (i > 0 && p.bullet === "none")
258
+ ok.setParagraphSpacing(shape, i, { beforePts: size * 0.5 });
259
+ });
260
+ ok.setShapeTextAnchor(shape, slot.valign === "middle" ? "center" : slot.valign === "bottom" ? "bottom" : "top");
261
+ // No autofit: the preview renderer shrinks and shifts autofit text even when it fits, and the editor types at the size the slot says.
262
+ if (slot.fill) {
263
+ const fill = resolveColor(template, slot.fill);
264
+ if (fill)
265
+ ok.setShapeFill(shape, col(fill));
266
+ }
267
+ }
268
+ function drawTable(s, template, slot, geometry, rows) {
269
+ const cols = Math.max(...rows.map((r) => r.length));
270
+ const plainRows = rows.map((r) => Array.from({ length: cols }, (_, c) => plain(r[c] ?? [])));
271
+ const rowH = Math.min(0.45, geometry.h / ok.inches(1) / Math.max(rows.length, 1));
272
+ const table = ok.addSlideTable(s, { ...geometry, h: ok.inches(rowH * rows.length), rows: plainRows, firstRow: true, bandRow: true });
273
+ const size = slot.size ?? 14;
274
+ const color = col(resolveColor(template, slot.color ?? "text") ?? "#1B1B1F");
275
+ const font = resolveFont(template, slot.font);
276
+ ok.getTableCells(table).forEach((cells, r) => cells.forEach((cell, c) => {
277
+ const runs = rows[r]?.[c] ?? [];
278
+ if (runs.some((x) => x.bold || x.italic || x.code || x.href)) {
279
+ ok.setTableCellParagraphs(cell, [{ runs: runs.map((x) => ({ text: x.text, format: { size, bold: !!x.bold || r === 0, italic: !!x.italic, font: x.code ? "Courier New" : font, color } })) }]);
280
+ }
281
+ else
282
+ ok.setTableCellTextFormat(cell, { size, font, bold: r === 0, color });
283
+ }));
284
+ }
285
+ function chartSpec(source, palette) {
286
+ const data = parse(source);
287
+ if (!data || typeof data !== "object")
288
+ throw new Error("chart block must be a YAML mapping (kind, categories, series)");
289
+ const kind = String(data.kind ?? "column");
290
+ if (!CHART_KINDS.includes(kind))
291
+ throw new Error(`chart kind "${kind}" is not one of ${CHART_KINDS.join(", ")}`);
292
+ const categories = Array.isArray(data.categories) ? data.categories.map(String) : [];
293
+ if (!categories.length)
294
+ throw new Error("chart needs categories: [a, b, …]");
295
+ const seriesIn = Array.isArray(data.series) ? data.series : [];
296
+ if (!seriesIn.length)
297
+ throw new Error("chart needs series: [{name, values}]");
298
+ const series = seriesIn.map((x, i) => {
299
+ const row = (x ?? {});
300
+ const values = Array.isArray(row.values) ? row.values.map(Number) : [];
301
+ if (values.length !== categories.length || values.some((v) => Number.isNaN(v)))
302
+ throw new Error(`series ${i + 1}: values must be ${categories.length} numbers`);
303
+ // Pie and doughnut color their slices from the theme; a series color would paint every slice the same.
304
+ const color = kind === "pie" || kind === "doughnut" ? undefined : palette[i % palette.length];
305
+ return { name: String(row.name ?? `Series ${i + 1}`), values, ...(color ? { color } : {}) };
306
+ });
307
+ const spec = { kind, categories, series };
308
+ if (typeof data.title === "string" && data.title.trim())
309
+ spec.title = data.title.trim();
310
+ return spec;
311
+ }
312
+ /** Keep a box on the canvas: a template that overshoots is drawn at the edge, not off it. */
313
+ function clamp(box, size, min = 0.1) {
314
+ const c = CANVAS[size];
315
+ const x = Math.min(Math.max(0, box.x), c.w - min);
316
+ const y = Math.min(Math.max(0, box.y), c.h - min);
317
+ return { x, y, w: Math.max(min, Math.min(box.w, c.w - x)), h: Math.max(min, Math.min(box.h, c.h - y)) };
318
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The slots inside a slide's markdown — shared by the deck parser on the
3
+ * server and the editor in the browser (no imports, so the web bundle
4
+ * carries nothing else). Content before the first `::name::` marker is the
5
+ * default content: its leading heading is the title, the rest goes to the
6
+ * layout's default slot. A fence hides markers inside it.
7
+ */
8
+ export declare const NOTES_SLOT = "notes";
9
+ /** The default content of a slide: what comes before the first `::slot::` marker. */
10
+ export declare const DEFAULT_SLOT = "_";
11
+ export declare const trimBlank: (s: string) => string;
12
+ /** The slots of a slide's content: DEFAULT_SLOT for what precedes the first marker, then one entry per `::name::`. */
13
+ export declare function slotsOf(content: string): Record<string, string>;
14
+ /** Content from slots: the default content first, then `::name::` blocks. */
15
+ export declare function joinSlots(slots: Record<string, string>): string;
16
+ /** The default content split: the leading heading (the title) and what follows it. */
17
+ export declare function splitDefault(text: string | undefined): {
18
+ heading: string | null;
19
+ rest: string;
20
+ };
21
+ /**
22
+ * What a layout's slot shows for this content: an explicit `::name::` block,
23
+ * else for `title` the leading heading of the default content, else for the
24
+ * layout's default slot the rest of it. `undefined` when nothing fills it.
25
+ */
26
+ export declare function getSlot(content: string, name: string, defaultSlot: string | undefined): string | undefined;
27
+ /**
28
+ * The content with one slot's text replaced, kept where it was: the title
29
+ * stays the default content's heading, the default slot its rest, anything
30
+ * else its `::name::` block (added at the end when new). Empty text removes
31
+ * the slot.
32
+ */
33
+ export declare function setSlot(content: string, name: string, text: string, defaultSlot: string | undefined): string;
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The slots inside a slide's markdown — shared by the deck parser on the
3
+ * server and the editor in the browser (no imports, so the web bundle
4
+ * carries nothing else). Content before the first `::name::` marker is the
5
+ * default content: its leading heading is the title, the rest goes to the
6
+ * layout's default slot. A fence hides markers inside it.
7
+ */
8
+ export const NOTES_SLOT = "notes";
9
+ /** The default content of a slide: what comes before the first `::slot::` marker. */
10
+ export const DEFAULT_SLOT = "_";
11
+ const FENCE_RE = /^(`{3,}|~{3,})/;
12
+ const SLOT_RE = /^::([a-z][a-z0-9_-]*)::[ \t]*$/i;
13
+ const HEADING_RE = /^\s*#{1,6}[ \t]+(.+?)[ \t]*#*[ \t]*(?:\n|$)/;
14
+ export const trimBlank = (s) => s.replace(/^\s*\n/, "").replace(/\s+$/, "");
15
+ /** The slots of a slide's content: DEFAULT_SLOT for what precedes the first marker, then one entry per `::name::`. */
16
+ export function slotsOf(content) {
17
+ const out = {};
18
+ let current = DEFAULT_SLOT;
19
+ let buf = [];
20
+ let fence = null;
21
+ const flush = () => {
22
+ const text = trimBlank(buf.join("\n"));
23
+ if (current === DEFAULT_SLOT ? text !== "" : true)
24
+ out[current] = out[current] ? `${out[current]}\n${text}` : text;
25
+ buf = [];
26
+ };
27
+ for (const line of content.replace(/\r\n?/g, "\n").split("\n")) {
28
+ const f = FENCE_RE.exec(line);
29
+ if (f) {
30
+ if (!fence)
31
+ fence = f[1];
32
+ else if (line.startsWith(fence))
33
+ fence = null;
34
+ buf.push(line);
35
+ continue;
36
+ }
37
+ const m = !fence && SLOT_RE.exec(line);
38
+ if (m) {
39
+ flush();
40
+ current = m[1].toLowerCase();
41
+ continue;
42
+ }
43
+ buf.push(line);
44
+ }
45
+ flush();
46
+ return out;
47
+ }
48
+ /** Content from slots: the default content first, then `::name::` blocks. */
49
+ export function joinSlots(slots) {
50
+ const parts = [];
51
+ if (slots[DEFAULT_SLOT]?.trim())
52
+ parts.push(trimBlank(slots[DEFAULT_SLOT]));
53
+ for (const [name, text] of Object.entries(slots)) {
54
+ if (name === DEFAULT_SLOT)
55
+ continue;
56
+ parts.push(`::${name}::\n${trimBlank(text)}`);
57
+ }
58
+ return parts.join("\n\n");
59
+ }
60
+ /** The default content split: the leading heading (the title) and what follows it. */
61
+ export function splitDefault(text) {
62
+ const dflt = text ?? "";
63
+ const m = HEADING_RE.exec(dflt);
64
+ if (!m)
65
+ return { heading: null, rest: trimBlank(dflt) };
66
+ return { heading: m[1], rest: trimBlank(dflt.slice(m[0].length)) };
67
+ }
68
+ /**
69
+ * What a layout's slot shows for this content: an explicit `::name::` block,
70
+ * else for `title` the leading heading of the default content, else for the
71
+ * layout's default slot the rest of it. `undefined` when nothing fills it.
72
+ */
73
+ export function getSlot(content, name, defaultSlot) {
74
+ const slots = slotsOf(content);
75
+ if (slots[name] !== undefined)
76
+ return slots[name];
77
+ const { heading, rest } = splitDefault(slots[DEFAULT_SLOT]);
78
+ if (name === "title")
79
+ return heading ?? undefined;
80
+ if (name === defaultSlot && rest)
81
+ return rest;
82
+ return undefined;
83
+ }
84
+ /**
85
+ * The content with one slot's text replaced, kept where it was: the title
86
+ * stays the default content's heading, the default slot its rest, anything
87
+ * else its `::name::` block (added at the end when new). Empty text removes
88
+ * the slot.
89
+ */
90
+ export function setSlot(content, name, text, defaultSlot) {
91
+ const slots = slotsOf(content);
92
+ const value = trimBlank(text);
93
+ const { heading, rest } = splitDefault(slots[DEFAULT_SLOT]);
94
+ const writeDefault = (h, r) => {
95
+ const parts = [h ? `# ${h}` : "", r].filter(Boolean);
96
+ if (parts.length)
97
+ slots[DEFAULT_SLOT] = parts.join("\n\n");
98
+ else
99
+ delete slots[DEFAULT_SLOT];
100
+ };
101
+ if (slots[name] !== undefined) {
102
+ if (value)
103
+ slots[name] = value;
104
+ else
105
+ delete slots[name];
106
+ }
107
+ else if (name === "title")
108
+ writeDefault(value ? value.replace(/\n+/g, " ") : null, rest);
109
+ else if (name === defaultSlot)
110
+ writeDefault(heading, value);
111
+ else if (value)
112
+ slots[name] = value;
113
+ return joinSlots(slots);
114
+ }