ClaudeMods
☰
ZH-TW
● 0 人在線上 · 瀏覽 0 次
贊助提交作品
GitHub 儲存庫 · 發布者 natsukium

figures

在 kitty-graphics 終端機內嵌繪製圖表、LaTeX 數學公式與工具結果圖片

已翻譯

關於這個 mod

figures

一個 Claude Code mod,使用 kitty graphics 協定,在終端機轉錄中內嵌繪製圖表、LaTeX 數學公式與工具結果圖片。

一則回覆,其中的 mermaid 流程圖與 LaTeX 公式各自以圖片繪製在原始碼下方

它會繪製什麼

  • 圖表。 Claude 的回覆中,標記為 mermaid、dot(或 graphviz)、d2、svg 的程式碼區塊會被轉譯,並繪製在回覆下方。程式碼仍顯示在圖片上方。
  • 數學公式。 LaTeX 顯示數學公式、$$ … $$、\[ … \],或標記為 math、latex、tex 的程式碼區塊,會使用 MathJax 排版。行內 $…$ 會保留為文字,因為圖片無法放在一行文字中;包含完整文件(\documentclass)的 latex 區塊也會保留為程式碼。
  • 工具結果圖片。 Read PNG、JPEG、GIF、WebP 或 SVG 檔案,或 MCP 工具回傳螢幕擷取畫面時,圖片會繪製在工具列下方;在這裡 Claude Code 原本只會顯示大小資訊。只讀取部分內容的 SVG 會維持原樣。在摺疊的工具群組中,它們會顯示為縮圖;按 ctrl+o 展開工具群組後會繪製完整尺寸。Claude 透過 SendUserFile 將 PNG、JPEG、GIF、WebP 或 SVG 傳送到其他裝置時,圖片也會繪製在附件列下方,因此你回到終端機時仍看得到。圖片會在該列第一次繪製時從磁碟讀取;之後覆寫檔案不會改變已顯示的圖片。

如果圖片依尺寸上限縮小後會低於自然尺寸的 60%,它會放大到終端機高度;如果仍然太小,則會在圖片下方說明,例如 shown at 43% · /figures to enlarge。

/figures 會開啟顯示目前工作階段圖片的面板,起始顯示最近一張小到難以閱讀的圖片;如果沒有,則顯示最新圖片。按 p 與 n 瀏覽圖片,按 o 使用 open(macOS)或 xdg-open 開啟目前圖片,按 Escape 或輸入下一個提示關閉面板。

轉錄旁邊的 /figures 面板,顯示回覆中的公式

外掛還附帶一個 diagrams skill,用來告訴 Claude 哪些語言會被繪製,以及應如何撰寫這些語言才能轉譯。

每張轉譯後的 PNG 都會快取在 $TMPDIR/claude-figures/ 下,檔名由原始碼與主題的雜湊值組成,因此恢復工作階段時可以重新繪製圖片而不用再次轉譯。工作階段開始時會刪除超過七天的檔案。

需求

需要 Claude Code 2.1.287 或更新版本,在啟用 mods(function-hook plugins)的終端機中執行,且該終端機被 Claude Code 視為支援 kitty graphics;請參閱 Terminal support。每種圖片也需要 PATH 中存在相應程式:

| 圖片 | 需要的程式 | | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | mermaid | mmdr 或 mmdc;建議 resvg | | dot、graphviz | Graphviz 的 dot 與 resvg | | d2 | d2 與 resvg | | svg、數學公式 | resvg | | PNG 以外的工具圖片 | magick(ImageMagick)或 sips(macOS 內建) |

  • 會先嘗試 mmdr,因為 mmdc 啟動無頭瀏覽器時,mmdr 只需幾毫秒就能完成轉譯。mmdc 會自行執行 mermaid.js,所以如果 mmdr 無法處理某個區塊,且系統已安裝 mmdc,就會交給 mmdc。
  • 沒有 resvg 時,mmdr 會以 1x 寫出 PNG,在高像素密度顯示器上看起來會比較模糊;有 resvg 時,圖表會先轉譯成 SVG,再依 scale 選項點陣化。
  • MathJax 4 及其 New Computer Modern 字型的所有字形範圍都會隨外掛打包,並在 Claude Code 內執行,因此數學公式不需要 Node.js。
  • 如果沒有安裝任何對應的轉譯器,程式碼區塊會無錯誤地保留為程式碼,因為它仍可作為文字閱讀。只有已安裝的轉譯器執行失敗時才會顯示錯誤。

