vscode-colaview 0.3.3 → 0.4.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.
- package/.vscode/settings.json +1 -1
- package/CHANGELOG.md +20 -4
- package/README.md +310 -286
- package/README_CN.md +310 -286
- package/RELEASE_NOTES.md +36 -4
- package/esbuild.js +2 -0
- package/package.json +58 -19
- 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 +65 -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 +64 -0
- package/src/preview/preview-manager.ts +783 -541
- package/src/preview/webview/app.ts +588 -370
- package/src/preview/webview/index.html +167 -146
- package/src/slides/deck.ts +397 -0
- package/src/slides/markdown.ts +393 -0
- package/src/slides/render.ts +484 -0
- package/src/slides/slides.css +1082 -0
- package/src/themes/built-in/bear.css +89 -38
- package/src/themes/built-in/dark.css +87 -28
- package/src/themes/built-in/dracula.css +93 -34
- package/src/themes/built-in/elegant.css +109 -45
- package/src/themes/built-in/gruvbox.css +97 -38
- package/src/themes/built-in/light.css +80 -20
- package/src/themes/built-in/midnight.css +93 -35
- package/src/themes/built-in/nord.css +93 -34
- package/src/themes/built-in/notion.css +98 -46
- package/src/themes/built-in/sepia.css +99 -44
- package/src/themes/built-in/solarized-dark.css +94 -35
- package/src/themes/built-in/writer.css +99 -52
- package/src/themes/theme-manager.ts +207 -180
- package/src/types/colamd-renderer.d.ts +47 -47
package/RELEASE_NOTES.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# Release Notes
|
|
2
2
|
|
|
3
|
-
## v0.
|
|
3
|
+
## v0.4.0 (2026-12-01)
|
|
4
4
|
|
|
5
|
-
### 🔧 Engine Upgrade — ColaMD 2.
|
|
5
|
+
### 🔧 Engine Upgrade — ColaMD 2.8.2, No More Patches
|
|
6
6
|
|
|
7
|
-
`@bytechain.cn/colamd` bumped from 2.7.
|
|
7
|
+
`@bytechain.cn/colamd` bumped from 2.7.11 to 2.8.2. The CM6 multi-line replace-decoration fix (`block: true` for whole-line replace decorations — previously carried as a `patch-package` patch) is now native to the engine, extended upstream to tables and HTML blocks as well. The `patches/` directory is gone; `patch-package` remains wired through `postinstall` but finds nothing to apply.
|
|
8
8
|
|
|
9
9
|
### 🎨 Mermaid Diagrams Drawn on the Page
|
|
10
10
|
|
|
11
11
|
Diagrams used to sit on the theme's code-block background with colors derived from `--code-block-bg`/`--code-block-text`. But a mermaid fence is a whole-block replacement — there is no code block behind it — so Elegant and Bear (light pages with dark code blocks) rendered pale lines on the dark block, washed out against the page.
|
|
12
12
|
|
|
13
|
-
Preview and HTML/PDF exports now derive the `--pele-*` tokens from the page colors (`--bg-color`/`--text-color`, mixed at 18%/28%/45% — the same ratios as the engine) and no longer paint a background behind the diagram, matching ColaMD-extend
|
|
13
|
+
Preview and HTML/PDF exports now derive the `--pele-*` tokens from the page colors (`--bg-color`/`--text-color`, mixed at 18%/28%/45% — the same ratios as the engine) and no longer paint a background behind the diagram, matching ColaMD-extend. Light themes get dark lines on paper; dark themes get light lines on the dark page.
|
|
14
14
|
|
|
15
15
|
### 🐛 Theme Variables Actually Apply Now
|
|
16
16
|
|
|
@@ -24,6 +24,38 @@ The 12 theme files are byte-identical with ColaMD-extend's (the 2.7.12+ theme co
|
|
|
24
24
|
|
|
25
25
|
Two rules mirrored from ColaMD-extend's `docx-style.ts`: dark themes fall back entirely to the light palette (light text on white paper is invisible), and light themes with a dark code block (Elegant/Bear) draw diagrams on white with the body text color instead of pale code-block text.
|
|
26
26
|
|
|
27
|
+
### 🧭 Menus Simplified, Theme Submenu Grouped
|
|
28
|
+
|
|
29
|
+
The editor context menu now offers just "Show Preview" and "Show Slides Preview" — exports and presenting live in the preview panel and the Command Palette. The preview context menu keeps **Present (Fullscreen)** pinned first and scopes exports to the current mode: documents get PDF / HTML / Word, slides get PDF / PPTX.
|
|
30
|
+
|
|
31
|
+
The theme submenu is grouped into Built-in · Light / Built-in · Dark / Custom with a scrollable max-height (70vh), so a long list of custom `.themes/` files no longer runs off the page.
|
|
32
|
+
|
|
33
|
+
Command titles are localized through `package.nls.json` / `package.nls.zh-cn.json` — menus and the Command Palette follow the VS Code display language. The preview commands also gain conflict-free keybindings: `Ctrl+Alt+V` opens the preview and `Ctrl+Alt+S` the slides preview, both scoped to Markdown files so Save (`Ctrl+S`) keeps working.
|
|
34
|
+
|
|
35
|
+
### 🎯 Rendering Aligned with the DSH Plugin
|
|
36
|
+
|
|
37
|
+
The preview now follows the same rendering contract as the DSH (DeepSeek Harness) plugin in ColaMD-themes:
|
|
38
|
+
|
|
39
|
+
- **Themes upgraded to the ColaMD 2.8 contract** — seed palette plus typography tokens (`--font-body`, `--font-mono`, `--editor-font-size`, `--editor-column-width`) in all 12 built-in themes, with JetBrains Mono / Fira Code webfont mirroring (CSP now allows `fonts.gstatic.com`; offline falls back to the theme stacks). Per-theme fonts are preserved: Elegant serif, Sepia Georgia, Writer mono at 15px.
|
|
40
|
+
- **BASE_CSS alignment layer** — the WebView injects the DSH document base translated to the editor DOM via zero-specificity `:where()` rules: 820px content column with 2.2rem gutters, 1.78 body line-height (per-theme values win: Elegant 1.9, Sepia 1.85, Writer 1.8), 0.01em letter-spacing, token-driven heading metrics (h1 2rem / 1.3 / −0.01em, h1/h2 underlines removed — as DSH renders them), plus inline-code / code-block / blockquote / list-marker / HR spacing.
|
|
41
|
+
|
|
42
|
+
The cascade is engine defaults < alignment layer < theme identity rules, so themes keep their character while the page grid matches DSH.
|
|
43
|
+
|
|
44
|
+
### 🐛 Slides Mode: Theme Switch No Longer Reverts
|
|
45
|
+
|
|
46
|
+
Switching themes in the slides preview snapped the deck back to the original theme. Root cause: `switchTheme` wrote `colaview.theme` to user settings, but a workspace value (e.g. `"colaview.theme": "elegant"` in `.vscode/settings.json`) shadows it — `getCurrentTheme()` kept returning the old theme and `pushSlides` recomposed the deck CSS from it. Theme writes now target the workspace when one is open, and theme switches pass the theme name explicitly down to `pushSlides`. Document mode was unaffected because its update message carries the CSS directly.
|
|
47
|
+
|
|
48
|
+
## v0.3.4 (2026-12-01)
|
|
49
|
+
|
|
50
|
+
### 🎬 Slide Preview, Presenting & Exports
|
|
51
|
+
|
|
52
|
+
Merged from the 0.2.x line onto the CodeMirror 6 preview layer (released on main as v0.3.4):
|
|
53
|
+
|
|
54
|
+
- **Slide preview** — a dedicated render layer (`src/slides/`) reusing the upstream `slides.css` line contract; pages split by `<!-- slide: cover -->`-style directives at 1920×1080, switchable inside the preview panel.
|
|
55
|
+
- **Fullscreen presenting** — documents and slides are handed to the system browser (`src/present/`): ←/→ paging, `F` fullscreen, click either half to navigate.
|
|
56
|
+
- **Slide exports** — PDF (one 1920×1080 page each), standalone `.slides.html` with paging/fullscreen, and PPTX (16:9, zero-dependency OOXML).
|
|
57
|
+
- **Localized preview menu** — context-menu labels follow `vscode.env.language`.
|
|
58
|
+
|
|
27
59
|
## v0.3.2 (2026-11-07)
|
|
28
60
|
|
|
29
61
|
### 🐛 PDF/HTML Export Fixed: `md is not defined`
|
package/esbuild.js
CHANGED
|
@@ -55,6 +55,8 @@ function copyResources() {
|
|
|
55
55
|
const copies = [
|
|
56
56
|
{ src: 'src/preview/webview/index.html', dst: 'out/preview/webview/index.html' },
|
|
57
57
|
{ src: 'src/themes/built-in', dst: 'out/themes/built-in' },
|
|
58
|
+
// 幻灯片版式表(上游 slides.css):预览/放映/PDF 共用
|
|
59
|
+
{ src: 'src/slides/slides.css', dst: 'out/slides/slides.css' },
|
|
58
60
|
];
|
|
59
61
|
|
|
60
62
|
// Copy ColaMD renderer CSS if available
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vscode-colaview",
|
|
3
3
|
"displayName": "ColaView MD",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"publisher": "bytechain",
|
|
6
6
|
"description": "ColaMD Preview — Rich Markdown Preview & PDF/HTML Export for VSCode",
|
|
7
7
|
"icon": "icon.png",
|
|
@@ -19,40 +19,71 @@
|
|
|
19
19
|
"activationEvents": [
|
|
20
20
|
"onLanguage:markdown",
|
|
21
21
|
"onCommand:colaview.showPreview",
|
|
22
|
+
"onCommand:colaview.showSlides",
|
|
23
|
+
"onCommand:colaview.presentDocument",
|
|
24
|
+
"onCommand:colaview.presentSlides",
|
|
22
25
|
"onCommand:colaview.exportPDF",
|
|
23
26
|
"onCommand:colaview.exportHTML",
|
|
24
|
-
"onCommand:colaview.exportWord"
|
|
27
|
+
"onCommand:colaview.exportWord",
|
|
28
|
+
"onCommand:colaview.exportSlidesPDF",
|
|
29
|
+
"onCommand:colaview.exportSlidesHTML",
|
|
30
|
+
"onCommand:colaview.exportPPTX"
|
|
25
31
|
],
|
|
26
32
|
"main": "./out/extension.js",
|
|
27
33
|
"contributes": {
|
|
28
34
|
"commands": [
|
|
29
35
|
{
|
|
30
36
|
"command": "colaview.showPreview",
|
|
31
|
-
"title": "
|
|
37
|
+
"title": "%colaview.commands.showPreview%",
|
|
32
38
|
"icon": "$(open-preview)"
|
|
33
39
|
},
|
|
40
|
+
{
|
|
41
|
+
"command": "colaview.showSlides",
|
|
42
|
+
"title": "%colaview.commands.showSlides%",
|
|
43
|
+
"icon": "$(preview)"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"command": "colaview.presentDocument",
|
|
47
|
+
"title": "%colaview.commands.presentDocument%"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"command": "colaview.presentSlides",
|
|
51
|
+
"title": "%colaview.commands.presentSlides%"
|
|
52
|
+
},
|
|
34
53
|
{
|
|
35
54
|
"command": "colaview.exportPDF",
|
|
36
|
-
"title": "
|
|
55
|
+
"title": "%colaview.commands.exportPDF%",
|
|
37
56
|
"icon": "$(export)"
|
|
38
57
|
},
|
|
39
58
|
{
|
|
40
59
|
"command": "colaview.exportHTML",
|
|
41
|
-
"title": "
|
|
60
|
+
"title": "%colaview.commands.exportHTML%",
|
|
42
61
|
"icon": "$(export)"
|
|
43
62
|
},
|
|
44
63
|
{
|
|
45
64
|
"command": "colaview.exportWord",
|
|
46
|
-
"title": "
|
|
65
|
+
"title": "%colaview.commands.exportWord%",
|
|
47
66
|
"icon": "$(export)"
|
|
48
67
|
},
|
|
68
|
+
{
|
|
69
|
+
"command": "colaview.exportSlidesPDF",
|
|
70
|
+
"title": "%colaview.commands.exportSlidesPDF%"
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
"command": "colaview.exportSlidesHTML",
|
|
74
|
+
"title": "%colaview.commands.exportSlidesHTML%"
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"command": "colaview.exportPPTX",
|
|
78
|
+
"title": "%colaview.commands.exportPPTX%"
|
|
79
|
+
},
|
|
49
80
|
{
|
|
50
81
|
"command": "colaview.switchTheme",
|
|
51
|
-
"title": "
|
|
82
|
+
"title": "%colaview.commands.switchTheme%"
|
|
52
83
|
},
|
|
53
84
|
{
|
|
54
85
|
"command": "colaview.importTheme",
|
|
55
|
-
"title": "
|
|
86
|
+
"title": "%colaview.commands.importTheme%"
|
|
56
87
|
}
|
|
57
88
|
],
|
|
58
89
|
"menus": {
|
|
@@ -60,6 +91,10 @@
|
|
|
60
91
|
{
|
|
61
92
|
"command": "colaview.showPreview",
|
|
62
93
|
"group": "navigation"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"command": "colaview.showSlides",
|
|
97
|
+
"group": "navigation"
|
|
63
98
|
}
|
|
64
99
|
],
|
|
65
100
|
"editor/context": [
|
|
@@ -68,15 +103,7 @@
|
|
|
68
103
|
"group": "1_colaview"
|
|
69
104
|
},
|
|
70
105
|
{
|
|
71
|
-
"command": "colaview.
|
|
72
|
-
"group": "1_colaview"
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
"command": "colaview.exportHTML",
|
|
76
|
-
"group": "1_colaview"
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
"command": "colaview.exportWord",
|
|
106
|
+
"command": "colaview.showSlides",
|
|
80
107
|
"group": "1_colaview"
|
|
81
108
|
}
|
|
82
109
|
],
|
|
@@ -128,7 +155,19 @@
|
|
|
128
155
|
"description": "Enable scroll sync between editor and preview"
|
|
129
156
|
}
|
|
130
157
|
}
|
|
131
|
-
}
|
|
158
|
+
},
|
|
159
|
+
"keybindings": [
|
|
160
|
+
{
|
|
161
|
+
"command": "colaview.showPreview",
|
|
162
|
+
"key": "ctrl+alt+v",
|
|
163
|
+
"when": "editorLangId == markdown"
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
"command": "colaview.showSlides",
|
|
167
|
+
"key": "ctrl+alt+s",
|
|
168
|
+
"when": "editorLangId == markdown"
|
|
169
|
+
}
|
|
170
|
+
]
|
|
132
171
|
},
|
|
133
172
|
"scripts": {
|
|
134
173
|
"vscode:prepublish": "npm run esbuild-base -- --production",
|
|
@@ -141,7 +180,7 @@
|
|
|
141
180
|
"postinstall": "patch-package"
|
|
142
181
|
},
|
|
143
182
|
"dependencies": {
|
|
144
|
-
"@bytechain.cn/colamd": "2.
|
|
183
|
+
"@bytechain.cn/colamd": "2.8.2",
|
|
145
184
|
"docx": "^9.15.2",
|
|
146
185
|
"katex": "^0.16.47",
|
|
147
186
|
"mermaid": "^11.15.0",
|
package/package.nls.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"colaview.commands.showPreview": " Markdown Preview",
|
|
3
|
+
"colaview.commands.showSlides": "Markdown Slides Preview",
|
|
4
|
+
"colaview.commands.presentDocument": "Present: Document Fullscreen",
|
|
5
|
+
"colaview.commands.presentSlides": "Present: Slides Fullscreen",
|
|
6
|
+
"colaview.commands.exportPDF": "Export to PDF...",
|
|
7
|
+
"colaview.commands.exportHTML": "Export to HTML...",
|
|
8
|
+
"colaview.commands.exportWord": "Export to Word...",
|
|
9
|
+
"colaview.commands.exportSlidesPDF": "Export Slides to PDF...",
|
|
10
|
+
"colaview.commands.exportSlidesHTML": "Export Slides to HTML...",
|
|
11
|
+
"colaview.commands.exportPPTX": "Export to PPTX...",
|
|
12
|
+
"colaview.commands.switchTheme": "Switch Preview Theme...",
|
|
13
|
+
"colaview.commands.importTheme": "Import Custom Theme..."
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"colaview.commands.showPreview": "MD-预览",
|
|
3
|
+
"colaview.commands.showSlides": "MD-幻灯片预览",
|
|
4
|
+
"colaview.commands.presentDocument": "全屏显示:文档",
|
|
5
|
+
"colaview.commands.presentSlides": "全屏显示:幻灯片",
|
|
6
|
+
"colaview.commands.exportPDF": "导出到 PDF...",
|
|
7
|
+
"colaview.commands.exportHTML": "导出到 HTML...",
|
|
8
|
+
"colaview.commands.exportWord": "导出到 Word...",
|
|
9
|
+
"colaview.commands.exportSlidesPDF": "幻灯片导出到 PDF...",
|
|
10
|
+
"colaview.commands.exportSlidesHTML": "幻灯片导出到 HTML...",
|
|
11
|
+
"colaview.commands.exportPPTX": "导出到 PPTX...",
|
|
12
|
+
"colaview.commands.switchTheme": "切换预览主题...",
|
|
13
|
+
"colaview.commands.importTheme": "导入自定义主题..."
|
|
14
|
+
}
|
package/src/commands/commands.ts
CHANGED
|
@@ -24,6 +24,54 @@ export function registerCommands(
|
|
|
24
24
|
previewManager.showPreview();
|
|
25
25
|
}),
|
|
26
26
|
|
|
27
|
+
vscode.commands.registerCommand('colaview.showSlides', () => {
|
|
28
|
+
if (!previewManager) {
|
|
29
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
previewManager.showSlides();
|
|
33
|
+
}),
|
|
34
|
+
|
|
35
|
+
vscode.commands.registerCommand('colaview.presentDocument', async () => {
|
|
36
|
+
if (!previewManager) {
|
|
37
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
await previewManager.handlePresent('document');
|
|
41
|
+
}),
|
|
42
|
+
|
|
43
|
+
vscode.commands.registerCommand('colaview.presentSlides', async () => {
|
|
44
|
+
if (!previewManager) {
|
|
45
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
await previewManager.handlePresent('slides');
|
|
49
|
+
}),
|
|
50
|
+
|
|
51
|
+
vscode.commands.registerCommand('colaview.exportSlidesPDF', async () => {
|
|
52
|
+
if (!previewManager) {
|
|
53
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
await previewManager.exportSlidesPDFToFile();
|
|
57
|
+
}),
|
|
58
|
+
|
|
59
|
+
vscode.commands.registerCommand('colaview.exportSlidesHTML', async () => {
|
|
60
|
+
if (!previewManager) {
|
|
61
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
await previewManager.exportSlidesHTMLToFile();
|
|
65
|
+
}),
|
|
66
|
+
|
|
67
|
+
vscode.commands.registerCommand('colaview.exportPPTX', async () => {
|
|
68
|
+
if (!previewManager) {
|
|
69
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
await previewManager.exportPPTXFile();
|
|
73
|
+
}),
|
|
74
|
+
|
|
27
75
|
vscode.commands.registerCommand('colaview.exportHTML', async () => {
|
|
28
76
|
const editor = vscode.window.activeTextEditor;
|
|
29
77
|
if (!editor || editor.document.languageId !== 'markdown') {
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// 最小 WordprocessingML 生成器 —— 纯同步、零依赖,产出真正的 `.docx`。
|
|
2
|
+
//
|
|
3
|
+
// 只做「够用」的那一层:Normal / Heading1-4 / Quote / Code / ListParagraph
|
|
4
|
+
// 与 9 级编号定义,表格用内联边框,包体通过 zipStore 以 STORE 方式写出。
|
|
5
|
+
// 单位备忘:Word 用 twip(1/20 磅),字号写半磅(`w:sz` 值为字号×2)。
|
|
6
|
+
|
|
7
|
+
import { zipStore } from '../pptx/zip';
|
|
8
|
+
|
|
9
|
+
/** 文档块模型:由块级 Markdown AST 转来(见 export/docx-exporter.ts)。 */
|
|
10
|
+
export type DocBlock =
|
|
11
|
+
| { kind: 'heading'; level: 1 | 2 | 3 | 4 | 5 | 6; text: string }
|
|
12
|
+
| { kind: 'paragraph'; text: string }
|
|
13
|
+
| { kind: 'list'; ordered: boolean; items: string[] }
|
|
14
|
+
| { kind: 'code'; text: string; lang?: string }
|
|
15
|
+
| { kind: 'quote'; text: string }
|
|
16
|
+
| { kind: 'table'; header: string[]; rows: string[][] }
|
|
17
|
+
| { kind: 'divider' };
|
|
18
|
+
|
|
19
|
+
/** 版式与字体覆盖项;缺省值见下方默认常量。 */
|
|
20
|
+
export interface DocxMeta {
|
|
21
|
+
/** 主色,形如 `#b0401f`;给出时用于标题与引用边线。 */
|
|
22
|
+
accent?: string;
|
|
23
|
+
/** 正文字体,例如 `Source Han Serif SC`。 */
|
|
24
|
+
fontBody?: string;
|
|
25
|
+
/** 等宽字体,例如 `JetBrains Mono`。 */
|
|
26
|
+
fontMono?: string;
|
|
27
|
+
/** 文档标题,写入 core.xml。 */
|
|
28
|
+
title?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const XML_DECL = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
|
|
32
|
+
/** 所有 zip 条目共用的固定时间戳,保证同一输入产出同一字节。 */
|
|
33
|
+
const FIXED_DATE = new Date(Date.UTC(2024, 0, 1));
|
|
34
|
+
const DEFAULT_ACCENT = 'B0401F';
|
|
35
|
+
const DEFAULT_FONT_BODY = 'Calibri';
|
|
36
|
+
const DEFAULT_FONT_MONO = 'Consolas';
|
|
37
|
+
/** A4 去掉左右各 1440 twip 页边距后的正文宽度。 */
|
|
38
|
+
const CONTENT_WIDTH_TWIPS = 9026;
|
|
39
|
+
|
|
40
|
+
const W_NS = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
|
|
41
|
+
const R_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
|
|
42
|
+
const PKG_REL_NS = 'http://schemas.openxmlformats.org/package/2006/relationships';
|
|
43
|
+
const CT_NS = 'http://schemas.openxmlformats.org/package/2006/content-types';
|
|
44
|
+
|
|
45
|
+
/** 转义 XML 文本/属性,并剔除 XML 1.0 不接受的控制字符。 */
|
|
46
|
+
function esc(value: string): string {
|
|
47
|
+
return value
|
|
48
|
+
.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, '')
|
|
49
|
+
.replace(/&/g, '&')
|
|
50
|
+
.replace(/</g, '<')
|
|
51
|
+
.replace(/>/g, '>')
|
|
52
|
+
.replace(/"/g, '"')
|
|
53
|
+
.replace(/'/g, ''');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 把 `#b0401f` / `b0401f` 规范成大写六位 RGB;非法值退回 fallback。 */
|
|
57
|
+
function hex(value: string | undefined, fallback: string): string {
|
|
58
|
+
if (value === undefined) return fallback;
|
|
59
|
+
const raw = value.trim().replace(/^#/, '');
|
|
60
|
+
return /^[0-9a-fA-F]{6}$/.test(raw) ? raw.toUpperCase() : fallback;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 字体名只做去空白与转义,保留调用方给的原名。 */
|
|
64
|
+
function font(value: string | undefined, fallback: string): string {
|
|
65
|
+
const name = (value ?? '').trim();
|
|
66
|
+
return name === '' ? fallback : name;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 把若干「路径 → 文本」条目打包成 zip。 */
|
|
70
|
+
function pack(files: ReadonlyArray<readonly [string, string]>): Uint8Array {
|
|
71
|
+
const encoder = new TextEncoder();
|
|
72
|
+
return zipStore(files.map(([path, text]) => ({ path, data: encoder.encode(text), date: FIXED_DATE })));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 一个 Word 文本 run。 */
|
|
76
|
+
function wRun(text: string, rPr = ''): string {
|
|
77
|
+
return `<w:r>${rPr === '' ? '' : `<w:rPr>${rPr}</w:rPr>`}<w:t xml:space="preserve">${esc(text)}</w:t></w:r>`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 按换行拆成多个 run,行间用 `w:br`(必须是 run 的子元素)。 */
|
|
81
|
+
function wLines(text: string, rPr = ''): string {
|
|
82
|
+
return text
|
|
83
|
+
.replace(/\r\n?/g, '\n')
|
|
84
|
+
.split('\n')
|
|
85
|
+
.map(
|
|
86
|
+
(line, i) =>
|
|
87
|
+
`<w:r>${rPr === '' ? '' : `<w:rPr>${rPr}</w:rPr>`}${i > 0 ? '<w:br/>' : ''}<w:t xml:space="preserve">${esc(line)}</w:t></w:r>`
|
|
88
|
+
)
|
|
89
|
+
.join('');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function wParagraph(inner: string, pPr = ''): string {
|
|
93
|
+
return `<w:p>${pPr === '' ? '' : `<w:pPr>${pPr}</w:pPr>`}${inner}</w:p>`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const wFonts = (name: string): string =>
|
|
97
|
+
`<w:rFonts w:ascii="${esc(name)}" w:hAnsi="${esc(name)}" w:eastAsia="${esc(name)}" w:cs="${esc(name)}"/>`;
|
|
98
|
+
|
|
99
|
+
/** Heading1-4 样式;level 5/6 复用 Heading4。 */
|
|
100
|
+
function headingStyle(level: 1 | 2 | 3 | 4, accent: string | undefined, bodyFont: string): string {
|
|
101
|
+
const sizes = { 1: 40, 2: 32, 3: 28, 4: 24 } as const;
|
|
102
|
+
const before = { 1: 360, 2: 280, 3: 240, 4: 200 } as const;
|
|
103
|
+
const after = { 1: 160, 2: 120, 3: 100, 4: 80 } as const;
|
|
104
|
+
const color = accent === undefined ? '' : `<w:color w:val="${accent}"/>`;
|
|
105
|
+
return `<w:style w:type="paragraph" w:styleId="Heading${level}"><w:name w:val="heading ${level}"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/><w:uiPriority w:val="9"/><w:qFormat/><w:pPr><w:keepNext/><w:keepLines/><w:spacing w:before="${before[level]}" w:after="${after[level]}"/><w:outlineLvl w:val="${level - 1}"/></w:pPr><w:rPr>${wFonts(bodyFont)}<w:b/><w:bCs/>${color}<w:sz w:val="${sizes[level]}"/><w:szCs w:val="${sizes[level]}"/></w:rPr></w:style>`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 9 级编号定义:bullet(numId 1)与 decimal(numId 2)。 */
|
|
109
|
+
function numberingXml(): string {
|
|
110
|
+
const levels = (ordered: boolean): string =>
|
|
111
|
+
Array.from({ length: 9 }, (_, lvl) => {
|
|
112
|
+
const indent = 720 + lvl * 360;
|
|
113
|
+
const fmt = ordered ? 'decimal' : 'bullet';
|
|
114
|
+
const text = ordered ? `%${lvl + 1}.` : '•';
|
|
115
|
+
const suff = ordered ? '<w:suff w:val="space"/>' : '';
|
|
116
|
+
return `<w:lvl w:ilvl="${lvl}"><w:start w:val="1"/><w:numFmt w:val="${fmt}"/>${suff}<w:lvlText w:val="${text}"/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="${indent}" w:hanging="360"/></w:pPr><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:hint="default"/></w:rPr></w:lvl>`;
|
|
117
|
+
}).join('');
|
|
118
|
+
return `${XML_DECL}<w:numbering xmlns:w="${W_NS}"><w:abstractNum w:abstractNumId="0"><w:multiLevelType w:val="hybridMultilevel"/>${levels(false)}</w:abstractNum><w:abstractNum w:abstractNumId="1"><w:multiLevelType w:val="hybridMultilevel"/>${levels(true)}</w:abstractNum><w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num><w:num w:numId="2"><w:abstractNumId w:val="1"/></w:num></w:numbering>`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function docxStyles(meta: DocxMeta): string {
|
|
122
|
+
const bodyFont = font(meta.fontBody, DEFAULT_FONT_BODY);
|
|
123
|
+
const monoFont = font(meta.fontMono, DEFAULT_FONT_MONO);
|
|
124
|
+
const accent = meta.accent === undefined ? undefined : hex(meta.accent, DEFAULT_ACCENT);
|
|
125
|
+
const quoteEdge = accent ?? 'BFBFBF';
|
|
126
|
+
return `${XML_DECL}<w:styles xmlns:w="${W_NS}"><w:docDefaults><w:rPrDefault><w:rPr>${wFonts(bodyFont)}<w:sz w:val="22"/><w:szCs w:val="22"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr></w:rPrDefault><w:pPrDefault><w:pPr><w:spacing w:after="0" w:line="240" w:lineRule="auto"/></w:pPr></w:pPrDefault></w:docDefaults><w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/><w:qFormat/><w:pPr><w:spacing w:before="0" w:after="160" w:line="320" w:lineRule="auto"/></w:pPr><w:rPr>${wFonts(bodyFont)}<w:sz w:val="22"/><w:szCs w:val="22"/></w:rPr></w:style>${headingStyle(1, accent, bodyFont)}${headingStyle(2, accent, bodyFont)}${headingStyle(3, accent, bodyFont)}${headingStyle(4, accent, bodyFont)}<w:style w:type="paragraph" w:styleId="Quote"><w:name w:val="Quote"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/><w:qFormat/><w:pPr><w:pBdr><w:left w:val="single" w:sz="18" w:space="8" w:color="${quoteEdge}"/></w:pBdr><w:spacing w:before="80" w:after="160"/><w:ind w:left="360" w:right="180"/></w:pPr><w:rPr><w:i/><w:iCs/><w:color w:val="595959"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Code"><w:name w:val="Code"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/><w:qFormat/><w:pPr><w:shd w:val="clear" w:color="auto" w:fill="F5F3EF"/><w:spacing w:before="0" w:after="0" w:line="240" w:lineRule="auto"/><w:ind w:left="120" w:right="120"/></w:pPr><w:rPr>${wFonts(monoFont)}<w:sz w:val="19"/><w:szCs w:val="19"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="ListParagraph"><w:name w:val="List Paragraph"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:spacing w:before="0" w:after="80"/><w:ind w:left="720" w:hanging="360"/></w:pPr></w:style></w:styles>`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 单元格宽度按列数均分;表头行重复、加粗、浅灰底。 */
|
|
130
|
+
function tableXml(header: readonly string[], rows: readonly string[][]): string {
|
|
131
|
+
let cols = header.length;
|
|
132
|
+
for (const row of rows) cols = Math.max(cols, row.length);
|
|
133
|
+
if (cols === 0) return '';
|
|
134
|
+
const width = Math.floor(CONTENT_WIDTH_TWIPS / cols);
|
|
135
|
+
const grid = `<w:tblGrid>${`<w:gridCol w:w="${width}"/>`.repeat(cols)}</w:tblGrid>`;
|
|
136
|
+
const cell = (text: string, isHeader: boolean): string =>
|
|
137
|
+
`<w:tc><w:tcPr><w:tcW w:w="${width}" w:type="dxa"/>${isHeader ? '<w:shd w:val="clear" w:color="auto" w:fill="F2F2F2"/>' : ''}</w:tcPr>${wParagraph(wRun(text, isHeader ? '<w:b/><w:bCs/>' : ''), '<w:spacing w:before="0" w:after="0" w:line="240" w:lineRule="auto"/>')}</w:tc>`;
|
|
138
|
+
const row = (cells: readonly string[], isHeader: boolean): string =>
|
|
139
|
+
`<w:tr>${isHeader ? '<w:trPr><w:tblHeader/></w:trPr>' : ''}${cells.map((text) => cell(text, isHeader)).join('')}</w:tr>`;
|
|
140
|
+
const headerRow = header.length === 0 ? '' : row(header, true);
|
|
141
|
+
const bodyRows = rows
|
|
142
|
+
.map((cells) => {
|
|
143
|
+
const padded = Array.from({ length: cols }, (_, i) => cells[i] ?? '');
|
|
144
|
+
return row(padded, false);
|
|
145
|
+
})
|
|
146
|
+
.join('');
|
|
147
|
+
return `<w:tbl><w:tblPr><w:tblW w:w="0" w:type="auto"/><w:tblBorders><w:top w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/><w:left w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/><w:bottom w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/><w:right w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/><w:insideH w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/><w:insideV w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/></w:tblBorders><w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr>${grid}${headerRow}${bodyRows}</w:tbl>`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function docxDocument(blocks: readonly DocBlock[]): string {
|
|
151
|
+
const body: string[] = [];
|
|
152
|
+
let lastWasTable = false;
|
|
153
|
+
for (const block of blocks) {
|
|
154
|
+
if (block.kind !== 'table') lastWasTable = false;
|
|
155
|
+
switch (block.kind) {
|
|
156
|
+
case 'heading': {
|
|
157
|
+
const level = Math.min(block.level, 4);
|
|
158
|
+
body.push(wParagraph(wLines(block.text), `<w:pStyle w:val="Heading${level}"/>`));
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
case 'paragraph': {
|
|
162
|
+
if (block.text.trim() === '') break;
|
|
163
|
+
body.push(wParagraph(wLines(block.text)));
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
case 'list': {
|
|
167
|
+
const numId = block.ordered ? 2 : 1;
|
|
168
|
+
for (const item of block.items) {
|
|
169
|
+
if (item.trim() === '') continue;
|
|
170
|
+
body.push(
|
|
171
|
+
wParagraph(
|
|
172
|
+
wLines(item),
|
|
173
|
+
`<w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="${numId}"/></w:numPr>`
|
|
174
|
+
)
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
case 'code': {
|
|
180
|
+
body.push(wParagraph(wLines(block.text), '<w:pStyle w:val="Code"/>'));
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case 'quote': {
|
|
184
|
+
if (block.text.trim() === '') break;
|
|
185
|
+
body.push(wParagraph(wLines(block.text), '<w:pStyle w:val="Quote"/>'));
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
case 'table': {
|
|
189
|
+
const table = tableXml(block.header, block.rows);
|
|
190
|
+
if (table !== '') {
|
|
191
|
+
body.push(table);
|
|
192
|
+
lastWasTable = true;
|
|
193
|
+
}
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
case 'divider': {
|
|
197
|
+
body.push(
|
|
198
|
+
'<w:p><w:pPr><w:pBdr><w:bottom w:val="single" w:sz="6" w:space="1" w:color="BFBFBF"/></w:pBdr><w:spacing w:before="120" w:after="120"/></w:pPr></w:p>'
|
|
199
|
+
);
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (lastWasTable) body.push('<w:p/>'); // Word 不希望 body 直接以表格收尾
|
|
205
|
+
const sectPr =
|
|
206
|
+
'<w:sectPr><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:linePitch="312"/></w:sectPr>';
|
|
207
|
+
return `${XML_DECL}<w:document xmlns:w="${W_NS}" xmlns:r="${R_NS}"><w:body>${body.join('')}${sectPr}</w:body></w:document>`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function coreXml(title: string): string {
|
|
211
|
+
return `${XML_DECL}<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title>${esc(title)}</dc:title><dc:creator>ColaView MD</dc:creator><cp:lastModifiedBy>ColaView MD</cp:lastModifiedBy><dcterms:created xsi:type="dcterms:W3CDTF">2024-01-01T00:00:00Z</dcterms:created><dcterms:modified xsi:type="dcterms:W3CDTF">2024-01-01T00:00:00Z</dcterms:modified></cp:coreProperties>`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function appXml(): string {
|
|
215
|
+
return `${XML_DECL}<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Application>ColaView MD</Application><DocSecurity>0</DocSecurity><ScaleCrop>false</ScaleCrop><Company></Company><LinksUpToDate>false</LinksUpToDate><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>16.0000</AppVersion></Properties>`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 生成一份 Word 文档。
|
|
220
|
+
*
|
|
221
|
+
* 标题样式为内置 Heading1-4,`level` 5/6 复用 Heading4;列表依赖
|
|
222
|
+
* numbering.xml 里的 numId 1(圆点)与 2(十进制);表格使用内联边框,
|
|
223
|
+
* 不依赖 TableGrid 样式。
|
|
224
|
+
*/
|
|
225
|
+
export function buildDocx(input: { blocks: DocBlock[]; meta?: DocxMeta }): Uint8Array {
|
|
226
|
+
const meta = input.meta ?? {};
|
|
227
|
+
const title = meta.title ?? 'ColaView Document';
|
|
228
|
+
const contentTypes = `${XML_DECL}<Types xmlns="${CT_NS}"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/><Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/><Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/><Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/></Types>`;
|
|
229
|
+
const rootRels = `${XML_DECL}<Relationships xmlns="${PKG_REL_NS}"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/></Relationships>`;
|
|
230
|
+
const documentRels = `${XML_DECL}<Relationships xmlns="${PKG_REL_NS}"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/></Relationships>`;
|
|
231
|
+
return pack([
|
|
232
|
+
['[Content_Types].xml', contentTypes],
|
|
233
|
+
['_rels/.rels', rootRels],
|
|
234
|
+
['docProps/core.xml', coreXml(title)],
|
|
235
|
+
['docProps/app.xml', appXml()],
|
|
236
|
+
['word/document.xml', docxDocument(input.blocks)],
|
|
237
|
+
['word/_rels/document.xml.rels', documentRels],
|
|
238
|
+
['word/styles.xml', docxStyles(meta)],
|
|
239
|
+
['word/numbering.xml', numberingXml()],
|
|
240
|
+
]);
|
|
241
|
+
}
|