← Back to Skills Marketplace
zhouchang1988

code-to-diagram

by Zhou Chang · GitHub ↗ · v1.0.2 · MIT-0
cross-platform ⚠ suspicious
161
Downloads
0
Stars
0
Active Installs
3
Versions
Install in OpenClaw
/install code-to-diagram
Description
分析源代码逻辑,生成包含 Mermaid 流程图(.mmd)的 markdown 文件并渲染为 PNG 图片。支持流程图、状态机、时序图、架构图等多种图表类型。
README (SKILL.md)

Code-to-Diagram Skill

分析指定目录或文件的源代码,提取控制流 / 数据流逻辑,输出两个文件:

  1. Markdown 文档.md):包含 Mermaid 源码(可在支持 Mermaid 的编辑器 / GitHub 中直接预览)
  2. 渲染后的 PNG 图片(高清)

两个文件默认保存在当前工作目录,也可通过 --output-dir 指定任意目录。


Claude 使用本 Skill 的步骤

第一步 —— 读取并分析代码

使用 ReadGlobGrep 工具理解代码结构,识别主要逻辑模式:

代码模式 推荐 Mermaid 图表类型
状态机 / 工作流 flowchart TDstateDiagram-v2
类继承关系 classDiagram
接口 / 消息传递 sequenceDiagram
实体关系 erDiagram
简单流程 flowchart LR
系统架构 / 分层架构 flowchart TB + subgraph
微服务架构 flowchart TB + subgraphC4Container
部署架构 flowchart TB

