teamshare-bridge 0.28.27 → 0.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,8 @@ exports.loadTemplateMirror = loadTemplateMirror;
8
8
  exports.resolveDeckTokens = resolveDeckTokens;
9
9
  exports.parseDeckSpec = parseDeckSpec;
10
10
  exports.planDeck = planDeck;
11
+ exports.defineTemplateMaster = defineTemplateMaster;
12
+ exports.applyTemplateTheme = applyTemplateTheme;
11
13
  /**
12
14
  * Slide Deck renderer (node tool `pptx-render`).
13
15
  *
@@ -27,9 +29,17 @@ exports.planDeck = planDeck;
27
29
  * outputName (`<Task subject> (YYYY-MM-DD)`), outlineMd (legacy fallback).
28
30
  */
29
31
  const pptxgenjs_1 = __importDefault(require("pptxgenjs"));
30
- const node_fs_1 = require("node:fs");
31
- const node_path_1 = require("node:path");
32
+ const jszip_1 = __importDefault(require("jszip"));
32
33
  const output_name_1 = require("./output-name");
34
+ // The template catalog + the designs read OUT of the real template files.
35
+ //
36
+ // The JSON is a STATIC import on purpose (2026-11): this used to read
37
+ // `deck-templates.json` off disk with three `cwd`-dependent candidates, none of
38
+ // which exists in an installed bridge (`npm` ships `dist/` only and tsc copied
39
+ // no JSON), so every template silently rendered as corporate. `tsc` DOES copy
40
+ // an imported JSON into `dist` (verified), which makes the lookup deterministic.
41
+ const deck_templates_json_1 = __importDefault(require("./deck-templates.json"));
42
+ const deck_themes_generated_1 = require("./deck-themes.generated");
33
43
  exports.KNOWN_LAYOUTS = [
34
44
  'title',
35
45
  'agenda',
@@ -107,32 +117,17 @@ function normalizeHex(raw, fallback) {
107
117
  return { color: fallback, changed: false };
108
118
  }
109
119
  /**
110
- * Load the template mirror at runtime (never statically imported: tsc does
111
- * not emit JSON assets into dist, so a static import would break at runtime).
112
- * Returns null when the file cannot be found or parsed.
120
+ * The template catalog mirror. A STATIC import: `tsc` emits the JSON next to
121
+ * the compiled module, so an installed bridge always has it. (The old
122
+ * filesystem hunt — `__dirname`, `cwd`, package root — found nothing in a
123
+ * global install, which silently collapsed every template to corporate.)
113
124
  */
114
125
  function loadTemplateMirror() {
115
- const candidates = [
116
- (0, node_path_1.join)(__dirname, 'deck-templates.json'),
117
- (0, node_path_1.join)(process.cwd(), 'src', 'lib', 'tools', 'deck-templates.json'),
118
- (0, node_path_1.join)((0, node_path_1.dirname)(__dirname), '..', '..', 'src', 'lib', 'tools', 'deck-templates.json'),
119
- ];
120
- for (const path of candidates) {
121
- try {
122
- if (!(0, node_fs_1.existsSync)(path))
123
- continue;
124
- const parsed = JSON.parse((0, node_fs_1.readFileSync)(path, 'utf8'));
125
- if (parsed &&
126
- Array.isArray(parsed.templates) &&
127
- parsed.templates.length > 0) {
128
- return { templates: parsed.templates };
129
- }
130
- }
131
- catch {
132
- /* try next candidate */
133
- }
134
- }
135
- return null;
126
+ const mirror = deck_templates_json_1.default;
127
+ const templates = mirror?.templates;
128
+ if (!Array.isArray(templates) || templates.length === 0)
129
+ return null;
130
+ return { templates };
136
131
  }
137
132
  function coerceDensity(raw, fallback) {
138
133
  if (typeof raw === 'string' && DENSITIES.includes(raw)) {
@@ -140,28 +135,42 @@ function coerceDensity(raw, fallback) {
140
135
  }
141
136
  return { density: fallback, changed: false };
142
137
  }
143
- /** Resolve template tokens: mirror lookup + accentColor/density overrides. */
138
+ /**
139
+ * Resolve template tokens.
140
+ *
141
+ * COLOURS AND FONTS COME FROM THE TEMPLATE FILE (2026-11), not from the
142
+ * catalog: the .pptx is the design source, so a replaced file changes the
143
+ * deck's colours and faces even before the catalog is touched. Layout
144
+ * PATTERNS (cover/title/footer treatment, corners, density) stay in the
145
+ * catalog, because they are prompt-facing style guidance rather than theme.
146
+ * The catalog remains the fallback for an id with no shipped file.
147
+ */
144
148
  function resolveDeckTokens(params) {
145
149
  const warnings = [];
146
150
  const requestedId = typeof params.template === 'string' ? params.template : 'corporate';
147
151
  const mirror = loadTemplateMirror();
148
152
  const entry = mirror?.templates.find((t) => t.id === requestedId);
149
- if (requestedId && !entry) {
153
+ const theme = (0, deck_themes_generated_1.deckThemeFor)(requestedId);
154
+ if (requestedId && !entry && !theme) {
150
155
  warnings.push(`unknown template "${requestedId}", fell back to corporate defaults`);
151
156
  }
157
+ if (entry && !theme) {
158
+ warnings.push(`template "${requestedId}" has no design file, used catalog colours`);
159
+ }
152
160
  const preview = entry?.preview ?? {};
153
161
  const base = {
154
- accent: normalizeHex(preview.accent, exports.FALLBACK_TOKENS.accent).color,
155
- background: normalizeHex(preview.background, exports.FALLBACK_TOKENS.background)
156
- .color,
157
- surface: normalizeHex(preview.surface, exports.FALLBACK_TOKENS.surface).color,
158
- textColor: normalizeHex(preview.textColor, exports.FALLBACK_TOKENS.textColor).color,
159
- bodyFont: typeof preview.bodyFont === 'string' && preview.bodyFont
160
- ? preview.bodyFont
161
- : exports.FALLBACK_TOKENS.bodyFont,
162
- headingFont: typeof preview.headingFont === 'string' && preview.headingFont
163
- ? preview.headingFont
164
- : exports.FALLBACK_TOKENS.headingFont,
162
+ accent: normalizeHex(theme?.accent ?? preview.accent, exports.FALLBACK_TOKENS.accent).color,
163
+ background: normalizeHex(theme?.background ?? preview.background, exports.FALLBACK_TOKENS.background).color,
164
+ surface: normalizeHex(theme?.surface ?? preview.surface, exports.FALLBACK_TOKENS.surface).color,
165
+ textColor: normalizeHex(theme?.textColor ?? preview.textColor, exports.FALLBACK_TOKENS.textColor).color,
166
+ bodyFont: theme?.bodyFont ||
167
+ (typeof preview.bodyFont === 'string' && preview.bodyFont
168
+ ? preview.bodyFont
169
+ : exports.FALLBACK_TOKENS.bodyFont),
170
+ headingFont: theme?.headingFont ||
171
+ (typeof preview.headingFont === 'string' && preview.headingFont
172
+ ? preview.headingFont
173
+ : exports.FALLBACK_TOKENS.headingFont),
165
174
  coverStyle: typeof preview.coverStyle === 'string' &&
166
175
  COVER_STYLES.includes(preview.coverStyle)
167
176
  ? preview.coverStyle
@@ -194,7 +203,12 @@ function resolveDeckTokens(params) {
194
203
  if (typeof densityRaw === 'string' && densityRaw !== 'auto') {
195
204
  base.density = coerceDensity(densityRaw, base.density).density;
196
205
  }
197
- return { tokens: base, templateId: entry?.id ?? 'corporate', warnings };
206
+ return {
207
+ tokens: base,
208
+ templateId: theme ? requestedId : (entry?.id ?? 'corporate'),
209
+ theme,
210
+ warnings,
211
+ };
198
212
  }
199
213
  /** Parse deckJson (text or object). Throws on invalid/empty specs. */
200
214
  function parseDeckSpec(raw) {
@@ -363,6 +377,63 @@ function presetShapeName(pptx, raw) {
363
377
  };
364
378
  return map[key] ?? null;
365
379
  }
380
+ /**
381
+ * The generated deck's slide master, recreated from the template file.
382
+ *
383
+ * `background` is the template master's own fill and `objects` is its
384
+ * decoration (percent geometry), both read out of `templates/decks/<id>.pptx`
385
+ * by `scripts/extract-deck-themes.mjs`. The master is what makes the file a
386
+ * real design rather than a pile of absolutely-positioned boxes: PowerPoint
387
+ * shows the background on every slide and new slides inherit it.
388
+ *
389
+ * pptxgenjs' master `objects` supports ONLY `rect` — `roundRect`/`ellipse` are
390
+ * silently dropped — so every decoration is emitted as a rectangle.
391
+ */
392
+ function defineTemplateMaster(pptx, tokens, theme, title) {
393
+ const objects = (theme.shapes ?? []).map((shape) => ({
394
+ rect: {
395
+ x: `${shape.x}%`,
396
+ y: `${shape.y}%`,
397
+ w: `${shape.w}%`,
398
+ h: `${shape.h}%`,
399
+ fill: { color: shape.fill },
400
+ line: { color: shape.fill, width: 0 },
401
+ },
402
+ }));
403
+ pptx.defineSlideMaster({
404
+ title,
405
+ background: { color: tokens.background },
406
+ objects,
407
+ });
408
+ }
409
+ /**
410
+ * Puts the TEMPLATE'S OWN THEME into the generated package.
411
+ *
412
+ * The theme part carries the colour scheme, the font scheme and the shape
413
+ * effect presets. Replacing it (byte for byte) is what makes PowerPoint's
414
+ * Design gallery show the template and makes a slide added after the fact
415
+ * inherit the design — the rendered slides alone cannot do that.
416
+ *
417
+ * Best-effort on purpose: a theme patch must never fail a render, so an
418
+ * unreadable package just ships as generated.
419
+ */
420
+ async function applyTemplateTheme(buffer, themeXml) {
421
+ try {
422
+ const zip = await jszip_1.default.loadAsync(buffer);
423
+ const themePath = 'ppt/theme/theme1.xml';
424
+ if (!zip.file(themePath))
425
+ return buffer;
426
+ zip.file(themePath, themeXml);
427
+ const out = await zip.generateAsync({
428
+ type: 'nodebuffer',
429
+ compression: 'DEFLATE',
430
+ });
431
+ return Buffer.isBuffer(out) ? out : Buffer.from(out);
432
+ }
433
+ catch {
434
+ return buffer;
435
+ }
436
+ }
366
437
  /** Image source pptxgenjs understands (remote URL or inline base64). */
367
438
  function imageSource(image) {
368
439
  if (typeof image.data === 'string' && image.data.trim()) {
@@ -1266,7 +1337,7 @@ class PptxDeckTool {
1266
1337
  const aspect = params.aspect === '4:3' ? '4:3' : '16:9';
1267
1338
  const deckJsonRaw = params.deckJson ?? params.deck_spec ?? params.spec;
1268
1339
  const outlineMd = typeof params.outlineMd === 'string' ? params.outlineMd : '';
1269
- const { tokens, templateId, warnings } = resolveDeckTokens(params);
1340
+ const { tokens, templateId, theme, warnings } = resolveDeckTokens(params);
1270
1341
  const fallbacks = [...warnings];
1271
1342
  const log = [];
1272
1343
  // -- legacy path: markdown outline, no deck.json -------------------------
@@ -1279,7 +1350,7 @@ class PptxDeckTool {
1279
1350
  error: 'No deck provided (deckJson or legacy outlineMd)',
1280
1351
  };
1281
1352
  }
1282
- return this.runLegacy(params, outlineMd, tokens, templateId, aspect, fallbacks, log);
1353
+ return this.runLegacy(params, outlineMd, tokens, templateId, theme, aspect, fallbacks, log);
1283
1354
  }
1284
1355
  let spec;
1285
1356
  try {
@@ -1301,14 +1372,28 @@ class PptxDeckTool {
1301
1372
  const pptx = new pptxgenjs_1.default();
1302
1373
  pptx.layout = aspect === '4:3' ? 'LAYOUT_4x3' : 'LAYOUT_16x9';
1303
1374
  pptx.author = 'TeamShare';
1375
+ // The template's theme + master (2026-11). Defined BEFORE any slide so
1376
+ // every slide is created on it; the theme part is replaced after the write.
1377
+ if (theme) {
1378
+ pptx.theme = {
1379
+ headFontFace: theme.headingFont,
1380
+ bodyFontFace: theme.bodyFont,
1381
+ };
1382
+ }
1383
+ const masterName = theme ? `TeamShare ${templateId}` : null;
1384
+ if (theme && masterName)
1385
+ defineTemplateMaster(pptx, tokens, theme, masterName);
1304
1386
  const docTitle = (typeof spec.title === 'string' && spec.title) ||
1305
1387
  this.inferTitle(spec) ||
1306
1388
  'Presentation';
1307
1389
  pptx.title = docTitle;
1308
1390
  spec.slides.forEach((slide, idx) => {
1309
1391
  const p = plan[idx];
1310
- const s = pptx.addSlide();
1311
- s.background = { color: tokens.background };
1392
+ const s = pptx.addSlide(masterName ?? undefined);
1393
+ // With a master, its background applies; without one, paint it per slide
1394
+ // (the built-in corporate fallback and the legacy path keep working).
1395
+ if (!masterName)
1396
+ s.background = { color: tokens.background };
1312
1397
  const title = typeof slide.title === 'string' ? stripMd(slide.title) : '';
1313
1398
  const subtitle = typeof slide.subtitle === 'string'
1314
1399
  ? stripMd(slide.subtitle)
@@ -1617,7 +1702,9 @@ class PptxDeckTool {
1617
1702
  if (notes)
1618
1703
  s.addNotes(notes);
1619
1704
  });
1620
- const buffer = (await pptx.write({ outputType: 'nodebuffer' }));
1705
+ let buffer = (await pptx.write({ outputType: 'nodebuffer' }));
1706
+ if (theme)
1707
+ buffer = await applyTemplateTheme(buffer, theme.themeXml);
1621
1708
  const base = (0, output_name_1.outputBaseName)(params);
1622
1709
  const safeName = docTitle.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64);
1623
1710
  const pptxName = base ? `${base} — Deck.pptx` : `${safeName}.pptx`;
@@ -1638,7 +1725,9 @@ class PptxDeckTool {
1638
1725
  mime: 'text/markdown',
1639
1726
  });
1640
1727
  }
1641
- log.push(`Template: ${templateId}`, `Slides: ${spec.slides.length}`, `Density: ${tokens.density}, Aspect: ${aspect}`, `Layouts: ${layoutsUsed.join(', ')}`, ...fallbacks.map((f) => `Fallback: ${f}`), `Generated PPTX: ${pptxName} (${buffer.length} bytes)`);
1728
+ log.push(`Template: ${templateId}`, theme
1729
+ ? `Theme: "${theme.themeName}" from templates/decks/${templateId}.pptx (master + colour scheme + fonts applied)`
1730
+ : 'Theme: built-in corporate fallback (no template file)', `Slides: ${spec.slides.length}`, `Density: ${tokens.density}, Aspect: ${aspect}`, `Layouts: ${layoutsUsed.join(', ')}`, ...fallbacks.map((f) => `Fallback: ${f}`), `Generated PPTX: ${pptxName} (${buffer.length} bytes)`);
1642
1731
  return { outputs, log };
1643
1732
  }
1644
1733
  inferTitle(spec) {
@@ -1773,7 +1862,7 @@ class PptxDeckTool {
1773
1862
  }
1774
1863
  return lines.join('\n');
1775
1864
  }
1776
- async runLegacy(params, outlineMd, tokens, templateId, aspect, fallbacks, log) {
1865
+ async runLegacy(params, outlineMd, tokens, templateId, theme, aspect, fallbacks, log) {
1777
1866
  const { docTitle, slides: slideData } = parseOutline(outlineMd);
1778
1867
  if (slideData.length === 0) {
1779
1868
  return {
@@ -1786,11 +1875,21 @@ class PptxDeckTool {
1786
1875
  pptx.layout = aspect === '4:3' ? 'LAYOUT_4x3' : 'LAYOUT_16x9';
1787
1876
  pptx.author = 'TeamShare';
1788
1877
  pptx.title = docTitle;
1789
- const cover = pptx.addSlide();
1878
+ if (theme) {
1879
+ pptx.theme = {
1880
+ headFontFace: theme.headingFont,
1881
+ bodyFontFace: theme.bodyFont,
1882
+ };
1883
+ }
1884
+ const masterName = theme ? `TeamShare ${templateId}` : null;
1885
+ if (theme && masterName)
1886
+ defineTemplateMaster(pptx, tokens, theme, masterName);
1887
+ const cover = pptx.addSlide(masterName ?? undefined);
1790
1888
  renderCover(cover, pptx, tokens, docTitle, undefined, undefined);
1791
1889
  slideData.forEach((slide, i) => {
1792
- const s = pptx.addSlide();
1793
- s.background = { color: tokens.background };
1890
+ const s = pptx.addSlide(masterName ?? undefined);
1891
+ if (!masterName)
1892
+ s.background = { color: tokens.background };
1794
1893
  const plan = {
1795
1894
  index: i,
1796
1895
  layout: 'bullets',
@@ -1813,11 +1912,15 @@ class PptxDeckTool {
1813
1912
  s.addNotes(slide.notes);
1814
1913
  addFooter(s, pptx, tokens, i + 2);
1815
1914
  });
1816
- const buffer = (await pptx.write({ outputType: 'nodebuffer' }));
1915
+ let buffer = (await pptx.write({ outputType: 'nodebuffer' }));
1916
+ if (theme)
1917
+ buffer = await applyTemplateTheme(buffer, theme.themeXml);
1817
1918
  const base = (0, output_name_1.outputBaseName)(params);
1818
1919
  const safeName = docTitle.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64);
1819
1920
  const pptxName = base ? `${base} — Deck.pptx` : `${safeName}.pptx`;
1820
- log.push(`Template: ${templateId}`, `Slides: ${slideData.length + 1} (1 title + ${slideData.length} content)`, `Density: ${tokens.density}, Aspect: ${aspect}`, 'Layouts: title, bullets', ...fallbacks.map((f) => `Fallback: ${f}`), `Generated PPTX: ${pptxName} (${buffer.length} bytes)`);
1921
+ log.push(`Template: ${templateId}`, theme
1922
+ ? `Theme: "${theme.themeName}" from templates/decks/${templateId}.pptx`
1923
+ : 'Theme: built-in corporate fallback (no template file)', `Slides: ${slideData.length + 1} (1 title + ${slideData.length} content)`, `Density: ${tokens.density}, Aspect: ${aspect}`, 'Layouts: title, bullets', ...fallbacks.map((f) => `Fallback: ${f}`), `Generated PPTX: ${pptxName} (${buffer.length} bytes)`);
1821
1924
  return {
1822
1925
  outputs: [
1823
1926
  {