vscode-colaview 0.0.0-stage → 0.3.2
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/launch.json +30 -0
- package/.vscode/settings.json +4 -0
- package/.vscode/tasks.json +18 -0
- package/.vscodeignore +23 -0
- package/CHANGELOG.md +141 -0
- package/LICENSE +21 -0
- package/README.md +278 -2
- package/README_CN.md +279 -0
- package/RELEASE_NOTES.md +292 -0
- package/esbuild.js +112 -0
- package/icon.png +0 -0
- package/package.json +162 -4
- package/patches/@bytechain.cn+colamd+2.7.11.patch +22 -0
- package/src/commands/commands.ts +105 -0
- package/src/export/css-collector.ts +50 -0
- package/src/export/docx-exporter.ts +962 -0
- package/src/export/html-exporter.ts +18 -0
- package/src/export/pdf-exporter.ts +121 -0
- package/src/export/render-export-images.ts +373 -0
- package/src/export/theme-style.ts +156 -0
- package/src/extension.ts +48 -0
- package/src/preview/preview-manager.ts +542 -0
- package/src/preview/webview/app.ts +366 -0
- package/src/preview/webview/index.html +134 -0
- package/src/themes/built-in/bear.css +38 -0
- package/src/themes/built-in/dark.css +20 -0
- package/src/themes/built-in/dracula.css +38 -0
- package/src/themes/built-in/elegant.css +52 -0
- package/src/themes/built-in/gruvbox.css +42 -0
- package/src/themes/built-in/light.css +20 -0
- package/src/themes/built-in/midnight.css +31 -0
- package/src/themes/built-in/nord.css +38 -0
- package/src/themes/built-in/notion.css +48 -0
- package/src/themes/built-in/sepia.css +47 -0
- package/src/themes/built-in/solarized-dark.css +35 -0
- package/src/themes/built-in/writer.css +44 -0
- package/src/themes/theme-manager.ts +180 -0
- package/src/types/colamd-renderer.d.ts +47 -0
- package/src/types.ts +20 -0
- package/test/fixtures/basic.md +34 -0
- package/test/fixtures/math.md +20 -0
- package/test/fixtures/mermaid.md +19 -0
- package/test/runTest.ts +30 -0
- package/test/suite/html-exporter.test.ts +20 -0
- package/test/suite/markdown-parser.test.ts +8 -0
- package/test/suite/theme-manager.test.ts +31 -0
- package/tsconfig.json +19 -0
package/esbuild.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// esbuild 打包脚本 — 扩展宿主(Node.js) + WebView 浏览器端 分别打包
|
|
2
|
+
const esbuild = require('esbuild');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const isWatch = process.argv.includes('--watch');
|
|
7
|
+
const isProd = process.argv.includes('--production');
|
|
8
|
+
|
|
9
|
+
// ── 扩展宿主 (Node.js, CJS) ──
|
|
10
|
+
const extensionOpts = {
|
|
11
|
+
entryPoints: ['src/extension.ts'],
|
|
12
|
+
bundle: true,
|
|
13
|
+
outfile: 'out/extension.js',
|
|
14
|
+
external: ['vscode', 'puppeteer-core'],
|
|
15
|
+
format: 'cjs',
|
|
16
|
+
platform: 'node',
|
|
17
|
+
target: 'node18',
|
|
18
|
+
sourcemap: !isProd,
|
|
19
|
+
minify: isProd,
|
|
20
|
+
logLevel: 'info',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// ── WebView 浏览器端 (IIFE, bundled) ──
|
|
24
|
+
const webviewOpts = {
|
|
25
|
+
entryPoints: ['src/preview/webview/app.ts'],
|
|
26
|
+
bundle: true,
|
|
27
|
+
outfile: 'out/preview/webview/app.js',
|
|
28
|
+
format: 'iife',
|
|
29
|
+
platform: 'browser',
|
|
30
|
+
target: 'es2020',
|
|
31
|
+
sourcemap: !isProd,
|
|
32
|
+
minify: isProd,
|
|
33
|
+
logLevel: 'info',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
async function build() {
|
|
37
|
+
if (isWatch) {
|
|
38
|
+
const ctx1 = await esbuild.context(extensionOpts);
|
|
39
|
+
const ctx2 = await esbuild.context(webviewOpts);
|
|
40
|
+
await Promise.all([ctx1.watch(), ctx2.watch()]);
|
|
41
|
+
console.log('Watching for changes...');
|
|
42
|
+
} else {
|
|
43
|
+
await Promise.all([
|
|
44
|
+
esbuild.build(extensionOpts),
|
|
45
|
+
esbuild.build(webviewOpts),
|
|
46
|
+
]);
|
|
47
|
+
copyResources();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 复制资源文件到 out/ 目录(CSS、WebView HTML 等)
|
|
53
|
+
*/
|
|
54
|
+
function copyResources() {
|
|
55
|
+
const copies = [
|
|
56
|
+
{ src: 'src/preview/webview/index.html', dst: 'out/preview/webview/index.html' },
|
|
57
|
+
{ src: 'src/themes/built-in', dst: 'out/themes/built-in' },
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
// Copy ColaMD renderer CSS if available
|
|
61
|
+
const colamdCss = 'node_modules/@bytechain.cn/colamd/dist/lib/colamd.css';
|
|
62
|
+
if (fs.existsSync(colamdCss)) {
|
|
63
|
+
copies.push({ src: colamdCss, dst: 'out/preview/webview/colamd.css' });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Copy static assets needed by the host-side renderers (Word export).
|
|
67
|
+
// These live in node_modules, which .vscodeignore strips from the vsix,
|
|
68
|
+
// so they must be copied into out/assets/ to survive packaging.
|
|
69
|
+
const katexCss = 'node_modules/katex/dist/katex.min.css';
|
|
70
|
+
if (fs.existsSync(katexCss)) {
|
|
71
|
+
copies.push({ src: katexCss, dst: 'out/assets/katex.min.css' });
|
|
72
|
+
}
|
|
73
|
+
// KaTeX 字体:Word 导出的公式截图页会把 katex.min.css 内联进 data: 页面,
|
|
74
|
+
// 相对字体 URL 解析不到,必须带上字体文件(运行时转 base64 内联)。
|
|
75
|
+
const katexFonts = 'node_modules/katex/dist/fonts';
|
|
76
|
+
if (fs.existsSync(katexFonts)) {
|
|
77
|
+
copies.push({ src: katexFonts, dst: 'out/assets/katex-fonts' });
|
|
78
|
+
}
|
|
79
|
+
const mermaidJs = 'node_modules/mermaid/dist/mermaid.min.js';
|
|
80
|
+
if (fs.existsSync(mermaidJs)) {
|
|
81
|
+
copies.push({ src: mermaidJs, dst: 'out/assets/mermaid.min.js' });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (const { src, dst } of copies) {
|
|
85
|
+
if (!fs.existsSync(src)) continue;
|
|
86
|
+
if (fs.statSync(src).isDirectory()) {
|
|
87
|
+
copyDirSync(src, dst);
|
|
88
|
+
} else {
|
|
89
|
+
fs.mkdirSync(path.dirname(dst), { recursive: true });
|
|
90
|
+
fs.copyFileSync(src, dst);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
console.log('Resources copied to out/');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 递归复制目录
|
|
98
|
+
*/
|
|
99
|
+
function copyDirSync(src, dst) {
|
|
100
|
+
fs.mkdirSync(dst, { recursive: true });
|
|
101
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
102
|
+
const srcPath = path.join(src, entry.name);
|
|
103
|
+
const dstPath = path.join(dst, entry.name);
|
|
104
|
+
if (entry.isDirectory()) {
|
|
105
|
+
copyDirSync(srcPath, dstPath);
|
|
106
|
+
} else {
|
|
107
|
+
fs.copyFileSync(srcPath, dstPath);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
build().catch(() => process.exit(1));
|
package/icon.png
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,164 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vscode-colaview",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"displayName": "ColaView MD",
|
|
4
|
+
"version": "0.3.2",
|
|
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.11",
|
|
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
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
diff --git a/node_modules/@bytechain.cn/colamd/dist/lib/colamd-renderer.js b/node_modules/@bytechain.cn/colamd/dist/lib/colamd-renderer.js
|
|
2
|
+
index 371c127..b629313 100644
|
|
3
|
+
--- a/node_modules/@bytechain.cn/colamd/dist/lib/colamd-renderer.js
|
|
4
|
+
+++ b/node_modules/@bytechain.cn/colamd/dist/lib/colamd-renderer.js
|
|
5
|
+
@@ -25981,7 +25981,7 @@ function NF(t, e) {
|
|
6
|
+
e.push({
|
|
7
|
+
from: n.from,
|
|
8
|
+
to: n.to,
|
|
9
|
+
- deco: Wt.replace({ widget: new jF(n.code, n.block, n.from, n.to) })
|
|
10
|
+
+ deco: Wt.replace({ widget: new jF(n.code, n.block, n.from, n.to), block: n.block && (function(t, e, r) { return t.doc.lineAt(e).from === e && t.doc.lineAt(Math.min(r, t.doc.length)).to === r; })(t, n.from, n.to) })
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
@@ -26071,7 +26071,7 @@ function KF(t, e, r) {
|
|
15
|
+
O.trim() && e.push({
|
|
16
|
+
from: n.from,
|
|
17
|
+
to: n.to,
|
|
18
|
+
- deco: Wt.replace({ widget: new FF(O, `${n.from}:${O.length}`, n.from, n.to) })
|
|
19
|
+
+ deco: Wt.replace({ widget: new FF(O, `${n.from}:${O.length}`, n.from, n.to), block: !0 })
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// VSCode 命令注册 — 所有 ColaView MD 命令入口
|
|
2
|
+
import * as vscode from 'vscode';
|
|
3
|
+
import { PreviewManager } from '../preview/preview-manager';
|
|
4
|
+
import { ThemeManager } from '../themes/theme-manager';
|
|
5
|
+
import { exportHTML } from '../export/html-exporter';
|
|
6
|
+
import { exportPDF } from '../export/pdf-exporter';
|
|
7
|
+
import { exportWord } from '../export/docx-exporter';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 注册所有 ColaView MD 命令
|
|
11
|
+
* @param context VSCode 扩展上下文
|
|
12
|
+
* @param previewManager 预览面板管理器(可能为 null)
|
|
13
|
+
*/
|
|
14
|
+
export function registerCommands(
|
|
15
|
+
context: vscode.ExtensionContext,
|
|
16
|
+
previewManager: PreviewManager | null
|
|
17
|
+
): void {
|
|
18
|
+
context.subscriptions.push(
|
|
19
|
+
vscode.commands.registerCommand('colaview.showPreview', () => {
|
|
20
|
+
if (!previewManager) {
|
|
21
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
previewManager.showPreview();
|
|
25
|
+
}),
|
|
26
|
+
|
|
27
|
+
vscode.commands.registerCommand('colaview.exportHTML', async () => {
|
|
28
|
+
const editor = vscode.window.activeTextEditor;
|
|
29
|
+
if (!editor || editor.document.languageId !== 'markdown') {
|
|
30
|
+
vscode.window.showWarningMessage('Open a Markdown file first');
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (!previewManager) {
|
|
34
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
previewManager.showPreview();
|
|
38
|
+
const fullHTML = await previewManager.getExportHTML();
|
|
39
|
+
if (!fullHTML) {
|
|
40
|
+
vscode.window.showWarningMessage('ColaView: Could not get rendered HTML from preview');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
await exportHTML(editor.document, fullHTML);
|
|
44
|
+
}),
|
|
45
|
+
|
|
46
|
+
vscode.commands.registerCommand('colaview.exportPDF', async () => {
|
|
47
|
+
const editor = vscode.window.activeTextEditor;
|
|
48
|
+
if (!editor || editor.document.languageId !== 'markdown') {
|
|
49
|
+
vscode.window.showWarningMessage('Open a Markdown file first');
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!previewManager) {
|
|
53
|
+
vscode.window.showErrorMessage('ColaView: Preview manager not available');
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
previewManager.showPreview();
|
|
57
|
+
const fullHTML = await previewManager.getExportHTML();
|
|
58
|
+
if (!fullHTML) {
|
|
59
|
+
vscode.window.showWarningMessage('ColaView: Could not get rendered HTML from preview');
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
await exportPDF(editor.document, fullHTML);
|
|
63
|
+
}),
|
|
64
|
+
|
|
65
|
+
vscode.commands.registerCommand('colaview.exportWord', async () => {
|
|
66
|
+
const editor = vscode.window.activeTextEditor;
|
|
67
|
+
if (!editor || editor.document.languageId !== 'markdown') {
|
|
68
|
+
vscode.window.showWarningMessage('Open a Markdown file first');
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
await exportWord(editor.document);
|
|
72
|
+
}),
|
|
73
|
+
|
|
74
|
+
vscode.commands.registerCommand('colaview.switchTheme', async () => {
|
|
75
|
+
const list = ThemeManager.getThemeList();
|
|
76
|
+
const items: vscode.QuickPickItem[] = [
|
|
77
|
+
...list.builtins.map(t => ({ label: t.charAt(0).toUpperCase() + t.slice(1), description: 'Built-in' })),
|
|
78
|
+
...list.customs.map(t => ({ label: t, description: 'Custom' })),
|
|
79
|
+
];
|
|
80
|
+
const picked = await vscode.window.showQuickPick(items);
|
|
81
|
+
if (picked) {
|
|
82
|
+
const name = picked.label.toLowerCase();
|
|
83
|
+
await ThemeManager.switchTheme(name);
|
|
84
|
+
// 自定义主题需要通过 handleSwitchTheme 发送 CSS
|
|
85
|
+
if (previewManager) {
|
|
86
|
+
previewManager.switchThemeFromCommand(name);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}),
|
|
90
|
+
|
|
91
|
+
vscode.commands.registerCommand('colaview.importTheme', async () => {
|
|
92
|
+
const uris = await vscode.window.showOpenDialog({
|
|
93
|
+
filters: { 'CSS Themes': ['css'] },
|
|
94
|
+
canSelectMany: false,
|
|
95
|
+
});
|
|
96
|
+
if (uris && uris[0]) {
|
|
97
|
+
await ThemeManager.importCustomTheme(uris[0]);
|
|
98
|
+
// 导入后刷新主题列表
|
|
99
|
+
if (previewManager) {
|
|
100
|
+
previewManager.refreshThemeList();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// CSS 变量收集器
|
|
2
|
+
// 从主题 CSS 文本中提取所有设计令牌的声明值
|
|
3
|
+
|
|
4
|
+
/** 设计令牌变量名清单 */
|
|
5
|
+
const DESIGN_TOKEN_NAMES = [
|
|
6
|
+
'--font-family-base', '--font-family-heading', '--font-family-code',
|
|
7
|
+
'--font-size-root', '--line-height-base',
|
|
8
|
+
'--color-bg', '--color-text', '--color-text-muted',
|
|
9
|
+
'--color-border', '--color-link', '--color-accent',
|
|
10
|
+
'--color-accent-dark', '--color-accent-light',
|
|
11
|
+
'--color-selection-bg',
|
|
12
|
+
'--code-bg', '--code-color', '--code-block-bg', '--code-block-text',
|
|
13
|
+
'--blockquote-bg', '--blockquote-border', '--blockquote-color',
|
|
14
|
+
'--table-header-bg', '--table-border',
|
|
15
|
+
'--radius-sm', '--radius-md', '--radius-lg',
|
|
16
|
+
'--mermaid-background', '--mermaid-border-color',
|
|
17
|
+
'--mermaid-node-stroke', '--mermaid-node-fill', '--mermaid-node-text',
|
|
18
|
+
'--mermaid-edge-stroke', '--mermaid-edge-stroke-width',
|
|
19
|
+
'--mermaid-cluster-stroke', '--mermaid-cluster-fill',
|
|
20
|
+
'--mermaid-label-text', '--mermaid-font-family', '--mermaid-font-size',
|
|
21
|
+
'--mermaid-cscale0', '--mermaid-cscale1', '--mermaid-cscale2',
|
|
22
|
+
'--mermaid-cscale3', '--mermaid-cscale4', '--mermaid-cscale5',
|
|
23
|
+
'--mermaid-cscale6', '--mermaid-cscale7', '--mermaid-cscale8',
|
|
24
|
+
'--mermaid-cscale9', '--mermaid-cscale10', '--mermaid-cscale11',
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 从 CSS 文本中提取所有设计令牌的声明值
|
|
29
|
+
* @param themeCSS 主题 CSS 文本(foundation + theme)
|
|
30
|
+
* @returns 变量名 → 变量值的映射
|
|
31
|
+
*/
|
|
32
|
+
export function collectCSSVariables(themeCSS: string): Record<string, string> {
|
|
33
|
+
const vars: Record<string, string> = {};
|
|
34
|
+
for (const name of DESIGN_TOKEN_NAMES) {
|
|
35
|
+
const escaped = name.replace(/-/g, '\\-');
|
|
36
|
+
const regex = new RegExp(`${escaped}\\s*:\\s*([^;]+);`);
|
|
37
|
+
const match = themeCSS.match(regex);
|
|
38
|
+
if (match) vars[name] = match[1].trim();
|
|
39
|
+
}
|
|
40
|
+
return vars;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 生成 :root CSS 块(用于导出 HTML)
|
|
45
|
+
* @param vars CSS 变量映射
|
|
46
|
+
*/
|
|
47
|
+
export function buildRootCSSBlock(vars: Record<string, string>): string {
|
|
48
|
+
const lines = Object.entries(vars).map(([k, v]) => ` ${k}: ${v};`);
|
|
49
|
+
return `:root {\n${lines.join('\n')}\n}`;
|
|
50
|
+
}
|