安裝

claude plugin marketplace add natsukium/claude-code-figures-plugin
claude plugin install figures@figures

使用 cargo install mermaid-rs-renderer 安裝 mmdr。

選項

使用 /config 或 claude plugin configure figures 設定以下選項。

| 選項 | 預設值 | 意義 | | --------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | theme | auto | auto 跟隨 Claude Code 的主題;也可使用 dark、default、forest、neutral、modern(僅 mmdr 支援;mmdc 會將其繪製為 default)。在 dark 下數學公式以淺色繪製,其他主題下以深色繪製 | | background | transparent | 圖表背景;transparent 會讓終端機背景透出 | | scale | 2 | 圖片的像素密度;大於 1 時需要 resvg | | cell_width_px | 8 | 假定一個終端機儲存格的寬度,單位是圖表排版使用的像素 | | cell_aspect | 2.1 | 假定一個終端機儲存格的高寬比 | | max_columns | 120 | 圖片允許的最大寬度,單位是儲存格;圖片也會維持在終端機範圍內 | | max_rows | 30 | 圖片在為了可讀性而放大前允許的最大高度,單位是儲存格 | | renderers | (空) | 按優先順序排列的轉譯器 id,以逗號分隔;見下文 | | tool_images | true | 繪製工具結果中找到的圖片 | | tool_image_max_rows | 12 | 工具結果縮圖允許的最大高度,單位是儲存格 |

renderers 會選擇並排列繪製各語言的程式。id 包括 mmdr 與 mmdc(mermaid)、dot、d2、svg、mathjax。如果某種語言列出了任何轉譯器,就只使用這些轉譯器,並依列出的順序執行;其他語言繼續使用預設設定。因此,mmdc 會只用 mmdc 繪製 mermaid,而 mmdc,mmdr 會先嘗試 mmdc。工作階段開始時會回報不存在轉譯器的 id。

終端機支援

只有當終端機的 XTVERSION 回覆名稱包含 kitty(0.28.0 或更新版本)或 Ghostty 時,Claude Code 才會啟用圖片。即使終端機用其他名稱回應 kitty graphics 查詢,只要回應正確,仍會取得替代文字,例如 [mermaid diagram 1]。設定 TERM 或 TERM_PROGRAM 不會有任何作用。

要在這類終端機使用外掛,請設定 Claude Code 讀取的覆寫值:

CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 claude

這也會略過 Claude Code 的 tmux 檢查。在 tmux 內,圖片還需要 set -g allow-passthrough on。

若要查看 Claude Code 的判斷結果,請用 --debug 啟動它,並在偵錯記錄中尋找 Terminal capabilities: 那一行。

限制

  • 圖片的儲存格尺寸根據 cell_width_px 與 cell_aspect 估算,因為 mods 不會得知終端機儲存格的像素尺寸。如果圖片看起來被拉伸,請調整這些值。
  • Mods 無法讀取終端機背景色,因此 theme=auto 會跟隨 Claude Code 自身的主題(/config):light 變體繪製淺色圖片,其他變體繪製深色圖片。當 Claude Code 自身的主題也是 auto 時,它解析後的值不會公開,因此由 COLORFGBG 決定終端機是否匯出該值,否則使用深色。
  • mmdr 會自行剖析 Mermaid,並非在所有情況都與 mermaid.js 一致。例如,帶標籤的鏈式邊(A --> B -->|ok| C)會被錯誤剖析;請每行寫一條邊。這類區塊會無錯誤地完成轉譯,因此不會交給 mmdc;可將 renderers 設為 mmdc 來避開 mmdr。
  • 數學字型缺少的文字,例如 \text{日本語},會由 resvg 使用系統字型繪製。
  • /figures 面板會和提示列共用畫面,因此在較短的終端機中只剩幾行空間;o 會在終端機外以完整尺寸開啟圖片。

開發

tsconfig.json 會延伸 .claude-plugin/types/ 中的引擎型別定義,Vite、Vitest 與 tsc 都會讀取這些定義。Claude Code 只有從這個資料夾載入外掛時才會寫入型別定義,而且沒有其他指令會單獨寫入它們,因此 pnpm types 會使用虛假的 API key 啟動 claude -p --plugin-dir .,型別定義存在後會忽略失敗的請求。build、test 與 typecheck 會先執行它。Claude Code 是開發相依套件,所以指令稿和 pnpm exec claude 使用 pnpm-lock.yaml 中鎖定的版本,與 PATH 上的 claude 版本無關。

