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
|
@@ -1,196 +1,196 @@
|
|
|
1
|
-
// 从「预览所用主题 CSS」中抽取令牌,并把它翻译成导出文档可用的语义样式。
|
|
2
|
-
//
|
|
3
|
-
// 背景:预览里主题的排版/强调色通过 `#editor .cm-content .cm-md-*` 选择器生效;
|
|
4
|
-
// 而 buildExportHTML 会把内容序列化成 `<article class="colamd-document">` 语义 HTML
|
|
5
|
-
// (<h1>/<strong>/<em>/<code>/<table>/<blockquote>…),并只把 `:root` 变量带过去。
|
|
6
|
-
// 结果:颜色令牌能跟过去,但主题的字体、加粗/行内代码强调色、表格表头下划线等丢光。
|
|
7
|
-
// 这里把主题 CSS 的编辑器选择器改写成语义选择器,再注入导出 HTML / Word 样式。
|
|
8
|
-
|
|
9
|
-
/** .cm-md-* 装饰类 → 导出文档里的语义标签映射(顺序敏感,长的先)。 */
|
|
10
|
-
const CLASS_TO_TAG: Array<[RegExp, string]> = [
|
|
11
|
-
[/\.cm-md-atxheading([1-6])\b/g, 'h$1'],
|
|
12
|
-
[/\.cm-md-strong\b/g, 'strong'],
|
|
13
|
-
[/\.cm-md-em\b/g, 'em'],
|
|
14
|
-
[/\.cm-md-strike\b/g, 's'],
|
|
15
|
-
[/\.cm-md-inlinecode\b/g, 'code'],
|
|
16
|
-
[/\.cm-md-highlight\b/g, 'mark'],
|
|
17
|
-
[/\.cm-md-link\b/g, 'a'],
|
|
18
|
-
[/\.cm-md-url\b/g, 'a'],
|
|
19
|
-
[/\.cm-md-blockquote\b/g, 'blockquote'],
|
|
20
|
-
[/\.cm-md-codeblock(?:-first|-last)?\b/g, 'pre'],
|
|
21
|
-
[/\.cm-md-codeinfo\b/g, 'pre'],
|
|
22
|
-
[/\.cm-md-table-widget\b/g, 'table'],
|
|
23
|
-
[/\.cm-md-table\b/g, 'table'],
|
|
24
|
-
[/\.cm-md-hr\b/g, 'hr'],
|
|
25
|
-
[/\.cm-md-task-checked\b/g, 'li'],
|
|
26
|
-
[/\.cm-md-task\b/g, 'li'],
|
|
27
|
-
[/\.cm-md-li-[0-6]\b/g, 'li'],
|
|
28
|
-
[/\.cm-md-footnote-ref\b/g, 'sup'],
|
|
29
|
-
[/\.cm-md-footnote-def\b/g, 'section'],
|
|
30
|
-
[/\.cm-md-image\b/g, 'img'],
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
interface ThemeTokens {
|
|
34
|
-
vars: Record<string, string>;
|
|
35
|
-
fontFamily: string;
|
|
36
|
-
lineHeight: number | null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** 解析主题 CSS 里的 `:root {}` 变量与 `.cm-content` 上的字体/行高。 */
|
|
40
|
-
function parseThemeTokens(themeCss: string): ThemeTokens {
|
|
41
|
-
const vars: Record<string, string> = {};
|
|
42
|
-
const rootMatch = /:root\s*\{([^}]*)\}/s.exec(themeCss);
|
|
43
|
-
if (rootMatch) {
|
|
44
|
-
const declRe = /(--[a-zA-Z0-9-]+)\s*:\s*([^;]+)/g;
|
|
45
|
-
let m: RegExpExecArray | null;
|
|
46
|
-
while ((m = declRe.exec(rootMatch[1]))) {
|
|
47
|
-
vars[m[1]] = m[2].trim();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 遍历所有 `.cm-content { … }` 块:字体/行高可能不在第一个块里,
|
|
52
|
-
// 优先取含 font-family 的那个(如 writer 主题的整站等宽字体)。
|
|
53
|
-
let fontFamily = '';
|
|
54
|
-
let lineHeight: number | null = null;
|
|
55
|
-
const blockRe = /([^{}]*\.cm-content\b[^{}]*)\{([^}]*)\}/gs;
|
|
56
|
-
let firstBlock: string | null = null;
|
|
57
|
-
let m: RegExpExecArray | null;
|
|
58
|
-
while ((m = blockRe.exec(themeCss))) {
|
|
59
|
-
const body = m[2];
|
|
60
|
-
if (firstBlock === null) firstBlock = body;
|
|
61
|
-
const fontMatch = /font-family\s*:\s*([^;]+)/i.exec(body);
|
|
62
|
-
if (fontMatch) {
|
|
63
|
-
fontFamily = fontMatch[1].trim();
|
|
64
|
-
const lineMatch = /line-height\s*:\s*([0-9.]+)/i.exec(body);
|
|
65
|
-
if (lineMatch) lineHeight = parseFloat(lineMatch[1]);
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (fontFamily === '' && firstBlock !== null) {
|
|
70
|
-
const lineMatch = /line-height\s*:\s*([0-9.]+)/i.exec(firstBlock);
|
|
71
|
-
if (lineMatch) lineHeight = parseFloat(lineMatch[1]);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return { vars, fontFamily, lineHeight };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 把主题 CSS 中针对编辑器 DOM 的规则翻译成导出文档(`.colamd-document` 语义结构)能命中的规则。
|
|
79
|
-
* 返回纯 CSS 文本(不含 <style> 包裹),供注入导出 HTML。
|
|
80
|
-
*/
|
|
81
|
-
export function translateThemeCssForExport(themeCss: string): string {
|
|
82
|
-
if (!themeCss) return '';
|
|
83
|
-
|
|
84
|
-
// 保留 :root 变量块:导出 HTML 里的 :root 是渲染端收集的「运行时变量」,
|
|
85
|
-
// 当 webview body 缺主题类时会退回 colamd 的浅色默认值。注入的样式排在
|
|
86
|
-
// 最后,同特异性下后者胜,能让主题变量稳定覆盖默认值(自定义主题的唯一变量来源)。
|
|
87
|
-
let css = themeCss;
|
|
88
|
-
|
|
89
|
-
// 导出的 <body> 即原编辑器 #editor 容器。
|
|
90
|
-
css = css.replace(/#editor\b/g, 'body');
|
|
91
|
-
|
|
92
|
-
// 编辑器内容容器 → 导出文档容器。
|
|
93
|
-
css = css.replace(/\.cm-content\b/g, '.colamd-document');
|
|
94
|
-
|
|
95
|
-
// 编辑器装饰类 → 语义标签。
|
|
96
|
-
for (const [re, tag] of CLASS_TO_TAG) {
|
|
97
|
-
css = css.replace(re, tag);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return css.trim();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 从 colamd.css(引擎内置样式)里提取主题作用域的正文规则并翻译成导出可用的语义选择器。
|
|
105
|
-
*
|
|
106
|
-
* 背景:各主题的正文差异化规则(加粗/行内代码/标题配色、表格表头、引用块等)以
|
|
107
|
-
* `body.theme-x .cm-content .cm-md-*` 形式写在引擎的 colamd.css 里,导出 HTML 虽然原样
|
|
108
|
-
* 内联了 colamd.css,但导出文档是语义化结构(.colamd-document strong 等),这些规则
|
|
109
|
-
* 全部无法命中 —— 表现为导出的加粗、行内代码等颜色与预览/ColaMD-extend 不一致。
|
|
110
|
-
* 这里按主题抽出这些规则,套用与 translateThemeCssForExport 相同的映射翻译后返回。
|
|
111
|
-
*/
|
|
112
|
-
export function translateEngineThemeCssForExport(colamdCss: string): string {
|
|
113
|
-
if (!colamdCss) return '';
|
|
114
|
-
|
|
115
|
-
// 抽出所有 body.theme-* 作用域、且作用于编辑器内容层的规则块
|
|
116
|
-
const ruleRe = /body\.theme-[a-z0-9-]+[^{}]*\.cm-content[^{}]*\{[^}]*\}/g;
|
|
117
|
-
const rules = colamdCss.match(ruleRe);
|
|
118
|
-
if (!rules || rules.length === 0) return '';
|
|
119
|
-
|
|
120
|
-
const out = rules.join('\n');
|
|
121
|
-
return translateThemeCssForExport(out);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* 深色主题导出 Word 时回落的浅色板,字段与 docx-exporter 的 DEFAULT_STYLE 一致。
|
|
126
|
-
* Word 是白纸,深色主题的浅色正文直接搬过去会看不见(镜像 colamd-extend
|
|
127
|
-
* docx-style.ts 的 LIGHT 常量:浅色跟随主题、深色整体回落浅色)。
|
|
128
|
-
*/
|
|
129
|
-
const LIGHT_TOKENS: Record<string, string> = {
|
|
130
|
-
text: '24292F',
|
|
131
|
-
secondaryText: '656D76',
|
|
132
|
-
link: '0969DA',
|
|
133
|
-
inlineCodeBackground: 'EFF1F3',
|
|
134
|
-
codeBlockBackground: 'F6F8FA',
|
|
135
|
-
codeBlockText: '24292F',
|
|
136
|
-
highlightBackground: 'FFF3BF',
|
|
137
|
-
blockquoteBorder: 'D0D7DE',
|
|
138
|
-
border: 'D0D7DE',
|
|
139
|
-
tableHeaderBackground: 'F6F8FA',
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
/** 取 #rgb / #rrggbb / #rrggbbaa 的主色,取不到返回 null。入参可带 `#`。 */
|
|
143
|
-
function themeHex(value: string | undefined): string | null {
|
|
144
|
-
if (!value) return null;
|
|
145
|
-
const raw = value.trim().replace(/^#/, '');
|
|
146
|
-
if (/^[0-9a-f]{6}$/i.test(raw)) return raw.toUpperCase();
|
|
147
|
-
if (/^[0-9a-f]{3}$/i.test(raw)) return raw.split('').map((digit) => digit + digit).join('').toUpperCase();
|
|
148
|
-
if (/^[0-9a-f]{8}$/i.test(raw)) return raw.slice(0, 6).toUpperCase();
|
|
149
|
-
return null;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** 感知亮度,用来判断主题底色是深是浅。入参不带 `#`。 */
|
|
153
|
-
function hexLuminance(hex: string): number {
|
|
154
|
-
const channel = (index: number): number => (Number.parseInt(hex.slice(index, index + 2), 16) || 0) / 255;
|
|
155
|
-
return 0.2126 * channel(0) + 0.7152 * channel(2) + 0.0722 * channel(4);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* 从主题 CSS 构造 Word 导出用的样式令牌(docx 的 DocxStyle)。只覆盖能可靠取到的字段,
|
|
160
|
-
* 其余由 markdownToDocx 的 normalizeStyle 回退到默认浅色样式。颜色返回原始值(hex),
|
|
161
|
-
* 由 docx-exporter 的 hexColor 归一化。
|
|
162
|
-
*
|
|
163
|
-
* 深色主题(--bg-color 亮度 < 0.5)整体回落浅色板,与 Mermaid 导出挑浅色配色是同一条规则。
|
|
164
|
-
*/
|
|
165
|
-
export function buildDocxStyleFromThemeCss(themeCss: string): Record<string, string | number | null> {
|
|
166
|
-
const { vars, fontFamily, lineHeight } = parseThemeTokens(themeCss);
|
|
167
|
-
|
|
168
|
-
const bg = themeHex(vars['--bg-color']);
|
|
169
|
-
const isDarkTheme = bg !== null && hexLuminance(bg) < 0.5;
|
|
170
|
-
const style: Record<string, string | number | null> = isDarkTheme ? { ...LIGHT_TOKENS } : {};
|
|
171
|
-
if (!isDarkTheme) {
|
|
172
|
-
const put = (key: string, varName: string) => {
|
|
173
|
-
if (vars[varName]) style[key] = vars[varName];
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
put('text', '--text-color');
|
|
177
|
-
put('secondaryText', '--text-secondary');
|
|
178
|
-
put('link', '--link-color');
|
|
179
|
-
put('inlineCodeBackground', '--code-bg');
|
|
180
|
-
put('codeBlockBackground', '--code-block-bg');
|
|
181
|
-
put('codeBlockText', '--code-block-text');
|
|
182
|
-
put('highlightBackground', '--highlight-bg');
|
|
183
|
-
put('blockquoteBorder', '--blockquote-border');
|
|
184
|
-
put('blockquoteBackground', '--blockquote-bg');
|
|
185
|
-
put('border', '--border-color');
|
|
186
|
-
put('tableHeaderBackground', '--table-header-bg');
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// 主题若显式定义了等宽字体栈(如自定义 .themes 主题),一并带上
|
|
190
|
-
if (vars['--font-mono']) style.monoFontStack = vars['--font-mono'];
|
|
191
|
-
|
|
192
|
-
if (fontFamily) style.bodyFontStack = fontFamily;
|
|
193
|
-
if (lineHeight !== null && Number.isFinite(lineHeight)) style.lineHeight = lineHeight;
|
|
194
|
-
|
|
195
|
-
return style;
|
|
1
|
+
// 从「预览所用主题 CSS」中抽取令牌,并把它翻译成导出文档可用的语义样式。
|
|
2
|
+
//
|
|
3
|
+
// 背景:预览里主题的排版/强调色通过 `#editor .cm-content .cm-md-*` 选择器生效;
|
|
4
|
+
// 而 buildExportHTML 会把内容序列化成 `<article class="colamd-document">` 语义 HTML
|
|
5
|
+
// (<h1>/<strong>/<em>/<code>/<table>/<blockquote>…),并只把 `:root` 变量带过去。
|
|
6
|
+
// 结果:颜色令牌能跟过去,但主题的字体、加粗/行内代码强调色、表格表头下划线等丢光。
|
|
7
|
+
// 这里把主题 CSS 的编辑器选择器改写成语义选择器,再注入导出 HTML / Word 样式。
|
|
8
|
+
|
|
9
|
+
/** .cm-md-* 装饰类 → 导出文档里的语义标签映射(顺序敏感,长的先)。 */
|
|
10
|
+
const CLASS_TO_TAG: Array<[RegExp, string]> = [
|
|
11
|
+
[/\.cm-md-atxheading([1-6])\b/g, 'h$1'],
|
|
12
|
+
[/\.cm-md-strong\b/g, 'strong'],
|
|
13
|
+
[/\.cm-md-em\b/g, 'em'],
|
|
14
|
+
[/\.cm-md-strike\b/g, 's'],
|
|
15
|
+
[/\.cm-md-inlinecode\b/g, 'code'],
|
|
16
|
+
[/\.cm-md-highlight\b/g, 'mark'],
|
|
17
|
+
[/\.cm-md-link\b/g, 'a'],
|
|
18
|
+
[/\.cm-md-url\b/g, 'a'],
|
|
19
|
+
[/\.cm-md-blockquote\b/g, 'blockquote'],
|
|
20
|
+
[/\.cm-md-codeblock(?:-first|-last)?\b/g, 'pre'],
|
|
21
|
+
[/\.cm-md-codeinfo\b/g, 'pre'],
|
|
22
|
+
[/\.cm-md-table-widget\b/g, 'table'],
|
|
23
|
+
[/\.cm-md-table\b/g, 'table'],
|
|
24
|
+
[/\.cm-md-hr\b/g, 'hr'],
|
|
25
|
+
[/\.cm-md-task-checked\b/g, 'li'],
|
|
26
|
+
[/\.cm-md-task\b/g, 'li'],
|
|
27
|
+
[/\.cm-md-li-[0-6]\b/g, 'li'],
|
|
28
|
+
[/\.cm-md-footnote-ref\b/g, 'sup'],
|
|
29
|
+
[/\.cm-md-footnote-def\b/g, 'section'],
|
|
30
|
+
[/\.cm-md-image\b/g, 'img'],
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
interface ThemeTokens {
|
|
34
|
+
vars: Record<string, string>;
|
|
35
|
+
fontFamily: string;
|
|
36
|
+
lineHeight: number | null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 解析主题 CSS 里的 `:root {}` 变量与 `.cm-content` 上的字体/行高。 */
|
|
40
|
+
function parseThemeTokens(themeCss: string): ThemeTokens {
|
|
41
|
+
const vars: Record<string, string> = {};
|
|
42
|
+
const rootMatch = /:root\s*\{([^}]*)\}/s.exec(themeCss);
|
|
43
|
+
if (rootMatch) {
|
|
44
|
+
const declRe = /(--[a-zA-Z0-9-]+)\s*:\s*([^;]+)/g;
|
|
45
|
+
let m: RegExpExecArray | null;
|
|
46
|
+
while ((m = declRe.exec(rootMatch[1]))) {
|
|
47
|
+
vars[m[1]] = m[2].trim();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 遍历所有 `.cm-content { … }` 块:字体/行高可能不在第一个块里,
|
|
52
|
+
// 优先取含 font-family 的那个(如 writer 主题的整站等宽字体)。
|
|
53
|
+
let fontFamily = '';
|
|
54
|
+
let lineHeight: number | null = null;
|
|
55
|
+
const blockRe = /([^{}]*\.cm-content\b[^{}]*)\{([^}]*)\}/gs;
|
|
56
|
+
let firstBlock: string | null = null;
|
|
57
|
+
let m: RegExpExecArray | null;
|
|
58
|
+
while ((m = blockRe.exec(themeCss))) {
|
|
59
|
+
const body = m[2];
|
|
60
|
+
if (firstBlock === null) firstBlock = body;
|
|
61
|
+
const fontMatch = /font-family\s*:\s*([^;]+)/i.exec(body);
|
|
62
|
+
if (fontMatch) {
|
|
63
|
+
fontFamily = fontMatch[1].trim();
|
|
64
|
+
const lineMatch = /line-height\s*:\s*([0-9.]+)/i.exec(body);
|
|
65
|
+
if (lineMatch) lineHeight = parseFloat(lineMatch[1]);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (fontFamily === '' && firstBlock !== null) {
|
|
70
|
+
const lineMatch = /line-height\s*:\s*([0-9.]+)/i.exec(firstBlock);
|
|
71
|
+
if (lineMatch) lineHeight = parseFloat(lineMatch[1]);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { vars, fontFamily, lineHeight };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 把主题 CSS 中针对编辑器 DOM 的规则翻译成导出文档(`.colamd-document` 语义结构)能命中的规则。
|
|
79
|
+
* 返回纯 CSS 文本(不含 <style> 包裹),供注入导出 HTML。
|
|
80
|
+
*/
|
|
81
|
+
export function translateThemeCssForExport(themeCss: string): string {
|
|
82
|
+
if (!themeCss) return '';
|
|
83
|
+
|
|
84
|
+
// 保留 :root 变量块:导出 HTML 里的 :root 是渲染端收集的「运行时变量」,
|
|
85
|
+
// 当 webview body 缺主题类时会退回 colamd 的浅色默认值。注入的样式排在
|
|
86
|
+
// 最后,同特异性下后者胜,能让主题变量稳定覆盖默认值(自定义主题的唯一变量来源)。
|
|
87
|
+
let css = themeCss;
|
|
88
|
+
|
|
89
|
+
// 导出的 <body> 即原编辑器 #editor 容器。
|
|
90
|
+
css = css.replace(/#editor\b/g, 'body');
|
|
91
|
+
|
|
92
|
+
// 编辑器内容容器 → 导出文档容器。
|
|
93
|
+
css = css.replace(/\.cm-content\b/g, '.colamd-document');
|
|
94
|
+
|
|
95
|
+
// 编辑器装饰类 → 语义标签。
|
|
96
|
+
for (const [re, tag] of CLASS_TO_TAG) {
|
|
97
|
+
css = css.replace(re, tag);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return css.trim();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 从 colamd.css(引擎内置样式)里提取主题作用域的正文规则并翻译成导出可用的语义选择器。
|
|
105
|
+
*
|
|
106
|
+
* 背景:各主题的正文差异化规则(加粗/行内代码/标题配色、表格表头、引用块等)以
|
|
107
|
+
* `body.theme-x .cm-content .cm-md-*` 形式写在引擎的 colamd.css 里,导出 HTML 虽然原样
|
|
108
|
+
* 内联了 colamd.css,但导出文档是语义化结构(.colamd-document strong 等),这些规则
|
|
109
|
+
* 全部无法命中 —— 表现为导出的加粗、行内代码等颜色与预览/ColaMD-extend 不一致。
|
|
110
|
+
* 这里按主题抽出这些规则,套用与 translateThemeCssForExport 相同的映射翻译后返回。
|
|
111
|
+
*/
|
|
112
|
+
export function translateEngineThemeCssForExport(colamdCss: string): string {
|
|
113
|
+
if (!colamdCss) return '';
|
|
114
|
+
|
|
115
|
+
// 抽出所有 body.theme-* 作用域、且作用于编辑器内容层的规则块
|
|
116
|
+
const ruleRe = /body\.theme-[a-z0-9-]+[^{}]*\.cm-content[^{}]*\{[^}]*\}/g;
|
|
117
|
+
const rules = colamdCss.match(ruleRe);
|
|
118
|
+
if (!rules || rules.length === 0) return '';
|
|
119
|
+
|
|
120
|
+
const out = rules.join('\n');
|
|
121
|
+
return translateThemeCssForExport(out);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 深色主题导出 Word 时回落的浅色板,字段与 docx-exporter 的 DEFAULT_STYLE 一致。
|
|
126
|
+
* Word 是白纸,深色主题的浅色正文直接搬过去会看不见(镜像 colamd-extend
|
|
127
|
+
* docx-style.ts 的 LIGHT 常量:浅色跟随主题、深色整体回落浅色)。
|
|
128
|
+
*/
|
|
129
|
+
const LIGHT_TOKENS: Record<string, string> = {
|
|
130
|
+
text: '24292F',
|
|
131
|
+
secondaryText: '656D76',
|
|
132
|
+
link: '0969DA',
|
|
133
|
+
inlineCodeBackground: 'EFF1F3',
|
|
134
|
+
codeBlockBackground: 'F6F8FA',
|
|
135
|
+
codeBlockText: '24292F',
|
|
136
|
+
highlightBackground: 'FFF3BF',
|
|
137
|
+
blockquoteBorder: 'D0D7DE',
|
|
138
|
+
border: 'D0D7DE',
|
|
139
|
+
tableHeaderBackground: 'F6F8FA',
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** 取 #rgb / #rrggbb / #rrggbbaa 的主色,取不到返回 null。入参可带 `#`。 */
|
|
143
|
+
function themeHex(value: string | undefined): string | null {
|
|
144
|
+
if (!value) return null;
|
|
145
|
+
const raw = value.trim().replace(/^#/, '');
|
|
146
|
+
if (/^[0-9a-f]{6}$/i.test(raw)) return raw.toUpperCase();
|
|
147
|
+
if (/^[0-9a-f]{3}$/i.test(raw)) return raw.split('').map((digit) => digit + digit).join('').toUpperCase();
|
|
148
|
+
if (/^[0-9a-f]{8}$/i.test(raw)) return raw.slice(0, 6).toUpperCase();
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 感知亮度,用来判断主题底色是深是浅。入参不带 `#`。 */
|
|
153
|
+
function hexLuminance(hex: string): number {
|
|
154
|
+
const channel = (index: number): number => (Number.parseInt(hex.slice(index, index + 2), 16) || 0) / 255;
|
|
155
|
+
return 0.2126 * channel(0) + 0.7152 * channel(2) + 0.0722 * channel(4);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 从主题 CSS 构造 Word 导出用的样式令牌(docx 的 DocxStyle)。只覆盖能可靠取到的字段,
|
|
160
|
+
* 其余由 markdownToDocx 的 normalizeStyle 回退到默认浅色样式。颜色返回原始值(hex),
|
|
161
|
+
* 由 docx-exporter 的 hexColor 归一化。
|
|
162
|
+
*
|
|
163
|
+
* 深色主题(--bg-color 亮度 < 0.5)整体回落浅色板,与 Mermaid 导出挑浅色配色是同一条规则。
|
|
164
|
+
*/
|
|
165
|
+
export function buildDocxStyleFromThemeCss(themeCss: string): Record<string, string | number | null> {
|
|
166
|
+
const { vars, fontFamily, lineHeight } = parseThemeTokens(themeCss);
|
|
167
|
+
|
|
168
|
+
const bg = themeHex(vars['--bg-color']);
|
|
169
|
+
const isDarkTheme = bg !== null && hexLuminance(bg) < 0.5;
|
|
170
|
+
const style: Record<string, string | number | null> = isDarkTheme ? { ...LIGHT_TOKENS } : {};
|
|
171
|
+
if (!isDarkTheme) {
|
|
172
|
+
const put = (key: string, varName: string) => {
|
|
173
|
+
if (vars[varName]) style[key] = vars[varName];
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
put('text', '--text-color');
|
|
177
|
+
put('secondaryText', '--text-secondary');
|
|
178
|
+
put('link', '--link-color');
|
|
179
|
+
put('inlineCodeBackground', '--code-bg');
|
|
180
|
+
put('codeBlockBackground', '--code-block-bg');
|
|
181
|
+
put('codeBlockText', '--code-block-text');
|
|
182
|
+
put('highlightBackground', '--highlight-bg');
|
|
183
|
+
put('blockquoteBorder', '--blockquote-border');
|
|
184
|
+
put('blockquoteBackground', '--blockquote-bg');
|
|
185
|
+
put('border', '--border-color');
|
|
186
|
+
put('tableHeaderBackground', '--table-header-bg');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 主题若显式定义了等宽字体栈(如自定义 .themes 主题),一并带上
|
|
190
|
+
if (vars['--font-mono']) style.monoFontStack = vars['--font-mono'];
|
|
191
|
+
|
|
192
|
+
if (fontFamily) style.bodyFontStack = fontFamily;
|
|
193
|
+
if (lineHeight !== null && Number.isFinite(lineHeight)) style.lineHeight = lineHeight;
|
|
194
|
+
|
|
195
|
+
return style;
|
|
196
196
|
}
|