vscode-colaview 0.4.1 → 0.4.3

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.
@@ -6,11 +6,10 @@
6
6
  // 这里照同一套契约输出行级 DOM,样式表直接复用上游 slides.css,只在选择器层面
7
7
  // 做三处映射:`body.presenting` → `.ct-stage.presenting`、`body.slides-printing`
8
8
  // → `.ct-stage.printing`、`#editor` → `.cm-page`。
9
- import * as path from 'path';
10
9
  import { escapeAttr, escapeHtml, renderInline } from './markdown';
11
- import type { MdBlock, MdListItem } from './markdown';
12
- import { deckChrome, paginateDeck, splitDeck } from './deck';
13
- import type { DeckChrome, Slide } from './deck';
10
+ import type { MdListItem } from './markdown';
11
+ import { splitDeck } from './deck';
12
+ import type { Slide } from './deck';
14
13
 
15
14
  /**
16
15
  * 把上游样式表的入口选择器映射到我们的舞台与页盒。
@@ -66,15 +65,6 @@ body {
66
65
  overflow: hidden;
67
66
  container-type: size;
68
67
  }
69
- .ct-stage .slide-chrome,
70
- .ct-stage .slide-foot {
71
- position: absolute;
72
- left: 0;
73
- right: 0;
74
- z-index: 2;
75
- }
76
- .ct-stage .slide-chrome { top: 0; }
77
- .ct-stage .slide-foot { bottom: 0; }
78
68
  .ct-stage .cm-scroller { height: 100%; }
79
69
  .ct-stage .cm-page .cm-content { height: 100%; box-sizing: border-box; }
80
70
  /* 页标题字体兜底:slides.css 缺失或 --slide-title-font 没落上时,也只回退到
@@ -83,13 +73,33 @@ body {
83
73
  font-family: var(--slide-title-font, var(--font-body, var(--font-family-base, sans-serif)));
84
74
  }
85
75
  .ct-stage .cm-line { box-sizing: border-box; }
76
+ /* 纸面校准(与上游导出同一条路线):主题的幻灯片令牌是「视口派生」的
77
+ (--slide-page-pad-x: 3.2vw、kicker: clamp(18px,1.25vw,24px) …),在固定
78
+ 1920×1080 的纸面上必须钉成设计空间的 px,否则 cqw 之外的 vw 会按窗口解析。
79
+ 正文基准同样钉在主题编辑字号:主题的 1.6.0 基准放大只服务侧栏放映,
80
+ 纸面以参照页为准。 */
81
+ .ct-stage.printing .cm-page,
82
+ .ct-stage.presenting .cm-page {
83
+ --slide-page-pad-x: 115.2px;
84
+ --slide-kicker-size: 13.824px;
85
+ --slide-chrome-pad-y: 34.56px;
86
+ --slide-page-top-pad: 24px;
87
+ --slide-chrome-logo-h: 30px;
88
+ --slide-foot-pad-y: 25.92px;
89
+ --slide-img-max-h: 777.6px;
90
+ --slide-figure-max-h: 712.8px;
91
+ }
86
92
  .ct-stage.presenting .cm-page .cm-content,
87
- .ct-stage.printing .cm-page .cm-content,
88
- .ct-stage.flow .cm-page .cm-content {
89
- font-size: var(--slide-body-size, 17px);
93
+ .ct-stage.printing .cm-page .cm-content {
94
+ font-size: var(--editor-font-size, 16px);
90
95
  line-height: 1.7;
91
- padding-top: max(var(--slide-page-top-pad, 24px), calc(var(--slide-chrome-pad-y, 34.56px) + var(--slide-chrome-logo-h, 30px))) !important;
92
- padding-bottom: calc(var(--slide-foot-pad-y, 25.92px) + 1em) !important;
96
+ }
97
+ /* cover / closing 与上游 spacer 同构整页居中;section 与普通页保持顶部对齐。 */
98
+ .ct-stage.printing .cm-page[data-slide-layout="cover"] .cm-content,
99
+ .ct-stage.printing .cm-page[data-slide-layout="closing"] .cm-content {
100
+ display: flex;
101
+ flex-direction: column;
102
+ justify-content: safe center;
93
103
  }
94
104
  .ct-stage .cm-md-table-widget table { border-collapse: collapse; }
95
105
  /* 图形 widget:块级 HTML(内联 SVG)与图,都是一张图占一行。 */
@@ -119,7 +129,7 @@ body {
119
129
  width: 100%;
120
130
  height: 100%;
121
131
  }
122
- .ct-stage.presenting[data-flow] {
132
+ .ct-stage[data-flow] {
123
133
  display: block;
124
134
  /* 整体缩放:纸面固定 1920×1080 排版,缩放只改变视觉尺寸,不改字号层级
125
135
  (与全屏、PDF 完全同一套数值)。 */
@@ -127,12 +137,12 @@ body {
127
137
  height: auto;
128
138
  overflow: visible;
129
139
  }
130
- .ct-stage.presenting[data-flow] .cm-page {
140
+ .ct-stage[data-flow] .cm-page {
131
141
  display: block;
132
142
  margin: 0 auto 24px;
133
143
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.16);
134
144
  }
135
- .ct-stage.presenting[data-flow] .cm-page:last-of-type { margin-bottom: 0; }
145
+ .ct-stage[data-flow] .cm-page:last-of-type { margin-bottom: 0; }
136
146
  .ct-stage.presenting .cm-page:not([data-active]) { display: none; }
137
147
  /* 放映:一次一页,整页按容器缩放(flow 不走这条,它在舞台层缩放)。 */
138
148
  .ct-stage.presenting:not([data-flow]) .cm-page[data-active] { display: block; zoom: var(--deck-scale, 1); }
@@ -153,7 +163,10 @@ body {
153
163
  * DECK_CSS 兜底。
154
164
  */
155
165
  export function composeDeckCss(themeCss: string, slidesCss: string): string {
156
- return `${adaptSlidesCss(slidesCss)}\n${themeCss}\n${DECK_CSS}`;
166
+ // 主题的幻灯片规则同样写在 body.presenting / body.slides-printing / #editor
167
+ // 作用域里,必须和基础份一起映射,否则分版式的配色、字号(pink-script-dark
168
+ // 的品红封面标题、28px 正文基准、3.2vw 页边距)全部落空,纸面退回默认排版。
169
+ return `${adaptSlidesCss(slidesCss)}\n${adaptSlidesCss(themeCss)}\n${DECK_CSS}`;
157
170
  }
158
171
 
159
172
  /** 行内语法:复用文档渲染器的解析,只把标签换成编辑面的类名。 */
@@ -182,14 +195,23 @@ function listMark(item: MdListItem): string {
182
195
  return item.task ? '☑' : '☐';
183
196
  }
184
197
 
198
+ /** 图片独占一段:`![](…)`,允许可选的 title。 */
199
+ function isImageParagraph(text: string): boolean {
200
+ return /^!\[[^\]]*\]\([^)\s]+(?:\s+"[^"]*")?\)$/.test(text.trim());
201
+ }
202
+
185
203
  /** 一页的行级 DOM。 */
186
- function slidePageHtml(slide: Slide, index: number, total: number, active: boolean, chrome: DeckChrome): string {
204
+ function slidePageHtml(slide: Slide, index: number, active: boolean): string {
187
205
  const layout = slide.layout === '' ? 'default' : slide.layout;
188
206
  const pageNo = index + 1;
189
207
  const rows: string[] = [];
190
208
  let started = false;
191
209
  const push = (classes: string[], inner: string, extra = ''): void => {
192
- const list = [...classes, `cm-md-slide-${layout}`, `cm-md-page-${pageNo}`];
210
+ const list = [...classes, `cm-md-page-${pageNo}`];
211
+ // 上游默认页用 cm-md-page-default(slides.css 的默认版式就挂在这个类上);
212
+ // 声明了版式的页才加 cm-md-slide-<layout>。
213
+ if (layout === 'default') list.push('cm-md-page-default');
214
+ else list.push(`cm-md-slide-${layout}`);
193
215
  if (!started) {
194
216
  list.push('cm-md-page-start');
195
217
  started = true;
@@ -197,22 +219,18 @@ function slidePageHtml(slide: Slide, index: number, total: number, active: boole
197
219
  rows.push(cmLine(list, inner, pageNo, extra));
198
220
  };
199
221
 
200
- if (slide.kicker !== '') push(['cm-md-page-kicker'], slideInline(slide.kicker));
222
+ // 上游 DOM 保留页首指令行(隐藏):page-start 落在它上面,
223
+ // quote 的 `directive + blockquote::before`、steps 的 counter-reset 才有落点。
224
+ if (slide.layout !== '' || slide.kicker !== '' || slide.label !== '' || slide.chapter !== '') {
225
+ push(['cm-md-slide-directive'], '', ' aria-hidden="true"');
226
+ }
201
227
  if (slide.title !== '') {
202
228
  const attr = slide.kicker === '' ? '' : ` data-slide-kicker="${escapeAttr(slide.kicker)}"`;
203
- /* 长标题按字数收敛字号:封面/章节标题是 em 级的巨字号,标题一长就会撑破
204
- 纸面,屏幕上被裁、导出的 PDF 也一样。这里按长度分档降一级字号。 */
205
- const titleWeight = [...slide.title].reduce((sum, ch) => sum + (/[\u3000-\u9fff]/.test(ch) ? 2 : 1), 0);
206
- const capScale =
207
- layout === 'cover' || layout === 'section' || layout === 'closing'
208
- ? titleWeight > 30 ? 0.5 : titleWeight > 20 ? 0.62 : titleWeight > 12 ? 0.76 : 1
209
- : layout === 'agenda'
210
- ? titleWeight > 24 ? 0.6 : titleWeight > 16 ? 0.75 : 1
211
- : 1;
212
- const scaleStyle = capScale === 1 ? '' : ` style="font-size: calc(var(--slide-title-size, 4.2em) * ${capScale})"`;
213
- push(['cm-md-page-title', 'cm-md-atxheading1'], slideInline(slide.title), attr + scaleStyle);
229
+ push(['cm-md-page-title', 'cm-md-atxheading1'], slideInline(slide.title), attr);
230
+ }
231
+ if (slide.lead !== '') {
232
+ push(isImageParagraph(slide.lead) ? ['cm-md-page-lead', 'cm-md-image'] : ['cm-md-page-lead'], slideInline(slide.lead));
214
233
  }
215
- if (slide.lead !== '') push(['cm-md-page-lead'], slideInline(slide.lead));
216
234
 
217
235
  let ordered = 0;
218
236
  for (const block of slide.blocks) {
@@ -221,12 +239,14 @@ function slidePageHtml(slide: Slide, index: number, total: number, active: boole
221
239
  continue;
222
240
  }
223
241
  if (block.kind === 'paragraph') {
224
- push([], slideInline(block.text));
242
+ push(isImageParagraph(block.text) ? ['cm-md-image'] : [], slideInline(block.text));
225
243
  continue;
226
244
  }
227
245
  if (block.kind === 'divider') continue;
228
246
  if (block.kind === 'quote') {
229
- push(['cm-md-blockquote'], `<span class="cm-md-quotemark">&gt;</span>${slideInline(block.text)}`);
247
+ // 上游放映/导出里引用标记不参与排版(只在编辑时显示),这里也不再注入 `>`,
248
+ // 否则纸面引文会多出一个灰色尖括号(参考 PDF 里没有)。
249
+ push(['cm-md-blockquote'], slideInline(block.text));
230
250
  continue;
231
251
  }
232
252
  if (block.kind === 'code') {
@@ -275,34 +295,26 @@ function slidePageHtml(slide: Slide, index: number, total: number, active: boole
275
295
  }
276
296
 
277
297
  const activeAttr = active ? ' data-active=""' : '';
278
- const logo = chrome.logo === '' ? '' : `<img class="slide-chrome-logo" src="${escapeAttr(chrome.logo)}" alt="" />`;
279
- const right = [slide.chapter, slide.label].filter((part) => part !== '').join(' · ');
298
+ // 渲染层不再注入页眉/页脚:全屏、分页预览与 PDF 都只保留 Markdown 内容本身,
299
+ // deck 块里的 logo / brand / title / meta 仅作为元数据,不落到纸面。
280
300
  return (
281
301
  `<section class="cm-page" data-page="${pageNo}" data-slide-layout="${escapeAttr(layout)}"${activeAttr}>` +
282
- `<div class="slide-chrome">` +
283
- `<div class="slide-chrome-left"><span class="slide-chrome-brand">${escapeHtml(chrome.brand)}</span>${logo}</div>` +
284
- `<div class="slide-chrome-right">${escapeHtml(right)}</div>` +
285
- `</div>` +
286
302
  `<div class="cm-scroller"><div class="cm-content">${rows.join('')}</div></div>` +
287
- `<div class="slide-foot">` +
288
- `<span class="slide-foot-brand">${escapeHtml(chrome.brand)}</span>` +
289
- `<span class="slide-foot-num">${pageNo} / ${total}</span>` +
290
- `</div>` +
291
303
  `</section>`
292
304
  );
293
305
  }
294
306
 
295
- /** 舞台:每页自带 chrome 与页脚(绝对定位),flow / presenting / printing 共用同一份结构。 */
296
- function deckStageHtml(slides: readonly Slide[], chrome: DeckChrome, mode: 'flow' | 'presenting' | 'printing'): string {
307
+ /** 舞台:三种模式共用同一份内容结构,均不带渲染层页眉/页脚。 */
308
+ function deckStageHtml(slides: readonly Slide[], mode: 'flow' | 'presenting' | 'printing'): string {
309
+ // 纸面(导出 / 分页预览)只走 printing 作用域,presenting-only 规则不再泄漏;
310
+ // 全屏放映挂 presenting,但同样只显示 Markdown 内容。
297
311
  const pages = slides
298
- // 打印时所有页都要可见(放映的 :not([data-active]) 隐藏规则会一并命中)。
299
- .map((slide, index) => slidePageHtml(slide, index, slides.length, mode === 'printing' || mode === 'flow' || index === 0, chrome))
312
+ .map((slide, index) => slidePageHtml(slide, index, mode !== 'presenting' || index === 0))
300
313
  .join('\n');
301
314
  const first = slides[0];
302
315
  const layout = first === undefined || first.layout === '' ? 'default' : first.layout;
303
- // flow 复用 presenting 的全部规则(字号、留白、chrome),只是把所有页同时摊开。
304
- // 打印同时挂 presenting:主题里不少版式字号只写在 presenting 作用域里。
305
- const stageClass = mode === 'flow' ? 'presenting' : mode === 'printing' ? 'presenting printing' : mode;
316
+ // flow 与纸面同一套令牌/字号(printing 作用域),只是把所有页同时摊开。
317
+ const stageClass = mode === 'presenting' ? 'presenting' : 'printing';
306
318
  const flowAttr = mode === 'flow' ? ' data-flow="1"' : '';
307
319
  return `<div class="ct-stage ${stageClass}" data-slide-layout="${escapeAttr(layout)}"${flowAttr}>${pages}</div>`;
308
320
  }
@@ -318,20 +330,10 @@ export interface DeckStage {
318
330
 
319
331
  /** Markdown → 舞台 HTML。 */
320
332
  export function renderDeckStage(markdown: string, title: string, mode: SlideStageMode = 'flow'): DeckStage {
321
- const deck = splitDeck(markdown, title);
322
- const chrome = deckChrome(deck);
323
- const slides = paginateDeck(deck);
324
- return { html: deckStageHtml(slides, chrome, mode), pageCount: slides.length };
325
- }
326
-
327
- /** 相对图片路径 → 绝对 `file://` URL(WebView / 浏览器里没有文档目录这一上下文)。 */
328
- export function rewriteImageSources(html: string, baseDir: string): string {
329
- return html.replace(/(<img\b[^>]*?\bsrc=)(["'])([^"']+)\2/gi, (all, head: string, quote: string, src: string) => {
330
- if (/^(?:[a-z]+:)?\/\//i.test(src) || /^[a-z]+:/i.test(src) || src.startsWith('#')) return all;
331
- const absolute = path.resolve(baseDir, decodeURI(src));
332
- const url = 'file:///' + absolute.replace(/\\/g, '/').replace(/^\//, '');
333
- return `${head}${quote}${url}${quote}`;
334
- });
333
+ // 页数与 `---` 一一对应(与 extend 导出一致);超长页交给整页 zoom 收敛,
334
+ // 不再做内容二次分页,否则会出现参考里没有的续页。
335
+ const slides = splitDeck(markdown, title);
336
+ return { html: deckStageHtml(slides, mode), pageCount: slides.length };
335
337
  }
336
338
 
337
339
  /** 全屏演示层样式:幻灯片一次一页,文档居中限宽。 */
@@ -389,21 +391,14 @@ const PRESENT_SCRIPT = `<script data-colaview-present>
389
391
  if (!page) return;
390
392
  var content = page.querySelector('.cm-content');
391
393
  if (!content) return;
392
- // 逐页 fit 走字号收缩(布局属性,会重排):zoom 会改动纸面的容器查询尺寸
393
- // (cqw/cqh),在放映与打印里都靠不住——与 ColaMD-themes 的 fitPaper 一致。
394
- content.style.fontSize = '';
395
- var base = parseFloat(getComputedStyle(content).fontSize) || 0;
396
- if (!base) return;
397
- var floor = base * 0.5;
398
- for (var i = 0; i < 8; i += 1) {
399
- var room = page.clientHeight;
400
- var need = content.scrollHeight;
401
- if (!room || need <= room + 1) return;
402
- var current = parseFloat(getComputedStyle(content).fontSize) || base;
403
- var next = current * (room / need) * 0.98;
404
- if (next < floor) { content.style.fontSize = floor.toFixed(2) + 'px'; return; }
405
- content.style.fontSize = next.toFixed(2) + 'px';
406
- }
394
+ // 与上游/纸面一致:装不下时对内容整体 zoom(字体、px 元素、间距一起缩),
395
+ // 不再只改字号——96px 步骤序号、320px 引号这类 px 值不会跟着字号缩。
396
+ // scrollHeight 与 zoom 无关,一次算出比例即可,避免循环里越缩越小。
397
+ content.style.zoom = '';
398
+ var room = page.clientHeight;
399
+ var need = content.scrollHeight;
400
+ if (!room || !need || need <= room + 1) return;
401
+ content.style.zoom = Math.max(0.2, (room / need) * 0.995).toFixed(4);
407
402
  }
408
403
  window.__colaviewFitSlides = function () { pages.forEach(fitPage); return true; };
409
404
  if (document.fonts && document.fonts.ready) document.fonts.ready.then(function () { window.__colaviewFitSlides(); });
@@ -418,8 +413,6 @@ const PRESENT_SCRIPT = `<script data-colaview-present>
418
413
  fitPage(pages[index]);
419
414
  var layout = pages[index].getAttribute('data-slide-layout') || 'default';
420
415
  stage.setAttribute('data-slide-layout', layout);
421
- var num = stage.querySelector('.slide-foot-num');
422
- if (num) num.textContent = (index + 1) + ' / ' + pages.length;
423
416
  hud.textContent = (index + 1) + ' / ' + pages.length + ' \\u2190/\\u2192 \\u7ffb\\u9875 \\u00b7 F \\u5168\\u5c4f' +
424
417
  (isFullscreen() ? '' : ' \\u00b7 \\u70b9\\u51fb\\u8fdb\\u5165\\u5168\\u5c4f');
425
418
  } else {
@@ -499,12 +492,11 @@ export interface StandaloneSlidesInput {
499
492
 
500
493
  /** 独立幻灯片文档:放映与幻灯片 PDF 都走这里(CSS 全内联,可在浏览器里直接打开)。 */
501
494
  export function renderSlidesDocument(input: StandaloneSlidesInput): string {
502
- const deck = splitDeck(input.markdown, input.title);
503
- const chrome = deckChrome(deck);
504
- const slides = paginateDeck(deck);
495
+ // 纸面页数与 `---` 一一对应(与 extend 导出一致);超长页交给整页缩放收敛。
496
+ const slides = splitDeck(input.markdown, input.title);
505
497
  const stageMode: SlideStageMode = input.mode === 'print' ? 'printing' : 'presenting';
506
498
  const css = composeDeckCss(input.themeCss, input.slidesCss);
507
- const html = deckStageHtml(slides, chrome, stageMode);
499
+ const html = deckStageHtml(slides, stageMode);
508
500
  return (
509
501
  `<!doctype html>\n<html lang="zh-CN">\n<head>\n` +
510
502
  `<meta charset="utf-8" />\n<meta name="viewport" content="width=device-width, initial-scale=1" />\n` +
@@ -1,29 +0,0 @@
1
- # ColaView MD v0.4.1
2
-
3
- ## 🎬 Slides Preview: Mermaid & Math Now Render
4
-
5
- In the slides deck, ```mermaid fences and `$$…$$` block math were drawn as raw source lines — the slides render layer is a standalone renderer and had no widget support. Worse, the deck paginator splits oversized code blocks by line budget, so a long mermaid fence was chopped into invalid partial sources that failed parsing with "Syntax error in text".
6
-
7
- - **Mermaid fences are never split** — `splitBlock` keeps a mermaid block whole (an oversized diagram occupies a page on its own).
8
- - **Hydration** — the renderer emits placeholder containers (source kept in a hidden node), and the WebView hydrates them after each deck render. When rendering fails, the source stays visible as a fallback.
9
-
10
- ## 🎯 Slide Rendering Aligned with ColaMD-extend
11
-
12
- Slides mermaid now renders through **pele** (the same renderer ColaMD-extend uses via `pele/lazy`): the SVG references `var(--pele-*)` tokens that DECK_CSS injects on the holder — the same mapping as the document preview — so diagrams inherit the active theme exactly instead of looking like pasted-in neutral-theme pictures. The standalone present/PDF document loads the pele global build from CDN and renders synchronously.
13
-
14
- - **Per-page fit via font-size shrink** (from ColaMD-themes `fitPaper`) — replaces the previous `zoom` approach, which distorts the pages' cqw/cqh container-query sizing in presenting and printing. Exposed as `window.__colaviewFitSlides`.
15
- - **Refit after hydration** — mermaid and KaTeX change each page's content height, so hydration re-runs the fit.
16
- - **Blank directive-only pages filtered** (from ColaMD-extend `deckPages`) — no more empty paper sheets in presenting and printing.
17
-
18
- ## 🐛 Present (Fullscreen): Browser Opens Reliably on Windows
19
-
20
- Presenting failed with "系统找不到指定的文件": `vscode.env.openExternal` with a `file:` URI goes through Windows ShellExecute and frequently fails. Presenting now opens the file through the platform shell — `start` on Windows, `open` on macOS, `xdg-open` on Linux — with `openExternal` as a fallback.
21
-
22
- ## ⌨️ Keybindings & Localization
23
-
24
- - `Ctrl+Alt+V` opens the preview, `Ctrl+Alt+S` the slides preview (Markdown files only, conflict-free with Save).
25
- - Command titles are localized via `package.nls.json` / `package.nls.zh-cn.json`, following the VS Code display language.
26
-
27
- ---
28
-
29
- **Download**: `vscode-colaview-0.4.1.vsix` — install via `Extensions → Install from VSIX`, or `code --install-extension vscode-colaview-0.4.1.vsix`.