Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.
HTML agent-skillsai-agentsarchitecture-diagramclaude-codeclaude-skillscodexcoding-agentsdeepseek-harnessdeveloper-toolsdiagramsdiagrams-as-codedsh-pluginflowchartllmmermaidopencodesequence-diagramsoftware-architecturesystem-designvisualization
Release v2.4.0: dual-theme self-contained SVG export
tt-a1ice1bcc5a6d07
diff --git a/.gitignore b/.gitignoreindex 8b978ac9..1dd711a2 100644--- a/.gitignore+++ b/.gitignore@@ -1,2 +1,3 @@.DS_Store*.DS_Store+.harnessed/diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ef99d824..ed1b9d83 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,6 +2,16 @@All notable changes are documented here. Format loosely follows [Keep a Changelog](https://keepachangelog.com/).+## [2.4.0] — 2026-04-18++### Changed+- **Download SVG is now dual-theme self-contained.** The exported `.svg` ships with BOTH dark and light CSS variable sets plus a `@media (prefers-color-scheme: light)` rule. Embedding the file via `<img src="x.svg">` in a GitHub README (or any host that exposes a color scheme) makes it follow the reader's dark/light preference automatically — no more shipping two PNGs wrapped in `<picture>`. The root `<svg>` no longer carries a `data-theme` attribute, so the media query can actually take effect; downstream consumers can still force a theme via `svg[data-theme="light"]` / `svg[data-theme="dark"]`.+- **`serializeSvg(scale, opts)`** grew a second argument: `opts.autoTheme: true` switches on the new dual-theme path. The raster pipeline (PNG / JPEG / WebP / Copy to clipboard) explicitly does NOT set it, so those paths keep locking colors to the viewer's current theme — canvas rasterization needs deterministic output and a raster can't react to `prefers-color-scheme` after encoding.+- **Background rect in auto-theme mode** now carries `class="c-bg-rect"` + `rect.c-bg-rect { fill: var(--bg); }` instead of a baked-in color, so the backdrop swaps along with the variables.++### Why+The v2.0 SVG export was good, but single-theme — users who wanted README embedding still had to export one PNG per theme and wrap them in `<picture><source media="(prefers-color-scheme: dark)">`. A single SVG that already knows both themes cuts that down to ``.+## [2.3.1] — 2026-04-15### Fixeddiff --git a/README.md b/README.mdindex c96d9527..29dde9d7 100644--- a/README.md+++ b/README.md@@ -8,12 +8,13 @@ Archify 是一个 [Claude Skill](https://support.claude.com/en/articles/12512180- **内置主题切换** —— 深色 / 浅色一键切,浏览器记住偏好- **一键复制到剪贴板** —— 直接贴到 Slack、飞书、微信、Notion、GitHub issue- **导出图片超清晰** —— PNG / JPEG / WebP 全部由浏览器在 4× 源分辨率下**原生光栅化**(不是位图放大,没有糊),或导出 SVG 做真矢量+- **SVG 自动跟系统深浅色** —— 导出的 SVG 内嵌两套变量 + `@media (prefers-color-scheme)`,贴到 GitHub README 里,读者切深浅色图跟着切(不用两张 PNG + `<picture>` 包起来)- **单文件 HTML** —— 零依赖,发一个文件就能分享- **聊天迭代** —— "把 Redis 挪到左边"、"鉴权服务换成玫红"、"加个 Kafka"-+<p align="right"><a href="./README_EN.md">English</a></p>@@ -37,20 +38,20 @@ Export 菜单 —— 复制到剪贴板 + 四种格式下载:## 版本演进-Archify 基于 [Cocoon-AI/architecture-diagram-generator](https://github.com/Cocoon-AI/architecture-diagram-generator) v1.0(只有深色主题的 HTML 输出)fork 重写。**2.0** 把模板重构成 CSS 变量驱动的可主题化系统,加入客户端导出流水线。**2.1** 加入剪贴板复制 + 键盘导航。**2.2** 加入打印样式 + 本地字体回退。**2.3** 修了一个存在已久的位图升采样 bug,所有光栅导出改为 4× 原生渲染(同时移除了 v2.1 引入的 1×/2×/4× 选择器 —— 那个选择器只是在诱导用户选出更糊的图)。--| 能力 | v1.0 | 2.0 | 2.1 | 2.2 | 2.3 |-|---|---|---|---|---|---|-| 深色主题 | ✓ | ✓ | ✓ | ✓ | ✓ |-| 浅色主题 | — | 切换 | 切换 | 切换 | 切换 + <kbd>T</kbd> 快捷键 |-| PNG / JPEG / WebP 下载 | 手动截图 | 2× 位图放大 | 1×/2×/4× 选择器(仍是放大)| 同 | **4× 原生光栅化,不糊** |-| SVG 下载 | — | 矢量 + 内联样式 | 同 | 同 | 同 |-| 复制 PNG 到剪贴板 | — | — | ✓ | 同 | 同(始终 4×)|-| 键盘快捷键 | — | — | <kbd>T</kbd>/<kbd>E</kbd> + 菜单导航 | 同 | 同 |-| 可访问性 | — | — | ARIA + focus-visible | 同 | 同 |-| 打印样式表 | — | — | — | ✓ | ✓(+ 横向 + 2 列卡片)|-| 导出时本地字体回退 | — | — | — | ✓ | ✓ |-| 样式模型 | 内联 `fill` / `stroke` | CSS 变量 + 语义 class | 同 | 同 | 同 |+Archify 基于 [Cocoon-AI/architecture-diagram-generator](https://github.com/Cocoon-AI/architecture-diagram-generator) v1.0(只有深色主题的 HTML 输出)fork 重写。**2.0** 把模板重构成 CSS 变量驱动的可主题化系统,加入客户端导出流水线。**2.1** 加入剪贴板复制 + 键盘导航。**2.2** 加入打印样式 + 本地字体回退。**2.3** 修了一个存在已久的位图升采样 bug,所有光栅导出改为 4× 原生渲染(同时移除了 v2.1 引入的 1×/2×/4× 选择器 —— 那个选择器只是在诱导用户选出更糊的图)。**2.4** SVG 导出升级成双主题自持版 —— 同一个 `.svg` 文件贴在 GitHub README 里,读者切深浅色图会自己跟着切。++| 能力 | v1.0 | 2.0 | 2.1 | 2.2 | 2.3 | 2.4 |+|---|---|---|---|---|---|---|+| 深色主题 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |+| 浅色主题 | — | 切换 | 切换 | 切换 | 切换 + <kbd>T</kbd> 快捷键 | 同 |+| PNG / JPEG / WebP 下载 | 手动截图 | 2× 位图放大 | 1×/2×/4× 选择器(仍是放大)| 同 | **4× 原生光栅化,不糊** | 同 |+| SVG 下载 | — | 矢量 + 内联样式(单主题)| 同 | 同 | 同 | **双主题自持**(`@media prefers-color-scheme`)|+| 复制 PNG 到剪贴板 | — | — | ✓ | 同 | 同(始终 4×)| 同 |+| 键盘快捷键 | — | — | <kbd>T</kbd>/<kbd>E</kbd> + 菜单导航 | 同 | 同 | 同 |+| 可访问性 | — | — | ARIA + focus-visible | 同 | 同 | 同 |+| 打印样式表 | — | — | — | ✓ | ✓(+ 横向 + 2 列卡片)| 同 |+| 导出时本地字体回退 | — | — | — | ✓ | ✓ | 同 |+| 样式模型 | 内联 `fill` / `stroke` | CSS 变量 + 语义 class | 同 | 同 | 同 | 同 |## 快速上手@@ -122,7 +123,7 @@ unzip archify.zip -d ./.claude/skills/|---|---|| **Copy to clipboard** | 当前图以 PNG 格式直接进系统剪贴板,粘贴到 Slack / Notion / 飞书 / GitHub / Figma || **Download PNG / JPEG / WebP** | 保存为光栅图。JPEG / WebP 会用当前主题的背景色填充(无透明);PNG 保留透明度 |-| **Download SVG** | 矢量导出,所有样式内联。可以在 Figma / Illustrator 里继续编辑。无损缩放 |+| **Download SVG** | 矢量导出,所有样式内联,**双主题自持**。内嵌了 dark + light 两套 CSS 变量 + `@media (prefers-color-scheme)` 规则 —— 同一个 `.svg` 贴到 GitHub README / 博客,读者切深浅色图自己跟着切。可以在 Figma / Illustrator 里继续编辑。无损缩放 |所有光栅导出(复制 + PNG/JPEG/WebP)都由浏览器在 **4× 源分辨率**下原生光栅化 —— 序列化后的 SVG 被设为 `4 × viewBox` 大小,浏览器直接在该分辨率下光栅化矢量,canvas 按自然大小绘制(没有位图升采样)。结果是视网膜屏、演示幻灯、打印输出都真正清晰。@@ -213,6 +214,7 @@ Archify 2.x 贡献:- 主题切换 + `localStorage` 持久化 + `prefers-color-scheme` 默认- 内置 PNG / JPEG / WebP / SVG 导出菜单 + 复制到剪贴板- 4× 原生光栅化(修复升采样导致的模糊)+- SVG 导出双主题自持(单文件跟随宿主 `prefers-color-scheme`)- 键盘导航 + 可访问性语义- 打印样式表 + 本地字体回退- 更新后的 `SKILL.md` 引导 Claude 使用 class 化、可主题化的画图方式diff --git a/README_EN.md b/README_EN.mdindex 76404e67..e149d546 100644--- a/README_EN.md+++ b/README_EN.md@@ -8,12 +8,13 @@ Archify is a [Claude Skill](https://support.claude.com/en/articles/12512180-usin- **Built-in theme toggle** — one click between dark and light, persists across sessions- **Copy PNG to clipboard** — one click, paste straight into Slack / Notion / GitHub- **Ultra-crisp image export** — PNG / JPEG / WebP rendered natively at 4× source resolution (no upsampling blur), or SVG for true vector+- **SVG follows system dark/light** — exported SVGs ship with both variable sets + `@media (prefers-color-scheme)`, so dropping one into a GitHub README makes it follow the reader's color preference (no more two PNGs wrapped in `<picture>`)- **Self-contained HTML** — zero dependencies, share by sending the file- **Iterate by chat** — "add Redis", "move auth to the left", "use emerald for the API"-+<p align="right"><a href="./README.md">中文</a></p>@@ -37,20 +38,20 @@ Live example: [`examples/web-app.html`](examples/web-app.html) — open in a bro## What's new-Archify is based on [Cocoon-AI/architecture-diagram-generator](https://github.com/Cocoon-AI/architecture-diagram-generator) v1.0 (dark-only, HTML output). **2.0** rewrote the template around a themeable CSS-variable system and added a client-side export pipeline. **2.1** added copy-to-clipboard + keyboard nav. **2.2** added a print stylesheet + local-font fallback. **2.3** fixed a long-standing upsampling bug and made every raster export genuinely sharp at 4× source resolution (the 1× / 2× / 4× selector introduced in 2.1 was removed at the same time — it only encouraged picking a soft-looking scale).--| Feature | v1.0 | 2.0 | 2.1 | 2.2 | 2.3 |-|---|---|---|---|---|---|-| Dark theme | Yes | Yes | Yes | Yes | Yes |-| Light theme | — | Toggle | Toggle | Toggle | Toggle + <kbd>T</kbd> shortcut |-| PNG / JPEG / WebP download | manual screenshot | 2× bitmap-upsampled | 1× / 2× / 4× selector (still upsampled) | same | **4× rasterized natively — no blur** |-| SVG download | — | Vector, styles inlined | Same | Same | Same |-| Copy PNG to clipboard | — | — | Yes | Same | Yes (always 4×) |-| Keyboard shortcuts | — | — | <kbd>T</kbd> / <kbd>E</kbd> + menu nav | Same | Same |-| Accessibility | — | — | ARIA + focus-visible | Same | Same |-| Print stylesheet | — | — | — | Yes | Yes (+ landscape + 2-col cards) |-| Local-font fallback on export | — | — | — | Yes | Yes |-| Styling model | Inline `fill` / `stroke` | CSS custom properties + semantic classes | Same | Same | Same |+Archify is based on [Cocoon-AI/architecture-diagram-generator](https://github.com/Cocoon-AI/architecture-diagram-generator) v1.0 (dark-only, HTML output). **2.0** rewrote the template around a themeable CSS-variable system and added a client-side export pipeline. **2.1** added copy-to-clipboard + keyboard nav. **2.2** added a print stylesheet + local-font fallback. **2.3** fixed a long-standing upsampling bug and made every raster export genuinely sharp at 4× source resolution (the 1× / 2× / 4× selector introduced in 2.1 was removed at the same time — it only encouraged picking a soft-looking scale). **2.4** upgraded the SVG export to a dual-theme self-contained file — drop the same `.svg` into a GitHub README and it follows the reader's dark/light preference automatically.++| Feature | v1.0 | 2.0 | 2.1 | 2.2 | 2.3 | 2.4 |+|---|---|---|---|---|---|---|+| Dark theme | Yes | Yes | Yes | Yes | Yes | Yes |+| Light theme | — | Toggle | Toggle | Toggle | Toggle + <kbd>T</kbd> shortcut | Same |+| PNG / JPEG / WebP download | manual screenshot | 2× bitmap-upsampled | 1× / 2× / 4× selector (still upsampled) | same | **4× rasterized natively — no blur** | Same |+| SVG download | — | Vector, styles inlined (single theme) | Same | Same | Same | **Dual-theme self-contained** (`@media prefers-color-scheme`) |+| Copy PNG to clipboard | — | — | Yes | Same | Yes (always 4×) | Same |+| Keyboard shortcuts | — | — | <kbd>T</kbd> / <kbd>E</kbd> + menu nav | Same | Same | Same |+| Accessibility | — | — | ARIA + focus-visible | Same | Same | Same |+| Print stylesheet | — | — | — | Yes | Yes (+ landscape + 2-col cards) | Same |+| Local-font fallback on export | — | — | — | Yes | Yes | Same |+| Styling model | Inline `fill` / `stroke` | CSS custom properties + semantic classes | Same | Same | Same | Same |## Quick start@@ -123,7 +124,7 @@ Open the generated HTML in any browser. Top-right you'll see two buttons:|---|---|| **Copy PNG** | Puts a PNG of the current diagram straight on your clipboard. Paste into Slack, Notion, GitHub, Figma. || **Download PNG / JPEG / WebP** | Saves a raster image. JPEG/WebP are painted over the current theme's background (no alpha); PNG keeps transparency. |-| **Download SVG** | Vector export with all styles inlined. Edit in Figma / Illustrator. Scales losslessly. |+| **Download SVG** | Vector export with all styles inlined, **dual-theme self-contained**. The file ships with both dark and light CSS variable sets plus a `@media (prefers-color-scheme)` rule — drop the same `.svg` into a GitHub README or blog and it follows the reader's preference automatically. Still editable in Figma / Illustrator. Scales losslessly. |Every raster export (Copy PNG, Download PNG/JPEG/WebP) is rendered natively by the browser at **4× the diagram's intrinsic resolution** — the serialized SVG is given a `width`/`height` of `4 × viewBox`, rasterized by the browser at that resolution, and drawn to the canvas at natural size (no upsampling). This produces genuinely crisp output for retina displays, slides, and print. There is no scale dial — maximum sharpness is the default and the only option.@@ -206,10 +207,14 @@ Each color has coordinated dark-mode and light-mode variants that switch togetheArchify is a fork / rewrite of [**Cocoon-AI/architecture-diagram-generator**](https://github.com/Cocoon-AI/architecture-diagram-generator) (MIT, v1.0) by [Cocoon AI](mailto:[email protected]). The original's clean visual design — color palette, grid background, summary-card layout, JetBrains Mono typography — is preserved. All credit for the original aesthetic belongs to them.-Archify 2.0 contributes:+Archify 2.x contributes:- Refactor of the template to a CSS-variable theme system (dark + light)- Theme toggle + `localStorage` persistence + `prefers-color-scheme` default-- Built-in PNG / JPEG / WebP / SVG export menu+- Built-in PNG / JPEG / WebP / SVG export menu + copy to clipboard+- 4× native rasterization (fixes upsampling blur)+- Dual-theme self-contained SVG export (single file follows the host's `prefers-color-scheme`)+- Keyboard navigation + accessibility semantics+- Print stylesheet + local-font fallback- Updated `SKILL.md` to guide Claude toward class-based (themeable) diagramsBoth projects are MIT-licensed.diff --git a/archify.zip b/archify.zipindex 4283b1ba..044c71e9 100644Binary files a/archify.zip and b/archify.zip differdiff --git a/archify/SKILL.md b/archify/SKILL.mdindex b919afde..bc0584d2 100644--- a/archify/SKILL.md+++ b/archify/SKILL.md@@ -3,7 +3,7 @@ name: archifydescription: Create professional architecture diagrams as standalone HTML files with SVG graphics, a built-in dark/light theme toggle, and one-click export to PNG / JPEG / WebP / SVG. Use when the user asks for system architecture diagrams, infrastructure diagrams, cloud architecture visualizations, security diagrams, network topology diagrams, or any technical diagram showing system components and their relationships.license: MITmetadata:- version: "2.3"+ version: "2.4"author: tt-a1ibased_on: Cocoon-AI/architecture-diagram-generator (MIT, v1.0)---@@ -15,7 +15,7 @@ Create professional technical architecture diagrams as self-contained HTML filesEvery diagram this skill produces ships with:- A **Dark / Light theme toggle** (top-right, persists in `localStorage`, respects `prefers-color-scheme` on first visit).-- An **Export menu** with **Copy PNG to clipboard** plus downloads for **PNG / JPEG / WebP** (all rasterized natively at 4× source resolution for maximum sharpness) and **SVG** (vector, styles inlined) — rendered entirely in-browser, no server.+- An **Export menu** with **Copy PNG to clipboard** plus downloads for **PNG / JPEG / WebP** (all rasterized natively at 4× source resolution for maximum sharpness) and **SVG** (vector, styles inlined). The SVG download is **dual-theme self-contained**: it ships with both dark and light variable sets plus a `@media (prefers-color-scheme)` rule, so embedding it in a GitHub README (or any `<img>` host that exposes a color scheme) makes it follow the reader's dark/light preference automatically. All rendering happens in-browser, no server.- A **CSS-variable-driven color system** so both themes remain visually consistent with the same SVG markup.## The Cardinal Rule: Use CSS Classes, Not Inline Colors@@ -230,4 +230,4 @@ Produce a single self-contained `.html` file with:- Inline SVG (no external images)- Small amount of embedded JS (theme toggle + export) — keep as-is from template-The file should render correctly when opened directly in any modern browser. The **Export** menu should cleanly copy PNG to the clipboard, download PNG / JPEG / WebP (all at 4× source resolution, rendered natively by the browser — no bitmap upsampling), and download a standalone SVG with inlined styles.+The file should render correctly when opened directly in any modern browser. The **Export** menu should cleanly copy PNG to the clipboard, download PNG / JPEG / WebP (all at 4× source resolution, rendered natively by the browser — no bitmap upsampling), and download a **dual-theme self-contained SVG** whose colors follow the embedding host's `prefers-color-scheme` (dark by default; swaps to light under a light host; `svg[data-theme="..."]` still works as a manual override).diff --git a/archify/assets/template.html b/archify/assets/template.htmlindex 7ff53539..70256ac0 100644--- a/archify/assets/template.html+++ b/archify/assets/template.html@@ -679,15 +679,24 @@/*** Serialize the main SVG into a standalone document.- * Inlines the host CSS + the current theme's resolved variables so the- * serialized SVG renders correctly both as a standalone file and when- * rasterized via an Image -> canvas pipeline (canvas does not read the- * host document's CSS).+ *+ * Two modes:+ * - Default (opts.autoTheme=false) — locks the serialized SVG to the+ * viewer's current theme. Used by the raster pipeline+ * (PNG/JPEG/WebP/clipboard) because canvas rasterization needs+ * deterministic colors; a raster cannot react to+ * prefers-color-scheme after encoding.+ * - autoTheme=true — emits BOTH dark and light variable sets plus a+ * `@media (prefers-color-scheme)` rule so the resulting SVG+ * self-themes when embedded in GitHub READMEs or other hosts that+ * expose a color scheme. Used for "Download SVG".*/- function serializeSvg(scale) {+ function serializeSvg(scale, opts) {// scale: integer multiplier for intrinsic SVG pixel dimensions used by// the raster path. Defaults to 1 (natural size) for SVG download.scale = scale || 1;+ opts = opts || {};+ var autoTheme = opts.autoTheme === true;var svg = document.querySelector('.diagram-container svg');var clone = svg.cloneNode(true);@@ -701,13 +710,10 @@clone.setAttribute('height', vb.height * scale);clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');- var theme = document.documentElement.getAttribute('data-theme') || 'dark';var hostStyle = Array.prototype.map.call(document.querySelectorAll('style'), function (s) {return s.textContent;}).join('\n');- var themeHost = document.querySelector('[data-theme="' + theme + '"]') || document.documentElement;- var computed = getComputedStyle(themeHost);var varNames = ['--bg','--grid','--text','--text-muted','--text-dim','--arrow','--arrow-emphasis','--mask',@@ -719,9 +725,23 @@'--messagebus-fill','--messagebus-stroke','--external-fill','--external-stroke'];- var vars = varNames.map(function (n) {- return n + ': ' + computed.getPropertyValue(n).trim() + ';';- }).join(' ');++ // Resolve the full variable set for a given data-theme via an+ // off-DOM probe, independent of what the viewer is currently set to.+ function resolveVars(themeAttr) {+ var probe = document.createElement('div');+ probe.setAttribute('data-theme', themeAttr);+ probe.style.cssText = 'position:absolute;width:0;height:0;visibility:hidden;';+ document.body.appendChild(probe);+ try {+ var c = getComputedStyle(probe);+ return varNames.map(function (n) {+ return n + ': ' + c.getPropertyValue(n).trim() + ';';+ }).join(' ');+ } finally {+ document.body.removeChild(probe);+ }+ }// Prepend a local()-only @font-face block for each weight so that the// sandboxed image-rendering context used during PNG/JPEG/WebP export@@ -734,21 +754,57 @@"; src: local('JetBrains Mono'), local('JetBrainsMono-Regular'); }";}).join('\n');- // IMPORTANT: inject the resolved variables AFTER hostStyle, otherwise- // hostStyle's ":root, [data-theme=\"dark\"] { ... }" rule overrides our- // chosen theme via later-in-cascade equal-specificity. Keep this order.var style = document.createElementNS('http://www.w3.org/2000/svg', 'style');- style.textContent =- fontFallback + "\n" +- "svg { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; }\n" +- hostStyle + "\n" +- ":root, svg { " + vars + " }\n";- clone.insertBefore(style, clone.firstChild);-var bgRect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');bgRect.setAttribute('width', '100%');bgRect.setAttribute('height', '100%');- bgRect.setAttribute('fill', computed.getPropertyValue('--bg').trim() || '#ffffff');++ if (autoTheme) {+ // Dual-theme SVG. Dark is the default (so hosts without+ // prefers-color-scheme still render), light swaps in via media+ // query, and svg[data-theme="..."] still lets downstream+ // consumers force a specific theme.+ var darkVars = resolveVars('dark');+ var lightVars = resolveVars('light');++ style.textContent =+ fontFallback + "\n" ++ "svg { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; }\n" ++ hostStyle + "\n" ++ ":root, svg { " + darkVars + " }\n" ++ "@media (prefers-color-scheme: light) { :root, svg { " + lightVars + " } }\n" ++ "svg[data-theme=\"light\"] { " + lightVars + " }\n" ++ "svg[data-theme=\"dark\"] { " + darkVars + " }\n" ++ "rect.c-bg-rect { fill: var(--bg); }\n";++ // Don't lock the serialized SVG to the viewer's current theme.+ clone.removeAttribute('data-theme');+ // Background follows the CSS variable, not a fixed color, so it+ // swaps with the media query.+ bgRect.setAttribute('class', 'c-bg-rect');+ } else {+ // Raster path: lock to the viewer's current theme.+ var theme = document.documentElement.getAttribute('data-theme') || 'dark';+ var themeHost = document.querySelector('[data-theme="' + theme + '"]') || document.documentElement;+ var computed = getComputedStyle(themeHost);+ var vars = varNames.map(function (n) {+ return n + ': ' + computed.getPropertyValue(n).trim() + ';';+ }).join(' ');++ // IMPORTANT: inject the resolved variables AFTER hostStyle,+ // otherwise hostStyle's ":root, [data-theme=\"dark\"] { ... }" rule+ // overrides our chosen theme via later-in-cascade equal-specificity.+ // Keep this order.+ style.textContent =+ fontFallback + "\n" ++ "svg { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; }\n" ++ hostStyle + "\n" ++ ":root, svg { " + vars + " }\n";++ bgRect.setAttribute('fill', computed.getPropertyValue('--bg').trim() || '#ffffff');+ }++ clone.insertBefore(style, clone.firstChild);clone.insertBefore(bgRect, style.nextSibling);return {@@ -933,7 +989,7 @@var base = diagramFilename();close(false);return (format === 'svg'- ? Promise.resolve(serializeSvg()).then(function (d) {+ ? Promise.resolve(serializeSvg(1, { autoTheme: true })).then(function (d) {download(new Blob([d.svgString], { type: 'image/svg+xml;charset=utf-8' }), base + '.svg');}): rasterize(format).then(function (blob) {diff --git a/examples/web-app.html b/examples/web-app.htmlindex c565a97f..de3ddfc8 100644--- a/examples/web-app.html+++ b/examples/web-app.html@@ -679,15 +679,24 @@/*** Serialize the main SVG into a standalone document.- * Inlines the host CSS + the current theme's resolved variables so the- * serialized SVG renders correctly both as a standalone file and when- * rasterized via an Image -> canvas pipeline (canvas does not read the- * host document's CSS).+ *+ * Two modes:+ * - Default (opts.autoTheme=false) — locks the serialized SVG to the+ * viewer's current theme. Used by the raster pipeline+ * (PNG/JPEG/WebP/clipboard) because canvas rasterization needs+ * deterministic colors; a raster cannot react to+ * prefers-color-scheme after encoding.+ * - autoTheme=true — emits BOTH dark and light variable sets plus a+ * `@media (prefers-color-scheme)` rule so the resulting SVG+ * self-themes when embedded in GitHub READMEs or other hosts that+ * expose a color scheme. Used for "Download SVG".*/- function serializeSvg(scale) {+ function serializeSvg(scale, opts) {// scale: integer multiplier for intrinsic SVG pixel dimensions used by// the raster path. Defaults to 1 (natural size) for SVG download.scale = scale || 1;+ opts = opts || {};+ var autoTheme = opts.autoTheme === true;var svg = document.querySelector('.diagram-container svg');var clone = svg.cloneNode(true);@@ -701,13 +710,10 @@clone.setAttribute('height', vb.height * scale);clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');- var theme = document.documentElement.getAttribute('data-theme') || 'dark';var hostStyle = Array.prototype.map.call(document.querySelectorAll('style'), function (s) {return s.textContent;}).join('\n');- var themeHost = document.querySelector('[data-theme="' + theme + '"]') || document.documentElement;- var computed = getComputedStyle(themeHost);var varNames = ['--bg','--grid','--text','--text-muted','--text-dim','--arrow','--arrow-emphasis','--mask',@@ -719,9 +725,23 @@'--messagebus-fill','--messagebus-stroke','--external-fill','--external-stroke'];- var vars = varNames.map(function (n) {- return n + ': ' + computed.getPropertyValue(n).trim() + ';';- }).join(' ');++ // Resolve the full variable set for a given data-theme via an+ // off-DOM probe, independent of what the viewer is currently set to.+ function resolveVars(themeAttr) {+ var probe = document.createElement('div');+ probe.setAttribute('data-theme', themeAttr);+ probe.style.cssText = 'position:absolute;width:0;height:0;visibility:hidden;';+ document.body.appendChild(probe);+ try {+ var c = getComputedStyle(probe);+ return varNames.map(function (n) {+ return n + ': ' + c.getPropertyValue(n).trim() + ';';+ }).join(' ');+ } finally {+ document.body.removeChild(probe);+ }+ }// Prepend a local()-only @font-face block for each weight so that the// sandboxed image-rendering context used during PNG/JPEG/WebP export@@ -734,21 +754,57 @@"; src: local('JetBrains Mono'), local('JetBrainsMono-Regular'); }";}).join('\n');- // IMPORTANT: inject the resolved variables AFTER hostStyle, otherwise- // hostStyle's ":root, [data-theme=\"dark\"] { ... }" rule overrides our- // chosen theme via later-in-cascade equal-specificity. Keep this order.var style = document.createElementNS('http://www.w3.org/2000/svg', 'style');- style.textContent =- fontFallback + "\n" +- "svg { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; }\n" +- hostStyle + "\n" +- ":root, svg { " + vars + " }\n";- clone.insertBefore(style, clone.firstChild);-var bgRect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');bgRect.setAttribute('width', '100%');bgRect.setAttribute('height', '100%');- bgRect.setAttribute('fill', computed.getPropertyValue('--bg').trim() || '#ffffff');++ if (autoTheme) {+ // Dual-theme SVG. Dark is the default (so hosts without+ // prefers-color-scheme still render), light swaps in via media+ // query, and svg[data-theme="..."] still lets downstream+ // consumers force a specific theme.+ var darkVars = resolveVars('dark');+ var lightVars = resolveVars('light');++ style.textContent =+ fontFallback + "\n" ++ "svg { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; }\n" ++ hostStyle + "\n" ++ ":root, svg { " + darkVars + " }\n" ++ "@media (prefers-color-scheme: light) { :root, svg { " + lightVars + " } }\n" ++ "svg[data-theme=\"light\"] { " + lightVars + " }\n" ++ "svg[data-theme=\"dark\"] { " + darkVars + " }\n" ++ "rect.c-bg-rect { fill: var(--bg); }\n";++ // Don't lock the serialized SVG to the viewer's current theme.+ clone.removeAttribute('data-theme');+ // Background follows the CSS variable, not a fixed color, so it+ // swaps with the media query.+ bgRect.setAttribute('class', 'c-bg-rect');+ } else {+ // Raster path: lock to the viewer's current theme.+ var theme = document.documentElement.getAttribute('data-theme') || 'dark';+ var themeHost = document.querySelector('[data-theme="' + theme + '"]') || document.documentElement;+ var computed = getComputedStyle(themeHost);+ var vars = varNames.map(function (n) {+ return n + ': ' + computed.getPropertyValue(n).trim() + ';';+ }).join(' ');++ // IMPORTANT: inject the resolved variables AFTER hostStyle,+ // otherwise hostStyle's ":root, [data-theme=\"dark\"] { ... }" rule+ // overrides our chosen theme via later-in-cascade equal-specificity.+ // Keep this order.+ style.textContent =+ fontFallback + "\n" ++ "svg { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; }\n" ++ hostStyle + "\n" ++ ":root, svg { " + vars + " }\n";++ bgRect.setAttribute('fill', computed.getPropertyValue('--bg').trim() || '#ffffff');+ }++ clone.insertBefore(style, clone.firstChild);clone.insertBefore(bgRect, style.nextSibling);return {@@ -933,7 +989,7 @@var base = diagramFilename();close(false);return (format === 'svg'- ? Promise.resolve(serializeSvg()).then(function (d) {+ ? Promise.resolve(serializeSvg(1, { autoTheme: true })).then(function (d) {download(new Blob([d.svgString], { type: 'image/svg+xml;charset=utf-8' }), base + '.svg');}): rasterize(format).then(function (blob) {