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,420 @@
|
|
|
1
|
+
// 轻量 Markdown 渲染器 — 幻灯片预览/放映/导出共用(不依赖 Milkdown 编辑器)
|
|
2
|
+
// 只覆盖常用语法,产出语义化 HTML;与文档预览的 Milkdown 渲染相互独立。
|
|
3
|
+
|
|
4
|
+
const ESCAPE_MAP: Record<string, string> = {
|
|
5
|
+
'&': '&',
|
|
6
|
+
'<': '<',
|
|
7
|
+
'>': '>',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function escapeHtml(value: string): string {
|
|
11
|
+
return value.replace(/[&<>]/g, (ch) => ESCAPE_MAP[ch] ?? ch);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function escapeAttr(value: string): string {
|
|
15
|
+
return value.replace(/[&<>"]/g, (ch) => (ch === '"' ? '"' : ESCAPE_MAP[ch] ?? ch));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 行内语法:代码 span、图片、链接、粗体、斜体、删除线、高亮。 */
|
|
19
|
+
export function renderInline(text: string): string {
|
|
20
|
+
const codes: string[] = [];
|
|
21
|
+
let out = escapeHtml(text);
|
|
22
|
+
out = out.replace(/`([^`]+)`/g, (_all, code: string) => {
|
|
23
|
+
codes.push(code);
|
|
24
|
+
return `\u0000${codes.length - 1}\u0000`;
|
|
25
|
+
});
|
|
26
|
+
out = out.replace(
|
|
27
|
+
/!\[([^\]]*)\]\(([^)\s]+?)(?:\s+"([^"]*)")?\)/g,
|
|
28
|
+
(_all, alt: string, src: string, title: string | undefined) =>
|
|
29
|
+
`<img src="${escapeAttr(src)}" alt="${escapeAttr(alt)}"${title ? ` title="${escapeAttr(title)}"` : ''}>`
|
|
30
|
+
);
|
|
31
|
+
out = out.replace(
|
|
32
|
+
/\[([^\]]+)\]\(([^)\s]+?)(?:\s+"([^"]*)")?\)/g,
|
|
33
|
+
(_all, label: string, href: string, title: string | undefined) =>
|
|
34
|
+
`<a href="${escapeAttr(href)}"${title ? ` title="${escapeAttr(title)}"` : ''}>${label}</a>`
|
|
35
|
+
);
|
|
36
|
+
out = out.replace(/(\*\*|__)(?=\S)([\s\S]*?\S)\1/g, '<strong>$2</strong>');
|
|
37
|
+
// 边界除了 ASCII 空白/标点,还要允许中文、全角标点紧邻,否则中文里的斜体会漏成字面量星号。
|
|
38
|
+
out = out.replace(
|
|
39
|
+
/(^|[\s((\[「]|[^\x00-\x7f])(\*|_)(?=\S)([\s\S]*?\S)\2(?=$|[\s))\]」.,!?;:,。!?;:、]|[^\x00-\x7f])/g,
|
|
40
|
+
'$1<em>$3</em>'
|
|
41
|
+
);
|
|
42
|
+
out = out.replace(/~~(?=\S)([\s\S]*?\S)~~/g, '<del>$1</del>');
|
|
43
|
+
out = out.replace(/==(?=\S)([\s\S]*?\S)==/g, '<mark>$1</mark>');
|
|
44
|
+
out = out.replace(/\u0000(\d+)\u0000/g, (_all, index: string) => `<code>${codes[Number(index)] ?? ''}</code>`);
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 去掉行内 Markdown 标记,得到导出用的纯文本。 */
|
|
49
|
+
export function plainInline(text: string): string {
|
|
50
|
+
return text
|
|
51
|
+
.replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
52
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
53
|
+
.replace(/`([^`]+)`/g, '$1')
|
|
54
|
+
.replace(/\*\*([^*]+)\*\*/g, '$1')
|
|
55
|
+
.replace(/__([^_]+)__/g, '$1')
|
|
56
|
+
.replace(/(^|[^\w*])\*([^*]+)\*/g, '$1$2')
|
|
57
|
+
.replace(/~~([^~]+)~~/g, '$1')
|
|
58
|
+
.replace(/==([^=]+)==/g, '$1')
|
|
59
|
+
.trim();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface ListNode {
|
|
63
|
+
text: string;
|
|
64
|
+
ordered: boolean;
|
|
65
|
+
task: boolean | null;
|
|
66
|
+
children: ListNode[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function paintList(nodes: ListNode[]): string {
|
|
70
|
+
if (nodes.length === 0) return '';
|
|
71
|
+
const tag = nodes[0]?.ordered === true ? 'ol' : 'ul';
|
|
72
|
+
const body = nodes
|
|
73
|
+
.map((node) => {
|
|
74
|
+
if (node.task === null) return `<li>${renderInline(node.text)}${paintList(node.children)}</li>`;
|
|
75
|
+
return (
|
|
76
|
+
`<li class="task-item"><input type="checkbox" disabled${node.task ? ' checked' : ''}> ` +
|
|
77
|
+
`${renderInline(node.text)}${paintList(node.children)}</li>`
|
|
78
|
+
);
|
|
79
|
+
})
|
|
80
|
+
.join('');
|
|
81
|
+
return `<${tag}>${body}</${tag}>`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 判断一行是否开启新的块级元素(段落收集到此为止)。 */
|
|
85
|
+
function startsBlock(line: string): boolean {
|
|
86
|
+
return (
|
|
87
|
+
/^\s*$/.test(line) ||
|
|
88
|
+
/^\s*(#{1,6})\s+/.test(line) ||
|
|
89
|
+
/^\s*(```|~~~)/.test(line) ||
|
|
90
|
+
/^\s*>/.test(line) ||
|
|
91
|
+
/^\s*([-*+]|\d+[.)])\s+/.test(line) ||
|
|
92
|
+
/^\s*([-*_])\s*\1\s*\1[\s\-*_]*$/.test(line) ||
|
|
93
|
+
/^\s*<[a-zA-Z!]/.test(line)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 列表项:保留缩进深度、是否有序与任务框状态。 */
|
|
98
|
+
export interface MdListItem {
|
|
99
|
+
text: string;
|
|
100
|
+
/** 0 为顶层,逐级 +1。 */
|
|
101
|
+
depth: number;
|
|
102
|
+
ordered: boolean;
|
|
103
|
+
/** `true` / `false` 表示任务框勾选态,`null` 表示普通条目。 */
|
|
104
|
+
task: boolean | null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 块级 AST:幻灯片渲染与 PPTX 导出共用同一份解析结果。 */
|
|
108
|
+
export type MdBlock =
|
|
109
|
+
| { kind: 'heading'; level: 1 | 2 | 3 | 4 | 5 | 6; text: string }
|
|
110
|
+
| { kind: 'paragraph'; text: string }
|
|
111
|
+
| { kind: 'code'; text: string; lang: string }
|
|
112
|
+
/** 块级公式:`$$ ... $$`,交给 WebView 的 KaTeX 渲染。 */
|
|
113
|
+
| { kind: 'math'; text: string }
|
|
114
|
+
| { kind: 'quote'; text: string }
|
|
115
|
+
| { kind: 'list'; ordered: boolean; items: MdListItem[] }
|
|
116
|
+
| { kind: 'table'; header: string[]; aligns: string[]; rows: string[][] }
|
|
117
|
+
/** 块级 HTML(内联 SVG 图、figure 容器等):原样透传,不转义。 */
|
|
118
|
+
| { kind: 'html'; text: string }
|
|
119
|
+
| { kind: 'divider' };
|
|
120
|
+
|
|
121
|
+
const LIST_MARKER = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/;
|
|
122
|
+
const TASK_MARKER = /^\[([ xX])\]\s+(.*)$/;
|
|
123
|
+
|
|
124
|
+
/** 行数组 → 块级 AST。 */
|
|
125
|
+
export function parseBlocksLines(lines: string[]): MdBlock[] {
|
|
126
|
+
const out: MdBlock[] = [];
|
|
127
|
+
let index = 0;
|
|
128
|
+
|
|
129
|
+
while (index < lines.length) {
|
|
130
|
+
const line = lines[index] ?? '';
|
|
131
|
+
if (/^\s*$/.test(line)) {
|
|
132
|
+
index += 1;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 围栏标记捕获整串重复字符(``` 与 ```` 各自成围栏):嵌套围栏(4 反引号包
|
|
137
|
+
// 3 反引号)只有同长的标记才能关。
|
|
138
|
+
const fence = /^\s*(`{3,}|~{3,})\s*([\w+#.-]*)\s*$/.exec(line);
|
|
139
|
+
if (fence !== null) {
|
|
140
|
+
const marker = fence[1] ?? '```';
|
|
141
|
+
const language = fence[2] ?? '';
|
|
142
|
+
const buffer: string[] = [];
|
|
143
|
+
index += 1;
|
|
144
|
+
while (index < lines.length && !(lines[index] ?? '').trimStart().startsWith(marker)) {
|
|
145
|
+
buffer.push(lines[index] ?? '');
|
|
146
|
+
index += 1;
|
|
147
|
+
}
|
|
148
|
+
index += 1;
|
|
149
|
+
out.push({ kind: 'code', text: buffer.join('\n'), lang: language });
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// 块级公式:`$$` 独占一行开合,或 `$$…$$` 单行成块。
|
|
154
|
+
const mathInline = /^\s*\$\$(.+)\$\$\s*$/.exec(line);
|
|
155
|
+
if (mathInline !== null) {
|
|
156
|
+
out.push({ kind: 'math', text: mathInline[1] ?? '' });
|
|
157
|
+
index += 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (/^\s*\$\$\s*$/.test(line)) {
|
|
161
|
+
const buffer: string[] = [];
|
|
162
|
+
index += 1;
|
|
163
|
+
while (index < lines.length && !/^\s*\$\$\s*$/.test(lines[index] ?? '')) {
|
|
164
|
+
buffer.push(lines[index] ?? '');
|
|
165
|
+
index += 1;
|
|
166
|
+
}
|
|
167
|
+
index += 1;
|
|
168
|
+
out.push({ kind: 'math', text: buffer.join('\n') });
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const heading = /^\s*(#{1,6})\s+(.*?)\s*#*\s*$/.exec(line);
|
|
173
|
+
if (heading !== null) {
|
|
174
|
+
const level = (heading[1] ?? '#').length as 1 | 2 | 3 | 4 | 5 | 6;
|
|
175
|
+
out.push({ kind: 'heading', level, text: heading[2] ?? '' });
|
|
176
|
+
index += 1;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)) {
|
|
181
|
+
out.push({ kind: 'divider' });
|
|
182
|
+
index += 1;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (/^\s*>/.test(line)) {
|
|
187
|
+
const quoted: string[] = [];
|
|
188
|
+
while (index < lines.length && /^\s*>/.test(lines[index] ?? '')) {
|
|
189
|
+
quoted.push((lines[index] ?? '').replace(/^\s*>\s?/, ''));
|
|
190
|
+
index += 1;
|
|
191
|
+
}
|
|
192
|
+
const inner = parseBlocksLines(quoted);
|
|
193
|
+
out.push({ kind: 'quote', text: inner.map((block) => blockPlainText(block)).join('\n') });
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// 表格:表头 + 分隔行
|
|
198
|
+
const next = lines[index + 1] ?? '';
|
|
199
|
+
if (line.includes('|') && /^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(next) && next.includes('-')) {
|
|
200
|
+
const cells = (row: string): string[] =>
|
|
201
|
+
row
|
|
202
|
+
.trim()
|
|
203
|
+
.replace(/^\|/, '')
|
|
204
|
+
.replace(/\|$/, '')
|
|
205
|
+
.split('|')
|
|
206
|
+
.map((cell) => cell.trim());
|
|
207
|
+
const header = cells(line);
|
|
208
|
+
const aligns = cells(next).map((cell) => {
|
|
209
|
+
const left = cell.startsWith(':');
|
|
210
|
+
const right = cell.endsWith(':');
|
|
211
|
+
if (left && right) return 'center';
|
|
212
|
+
if (right) return 'right';
|
|
213
|
+
if (left) return 'left';
|
|
214
|
+
return '';
|
|
215
|
+
});
|
|
216
|
+
index += 2;
|
|
217
|
+
const rows: string[][] = [];
|
|
218
|
+
while (index < lines.length && (lines[index] ?? '').includes('|') && !/^\s*$/.test(lines[index] ?? '')) {
|
|
219
|
+
rows.push(cells(lines[index] ?? ''));
|
|
220
|
+
index += 1;
|
|
221
|
+
}
|
|
222
|
+
out.push({ kind: 'table', header, aligns, rows });
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// 块级 HTML:以 `<` 开头的行到空行为止原样透传(内联 SVG、figure 容器等)。
|
|
227
|
+
if (/^\s*<[a-zA-Z!]/.test(line)) {
|
|
228
|
+
const buffer: string[] = [];
|
|
229
|
+
while (index < lines.length && !/^\s*$/.test(lines[index] ?? '')) {
|
|
230
|
+
buffer.push(lines[index] ?? '');
|
|
231
|
+
index += 1;
|
|
232
|
+
}
|
|
233
|
+
out.push({ kind: 'html', text: buffer.join('\n') });
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const listItem = LIST_MARKER.exec(line);
|
|
238
|
+
if (listItem !== null) {
|
|
239
|
+
const items: MdListItem[] = [];
|
|
240
|
+
let ordered = /\d/.test(listItem[2] ?? '');
|
|
241
|
+
while (index < lines.length) {
|
|
242
|
+
const current = lines[index] ?? '';
|
|
243
|
+
const match = LIST_MARKER.exec(current);
|
|
244
|
+
if (match !== null) {
|
|
245
|
+
ordered = ordered || /\d/.test(match[2] ?? '');
|
|
246
|
+
const depth = Math.floor((match[1] ?? '').replace(/\t/g, ' ').length / 2);
|
|
247
|
+
const raw = match[3] ?? '';
|
|
248
|
+
const task = TASK_MARKER.exec(raw);
|
|
249
|
+
items.push({
|
|
250
|
+
text: task ? task[2] ?? '' : raw,
|
|
251
|
+
depth,
|
|
252
|
+
ordered: /\d/.test(match[2] ?? ''),
|
|
253
|
+
task: task ? task[1] !== ' ' : null,
|
|
254
|
+
});
|
|
255
|
+
index += 1;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (/^\s{2,}\S/.test(current) && !/^\s*$/.test(current)) {
|
|
259
|
+
const previous = items[items.length - 1];
|
|
260
|
+
if (previous !== undefined) {
|
|
261
|
+
previous.text = `${previous.text} ${current.trim()}`;
|
|
262
|
+
}
|
|
263
|
+
index += 1;
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
out.push({ kind: 'list', ordered, items });
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// 段落:至少消费当前一行,避免外层循环原地打转。
|
|
273
|
+
const paragraph: string[] = [];
|
|
274
|
+
do {
|
|
275
|
+
paragraph.push((lines[index] ?? '').trim());
|
|
276
|
+
index += 1;
|
|
277
|
+
} while (index < lines.length && !startsBlock(lines[index] ?? ''));
|
|
278
|
+
out.push({ kind: 'paragraph', text: paragraph.join(' ') });
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return out;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 去掉 HTML 注释(含跨行),但代码块里的原样保留。
|
|
286
|
+
* 幻灯片指令 `<!-- slide: cover -->` 在渲染层不需要出现,注释一律摘除。
|
|
287
|
+
*/
|
|
288
|
+
function stripHtmlComments(lines: readonly string[]): string[] {
|
|
289
|
+
const out: string[] = [];
|
|
290
|
+
let inFence = false;
|
|
291
|
+
let inComment = false;
|
|
292
|
+
for (const line of lines) {
|
|
293
|
+
if (/^\s*(```|~~~)/.test(line)) {
|
|
294
|
+
inFence = !inFence;
|
|
295
|
+
out.push(line);
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (inFence) {
|
|
299
|
+
out.push(line);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
let rest = line;
|
|
303
|
+
let kept = '';
|
|
304
|
+
while (rest !== '') {
|
|
305
|
+
if (inComment) {
|
|
306
|
+
const close = rest.indexOf('-->');
|
|
307
|
+
if (close < 0) {
|
|
308
|
+
rest = '';
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
rest = rest.slice(close + 3);
|
|
312
|
+
inComment = false;
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
const open = rest.indexOf('<!--');
|
|
316
|
+
if (open < 0) {
|
|
317
|
+
kept += rest;
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
kept += rest.slice(0, open);
|
|
321
|
+
rest = rest.slice(open + 4);
|
|
322
|
+
inComment = true;
|
|
323
|
+
}
|
|
324
|
+
out.push(kept);
|
|
325
|
+
}
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Markdown → 块级 AST。 */
|
|
330
|
+
export function parseBlocks(markdown: string): MdBlock[] {
|
|
331
|
+
const raw = markdown.replace(/\r\n?/g, '\n').split('\n');
|
|
332
|
+
return parseBlocksLines(stripHtmlComments(raw));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** 取块的纯文本(去掉行内标记),供引用块与导出复用。 */
|
|
336
|
+
export function blockPlainText(block: MdBlock): string {
|
|
337
|
+
if (block.kind === 'heading' || block.kind === 'paragraph') return plainInline(block.text);
|
|
338
|
+
if (block.kind === 'code') return block.text;
|
|
339
|
+
if (block.kind === 'math') return block.text;
|
|
340
|
+
if (block.kind === 'quote') return block.text;
|
|
341
|
+
if (block.kind === 'list') return block.items.map((item) => plainInline(item.text)).join('\n');
|
|
342
|
+
if (block.kind === 'table') return [block.header, ...block.rows].map((row) => row.join(' | ')).join('\n');
|
|
343
|
+
return '';
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** 列表条目 → 嵌套 `<ul>/<ol>`(按 depth 建树后交给 paintList)。 */
|
|
347
|
+
function renderListBlock(block: { ordered: boolean; items: MdListItem[] }): string {
|
|
348
|
+
const roots: ListNode[] = [];
|
|
349
|
+
const stack: { node: ListNode; depth: number }[] = [];
|
|
350
|
+
for (const item of block.items) {
|
|
351
|
+
const node: ListNode = { text: item.text, ordered: item.ordered, task: item.task, children: [] };
|
|
352
|
+
while (stack.length > 0 && (stack[stack.length - 1]?.depth ?? -1) >= item.depth) {
|
|
353
|
+
stack.pop();
|
|
354
|
+
}
|
|
355
|
+
const parent = stack[stack.length - 1];
|
|
356
|
+
if (parent === undefined) roots.push(node);
|
|
357
|
+
else parent.node.children.push(node);
|
|
358
|
+
stack.push({ node, depth: item.depth });
|
|
359
|
+
}
|
|
360
|
+
return paintList(roots);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** 块级 AST → HTML。 */
|
|
364
|
+
export function renderBlocksHtml(blocks: readonly MdBlock[]): string {
|
|
365
|
+
const out: string[] = [];
|
|
366
|
+
for (const block of blocks) {
|
|
367
|
+
if (block.kind === 'heading') {
|
|
368
|
+
out.push(`<h${block.level}>${renderInline(block.text)}</h${block.level}>`);
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
if (block.kind === 'paragraph') {
|
|
372
|
+
out.push(`<p>${renderInline(block.text)}</p>`);
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (block.kind === 'code') {
|
|
376
|
+
const cls = block.lang ? ` class="language-${escapeAttr(block.lang)}"` : '';
|
|
377
|
+
out.push(`<pre><code${cls}>${escapeHtml(block.text)}</code></pre>`);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (block.kind === 'html') {
|
|
381
|
+
out.push(`<div class="html-block">\n${block.text}\n</div>`);
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (block.kind === 'quote') {
|
|
385
|
+
out.push(`<blockquote>\n${renderBlocksHtml(parseBlocks(block.text))}\n</blockquote>`);
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
if (block.kind === 'divider') {
|
|
389
|
+
out.push('<hr>');
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (block.kind === 'list') {
|
|
393
|
+
out.push(renderListBlock(block));
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
if (block.kind === 'math') {
|
|
397
|
+
// 语义 HTML 场景没有 KaTeX 运行时,保留 $$ 源码(webview / 放映端可再水合)。
|
|
398
|
+
out.push(`<div class="math-block">\n$$${escapeHtml(block.text)}$$\n</div>`);
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
const th = block.header
|
|
402
|
+
.map((cell, i) => `<th${block.aligns[i] ? ` style="text-align:${block.aligns[i]}"` : ''}>${renderInline(cell)}</th>`)
|
|
403
|
+
.join('');
|
|
404
|
+
const body = block.rows
|
|
405
|
+
.map(
|
|
406
|
+
(row) =>
|
|
407
|
+
`<tr>${row
|
|
408
|
+
.map((cell, i) => `<td${block.aligns[i] ? ` style="text-align:${block.aligns[i]}"` : ''}>${renderInline(cell)}</td>`)
|
|
409
|
+
.join('')}</tr>`
|
|
410
|
+
)
|
|
411
|
+
.join('\n');
|
|
412
|
+
out.push(`<table>\n<thead><tr>${th}</tr></thead>\n<tbody>\n${body}\n</tbody>\n</table>`);
|
|
413
|
+
}
|
|
414
|
+
return out.join('\n');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Markdown → 语义化 HTML(不含 `<html>` 外壳)。 */
|
|
418
|
+
export function renderMarkdown(markdown: string): string {
|
|
419
|
+
return renderBlocksHtml(parseBlocks(markdown));
|
|
420
|
+
}
|