@bytechain.cn/colamd-themes 0.3.0 → 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.
Files changed (39) hide show
  1. package/.openclaw/skills/colamd-themes/SKILL.md +15 -124
  2. package/.trae/rules/colamd-themes.md +15 -124
  3. package/CHANGELOG.md +142 -3
  4. package/README.md +12 -1
  5. package/README_CN.md +12 -1
  6. package/RELEASENOTE.md +212 -0
  7. package/dist/cli.js +278 -2
  8. package/dist/cli.js.map +1 -1
  9. package/dist/export-cli.d.ts +1 -0
  10. package/dist/export-cli.js +208 -176
  11. package/dist/export-cli.js.map +1 -1
  12. package/dist/extractors/url-extractor.d.ts +6 -0
  13. package/dist/extractors/url-extractor.js +51 -2
  14. package/dist/extractors/url-extractor.js.map +1 -1
  15. package/dist/generator.d.ts +4 -0
  16. package/dist/generator.js +74 -7
  17. package/dist/generator.js.map +1 -1
  18. package/dist/renderer.d.ts +1 -1
  19. package/dist/renderer.js +87 -14
  20. package/dist/renderer.js.map +1 -1
  21. package/dist/theme-store.d.ts +7 -6
  22. package/dist/theme-store.js +84 -34
  23. package/dist/theme-store.js.map +1 -1
  24. package/dist/utils/cli-utils.d.ts +55 -0
  25. package/dist/utils/cli-utils.js +80 -0
  26. package/dist/utils/cli-utils.js.map +1 -0
  27. package/package.json +1 -1
  28. package/skills/colamd-themes/SKILL.md +30 -0
  29. package/src/cli.ts +289 -2
  30. package/src/export-cli.ts +248 -193
  31. package/src/extractors/url-extractor.ts +56 -2
  32. package/src/generator.ts +88 -7
  33. package/src/renderer.ts +96 -14
  34. package/src/theme-store.ts +93 -35
  35. package/src/utils/cli-utils.ts +84 -0
  36. package/.claude/skills/colamd-themes-preview.skill.md +0 -333
  37. package/.claude/skills/colamd-themes.skill.md +0 -155
  38. package/.hermes/skills/tools/colamd-themes/SKILL.md +0 -159
  39. package/.opencode/skills/colamd-themes/SKILL.md +0 -162
package/src/cli.ts CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  import { Command } from "commander";
4
4
  import { existsSync, readdirSync, readFileSync } from "node:fs";
5
- import { join, isAbsolute } from "node:path";
5
+ import { join, isAbsolute, dirname, resolve } from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+ import { execSync } from "node:child_process";
6
8
  import chalk from "chalk";
7
9
  import ora from "ora";
8
10
  import { UrlExtractor } from "./extractors/url-extractor.js";
@@ -15,13 +17,210 @@ import { MERMAID_PRESETS } from "./mermaid-presets.js";
15
17
  import { validateSeedPalette, summarizeIssues } from "./validators.js";
16
18
  import type { ThemeStyle } from "./models.js";
17
19
  import { registerExportCommands } from "./export-cli.js";
