vscode-colaview 0.3.0 → 0.3.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.
package/package.json CHANGED
@@ -1,186 +1,164 @@
1
- {
2
- "name": "vscode-colaview",
3
- "displayName": "ColaView MD",
4
- "version": "0.3.0",
5
- "publisher": "bytechain",
6
- "description": "ColaMD Preview — Rich Markdown Preview & PDF/HTML Export for VSCode",
7
- "icon": "icon.png",
8
- "repository": {
9
- "type": "git",
10
- "url": "https://github.com/bytechain/vscode-colaview.git"
11
- },
12
- "license": "MIT",
13
- "engines": {
14
- "vscode": "^1.85.0"
15
- },
16
- "categories": [
17
- "Other"
18
- ],
19
- "activationEvents": [
20
- "onLanguage:markdown",
21
- "onCommand:colaview.showPreview",
22
- "onCommand:colaview.exportPDF",
23
- "onCommand:colaview.exportHTML",
24
- "onCommand:colaview.exportWord"
25
- ],
26
- "main": "./out/extension.js",
27
- "contributes": {
28
- "commands": [
29
- {
30
- "command": "colaview.showPreview",
31
- "title": "Show Preview",
32
- "icon": "$(open-preview)"
33
- },
34
- {
35
- "command": "colaview.exportPDF",
36
- "title": "Export to PDF...",
37
- "icon": "$(export)"
38
- },
39
- {
40
- "command": "colaview.exportHTML",
41
- "title": "Export to HTML...",
42
- "icon": "$(export)"
43
- },
44
- {
45
- "command": "colaview.exportWord",
46
- "title": "Export to Word...",
47
- "icon": "$(export)"
48
- },
49
- {
50
- "command": "colaview.switchTheme",
51
- "title": "Switch Preview Theme..."
52
- },
53
- {
54
- "command": "colaview.importTheme",
55
- "title": "Import Custom Theme..."
56
- }
57
- ],
58
- "menus": {
59
- "editor/title": [
60
- {
61
- "command": "colaview.showPreview",
62
- "group": "navigation"
63
- }
64
- ],
65
- "editor/context": [
66
- {
67
- "command": "colaview.showPreview",
68
- "group": "1_colaview"
69
- },
70
- {
71
- "command": "colaview.exportPDF",
72
- "group": "1_colaview"
73
- },
74
- {
75
- "command": "colaview.exportHTML",
76
- "group": "1_colaview"
77
- },
78
- {
79
- "command": "colaview.exportWord",
80
- "group": "1_colaview"
81
- }
82
- ],
83
- "explorer/context": [
84
- {
85
- "command": "colaview.exportPDF",
86
- "group": "1_colaview"
87
- },
88
- {
89
- "command": "colaview.exportHTML",
90
- "group": "1_colaview"
91
- },
92
- {
93
- "command": "colaview.exportWord",
94
- "group": "1_colaview"
95
- }
96
- ]
97
- },
98
- "configuration": {
99
- "title": "ColaView MD",
100
- "properties": {
101
- "colaview.theme": {
102
- "type": "string",
103
- "default": "elegant",
104
- "enum": [
105
- "light",
106
- "dark",
107
- "elegant",
108
- "sepia",
109
- "notion",
110
- "bear",
111
- "writer",
112
- "solarized-dark",
113
- "nord",
114
- "gruvbox",
115
- "dracula",
116
- "midnight"
117
- ],
118
- "description": "Visual theme for preview and export"
119
- },
120
- "colaview.plugins.math": {
121
- "type": "boolean",
122
- "default": true,
123
- "description": "Enable KaTeX math formula rendering"
124
- },
125
- "colaview.plugins.mermaid": {
126
- "type": "boolean",
127
- "default": true,
128
- "description": "Enable Mermaid diagram rendering"
129
- },
130
- "colaview.pdfMethod": {
131
- "type": "string",
132
- "default": "puppeteer",
133
- "enum": [
134
- "puppeteer",
135
- "webviewPrint"
136
- ],
137
- "description": "PDF export method"
138
- },
139
- "colaview.autoPreview": {
140
- "type": "boolean",
141
- "default": false,
142
- "description": "Auto show preview on opening Markdown"
143
- },
144
- "colaview.customThemesPath": {
145
- "type": "string",
146
- "default": "",
147
- "description": "Custom theme CSS directory path"
148
- },
149
- "colaview.scrollSync": {
150
- "type": "boolean",
151
- "default": false,
152
- "description": "Enable scroll sync between editor and preview"
153
- }
154
- }
155
- }
156
- },
157
- "scripts": {
158
- "vscode:prepublish": "npm run esbuild-base -- --production",
159
- "esbuild-base": "node esbuild.js",
160
- "build": "node esbuild.js",
161
- "watch": "node esbuild.js --watch",
162
- "pretest": "npm run build",
163
- "test": "node ./out/test/runTest.js",
164
- "package": "vsce package"
165
- },
166
- "dependencies": {
167
- "@bytechain.cn/colamd": "^2.7.9",
168
- "docx": "^9.15.2",
169
- "katex": "^0.16.47",
170
- "mermaid": "^11.15.0",
171
- "puppeteer-core": "^25.1.0",
172
- "remark-gfm": "^4.0.1",
173
- "remark-parse": "^11.0.0",
174
- "unified": "^11.0.5"
175
- },
176
- "devDependencies": {
177
- "@types/katex": "^0.16.8",
178
- "@types/node": "^20.0.0",
179
- "@types/vscode": "^1.85.0",
180
- "@vscode/test-electron": "^2.4.0",
181
- "@vscode/vsce": "^3.0.0",
182
- "esbuild": "^0.28.0",
183
- "eslint": "^9.0.0",
184
- "typescript": "^5.7.0"
185
- }
186
- }
1
+ {
2
+ "name": "vscode-colaview",
3
+ "displayName": "ColaView MD",
4
+ "version": "0.3.3",
5
+ "publisher": "bytechain",
6
+ "description": "ColaMD Preview — Rich Markdown Preview & PDF/HTML Export for VSCode",
7
+ "icon": "icon.png",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/bytechain/vscode-colaview.git"
11
+ },
12
+ "license": "MIT",
13
+ "engines": {
14
+ "vscode": "^1.85.0"
15
+ },
16
+ "categories": [
17
+ "Other"
18
+ ],
19
+ "activationEvents": [
20
+ "onLanguage:markdown",
21
+ "onCommand:colaview.showPreview",
22
+ "onCommand:colaview.exportPDF",
23
+ "onCommand:colaview.exportHTML",
24
+ "onCommand:colaview.exportWord"
25
+ ],
26
+ "main": "./out/extension.js",
27
+ "contributes": {
28
+ "commands": [
29
+ {
30
+ "command": "colaview.showPreview",
31
+ "title": "Show Preview",
32
+ "icon": "$(open-preview)"
33
+ },
34
+ {
35
+ "command": "colaview.exportPDF",
36
+ "title": "Export to PDF...",
37
+ "icon": "$(export)"
38
+ },
39
+ {
40
+ "command": "colaview.exportHTML",
41
+ "title": "Export to HTML...",
42
+ "icon": "$(export)"
43
+ },
44
+ {
45
+ "command": "colaview.exportWord",
46
+ "title": "Export to Word...",
47
+ "icon": "$(export)"
48
+ },
49
+ {
50
+ "command": "colaview.switchTheme",
51
+ "title": "Switch Preview Theme..."
52
+ },
53
+ {
54
+ "command": "colaview.importTheme",
55
+ "title": "Import Custom Theme..."
56
+ }
57
+ ],
58
+ "menus": {
59
+ "editor/title": [
60
+ {
61
+ "command": "colaview.showPreview",
62
+ "group": "navigation"
63
+ }
64
+ ],
65
+ "editor/context": [
66
+ {
67
+ "command": "colaview.showPreview",
68
+ "group": "1_colaview"
69
+ },
70
+ {
71
+ "command": "colaview.exportPDF",
72
+ "group": "1_colaview"
73
+ },
74
+ {
75
+ "command": "colaview.exportHTML",
76
+ "group": "1_colaview"
77
+ },
78
+ {
79
+ "command": "colaview.exportWord",
80
+ "group": "1_colaview"
81
+ }
82
+ ],
83
+ "explorer/context": [
84
+ {
85
+ "command": "colaview.exportPDF",
86
+ "group": "1_colaview"
87
+ },
88
+ {
89
+ "command": "colaview.exportHTML",
90
+ "group": "1_colaview"
91
+ },
92
+ {
93
+ "command": "colaview.exportWord",
94
+ "group": "1_colaview"
95
+ }
96
+ ]
97
+ },
98
+ "configuration": {
99
+ "title": "ColaView MD",
100
+ "properties": {
101
+ "colaview.theme": {
102
+ "type": "string",
103
+ "default": "elegant",
104
+ "enum": [
105
+ "light",
106
+ "dark",
107
+ "elegant",
108
+ "sepia",
109
+ "notion",
110
+ "bear",
111
+ "writer",
112
+ "solarized-dark",
113
+ "nord",
114
+ "gruvbox",
115
+ "dracula",
116
+ "midnight"
117
+ ],
118
+ "description": "Visual theme for preview and export"
119
+ },
120
+ "colaview.autoPreview": {
121
+ "type": "boolean",
122
+ "default": false,
123
+ "description": "Auto show preview on opening Markdown"
124
+ },
125
+ "colaview.scrollSync": {
126
+ "type": "boolean",
127
+ "default": false,
128
+ "description": "Enable scroll sync between editor and preview"
129
+ }
130
+ }
131
+ }
132
+ },
133
+ "scripts": {
134
+ "vscode:prepublish": "npm run esbuild-base -- --production",
135
+ "esbuild-base": "node esbuild.js",
136
+ "build": "node esbuild.js",
137
+ "watch": "node esbuild.js --watch",
138
+ "pretest": "npm run build",
139
+ "test": "node ./out/test/runTest.js",
140
+ "package": "vsce package",
141
+ "postinstall": "patch-package"
142
+ },
143
+ "dependencies": {
144
+ "@bytechain.cn/colamd": "2.7.13-rc.1",
145
+ "docx": "^9.15.2",
146
+ "katex": "^0.16.47",
147
+ "mermaid": "^11.15.0",
148
+ "puppeteer-core": "^25.1.0",
149
+ "remark-gfm": "^4.0.1",
150
+ "remark-parse": "^11.0.0",
151
+ "unified": "^11.0.5"
152
+ },
153
+ "devDependencies": {
154
+ "@types/katex": "^0.16.8",
155
+ "@types/node": "^20.0.0",
156
+ "@types/vscode": "^1.85.0",
157
+ "@vscode/test-electron": "^2.4.0",
158
+ "@vscode/vsce": "^3.0.0",
159
+ "esbuild": "^0.28.0",
160
+ "eslint": "^9.0.0",
161
+ "patch-package": "^8.0.1",
162
+ "typescript": "^5.7.0"
163
+ }
164
+ }
@@ -112,6 +112,14 @@ interface DocxContext {
112
112
  style: DocxStyle;
113
113
  bodyFont: string;
114
114
  monoFont: string;
115
+ /**
116
+ * 代码用的字体(含 eastAsia 槽位)。
117
+ * 以前只用 `font: monoFont` 字符串:docx 会把它同时写进 w:rFonts 的 ascii / hAnsi /
118
+ * cs / eastAsia 四个槽位,如果选中了 Windows 没有的字体(如 Menlo),Word 就整段
119
+ * 替换,代码块变成 ♣ ○ ◇ 这类符号乱码。eastAsia 单独给一个中文字体,代码里的
120
+ * 中文注释才不会乱码(镜像 colamd-extend aad3bf6 的修复)。
121
+ */
122
+ codeFont: { ascii: string; hAnsi: string; cs: string; eastAsia: string };
115
123
  bodySizeHalf: number;
116
124
  paragraphLine: number;
117
125
  codeSizeHalf: number;
@@ -240,6 +248,15 @@ function halfPoints(px: number): number {
240
248
  return Math.max(2, Math.round(px * 1.5));
241
249
  }
242
250
 
251
+ /** 代码的 eastAsia 中文字体:按平台选一个 Word 认得的(镜像 colamd-extend aad3bf6)。 */
252
+ function codeEastAsiaFont(): string {
253
+ return process.platform === 'darwin'
254
+ ? 'PingFang SC'
255
+ : process.platform === 'win32'
256
+ ? 'Microsoft YaHei'
257
+ : 'Noto Sans CJK SC';
258
+ }
259
+
243
260
  /** em 距离 → twips(1px = 15 twips)。 */
244
261
  function emTwips(px: number, em: number): number {
245
262
  return Math.round(px * em * 15);
@@ -387,9 +404,10 @@ function inlineRuns(
387
404
  } else if (node.type === 'inlineCode') {
388
405
  runs.push(new TextRun({
389
406
  text: node.value ?? '',
390
- font: context.monoFont,
407
+ font: context.codeFont,
391
408
  size: context.codeSizeHalf,
392
409
  shading: { fill: context.style.inlineCodeBackground },
410
+ noProof: true, // 变量名不再带拼写红波浪线
393
411
  ...style,
394
412
  }));
395
413
  } else if (node.type === 'strong') {
@@ -570,6 +588,7 @@ function tableForNode(node: MarkdownNode, context: DocxContext): Table {
570
588
  }
571
589
 
572
590
  function codeParagraph(node: MarkdownNode, context: DocxContext, extra: IParagraphOptions = {}): Paragraph {
591
+ // 每一行自己一个 run,行间用 `<w:br/>`;空行也要留一个 break,不然空行会被吃掉。
573
592
  const lines = (node.value ?? '').replace(/\n+$/, '').split('\n');
574
593
  const children: ParagraphChild[] = lines.map((line, index) => (
575
594
  line === ''
@@ -577,17 +596,28 @@ function codeParagraph(node: MarkdownNode, context: DocxContext, extra: IParagra
577
596
  : new TextRun({
578
597
  text: line,
579
598
  break: index > 0 ? 1 : undefined,
580
- font: context.monoFont,
599
+ font: context.codeFont,
581
600
  size: context.codeSizeHalf,
582
601
  color: context.style.codeBlockText,
602
+ noProof: true, // 变量名不再带拼写红波浪线
583
603
  })
584
604
  ));
585
605
  const codeLine = Math.round(context.style.bodySize * 0.875 * 1.6 * 15);
586
606
  const codePad = emTwips(context.style.bodySize, 1.0);
607
+ const codeBorder = { color: context.style.border, space: 4, style: BorderStyle.SINGLE, size: 4 };
587
608
  return new Paragraph({
588
609
  ...extra,
610
+ // 显式左对齐:不写的话继承默认样式,默认是两端对齐时每一行都会被拉满行宽,
611
+ // 键留在左边、值被顶到右边(镜像 colamd-extend aad3bf6)。
612
+ alignment: AlignmentType.LEFT,
589
613
  shading: { fill: context.style.codeBlockBackground },
590
614
  spacing: { before: codePad, after: codePad, line: codeLine, lineRule: LineRuleType.AT_LEAST },
615
+ border: {
616
+ top: codeBorder,
617
+ bottom: codeBorder,
618
+ left: codeBorder,
619
+ right: codeBorder,
620
+ },
591
621
  children,
592
622
  });
593
623
  }
@@ -860,10 +890,17 @@ export async function markdownToDocx(input: DocxExportInput): Promise<Buffer> {
860
890
  prepareLists(tree.children, listState);
861
891
  const remoteImages = await remoteImagesFor(tree);
862
892
 
893
+ const monoFont = fontName(style.monoFontStack, true);
863
894
  const context: DocxContext = {
864
895
  style,
865
896
  bodyFont: fontName(style.bodyFontStack, false),
866
- monoFont: fontName(style.monoFontStack, true),
897
+ monoFont,
898
+ codeFont: {
899
+ ascii: monoFont,
900
+ hAnsi: monoFont,
901
+ cs: monoFont,
902
+ eastAsia: codeEastAsiaFont(),
903
+ },
867
904
  bodySizeHalf: halfPoints(style.bodySize),
868
905
  paragraphLine: Math.round(style.lineHeight * style.bodySize * 15),
869
906
  codeSizeHalf: halfPoints(style.bodySize * 0.875),
@@ -906,9 +943,11 @@ export async function exportWord(document: vscode.TextDocument): Promise<void> {
906
943
  const markdown = document.getText();
907
944
  const sourcePath = document.fileName;
908
945
 
909
- const { content, images, imageSizes } = await renderMathAndMermaidToImages(markdown);
946
+ // 先取主题令牌:Mermaid 图的配色跟随当前预览主题(浅色跟随、深色回落浅色),
947
+ // 再用同一份令牌构建 Word 文档样式,两边保持一致。
910
948
  const themeCss = ThemeManager.loadThemeCSS(ThemeManager.getCurrentTheme());
911
949
  const style = buildDocxStyleFromThemeCss(themeCss);
950
+ const { content, images, imageSizes } = await renderMathAndMermaidToImages(markdown, style);
912
951
  const buffer = await markdownToDocx({ content, sourcePath, images, imageSizes, style });
913
952
 
914
953
  const defaultName = document.fileName.replace(/\.md$/i, '.docx');