pnpm install
pnpm build              # regenerate hooks/vendor/
pnpm test               # unit tests (Vitest, tests/unit/*.spec.ts)
pnpm typecheck
pnpm exec claude plugin validate .
pnpm exec claude plugin test .    # hooks through the engine (tests/engine/*.test.ts)

Claude Code 會將 hooks/ 作為 TypeScript 原始碼載入,因此只有第三方程式碼會被建置。pnpm build 會使用 Vite 將 MathJax 與 @noble/hashes 打包到 hooks/vendor/,並提交這些檔案,讓儲存庫保持可直接安裝。Hooks 模組不能匯入超過 1 MiB 的檔案,也不能在執行階段匯入模組,因此建置會將套件拆分成多個區塊,並把字型的每個字形範圍寫成 JSON;外掛會在公式第一次使用該字形範圍時讀取它。

引擎拒絕在匯入之間傳遞 $ 的模組,因此 hooks/register.tsx 會把外掛需要的 $ 包裝在 Io 物件(hooks/io.ts)中,其他每個模組都會使用該物件。

授權

Apache-2.0。hooks/vendor/ 由 MathJax 及其 New Computer Modern font 建置,兩者均為 Apache-2.0;noble-hashes 使用 MIT 授權。

安裝

請先查看作者 README,確認 marketplace 與外掛名稱;指令可能隨儲存庫結構而變動。

claude plugin marketplace add natsukium/claude-code-figures-plugin
claude plugin install figures
原文 / README

figures

A Claude Code mod that draws diagrams, LaTeX math, and tool-result images inline in the terminal transcript, using the kitty graphics protocol.

A reply with a mermaid flowchart and a LaTeX formula, each drawn as a picture under its source

What it draws

  • Diagrams. A code block tagged mermaid, dot (or graphviz), d2, or svg in Claude's reply is rendered and drawn under the reply. The code stays visible above the picture.
  • Math. LaTeX display math, $$ … $$, \[ … \], or a math, latex, or tex code block, is typeset with MathJax. Inline $…$ stays text, since a picture cannot sit inside a line, and a latex block holding a whole document (\documentclass) stays code.
  • Tool-result images. Read on a PNG, JPEG, GIF, WebP, or SVG file, or a screenshot an MCP tool returns, is drawn under the tool's row, where Claude Code otherwise shows only a size line; an SVG read only in part is left alone. In a collapsed tool group they are thumbnails; ctrl+o unfolds the group and draws them full size. A PNG, JPEG, GIF, WebP, or SVG Claude sends to another device with SendUserFile is drawn under its attachment line too, so it is on screen when you come back to the terminal. It is read from disk when the row is first drawn; overwriting the file later leaves that picture as it was.

A picture the size caps would shrink below 60% of its natural size grows up to the terminal's height, and one still smaller says so under it, such as shown at 43% · /figures to enlarge.

/figures opens a pane with the session's pictures, starting on the newest one drawn too small to read, or else the newest. p and n step through them, o opens the current one with open (macOS) or xdg-open, and Escape or your next prompt closes the pane.

The /figures pane beside the transcript, showing the formula from the reply

The plugin also ships a diagrams skill, which tells Claude which languages are drawn and how to write them so they render.

Each rendered PNG is cached under $TMPDIR/claude-figures/, named by a hash of the source and the theme, so a resumed session draws its pictures again without re-rendering. Files older than seven days are removed when a session starts.

Requirements

Claude Code 2.1.287 or later, with mods (function-hook plugins) enabled, in a terminal it treats as kitty-graphics capable; see Terminal support. Each kind of picture also needs programs on PATH:

| Picture | Needs | | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | mermaid | mmdr or mmdc; resvg recommended | | dot, graphviz | Graphviz's dot and resvg | | d2 | d2 and resvg | | svg, math | resvg | | Tool images other than PNG | magick (ImageMagick) or sips (built into macOS) |

  • mmdr is tried first, since it renders in milliseconds where mmdc starts a headless browser. mmdc runs mermaid.js itself, so a block mmdr fails on is handed to it when it is installed.
  • Without resvg, mmdr writes its PNG at 1x, which looks soft on a high-density display; with it, the diagram is rendered to SVG and rasterized at the scale option.
  • MathJax 4 and every glyph range of its New Computer Modern font are bundled, and run inside Claude Code, so math needs no Node.js.
  • A block none of whose renderers is installed is left as code without an error, since it still reads as text. An error is shown only when an installed renderer fails.

Install