识别架构图的信号

  • 用户明确要求"架构图"、"系统图"、"部署图"、"服务拓扑"
  • 代码有多层目录结构(如 controllers/services/models/repositories/
  • 代码有微服务边界或模块间调用
  • 需要展示系统组件及其依赖关系

第二步 —— 生成 Mermaid 源码

根据分析结果,编写完整的 Mermaid 图表源码。

重要:语言规则

  • 图表中的文字描述必须使用中文,包括节点标签、连线说明、注释等。
  • 以下内容保留原文(不翻译):代码中的类名、函数名、变量名、枚举值、协议名等专有标识符。
  • 示例:
    • 正确:A["初始化 TaskManager"] --> B{"检查 isRunning 状态"}
    • 错误:A["Initialize TaskManager"] --> B{"Check isRunning status"}

重要:换行规则

  • 如果需要在节点文本 / 标签中显示多行文字,必须使用 \x3Cbr/> 而不是 \
    • 正确:A["第一行\x3Cbr/>第二行"]
    • 错误:A["第一行\ 第二行"]
  • Mermaid 语句之间用正常的换行分隔即可。

第三步 —— 写入 .mmd 文件并调用渲染脚本

推荐方式:先用 Write 工具将 Mermaid 源码写入 .mmd 文件,然后用 --file 参数调用渲染脚本。这样可以避免 shell 转义问题,也能正确处理各种特殊字符。

node ~/.claude/skills/code-to-diagram/scripts/code_to_diagram.js render \
  --file \x3C路径/diagram.mmd> \
  --name \x3C输出文件基础名> \
  --output-dir \x3C保存目录>

也可以用 --content 传入内联源码(脚本会自动将字面 \ 转换为真正换行):

node ~/.claude/skills/code-to-diagram/scripts/code_to_diagram.js render \
  --content "\x3Cmermaid 源码>" \
  --name    \x3C输出文件基础名,不含扩展名> \
  --output-dir \x3C保存目录>

脚本执行完毕后,最后一行输出 JSON,包含两个输出文件的绝对路径:

{"md":"/workspace/project/task_flow.md","png":"/workspace/project/task_flow.png"}

第四步 —— 展示图片给用户

使用 Read 工具读取 .png 路径,将渲染好的图片内联展示在对话中。


命令行参数说明

node code_to_diagram.js render [选项]

选项:
  --content,    -c  \x3C字符串>   Mermaid 源码(与 --file 二选一)
  --file,       -f  \x3C路径>     已有的 .mmd 文件路径(与 --content 二选一,推荐)
  --name,       -n  \x3C字符串>   输出文件基础名,不含扩展名(默认:diagram)
  --output-dir, -o  \x3C路径>     输出目录(默认:当前工作目录,不存在时自动创建)
  --theme,      -t  \x3C主题>     default | forest | dark | neutral(默认:dark)
  --width,      -W  \x3C像素>     画布宽度(默认:2400)
  --height,     -H  \x3C像素>     画布高度(默认:4000)
  --scale,      -s  \x3C倍数>     Puppeteer 缩放系数(默认:3)
  --bg,         -b  \x3C颜色>     背景颜色(默认:#0d1117)
  --help,       -h             显示帮助信息

依赖说明

本 Skill 依赖 mermaid-climmdc)。

脚本会自动查找系统中的 mmdc。如果未找到,会自动通过 npx 调用,无需手动安装。 前提是系统中已有 Node.js(包含 npx)。


使用示例

推荐方式:先写文件再渲染

Claude 先用 Write 工具创建 .mmd 文件,然后调用脚本渲染:

node ~/.claude/skills/code-to-diagram/scripts/code_to_diagram.js render \
  --file /workspace/myproject/state_machine.mmd \
  --name state_machine \
  --output-dir /workspace/myproject

用亮色主题重新渲染已有的 .mmd 文件

node ~/.claude/skills/code-to-diagram/scripts/code_to_diagram.js render \
  --file /workspace/myproject/state_machine.mmd \
  --theme default \
  --bg white \
  --output-dir /workspace/myproject

绘制系统架构图

使用 flowchart TB + subgraph 绘制分层架构:

flowchart TB
    subgraph 客户端层["客户端层"]
        A[Web App]
        B[Mobile App]
    end
    subgraph 服务层["服务层"]
        C[API Gateway]
        D[User Service]
        E[Order Service]
    end
    subgraph 数据层["数据层"]
        F[(PostgreSQL)]
        G[(Redis Cache)]
    end
    A --> C
    B --> C
    C --> D
    C --> E
    D --> F
    E --> F
    D --> G

使用 C4 Model 绘制标准化架构图(需要 mermaid-cli 10.6+):

C4Context
    title 电商系统架构图
    Person(user, "用户", "系统使用者")
    System(web_app, "Web应用", "提供用户界面")
    System(api, "API服务", "业务逻辑处理")
    SystemDb(db, "数据库", "PostgreSQL")
    Rel(user, web_app, "使用", "HTTPS")
    Rel(web_app, api, "调用", "REST API")
    Rel(api, db, "读写", "TCP/IP")

输出文件说明

文件 说明
\x3C输出目录>/\x3C名称>.md 包含 Mermaid 源码的 Markdown 文档,可在 GitHub / 编辑器中直接预览图表
\x3C输出目录>/\x3C名称>.png 高分辨率渲染图片(默认 scale=3,有效分辨率 7200×12000)
Usage Guidance
This skill appears to do what it says: it reads code you point it at, generates Mermaid source, and renders a PNG via mermaid-cli. Things to consider before installing or running it: - Runtime requirements: Node.js (and npx) are required. If mmdc isn't installed, the script will call 'npx' which may download packages from the network at runtime. - File access: the agent/instructions will read the project directory you ask it to analyze — do NOT point it at directories containing secrets or sensitive credentials unless you trust the outputs. - Puppeteer config: the script writes a temporary Puppeteer config that disables the Chromium sandbox (--no-sandbox). This is commonly used to allow rendering in containers but reduces sandboxing protections; run in a trusted environment. - Outputs and temp files: the script writes temporary .mmd files (when using --content) and writes outputs to the chosen output directory. Ensure you choose a safe output path. If you're comfortable with the above (or pre-install mermaid-cli to avoid npx downloads), the skill is proportionate to its purpose. If you have strict network or sandboxing policies, inspect the script and run it in an isolated environment first.
Capability Analysis
Type: OpenClaw Skill Name: code-to-diagram Version: 1.0.2 The skill is a legitimate utility designed to analyze code and generate Mermaid diagrams (PNG and Markdown). The core logic in `scripts/code_to_diagram.js` uses `spawnSync` with `shell: false` to execute the Mermaid CLI, which is a secure practice to prevent shell injection. The instructions in `SKILL.md` are strictly aligned with the stated purpose of diagram generation and do not contain any malicious prompt injection or hidden commands. No indicators of data exfiltration, persistence, or unauthorized execution were found.
Capability Assessment
Purpose & Capability
Name/description (generate Mermaid diagrams from source code) match the included SKILL.md and scripts. The skill only needs to read code files, produce .mmd and PNG outputs, and call mermaid-cli (mmdc/npx) to render — all of which are consistent with the stated functionality.
Instruction Scope
SKILL.md instructs the agent to read project files (via Read/Glob/Grep) to extract control/data flow — this is expected for code analysis. It also enforces Chinese labels in diagrams (a presentation rule). Nothing in the instructions instructs the agent to access unrelated system credentials or exfiltrate data; however, the skill will read whatever project directory you point it at, so supplying a repository with secrets would cause those files to be read and potentially included in outputs.
Install Mechanism
No install spec is provided (instruction-only), which is low risk. The bundled script attempts to run mmdc and will fallback to 'npx @mermaid-js/mermaid-cli' if mmdc isn't found — this will perform a network fetch at runtime if mermaid-cli isn't installed. That behavior is expected for rendering but implies transient downloads and runtime network use.
Credentials
The skill requests no environment variables, no credentials, and no config paths. The script requires Node.js/npx to be present (documented). There are no unrelated credential requests.
Persistence & Privilege
The skill is not always: true and does not claim persistent system-wide privileges or alter other skills' configs. It writes temporary files and outputs to user-specified output directories only (creates output dir if missing).
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install code-to-diagram
  3. After installation, invoke the skill by name or use /code-to-diagram
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.2
Release v1.0.2 from GitHub at 2026-04-20 15:17
v1.0.1
Release v1.0.1 from GitHub at 2026-04-20 15:17
v1.0.0
- 首次发布,版本 1.0.0 - 支持分析源码逻辑,自动生成带有 Mermaid 流程图的 markdown 文件,并渲染为高清 PNG 图片 - 支持多种图表类型(流程图、状态机、时序图、类图、ER 图)并推荐最佳 Mermaid 语法 - 图表文字全中文,可灵活输出到指定目录 - 依赖 mermaid-cli (自动查找/调用,无需手动安装) - 提供详细命令行参数和使用示例
Metadata
Slug code-to-diagram
Version 1.0.2
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 3
Frequently Asked Questions

What is code-to-diagram?

分析源代码逻辑,生成包含 Mermaid 流程图(.mmd)的 markdown 文件并渲染为 PNG 图片。支持流程图、状态机、时序图、架构图等多种图表类型。 It is an AI Agent Skill for Claude Code / OpenClaw, with 161 downloads so far.

How do I install code-to-diagram?

Run "/install code-to-diagram" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is code-to-diagram free?

Yes, code-to-diagram is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does code-to-diagram support?

code-to-diagram is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created code-to-diagram?

It is built and maintained by Zhou Chang (@zhouchang1988); the current version is v1.0.2.

💬 Comments