20
+ import { BUILTIN_THEMES } from "./theme-store.js";
21
+
22
+ /**
23
+ * Dynamically read version from package.json to avoid hardcoding.
24
+ * This ensures CLI --version always matches the published package version.
25
+ */
26
+ function getPackageVersion(): string {
27
+ try {
28
+ const __filename = fileURLToPath(import.meta.url);
29
+ const __dirname = dirname(__filename);
30
+ const pkgPath = join(__dirname, "../package.json");
31
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
32
+ return pkg.version ?? "0.0.0";
33
+ } catch {
34
+ return "0.0.0";
35
+ }
36
+ }
37
+
38
+ /** Font stacks for each built-in theme — parsed dynamically from colamd.css. */
39
+ const THEME_FONTS: Record<string, string[]> = getBuiltinFontStacks();
40
+
41
+ /** Find the path to @bytechain.cn/colamd/dist/lib/colamd.css */
42
+ function findColamdCssPath(): string {
43
+ const candidates = [
44
+ resolve(dirname(new URL(import.meta.url).pathname), "../node_modules/@bytechain.cn/colamd/dist/lib/colamd.css"),
45
+ resolve(process.cwd(), "node_modules/@bytechain.cn/colamd/dist/lib/colamd.css"),
46
+ ];
47
+ for (const p of candidates) {
48
+ if (existsSync(p)) return p;
49
+ }
50
+ return "";
51
+ }
52
+
53
+ /**
54
+ * Parse font-family values for each built-in theme from colamd.css.
55
+ * Dynamic approach: reads `body.theme-{name} #editor .ProseMirror { font-family: ... }`
56
+ * instead of hardcoding font stacks.
57
+ */
58
+ function getBuiltinFontStacks(): Record<string, string[]> {
59
+ const cssPath = findColamdCssPath();
60
+ if (!cssPath) {
61
+ // Fallback hardcoded defaults if colamd.css not found
62
+ return {
63
+ light: ["Noto Sans SC", "sans-serif"],
64
+ dark: ["Noto Sans SC", "sans-serif"],
65
+ elegant: ["LXGW WenKai", "Noto Serif SC", "serif"],
66
+ newsprint: ["Noto Serif SC", "serif"],
67
+ };
68
+ }
69
+
70
+ const css = readFileSync(cssPath, "utf-8");
71
+ const result: Record<string, string[]> = {};
72
+ const names = ["light", "dark", "elegant", "newsprint"];
73
+
74
+ for (const theme of names) {
75
+ // Match: body.theme-elegant #editor .ProseMirror { ... font-family: 'LXGW WenKai', ...; }
76
+ const re = new RegExp(
77
+ `body\\.theme-${theme}\\s+#editor\\s+\\.ProseMirror\\s*\\{[^}]*font-family:\\s*([^;]+)`,
78
+ "i"
79
+ );
80
+ const m = css.match(re);
81
+ if (m) {
82
+ const fonts = m[1].split(",").map((s) => s.trim().replace(/^['"]|['"]$/g, "").trim());
83
+ result[theme] = fonts;
84
+ }
85
+ }
86
+
87
+ // Fill any missing themes with fallback
88
+ for (const theme of names) {
89
+ if (!result[theme]) {
90
+ result[theme] = ["sans-serif"];
91
+ }
92
+ }
93
+
94
+ return result;
95
+ }
96
+
97
+ /**
98
+ * Check if a single font name is available on the system.
99
+ * Multi-platform: fc-match (Linux/macOS/WSL), PowerShell registry (Windows),
100
+ * system_profiler (macOS), universal directory scan fallback.
101
+ */
102
+ function checkFont(fontName: string): string | null {
103
+ // ── try fc-match first (Linux, WSL, macOS with fontconfig) ──
104
+ try {
105
+ const out = execSync(`fc-match "${fontName}" 2>/dev/null`, {
106
+ encoding: "utf8",
107
+ maxBuffer: 1024,
108
+ shell: "/bin/sh",
109
+ }).trim();
110
+ if (out && !out.startsWith("fontconfig error")) {
111
+ const parts = out.split(":").slice(1).join(":").trim();
112
+ if (parts) return parts;
113
+ }
114
+ } catch {}
115
+
116
+ // ── Windows: PowerShell registry query ──
117
+ if (process.platform === "win32") {
118
+ try {
119
+ const safe = fontName.replace(/'/g, "''");
120
+ const ps = `$n='${safe}';$p=Get-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts' 2>$null;foreach($v in $p.PSObject.Properties){if($v.Value -like"*$n*"){Write-Output $v.Value;exit 0}}`;
121
+ const out = execSync(
122
+ `powershell -NoProfile -Command "${ps.replace(/"/g, '\\"')}"`,
123
+ { encoding: "utf8", timeout: 5000 }
124
+ ).trim();
125
+ if (out && out !== "NOT_FOUND") return out;
126
+ } catch {}
127
+ }
128
+
129
+ // ── macOS: system_profiler (slow but reliable) ──
130
+ if (process.platform === "darwin") {
131
+ try {
132
+ const kw = fontName.toLowerCase();
133
+ const out = execSync(
134
+ `system_profiler SPFontsDataType 2>/dev/null | grep -i "${kw}"`,
135
+ { encoding: "utf8", maxBuffer: 1024 * 1024, timeout: 10000 }
136
+ ).trim();
137
+ if (out) return out.split("\n")[0].trim();
138
+ } catch {}
139
+ }
140
+
141
+ // ── Universal: scan known font directories ──
142
+ const fontDirs: string[] = [];
143
+ if (process.platform === "win32") {
144
+ const sysRoot = process.env.SystemRoot || "C:\\Windows";
145
+ fontDirs.push(join(sysRoot, "Fonts"));
146
+ } else if (process.platform === "darwin") {
147
+ const home = process.env.HOME || "";
148
+ fontDirs.push("/Library/Fonts", "/System/Library/Fonts");
149
+ if (home) fontDirs.push(join(home, "Library", "Fonts"));
150
+ } else {
151
+ // Linux / WSL
152
+ fontDirs.push(
153
+ "/usr/share/fonts",
154
+ "/usr/local/share/fonts",
155
+ );
156
+ const home = process.env.HOME || "";
157
+ if (home) fontDirs.push(join(home, ".local", "share", "fonts"));
158
+ }
159
+
160
+ const kw = fontName.replace(/[()\[\]]/g, "").split(/\s+/).filter(Boolean);
161
+ if (kw.length === 0) return null;
162
+
163
+ for (const dir of fontDirs) {
164
+ if (!existsSync(dir)) continue;
165
+ try {
166
+ const files = readdirSync(dir, { recursive: true });
167
+ const found = files.find((f) => {
168
+ const lower = typeof f === "string" ? f.toLowerCase() : "";
169
+ return /\.(ttf|ttc|otf|woff2?)$/i.test(lower) && kw.every((k) => lower.includes(k.toLowerCase()));
170
+ });
171
+ if (found) return typeof found === "string" ? found : String(found);
172
+ } catch {}
173
+ }
174
+
175
+ return null;
176
+ }
177
+
178
+ /**
179
+ * Parse unique font-family names from a CSS string.
180
+ * Extracts font names from all `font-family: ...` declarations,
181
+ * filters out CSS variable references (var(--...)).
182
+ */
183
+ function parseFontsFromCSS(css: string): string[] {
184
+ const names = new Set<string>();
185
+ const isKeyword = /^(inherit|initial|unset|revert)$/i;
186
+ // Match all font-family declarations
187
+ const re = /font-family:\s*([^;{}]+)/gi;
188
+ let match: RegExpExecArray | null;
189
+ while ((match = re.exec(css)) !== null) {
190
+ const value = match[1];
191
+ // Split by comma, trim quotes, skip var() references
192
+ for (const part of value.split(",")) {
193
+ let name = part.trim().replace(/^['"]|['"]$/g, "").trim();
194
+ // Strip !important, trailing semicolons
195
+ name = name.replace(/\s*!important\s*$/i, "").trim();
196
+ if (name && !name.startsWith("var(") && !isKeyword.test(name)) {
197
+ names.add(name);
198
+ }
199
+ }
200
+ }
201
+ return Array.from(names);
202
+ }
203
+
204
+ let configCache: { themes: Record<string, string> } | null = null;
205
+
206
+ async function loadCustomThemes(): Promise<Record<string, string>> {
207
+ if (configCache) return configCache.themes;
208
+ try {
209
+ const { getConfig } = await import("./theme-store.js");
210
+ const config = await getConfig();
211
+ configCache = config;
212
+ return config.themes;
213
+ } catch {
214
+ return {};
215
+ }
216
+ }
18
217
 
19
218
  const program = new Command();
20
219
 
21
220
  program
22
221
  .name("colamd-themes")
23
222
  .description("ColaMD Theme Development Tool — extract formatting and generate v3.0 paradigm CSS themes")
24
- .version("0.2.0");
223
+ .version(getPackageVersion());
25
224
 
26
225
  // ── Shared extract options builder ──
27
226
 
@@ -181,6 +380,94 @@ program
181
380
  if (issues.filter((i) => i.level === "MUST" || i.level === "MUST_NOT").length > 0) process.exit(1);
182
381
  });
183
382
 
383
+ // ── check-fonts ──
384
+ program
385
+ .command("check-fonts")
386
+ .description("Check whether built-in/custom theme fonts are installed on this system")
387
+ .argument("[theme]", "Theme name (built-in, registered custom, or .css file path); omit to check all")
388
+ .action(async (theme?: string) => {
389
+ const customThemes = await loadCustomThemes();
390
+ let fontSources: { name: string; fonts: string[] }[] = [];
391
+
392
+ if (theme) {
393
+ // 1. Check if it's a built-in theme
394
+ if (BUILTIN_THEMES.includes(theme as any)) {
395
+ fontSources.push({ name: theme, fonts: THEME_FONTS[theme] });
396
+ }
397
+ // 2. Check if it's a registered custom theme
398
+ else if (customThemes[theme]) {
399
+ const css = readFileSync(customThemes[theme], "utf-8");
400
+ const fonts = parseFontsFromCSS(css);
401
+ if (fonts.length === 0) {
402
+ console.log(chalk.yellow(` 主题 "${theme}" 中未找到 font-family 声明`));
403
+ }
404
+ fontSources.push({ name: `${theme} (custom)`, fonts });
405
+ }
406
+ // 3. Treat as direct CSS file path
407
+ else if (existsSync(theme) && theme.endsWith(".css")) {
408
+ const css = readFileSync(theme, "utf-8");
409
+ const fonts = parseFontsFromCSS(css);
410
+ if (fonts.length === 0) {
411
+ console.log(chalk.yellow(` 文件 ${theme} 中未找到 font-family 声明`));
412
+ }
413
+ fontSources.push({ name: theme, fonts });
414
+ }
415
+ // 4. Unknown
416
+ else {
417
+ console.error(chalk.red(`Unknown theme: "${theme}".\n Built-in: ${BUILTIN_THEMES.join(", ")}\n Registered: ${Object.keys(customThemes).join(", ") || "(none)"}`));
418
+ process.exit(1);
419
+ }
420
+ } else {
421
+ // No argument: built-in themes + all registered custom themes
422
+ for (const t of Object.keys(THEME_FONTS)) {
423
+ fontSources.push({ name: t, fonts: THEME_FONTS[t] });
424
+ }
425
+ for (const [name, cssPath] of Object.entries(customThemes)) {
426
+ const css = readFileSync(cssPath, "utf-8");
427
+ const fonts = parseFontsFromCSS(css);
428
+ if (fonts.length > 0) {
429
+ fontSources.push({ name: `${name} (custom)`, fonts });
430
+ }
431
+ }
432
+ }
433
+
434
+ console.log(chalk.bold("══ 主题字体检查 ══"));
435
+ let total = 0;
436
+ let missing = 0;
437
+
438
+ for (const { name, fonts } of fontSources) {
439
+ if (fonts.length === 0) continue;
440
+ console.log(`\n 主题:${chalk.cyan(name)}`);
441
+ for (const f of fonts) {
442
+ total++;
443
+ const result = checkFont(f);
444
+ if (result) {
445
+ console.log(` ${chalk.green("✔")} ${f} -> ${result}`);
446
+ } else {
447
+ console.log(` ${chalk.red("✘")} ${f} -> 未找到`);
448
+ missing++;
449
+ }
450
+ }
451
+ }
452
+
453
+ console.log("");
454
+ if (missing > 0) {
455
+ console.log(`${chalk.yellow(`⚠ ${missing}/${total}`)} 个字体缺失,导出时可能回退到系统默认字体`);
456
+ console.log(" 建议安装:");
457
+ if (process.platform === "win32") {
458
+ console.log(" Windows: 从 https://github.com/lxgw/LxgwWenKai/releases 下载字体双击安装");
459
+ } else if (process.platform === "darwin") {
460
+ console.log(" brew install --cask font-lxgw-wenkai");
461
+ console.log(" brew install --cask font-noto-serif-cjk-sc");
462
+ } else {
463
+ console.log(" sudo apt-get install -y fonts-noto-cjk-extra # Linux/WSL");
464
+ }
465
+ process.exit(1);
466
+ } else {
467
+ console.log(chalk.green("✔ 所有主题字体检查通过"));
468
+ }
469
+ });
470
+
184
471
  // ── Export commands (set-theme, export-html, export-pdf, export) ──
185
472
  registerExportCommands(program);
186
473