dsh-ppt 0.23.1 → 0.24.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.
@@ -18,9 +18,24 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, w
18
18
  import { resolve as resolvePath } from 'node:path'
19
19
  import { deflateRawSync } from 'node:zlib'
20
20
  import * as deckMedia from './deck-media.mjs'
21
- import { renderDelivery } from './deck-delivery.mjs'
21
+ import { renderDelivery, renderHandoutMarkdown } from './deck-delivery.mjs'
22
22
  import { renderDataView } from './deck-data-view.mjs'
23
23
  import { normalizeVisual, visualHtmlStyle, visualShapeXml, VISUAL_CSS } from './deck-visual.mjs'
24
+ import {
25
+ containsLatexMath,
26
+ looksLikeRawLatex,
27
+ unwrapOuterMathDelimiters,
28
+ splitMathSegments,
29
+ parseLatexMath,
30
+ astToMathMl,
31
+ latexToMathMl,
32
+ astToOmml,
33
+ latexToOmml,
34
+ astToOoxmlInlineRuns,
35
+ has2dMathConstruct,
36
+ measureMathAst,
37
+ fitFormulaBoxIn,
38
+ } from './deck-math.mjs'
24
39
  import {
25
40
  THEMES,
26
41
  THEME_IDS,
@@ -61,9 +76,15 @@ import {
61
76
  computeBadgePillWidth,
62
77
  roundRectAdj,
63
78
  ooxmlShape,
79
+ ooxmlCustGeomShape,
80
+ ooxmlMathShape,
64
81
  ooxmlRichParagraph,
65
82
  getChromeConfig,
66
83
  parseHarveyValue,
84
+ computeMarimekkoModel,
85
+ computeSankeyLayout,
86
+ partitionValueChainItems,
87
+ computeSwotModel,
67
88
  renderSlideChromePptx,
68
89
  renderCoreSlidePptx,
69
90
  renderCoreSlideHtml,
@@ -102,9 +123,28 @@ export {
102
123
  formatSourceLine,
103
124
  computeBadgePillWidth,
104
125
  roundRectAdj,
126
+ ooxmlCustGeomShape,
127
+ ooxmlMathShape,
105
128
  getChromeConfig,
106
129
  parseHarveyValue,
130
+ computeMarimekkoModel,
131
+ computeSankeyLayout,
132
+ partitionValueChainItems,
133
+ computeSwotModel,
107
134
  renderSlideChromePptx,
135
+ containsLatexMath,
136
+ looksLikeRawLatex,
137
+ unwrapOuterMathDelimiters,
138
+ splitMathSegments,
139
+ parseLatexMath,
140
+ astToMathMl,
141
+ latexToMathMl,
142
+ astToOmml,
143
+ latexToOmml,
144
+ astToOoxmlInlineRuns,
145
+ has2dMathConstruct,
146
+ measureMathAst,
147
+ fitFormulaBoxIn,
108
148
  }
109
149
 
110
150
  /**
@@ -350,6 +390,7 @@ function createSection(heading = '', level = 0, coverOnly = false) {
350
390
  table: null,
351
391
  notes: [],
352
392
  directive: null,
393
+ formula: '',
353
394
  takeaway: '',
354
395
  source: '',
355
396
  }
@@ -446,7 +487,9 @@ function tableSlidesFrom(section, ui) {
446
487
  function specialSlidesFor(section, ui) {
447
488
  const out = []
448
489
  if (section.quote.length > 0) out.push(quoteSlideFrom(section, ui))
449
- if (section.table !== null) out.push(...tableSlidesFrom(section, ui))
490
+ const dirLayout = section.directive?.layout
491
+ const tableOwnedByDirective = dirLayout && ['marimekko', 'harvey', 'sankey'].includes(dirLayout)
492
+ if (section.table !== null && !tableOwnedByDirective) out.push(...tableSlidesFrom(section, ui))
450
493
  if (out.length > 0 && section.notes.length > 0) out[0].notes = section.notes.join('\n')
451
494
  return out
452
495
  }
@@ -460,12 +503,17 @@ function directiveSlideFrom(section, ui, pointIndexRef) {
460
503
  ...section.paragraphs.slice(spec.layout === 'callout' ? 1 : 0),
461
504
  ]
462
505
  const items = normalizeSlideItems(rawPoints, spec.layout)
506
+ const dirRows = section.table && ['marimekko', 'harvey', 'sankey'].includes(spec.layout)
507
+ ? [section.table.header, ...section.table.rows]
508
+ : undefined
463
509
  const slide = {
464
510
  layout: spec.layout,
465
511
  ...(spec.variant ? { variant: spec.variant } : {}),
466
512
  kicker: label,
467
513
  title: label,
514
+ ...(section.formula ? { formula: section.formula } : {}),
468
515
  ...(spec.layout === 'callout' && section.paragraphs[0] ? { text: section.paragraphs[0] } : {}),
516
+ ...(dirRows ? { rows: dirRows } : {}),
469
517
  ...(rawPoints.length > 0 ? { bullets: rawPoints } : {}),
470
518
  ...(items ? { items } : {}),
471
519
  ...(section.takeaway ? { takeaway: section.takeaway } : {}),
@@ -493,6 +541,8 @@ export function parseMarkdownDeck(titleInput, content, lang = 'zh') {
493
541
  let firstH1Seen = false
494
542
  let inDirective = false
495
543
  let awaitingTakeawayLine = false
544
+ let inMathBlock = false
545
+ let mathBlockLines = []
496
546
 
497
547
  const flush = () => {
498
548
  if (
@@ -500,6 +550,7 @@ export function parseMarkdownDeck(titleInput, content, lang = 'zh') {
500
550
  (current.heading !== '' ||
501
551
  current.bullets.length > 0 ||
502
552
  current.paragraphs.length > 0 ||
553
+ current.formula !== '' ||
503
554
  current.table !== null ||
504
555
  current.quote.length > 0 ||
505
556
  current.directive !== null)
@@ -509,10 +560,43 @@ export function parseMarkdownDeck(titleInput, content, lang = 'zh') {
509
560
  current = null
510
561
  inDirective = false
511
562
  awaitingTakeawayLine = false
563
+ inMathBlock = false
564
+ mathBlockLines = []
512
565
  }
513
566
 
514
567
  for (const rawLine of body.split('\n')) {
515
568
  const line = rawLine.trim()
569
+ if (inMathBlock) {
570
+ if (line === '$$') {
571
+ if (current === null) current = createSection()
572
+ const joinedFormula = mathBlockLines.join(' ').trim()
573
+ if (joinedFormula !== '') {
574
+ current.formula = current.formula ? current.formula + ' ; ' + joinedFormula : joinedFormula
575
+ }
576
+ inMathBlock = false
577
+ mathBlockLines = []
578
+ } else {
579
+ mathBlockLines.push(line)
580
+ }
581
+ continue
582
+ }
583
+
584
+ if (line === '$$') {
585
+ if (current === null) current = createSection()
586
+ inMathBlock = true
587
+ mathBlockLines = []
588
+ continue
589
+ }
590
+
591
+ if (line.startsWith('$$') && line.endsWith('$$') && line.length > 4) {
592
+ if (current === null) current = createSection()
593
+ const inlineDisplayFormula = line.slice(2, -2).trim()
594
+ if (inlineDisplayFormula !== '') {
595
+ current.formula = current.formula ? current.formula + ' ; ' + inlineDisplayFormula : inlineDisplayFormula
596
+ }
597
+ continue
598
+ }
599
+
516
600
  if (line === '') {
517
601
  awaitingTakeawayLine = false
518
602
  if (!inDirective && current !== null && current.heading === '') flush()
@@ -739,6 +823,26 @@ export function parseMarkdownDeck(titleInput, content, lang = 'zh') {
739
823
  continue
740
824
  }
741
825
 
826
+ if (section.formula !== '') {
827
+ const label = section.heading || (ui.pointKicker + ' ' + (++pointRef.count))
828
+ const rawPoints = [...section.bullets, ...section.paragraphs]
829
+ const items = normalizeSlideItems(rawPoints, 'callout')
830
+ const slide = {
831
+ layout: 'callout',
832
+ variant: 'formula',
833
+ kicker: label,
834
+ title: label,
835
+ formula: section.formula,
836
+ ...(rawPoints.length > 0 ? { bullets: rawPoints } : {}),
837
+ ...(items ? { items } : {}),
838
+ ...(section.takeaway ? { takeaway: section.takeaway } : {}),
839
+ ...(section.source ? { source: section.source } : {}),
840
+ }
841
+ if (specials.length === 0 && notes !== '') slide.notes = notes
842
+ slides.push(slide)
843
+ continue
844
+ }
845
+
742
846
  if (section.heading === '') {
743
847
  const points = [...section.bullets, ...section.paragraphs].flatMap((part) => splitSentences(part))
744
848
  for (const [pageIndex, pagePoints] of chunk(points, 8).entries()) {
@@ -830,6 +934,9 @@ function normalizeSlideContent(raw, index) {
830
934
  const subtitle = cleanRichText(source.subtitle ?? '')
831
935
  const kicker = stripInlineMarkdown(source.kicker ?? '')
832
936
  const text = cleanRichText(source.text ?? '')
937
+ const formula = typeof source.formula === 'string'
938
+ ? source.formula.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, '').trim()
939
+ : ''
833
940
  const takeaway = cleanRichText(source.takeaway ?? '')
834
941
  const slideSource = stripInlineMarkdown(source.source ?? '')
835
942
  const notes = String(source.notes ?? '').trim()
@@ -857,11 +964,13 @@ function normalizeSlideContent(raw, index) {
857
964
  ?? (!hasStringColumns ? source.columns : undefined)
858
965
  ?? source.steps
859
966
  ?? source.quadrants
967
+ ?? (layout === 'sankey' && bullets.length > 0 ? bullets : undefined)
860
968
  const items = normalizeSlideItems(rawItemsInput, layout)
861
969
 
862
970
  const commonExtras = {
863
971
  ...(variant !== '' ? { variant } : {}),
864
972
  ...(chrome ? { chrome } : {}),
973
+ ...(formula !== '' ? { formula } : {}),
865
974
  ...(takeaway !== '' ? { takeaway } : {}),
866
975
  ...(slideSource !== '' ? { source: slideSource } : {}),
867
976
  ...(activeIndex !== undefined ? { activeIndex } : {}),
@@ -895,7 +1004,7 @@ function normalizeSlideContent(raw, index) {
895
1004
  return { layout, kicker, title: truncate(quoteText, 220), subtitle, ...commonExtras }
896
1005
  }
897
1006
 
898
- if (layout === 'harvey') {
1007
+ if (layout === 'harvey' || layout === 'marimekko' || layout === 'sankey') {
899
1008
  let rows = []
900
1009
  if (Array.isArray(source.rows)) {
901
1010
  rows = source.rows
@@ -1074,12 +1183,17 @@ export function buildDeck(options = {}) {
1074
1183
  const jsonPath = resolvePath(outputDir, fileName + '.json')
1075
1184
  const htmlPath = resolvePath(outputDir, fileName + '.html')
1076
1185
  const pptxPath = resolvePath(outputDir, fileName + '.pptx')
1186
+ const handoutPath = options.handout === true ? resolvePath(outputDir, fileName + '.handout.md') : undefined
1077
1187
 
1078
- commitDeckArtifacts([
1188
+ const artifacts = [
1079
1189
  { path: jsonPath, data: JSON.stringify(manifest, null, 2) + '\n', encoding: 'utf8' },
1080
- { path: htmlPath, data: renderHtml(manifest, theme, language, { pptx: pptxPath, json: jsonPath }), encoding: 'utf8' },
1190
+ { path: htmlPath, data: renderHtml(manifest, theme, language, { pptx: pptxPath, json: jsonPath, ...(handoutPath ? { handout: handoutPath } : {}) }), encoding: 'utf8' },
1081
1191
  { path: pptxPath, data: buildPptx(manifest, theme, language) },
1082
- ])
1192
+ ]
1193
+ if (handoutPath) {
1194
+ artifacts.push({ path: handoutPath, data: renderHandoutMarkdown(manifest, language.id), encoding: 'utf8' })
1195
+ }
1196
+ commitDeckArtifacts(artifacts)
1083
1197
 
1084
1198
  return {
1085
1199
  ok: true,
@@ -1096,6 +1210,7 @@ export function buildDeck(options = {}) {
1096
1210
  htmlPath,
1097
1211
  pptxPath,
1098
1212
  jsonPath,
1213
+ ...(handoutPath ? { handoutPath } : {}),
1099
1214
  }
1100
1215
  }
1101
1216
 
@@ -1708,15 +1823,15 @@ ${slides}
1708
1823
  } else if (lower === 's') {
1709
1824
  if (document.body.classList.contains('presenting')) { toast(ui.presenterHint); }
1710
1825
  else { document.body.classList.toggle('notes-open'); syncNotes(); }
1711
- } else if (lower === 'v') {
1712
- if (presenterOpen()) closePresenter();
1826
+ } else if (lower === 'v' || lower === 'p') {
1827
+ event.preventDefault();
1828
+ if (typeof window.__dshTogglePresenter === 'function') window.__dshTogglePresenter();
1829
+ else if (presenterOpen()) closePresenter();
1713
1830
  else openPresenter();
1714
1831
  } else if (lower === 'b') {
1715
1832
  setBlank(document.body.classList.contains('blank-black') ? null : 'black');
1716
1833
  } else if (lower === 'w') {
1717
1834
  setBlank(document.body.classList.contains('blank-white') ? null : 'white');
1718
- } else if (lower === 'p') {
1719
- window.print();
1720
1835
  } else if (key === '?') {
1721
1836
  helpPanel.hidden = !helpPanel.hidden;
1722
1837
  }
@@ -1745,7 +1860,11 @@ ${slides}
1745
1860
  setNotes(!notesOpen());
1746
1861
  });
1747
1862
  notesClose.addEventListener('click', () => setNotes(false));
1748
- presenterBtn.addEventListener('click', () => { if (presenterOpen()) closePresenter(); else openPresenter(); });
1863
+ presenterBtn.addEventListener('click', () => {
1864
+ if (typeof window.__dshTogglePresenter === 'function') window.__dshTogglePresenter();
1865
+ else if (presenterOpen()) closePresenter();
1866
+ else openPresenter();
1867
+ });
1749
1868
  overviewBtn.addEventListener('click', () => setOverview(!document.body.classList.contains('overview')));
1750
1869
  helpBtn.addEventListener('click', () => { helpPanel.hidden = !helpPanel.hidden; });
1751
1870
  frames.forEach((frame, i) => {
@@ -1883,12 +2002,14 @@ function bulletTimingXml(spid, clicks) {
1883
2002
 
1884
2003
  function slideXml(slide, index, total, theme, langId, motion, manifest = {}) {
1885
2004
  const p = theme.palette
2005
+ const shapesXml = slideShapeList(slide, index, total, theme, langId, manifest)
1886
2006
  let motionXml = ''
1887
2007
  if (motion) {
1888
2008
  motionXml = '<p:transition spd="med"><p:fade/></p:transition>'
1889
2009
  const bullets = Array.isArray(slide.bullets) ? slide.bullets : []
1890
- if (slide.layout === 'bullets' && bullets.length >= 2) {
1891
- motionXml += bulletTimingXml((index + 1) * 10 + 3, bullets.length)
2010
+ const bodySpid = (index + 1) * 10 + 3
2011
+ if (slide.layout === 'bullets' && bullets.length >= 2 && shapesXml.includes('id="' + bodySpid + '"')) {
2012
+ motionXml += bulletTimingXml(bodySpid, bullets.length)
1892
2013
  }
1893
2014
  }
1894
2015
  return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
@@ -1898,7 +2019,7 @@ function slideXml(slide, index, total, theme, langId, motion, manifest = {}) {
1898
2019
  '<p:cSld><p:bg><p:bgPr><a:solidFill><a:srgbClr val="' + hex(p.bg) + '"/></a:solidFill><a:effectLst/></p:bgPr></p:bg>' +
1899
2020
  '<p:spTree><p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr>' +
1900
2021
  '<p:grpSpPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/><a:chOff x="0" y="0"/><a:chExt cx="0" cy="0"/></a:xfrm></p:grpSpPr>' +
1901
- slideShapeList(slide, index, total, theme, langId, manifest) +
2022
+ shapesXml +
1902
2023
  '</p:spTree></p:cSld>' +
1903
2024
  '<p:clrMapOvr><a:overrideClrMapping bg1="lt1" tx1="dk1" bg2="lt2" tx2="dk2" accent1="accent1" accent2="accent2" accent3="accent3" accent4="accent4" accent5="accent5" accent6="accent6" hlink="hlink" folHlink="folHlink"/></p:clrMapOvr>' +
1904
2025
  motionXml +
@@ -13,7 +13,7 @@ const clone = value => structuredClone(value)
13
13
  const digest = value => createHash('sha256').update(JSON.stringify(value)).digest('hex')
14
14
  const fail = (zh, en) => { throw new Error('dsh-ppt:' + zh + ' / ' + en) }
15
15
  const token = /\{\{([A-Za-z][A-Za-z0-9]*)\}\}/g
16
- const fields = ['title','subtitle','kicker','text','takeaway','source','bullets','items','columns','rows','notes','chart']
16
+ const fields = ['title','subtitle','kicker','text','takeaway','source','formula','bullets','items','columns','rows','notes','chart']
17
17
  const itemAliases = ['cards','metrics','steps','columns','quadrants']
18
18
  const numericPattern = /[-+]?(?:\d{1,3}(?:,\d{3})+(?:\.\d+)?|\d+(?:\.\d+)?|\.\d+)(?:[eE][-+]?\d+)?[%%]?/g
19
19
  export const dataFingerprint = model => digest({ sources: model.sources, metrics: model.metrics.map(({ ast, dependencies, evidence, ...m }) => m) })
@@ -116,6 +116,14 @@ export function substituteData(value, model, numericValue = false) {
116
116
  return math.format(model.values[id],metric)
117
117
  })
118
118
  }
119
+ function substituteFormula(value, model) {
120
+ if (typeof value !== 'string') return value
121
+ return value.replace(token, (_, id) => {
122
+ const metric = model.metrics.find(m => m.id === id)
123
+ if (!metric) fail('未知指标:' + id, 'Unknown metric: ' + id)
124
+ return math.format(model.values[id], metric)
125
+ })
126
+ }
119
127
  function chartTemplate(chart) {
120
128
  if (!chart || !chart.rows) return clone(chart)
121
129
  return { ...chart, rows:undefined,categories:chart.rows.slice(1).map(r=>r[0]),series:chart.rows[0].slice(1).map((name,c)=>({name,values:chart.rows.slice(1).map(r=>r[c+1])})) }
@@ -150,6 +158,7 @@ function liftItemAliases(slide) {
150
158
  export function resolveTemplate(template, model) {
151
159
  const next=liftItemAliases(clone(template))
152
160
  for (const key of ['title','subtitle','kicker','text','takeaway','source','notes']) if(typeof next[key]==='string')next[key]=substituteData(next[key],model)
161
+ if (typeof next.formula === 'string') next.formula = substituteFormula(next.formula, model)
153
162
  if(Array.isArray(next.bullets))next.bullets=next.bullets.map(v=>substituteData(v,model))
154
163
  if(Array.isArray(next.items))next.items=next.items.map(v=>resolveItem(v,model))
155
164
  if(Array.isArray(next.columns))next.columns=next.columns.map(v=>typeof v==='string'?substituteData(v,model):v)