claude plugin marketplace add natsukium/claude-code-figures-plugin
claude plugin install figures@figures

Install mmdr with cargo install mermaid-rs-renderer.

Options

Set these with /config or claude plugin configure figures.

| Option | Default | Meaning | | --------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | theme | auto | auto follows Claude Code's theme; or dark, default, forest, neutral, modern (mmdr only; mmdc draws it as default). Math is drawn light on dark and dark otherwise | | background | transparent | Diagram background; transparent lets the terminal's background show through | | scale | 2 | Pixel density of the picture; above 1 needs resvg | | cell_width_px | 8 | Assumed width of one terminal cell, in the pixels diagrams are laid out in | | cell_aspect | 2.1 | Assumed height-to-width ratio of one terminal cell | | max_columns | 120 | Widest a picture may be, in cells; it is also kept inside the terminal | | max_rows | 30 | Tallest a picture may be before it is grown for legibility, in cells | | renderers | (empty) | Renderer ids in preference order, comma-separated; see below | | tool_images | true | Draw images found in tool results | | tool_image_max_rows | 12 | Tallest a tool-result thumbnail may be, in cells |

renderers picks and orders the programs that draw each language. The ids are mmdr and mmdc (mermaid), dot, d2, svg, and mathjax. A language with any of its renderers listed uses only those, in the listed order; the others keep the default. So mmdc draws mermaid with mmdc alone, and mmdc,mmdr tries mmdc first. An id no renderer has is reported when a session starts.

Terminal support

Claude Code enables pictures only when the terminal's XTVERSION reply names kitty (0.28.0 or later) or Ghostty. A terminal that answers the kitty graphics query correctly under any other name still gets the alt text, such as [mermaid diagram 1]. Setting TERM or TERM_PROGRAM has no effect.

To use the plugin on such a terminal, set the override Claude Code reads:

CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 claude

This also skips Claude Code's tmux check. Inside tmux, the pictures additionally need set -g allow-passthrough on.

To see what Claude Code decided, start it with --debug and look for the Terminal capabilities: line in the debug log.

Limitations

  • The picture's size in cells is estimated from cell_width_px and cell_aspect, since mods are not told the terminal's cell size in pixels. Adjust them if pictures look stretched.
  • Mods cannot read the terminal's background color, so theme=auto follows Claude Code's own theme (/config): a light variant draws light pictures, the others dark. When Claude Code's theme is itself auto, its resolved value is not exposed, so COLORFGBG decides where the terminal exports it, and dark otherwise.
  • mmdr parses Mermaid on its own and does not match mermaid.js in every case. For example, a chained edge with labels (A --> B -->|ok| C) is misparsed; write one edge per line. Such a block renders without an error, so it is not handed to mmdc; set renderers to mmdc to avoid mmdr.
  • Text in a script the math font lacks, such as \text{日本語}, is drawn by resvg with a system font.
  • The /figures pane shares the screen with the prompt, so in a short terminal it has only a few rows; o opens the picture at full size outside the terminal.

Development

tsconfig.json extends the engine's typings in .claude-plugin/types/, which Vite and Vitest read as well as tsc. Claude Code writes them only when it loads the plugin from this folder, and no command writes them alone, so pnpm types starts claude -p --plugin-dir . with a dummy API key and ignores the failed request once the typings exist. build, test, and typecheck run it first. Claude Code is a dev dependency, so the scripts and pnpm exec claude use the version pinned in pnpm-lock.yaml, whatever claude is on PATH.

pnpm install
pnpm build              # regenerate hooks/vendor/
pnpm test               # unit tests (Vitest, tests/unit/*.spec.ts)
pnpm typecheck
pnpm exec claude plugin validate .
pnpm exec claude plugin test .    # hooks through the engine (tests/engine/*.test.ts)

Claude Code loads hooks/ as TypeScript source, so only third-party code is built. pnpm build bundles MathJax and @noble/hashes with Vite into hooks/vendor/, which is committed so the repository stays installable as is. A hooks module imports no file over 1 MiB and cannot import modules at run time, so the build splits the bundle into chunks and writes each of the font's glyph ranges as JSON, which the plugin reads the first time a formula uses it.

The engine refuses a module that passes $ across an import, so hooks/register.tsx wraps what the plugin needs from $ in an Io object (hooks/io.ts), and every other module takes that.

License

Apache-2.0. hooks/vendor/ is built from MathJax and its New Computer Modern font, both Apache-2.0, and @noble/hashes, MIT.

更多類似作品