Skip to content
bencodePublic

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

157 Commits

Folders and files

Repository files navigation

sumi

sumi (墨, ink) lays out Mermaid diagrams in pure Go. It returns paths and positioned text that your application draws natively or exports as SVG.

  • Feature checklist — supported features, partial support, and known limitations.
  • Online playground — editable examples of every supported diagram type, rendered by Go; also run locally.
  • Integration guide — draw diagrams with your own fonts, renderer, and theme, or export SVG.
  • Rendering — no browser, JavaScript runtime, or external layout service required by the Go library.
  • Status — 0.x; the API may change.

Supported diagrams

  • Sequence diagrams
  • Flowcharts (flowchart, graph, flowchart-elk)
  • State diagrams (stateDiagram, stateDiagram-v2)
  • Class diagrams (classDiagram, classDiagram-v2)
  • Entity relationship diagrams (erDiagram)
  • Gantt diagrams (gantt)
  • Pie charts (pie)
  • Other Mermaid diagram types

Install

Requires Go 1.27 or later.

go get github.com/bencode/sumi

Draw a diagram

package main

import (
	"log"
	"os"

	"github.com/bencode/sumi/mermaid"
	"github.com/bencode/sumi/scene"
	"github.com/bencode/sumi/svg"
)

func main() {
	s, err := mermaid.Layout(`sequenceDiagram
    Alice->>Bob: Hello Bob
    Bob-->>Alice: Hello Alice`, mermaid.Options{})
	if err != nil {
		log.Fatal(err)
	}
	if err := os.WriteFile("hello.svg", svg.Render(s, scene.Light), 0o644); err != nil {
		log.Fatal(err)
	}
}
  • mermaid.Layout reports source errors as *mermaid.Error, with 1-based Line and Col; IsUnsupported() identifies explicitly unsupported features.
  • scene.Scene contains items in drawing order: paint each item's paths, then its text lines at their baselines. Coordinates use DIPs, with the origin at the top left.
  • mermaid.Options.Measurer accepts a scene.Measurer; nil uses built-in Arial measurements. Match the font metrics of your native renderer.
  • svg.Render uses scene.Light, scene.Dark, or your own palette. Theme changes do not require layout again; explicit author colors remain fixed.

Command line

sumi checks diagrams and renders them as images, for people and coding agents who write Mermaid:

go install github.com/bencode/sumi/cmd/sumi@latest

sumi check doc.md                # every mermaid code block of a Markdown file
sumi check flow.mmd              # any other file is one diagram
sumi render doc.md -o out        # out/doc-01.png, out/doc-02.png, ...
  • check reports errors as doc.md:44:9: message in the file's own lines, with the line and a caret under the column. It exits 1 when a diagram has an error, 2 when it cannot run.
  • render checks first and writes nothing unless every diagram is good. For each image it prints its number, the line its source starts on, its path, and its size in CSS pixels. -scale N sets the PNG's pixels per CSS pixel (default 2); -format svg writes SVG instead.
  • PNGs are drawn in Go with the system's fonts: Arial, Menlo, and Hiragino Sans GB or Arial Unicode for CJK on macOS; Liberation Sans, DejaVu Sans Mono and Noto Sans CJK on Linux.
  • skills/mermaid is a skill for Claude Code and Codex: link it into their skills directories (~/.claude/skills, ~/.agents/skills) and agents check and look at the diagrams they write.

Local playground

Requires Go 1.27+, Node 24, and pnpm 11.23.0. From the repository root, run the Go service in one terminal:

go run ./cmd/sumi-demo -listen 127.0.0.1:8080

Start the frontend in another:

pnpm --dir web install --frozen-lockfile
pnpm --dir web dev

Open http://localhost:5173. Each example is editable; previews update 400ms after you stop typing. Errors keep the last successful preview. You can reset an example, copy its current source, or download it. Edits stay in page memory and reset on reload. The theme selector changes both the editor and preview.

  • The Vite frontend proxies /api and /healthz to Go at 127.0.0.1:8080.
  • POST /api/render accepts {"source":"..."} with an optional width from 0 to 4096, the width a gantt diagram fills (0 is 1200), and returns lightSVG, darkSVG, width, and height. Requests are limited to 64 KiB.
  • Source/request errors return 400, oversized requests 413, unsupported features or empty scenes 422, and internal failures 500. Error JSON has message and, when available, line, column, and unsupported.
  • GET /healthz returns {"status":"ok"}. The service uses no database and stores no submitted source.
  • Edit docs/examples/catalog.json to organize examples. Category IDs name subdirectories; example IDs name .mmd files. Add the source and a catalog entry to add an example.
  • Frontend and service code live in web/, cmd/sumi-demo/, and internal/demo/. See the Docker Compose deployment guide for container builds and the shared Caddy setup.

Checks

go test ./...
go vet ./...
pnpm --dir web check
pnpm --dir web build
pnpm --dir web preview

The build writes ignored files to web/dist/. Preview the build at http://localhost:4173 with the Go service still running. go run ./internal/preview remains available for low-level scene and upstream sequence fixture previews in out/.

Limits

  • Targets the supported subset of Mermaid v12.3.0 with default layout settings.
  • Front matter titles work; front matter configuration and %%{init}%% directives are accepted but ignored.
  • HTML beyond supported line breaks, formulas, icons, and most CSS are not drawn. Links and callbacks do not become interactive.
  • See the feature checklist for type-specific behavior and restrictions.

License

MIT. See LICENSE.

The exception is mermaid/internal/elk, written after the Eclipse Layout Kernel and licensed under EPL-2.0 OR GPL-3.0-or-later. See its LICENSE and NOTICE.

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages