vscode-colaview 0.3.3 → 0.4.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.
- package/.release-notes-v0.4.1.md +29 -0
- package/.vscode/settings.json +1 -1
- package/CHANGELOG.md +45 -16
- package/README.md +318 -287
- package/README_CN.md +318 -287
- package/RELEASE_NOTES.md +80 -21
- package/esbuild.js +2 -0
- package/package.json +204 -164
- package/package.nls.json +14 -0
- package/package.nls.zh-cn.json +14 -0
- package/src/commands/commands.ts +48 -0
- package/src/docx/docx-builder.ts +241 -0
- package/src/export/docx-exporter.ts +961 -961
- package/src/export/html-exporter.ts +8 -2
- package/src/export/pdf-exporter.ts +69 -0
- package/src/export/pptx-exporter.ts +63 -0
- package/src/export/render-export-images.ts +386 -386
- package/src/export/theme-style.ts +195 -195
- package/src/pptx/pptx-builder.ts +249 -0
- package/src/pptx/zip.ts +158 -0
- package/src/present/presenter.ts +89 -0
- package/src/preview/preview-manager.ts +783 -541
- package/src/preview/webview/app.ts +695 -370
- package/src/preview/webview/index.html +167 -146
- package/src/slides/deck.ts +408 -0
- package/src/slides/markdown.ts +420 -0
- package/src/slides/render.ts +580 -0
- package/src/slides/slides.css +1082 -0
- package/src/themes/built-in/bear.css +64 -13
- package/src/themes/built-in/dark.css +64 -5
- package/src/themes/built-in/dracula.css +64 -5
- package/src/themes/built-in/elegant.css +75 -11
- package/src/themes/built-in/gruvbox.css +65 -6
- package/src/themes/built-in/light.css +62 -2
- package/src/themes/built-in/midnight.css +64 -6
- package/src/themes/built-in/nord.css +64 -5
- package/src/themes/built-in/notion.css +63 -11
- package/src/themes/built-in/sepia.css +65 -10
- package/src/themes/built-in/solarized-dark.css +64 -5
- package/src/themes/built-in/writer.css +66 -19
- package/src/themes/theme-manager.ts +207 -180
- package/src/types/colamd-renderer.d.ts +47 -47
- package/src/types/pele-lazy.d.ts +21 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
// 幻灯片分页 — 与 ColaMD-extend / ColaMD-themes 同一套 deck 契约:
|
|
2
|
+
// `---` 结束当前页,页首 `<!-- slide: <layout>` + kicker/label/chapter 字段声明版式,
|
|
3
|
+
// `<!-- deck-brand: … -->` 一类进入整副牌的页眉页脚(chrome)。
|
|
4
|
+
import { parseBlocksLines, plainInline } from './markdown';
|
|
5
|
+
import type { MdBlock, MdListItem } from './markdown';
|
|
6
|
+
|
|
7
|
+
/** 一页幻灯片。 */
|
|
8
|
+
export interface Slide {
|
|
9
|
+
/** 版式名(cover / section / agenda / kpi / timeline / steps / matrix / two-col / figure / quote / closing),空串为默认版式。 */
|
|
10
|
+
layout: string;
|
|
11
|
+
/** 页标题(页首第一个标题行)。 */
|
|
12
|
+
title: string;
|
|
13
|
+
/** 标题上方的小字(页头指令优先,其次取标题前紧邻的段落)。 */
|
|
14
|
+
kicker: string;
|
|
15
|
+
/** 页头右上的 label(`label: …`)。 */
|
|
16
|
+
label: string;
|
|
17
|
+
/** 页头右上的章节名(`chapter: …`)。 */
|
|
18
|
+
chapter: string;
|
|
19
|
+
/** 标题后的第一段,按导语排版(更大、次要色)。 */
|
|
20
|
+
lead: string;
|
|
21
|
+
/** 其余正文块。 */
|
|
22
|
+
blocks: MdBlock[];
|
|
23
|
+
/** 是否为一个 section 的首屏(续页为 false)。 */
|
|
24
|
+
pageStart: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** deck 级页眉页脚(`<!-- deck-brand: … -->` 一类指令)。 */
|
|
28
|
+
export interface DeckChrome {
|
|
29
|
+
logo: string;
|
|
30
|
+
brand: string;
|
|
31
|
+
title: string;
|
|
32
|
+
meta: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const SLIDE_COMMENT_OPEN = /^\s*<!--\s*(?:slide|deck)\b/;
|
|
36
|
+
const PAGE_FIELD_RE = /^([a-z][a-z0-9-]*)\s*:\s*(.+)$/;
|
|
37
|
+
const PAGE_FIELD_KEYS = new Set(['kicker', 'label', 'chapter']);
|
|
38
|
+
const DECK_FIELD_KEYS = new Set(['logo', 'brand', 'title', 'meta']);
|
|
39
|
+
const HR_LINE = /^\s*([-*_])(\s*\1){2,}\s*$/;
|
|
40
|
+
const HEADING_LINE = /^\s*#{1,2}\s+/;
|
|
41
|
+
const YAML_FENCE = /^---\s*$/;
|
|
42
|
+
|
|
43
|
+
/** 去掉 YAML frontmatter(幻灯片源文件常带 title/author)。 */
|
|
44
|
+
function stripFrontmatter(markdown: string): string {
|
|
45
|
+
const lines = markdown.replace(/\r\n?/g, '\n').split('\n');
|
|
46
|
+
if (lines.length > 1 && YAML_FENCE.test(lines[0] ?? '')) {
|
|
47
|
+
for (let i = 1; i < lines.length; i += 1) {
|
|
48
|
+
if (YAML_FENCE.test(lines[i] ?? '')) return lines.slice(i + 1).join('\n');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return lines.join('\n');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 取出一段 HTML 注释的正文;`<!-- slide: cover` 这种跨行块会一直吃到 `-->`。 */
|
|
55
|
+
function takeCommentBlock(lines: readonly string[], start: number): { body: string; next: number } {
|
|
56
|
+
const first = lines[start] ?? '';
|
|
57
|
+
const open = first.indexOf('<!--');
|
|
58
|
+
const rest = open >= 0 ? first.slice(open + 4) : first;
|
|
59
|
+
const close = rest.indexOf('-->');
|
|
60
|
+
if (close >= 0) return { body: rest.slice(0, close), next: start + 1 };
|
|
61
|
+
const parts: string[] = [rest];
|
|
62
|
+
let i = start + 1;
|
|
63
|
+
while (i < lines.length) {
|
|
64
|
+
const line = lines[i] ?? '';
|
|
65
|
+
const end = line.indexOf('-->');
|
|
66
|
+
if (end >= 0) {
|
|
67
|
+
parts.push(line.slice(0, end));
|
|
68
|
+
i += 1;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
parts.push(line);
|
|
72
|
+
i += 1;
|
|
73
|
+
}
|
|
74
|
+
return { body: parts.join('\n'), next: i };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 把 `kicker: 路线图 label: 四章` 这种同行多字段拆成字段行。 */
|
|
78
|
+
function splitFieldLine(text: string): string[] {
|
|
79
|
+
return text
|
|
80
|
+
.split(/\n/)
|
|
81
|
+
.flatMap((line) => line.split(/\s+(?=[a-z][a-z0-9-]*\s*:)/))
|
|
82
|
+
.map((line) => line.trim())
|
|
83
|
+
.filter((line) => line !== '');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 读页头字段(kicker/label/chapter)。 */
|
|
87
|
+
function readPageFields(text: string, target: { kicker: string; label: string; chapter: string }): void {
|
|
88
|
+
for (const line of splitFieldLine(text)) {
|
|
89
|
+
const field = PAGE_FIELD_RE.exec(line);
|
|
90
|
+
if (field === null) continue;
|
|
91
|
+
const key = field[1] ?? '';
|
|
92
|
+
if (!PAGE_FIELD_KEYS.has(key)) continue;
|
|
93
|
+
target[key as 'kicker' | 'label' | 'chapter'] = (field[2] ?? '').trim();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 读 deck 级字段(logo/brand/title/meta)。 */
|
|
98
|
+
function readDeckFields(text: string, chrome: DeckChrome): void {
|
|
99
|
+
for (const line of splitFieldLine(text)) {
|
|
100
|
+
const field = PAGE_FIELD_RE.exec(line);
|
|
101
|
+
if (field === null) continue;
|
|
102
|
+
const key = field[1] ?? '';
|
|
103
|
+
if (!DECK_FIELD_KEYS.has(key)) continue;
|
|
104
|
+
chrome[key as keyof DeckChrome] = (field[2] ?? '').trim();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 解析一页的标题/导语/kicker 角色(与上游 PageRoles 同规则)。 */
|
|
109
|
+
function readRoles(blocks: readonly MdBlock[]): { titleIndex: number; leadIndex: number; kickerIndex: number } {
|
|
110
|
+
let titleIndex = -1;
|
|
111
|
+
for (let i = 0; i < blocks.length; i += 1) {
|
|
112
|
+
const block = blocks[i];
|
|
113
|
+
if (block.kind === 'paragraph' && block.text.trim() === '') continue;
|
|
114
|
+
if (block.kind === 'heading' && block.level <= 4) titleIndex = i;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
let leadIndex = -1;
|
|
118
|
+
if (titleIndex >= 0) {
|
|
119
|
+
for (let i = titleIndex + 1; i < blocks.length; i += 1) {
|
|
120
|
+
const block = blocks[i];
|
|
121
|
+
if (block.kind === 'paragraph' && block.text.trim() === '') continue;
|
|
122
|
+
if (block.kind === 'paragraph') leadIndex = i;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
let kickerIndex = -1;
|
|
127
|
+
if (titleIndex > 0) {
|
|
128
|
+
for (let i = titleIndex - 1; i >= 0; i -= 1) {
|
|
129
|
+
const block = blocks[i];
|
|
130
|
+
if (block.kind === 'paragraph' && block.text.trim() === '') continue;
|
|
131
|
+
if (block.kind === 'paragraph') kickerIndex = i;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { titleIndex, leadIndex, kickerIndex };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 把一页的原始行收成一页幻灯片。 */
|
|
139
|
+
function buildSlide(
|
|
140
|
+
pageLines: readonly string[],
|
|
141
|
+
directives: { layout: string; kicker: string; label: string; chapter: string },
|
|
142
|
+
pageStart: boolean
|
|
143
|
+
): Slide | null {
|
|
144
|
+
const blocks = parseBlocksLines([...pageLines]);
|
|
145
|
+
const { titleIndex, leadIndex, kickerIndex } = readRoles(blocks);
|
|
146
|
+
const heading = titleIndex >= 0 ? blocks[titleIndex] : undefined;
|
|
147
|
+
const title = heading !== undefined && heading.kind === 'heading' ? heading.text.trim() : '';
|
|
148
|
+
const leadBlock = leadIndex >= 0 ? blocks[leadIndex] : undefined;
|
|
149
|
+
const lead = leadBlock !== undefined && leadBlock.kind === 'paragraph' ? leadBlock.text : '';
|
|
150
|
+
const kickerBlock = kickerIndex >= 0 && directives.kicker === '' ? blocks[kickerIndex] : undefined;
|
|
151
|
+
const kicker =
|
|
152
|
+
directives.kicker !== ''
|
|
153
|
+
? directives.kicker
|
|
154
|
+
: kickerBlock !== undefined && kickerBlock.kind === 'paragraph'
|
|
155
|
+
? plainInline(kickerBlock.text)
|
|
156
|
+
: '';
|
|
157
|
+
const body = blocks.filter((_, i) => i !== titleIndex && i !== leadIndex && i !== kickerIndex);
|
|
158
|
+
while (body.length > 0 && body[0]?.kind === 'divider') body.shift();
|
|
159
|
+
while (body.length > 0 && body[body.length - 1]?.kind === 'divider') body.pop();
|
|
160
|
+
if (title === '' && lead === '' && body.length === 0 && kicker === '') return null;
|
|
161
|
+
return {
|
|
162
|
+
layout: directives.layout,
|
|
163
|
+
title,
|
|
164
|
+
kicker,
|
|
165
|
+
label: directives.label,
|
|
166
|
+
chapter: directives.chapter,
|
|
167
|
+
lead,
|
|
168
|
+
blocks: body,
|
|
169
|
+
pageStart,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 把 Markdown 切成幻灯片页。
|
|
175
|
+
*
|
|
176
|
+
* 一个 `---` 都没有的普通文档退回「按 `#`/`##` 标题分页」,避免把长文切碎。
|
|
177
|
+
*/
|
|
178
|
+
export function splitDeck(markdown: string, fallbackTitle = ''): Slide[] {
|
|
179
|
+
const source = stripFrontmatter(markdown);
|
|
180
|
+
const lines = source.split('\n');
|
|
181
|
+
const chrome: DeckChrome = { logo: '', brand: '', title: '', meta: '' };
|
|
182
|
+
|
|
183
|
+
type Page = { lines: string[]; layout: string; kicker: string; label: string; chapter: string; hasContent: boolean };
|
|
184
|
+
const pages: Page[] = [];
|
|
185
|
+
let page: Page = { lines: [], layout: '', kicker: '', label: '', chapter: '', hasContent: false };
|
|
186
|
+
const flush = (): void => {
|
|
187
|
+
if (page.hasContent || page.layout !== '' || page.kicker !== '' || page.label !== '' || page.chapter !== '') pages.push(page);
|
|
188
|
+
page = { lines: [], layout: '', kicker: '', label: '', chapter: '', hasContent: false };
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
let sawRule = false;
|
|
192
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
193
|
+
const line = lines[i] ?? '';
|
|
194
|
+
if (SLIDE_COMMENT_OPEN.test(line)) {
|
|
195
|
+
const { body, next } = takeCommentBlock(lines, i);
|
|
196
|
+
i = next - 1;
|
|
197
|
+
const text = body.trim();
|
|
198
|
+
if (/^slide-(css|js)\b/.test(text)) continue;
|
|
199
|
+
// `<!-- deck-brand: X -->` 与跨行的 `<!-- deck-brand: X` + `deck-meta: Y -->` 都归到 deck 字段。
|
|
200
|
+
if (/^deck\b/.test(text)) {
|
|
201
|
+
const normalized = text
|
|
202
|
+
.split('\n')
|
|
203
|
+
.map((entry) => entry.trim().replace(/^deck-/, '').replace(/^deck\s+/, ''))
|
|
204
|
+
.join('\n');
|
|
205
|
+
readDeckFields(normalized, chrome);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const layout = /^slide\s*:\s*([a-z][a-z0-9-]*)\b([\s\S]*)$/.exec(text);
|
|
209
|
+
if (layout !== null) {
|
|
210
|
+
// 页头指令只在「本页还没出现正文」时生效。
|
|
211
|
+
if (!page.hasContent) {
|
|
212
|
+
page.layout = layout[1] ?? '';
|
|
213
|
+
readPageFields(layout[2] ?? '', page);
|
|
214
|
+
}
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
const keyed = /^slide-(kicker|label|chapter)\s*:\s*([\s\S]*)$/.exec(text);
|
|
218
|
+
if (keyed !== null) {
|
|
219
|
+
if (!page.hasContent) {
|
|
220
|
+
const key = keyed[1] as 'kicker' | 'label' | 'chapter';
|
|
221
|
+
page[key] = (keyed[2] ?? '').trim();
|
|
222
|
+
}
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
// 未知指令:整段吞掉,绝不能让 `<!-- … -->` 变成正文。
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (HR_LINE.test(line)) {
|
|
229
|
+
sawRule = true;
|
|
230
|
+
flush();
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (line.trim() !== '') page.hasContent = true;
|
|
234
|
+
page.lines.push(line);
|
|
235
|
+
}
|
|
236
|
+
flush();
|
|
237
|
+
|
|
238
|
+
const slides: Slide[] = [];
|
|
239
|
+
// 单页文档也可能带页头指令(`<!-- slide: kpi -->`),有指令就走页路径。
|
|
240
|
+
const hasDirectives = pages.some((entry) => entry.layout !== '' || entry.kicker !== '' || entry.label !== '' || entry.chapter !== '');
|
|
241
|
+
if (sawRule || pages.length > 1 || hasDirectives) {
|
|
242
|
+
for (const entry of pages) {
|
|
243
|
+
const slide = buildSlide(entry.lines, entry, true);
|
|
244
|
+
if (slide !== null) slides.push(slide);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (slides.length === 0) {
|
|
248
|
+
// 没有分页符的普通文档:按 `#`/`##` 标题分页。
|
|
249
|
+
const hasHeading = lines.some((line) => HEADING_LINE.test(line));
|
|
250
|
+
const chunks: string[][] = [];
|
|
251
|
+
let buffer: string[] = [];
|
|
252
|
+
for (const line of lines) {
|
|
253
|
+
if (SLIDE_COMMENT_OPEN.test(line)) continue;
|
|
254
|
+
if (hasHeading && HEADING_LINE.test(line) && buffer.some((entry) => entry.trim() !== '')) {
|
|
255
|
+
chunks.push(buffer);
|
|
256
|
+
buffer = [];
|
|
257
|
+
}
|
|
258
|
+
buffer.push(line);
|
|
259
|
+
}
|
|
260
|
+
if (buffer.some((line) => line.trim() !== '')) chunks.push(buffer);
|
|
261
|
+
for (const chunk of chunks) {
|
|
262
|
+
const slide = buildSlide(chunk, { layout: '', kicker: '', label: '', chapter: '' }, true);
|
|
263
|
+
if (slide !== null) slides.push(slide);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (slides.length === 0) {
|
|
268
|
+
slides.push({ layout: 'cover', title: fallbackTitle, kicker: '', label: '', chapter: '', lead: '', blocks: [], pageStart: true });
|
|
269
|
+
}
|
|
270
|
+
const first = slides[0];
|
|
271
|
+
if (first !== undefined && first.title === '' && fallbackTitle !== '' && first.blocks.length === 0) {
|
|
272
|
+
slides[0] = { ...first, title: fallbackTitle };
|
|
273
|
+
}
|
|
274
|
+
deckChromeCache.set(slides, chrome);
|
|
275
|
+
return slides;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** deck 级页眉页脚只解析一次,用 WeakMap 挂在返回值上。 */
|
|
279
|
+
const deckChromeCache = new WeakMap<readonly Slide[], DeckChrome>();
|
|
280
|
+
|
|
281
|
+
/** 取该 deck 的页眉页脚信息。 */
|
|
282
|
+
export function deckChrome(slides: readonly Slide[]): DeckChrome {
|
|
283
|
+
return deckChromeCache.get(slides) ?? { logo: '', brand: '', title: '', meta: '' };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** 逐页排版:超出一页预算的内容自动续页(续页沿用标题)。 */
|
|
287
|
+
export function paginateDeck(slides: readonly Slide[], budget = 820): Slide[] {
|
|
288
|
+
const pages: Slide[] = [];
|
|
289
|
+
for (const slide of slides) {
|
|
290
|
+
// 过滤纯指令/空白页(与上游 deckPages 一致):无标题、lead、kicker 与
|
|
291
|
+
// 正文的页会在放映与打印里产出空白纸面。
|
|
292
|
+
if (slide.blocks.length === 0 && slide.title === '' && slide.lead === '' && slide.kicker === '') {
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const weight = blockWeights(slide.blocks);
|
|
296
|
+
if (weight <= budget) {
|
|
297
|
+
pages.push({ ...slide, pageStart: true });
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
let current: MdBlock[] = [];
|
|
301
|
+
let used = slide.title === '' ? 0 : 60;
|
|
302
|
+
let pageIndex = 0;
|
|
303
|
+
const flush = (): void => {
|
|
304
|
+
if (current.length === 0) return;
|
|
305
|
+
pages.push({
|
|
306
|
+
...slide,
|
|
307
|
+
title: pageIndex === 0 ? slide.title : '',
|
|
308
|
+
lead: pageIndex === 0 ? slide.lead : '',
|
|
309
|
+
blocks: current,
|
|
310
|
+
pageStart: pageIndex === 0,
|
|
311
|
+
});
|
|
312
|
+
pageIndex += 1;
|
|
313
|
+
current = [];
|
|
314
|
+
used = 0;
|
|
315
|
+
};
|
|
316
|
+
for (const block of slide.blocks) {
|
|
317
|
+
const size = blockWeight(block);
|
|
318
|
+
if (used + size > budget && current.length > 0) flush();
|
|
319
|
+
if (size > budget) {
|
|
320
|
+
for (const part of splitBlock(block, budget)) {
|
|
321
|
+
if (used + blockWeight(part) > budget && current.length > 0) flush();
|
|
322
|
+
current.push(part);
|
|
323
|
+
used += blockWeight(part);
|
|
324
|
+
}
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
current.push(block);
|
|
328
|
+
used += size;
|
|
329
|
+
}
|
|
330
|
+
flush();
|
|
331
|
+
}
|
|
332
|
+
return pages;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function blockWeight(block: MdBlock): number {
|
|
336
|
+
if (block.kind === 'heading') return block.text.length + 30;
|
|
337
|
+
if (block.kind === 'paragraph') return block.text.length + 24;
|
|
338
|
+
if (block.kind === 'quote') return block.text.length + 30;
|
|
339
|
+
if (block.kind === 'code') return block.text.split('\n').length * 34 + 40;
|
|
340
|
+
if (block.kind === 'math') return block.text.split('\n').length * 40 + 120;
|
|
341
|
+
if (block.kind === 'table') return (block.rows.length + 1) * 34 + 40;
|
|
342
|
+
if (block.kind === 'html') return 320;
|
|
343
|
+
if (block.kind === 'divider') return 30;
|
|
344
|
+
return block.items.reduce((sum: number, item: MdListItem) => sum + item.text.length + 20, 0) + 30;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function blockWeights(blocks: readonly MdBlock[]): number {
|
|
348
|
+
return blocks.reduce((sum, block) => sum + blockWeight(block), 0);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function splitBlock(block: MdBlock, budget: number): MdBlock[] {
|
|
352
|
+
// mermaid 围栏不可按行切分:切开的每一段都是非法源码,水合时会报
|
|
353
|
+
// "Syntax error in text"。整块保留,超预算时独占一页。
|
|
354
|
+
if (block.kind === 'code' && block.lang.toLowerCase() === 'mermaid') {
|
|
355
|
+
return [block];
|
|
356
|
+
}
|
|
357
|
+
if (block.kind === 'code') {
|
|
358
|
+
const lines = block.text.split('\n');
|
|
359
|
+
const chunks: MdBlock[] = [];
|
|
360
|
+
let current: string[] = [];
|
|
361
|
+
let used = 40;
|
|
362
|
+
for (const line of lines) {
|
|
363
|
+
if (used + 34 > budget && current.length > 0) {
|
|
364
|
+
chunks.push({ ...block, text: current.join('\n') });
|
|
365
|
+
current = [];
|
|
366
|
+
used = 40;
|
|
367
|
+
}
|
|
368
|
+
current.push(line);
|
|
369
|
+
used += 34;
|
|
370
|
+
}
|
|
371
|
+
if (current.length > 0) chunks.push({ ...block, text: current.join('\n') });
|
|
372
|
+
return chunks;
|
|
373
|
+
}
|
|
374
|
+
if (block.kind === 'table') {
|
|
375
|
+
const rows = block.rows;
|
|
376
|
+
const chunks: MdBlock[] = [];
|
|
377
|
+
let current: string[][] = [];
|
|
378
|
+
let used = 74;
|
|
379
|
+
for (const row of rows) {
|
|
380
|
+
if (used + 34 > budget && current.length > 0) {
|
|
381
|
+
chunks.push({ ...block, rows: current });
|
|
382
|
+
current = [];
|
|
383
|
+
used = 74;
|
|
384
|
+
}
|
|
385
|
+
current.push(row);
|
|
386
|
+
used += 34;
|
|
387
|
+
}
|
|
388
|
+
if (current.length > 0) chunks.push({ ...block, rows: current });
|
|
389
|
+
return chunks;
|
|
390
|
+
}
|
|
391
|
+
if (block.kind === 'list') {
|
|
392
|
+
const chunks: MdBlock[] = [];
|
|
393
|
+
let current: MdListItem[] = [];
|
|
394
|
+
let used = 30;
|
|
395
|
+
for (const item of block.items) {
|
|
396
|
+
if (used + item.text.length + 20 > budget && current.length > 0) {
|
|
397
|
+
chunks.push({ ...block, items: current });
|
|
398
|
+
current = [];
|
|
399
|
+
used = 30;
|
|
400
|
+
}
|
|
401
|
+
current.push(item);
|
|
402
|
+
used += item.text.length + 20;
|
|
403
|
+
}
|
|
404
|
+
if (current.length > 0) chunks.push({ ...block, items: current });
|
|
405
|
+
return chunks;
|
|
406
|
+
}
|
|
407
|
+
return [block];
|
|
408
|
+
}
|