Documentation
¶
Overview ¶
Package mermaid renders Mermaid diagrams to SVG in pure Go, with no headless browser or JavaScript runtime required.
Supported diagram types are detected from the source header: flowchart (graph/flowchart), sequenceDiagram, classDiagram, stateDiagram-v2, erDiagram, pie, journey, quadrantChart, gitGraph, timeline, mindmap, gantt, C4 (C4Context/C4Container), requirementDiagram, sankey-beta, and xychart-beta. Unsupported types return ErrUnsupported; see DiagramTypes.
Render returns SVG. For PNG, use the github.com/zkrebbekx/go-mermaid/raster subpackage, which keeps the rasterizer dependency out of the core library.
Basic use:
svg, err := mermaid.Render("graph TD\n A --> B")
if err != nil {
log.Fatal(err)
}
os.Stdout.Write(svg)
Rendering is configured with functional options:
svg, err := mermaid.Render(src,
mermaid.WithTheme(mermaid.Dark),
mermaid.WithPadding(24),
)
Index ¶
- Variables
- func DiagramTypes() []string
- func Render(src string, opts ...Option) (out []byte, err error)
- func RenderTo(w io.Writer, src string, opts ...Option) error
- func Themes() []string
- type Option
- func WithBackground(color string) Option
- func WithCurvedEdges(on bool) Option
- func WithCustomTheme(name string, p Palette) Option
- func WithFont(face string, size float64) Option
- func WithPadding(px float64) Option
- func WithSpacing(nodeSep, rankSep float64) Option
- func WithTheme(t Theme) Option
- func WithTransparentBackground() Option
- type Palette
- type ParseError
- type Theme
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrParse indicates the source could not be lexed or parsed. ErrParse = errors.New("mermaid: parse error") // ErrLayout indicates the graph could not be laid out. ErrLayout = errors.New("mermaid: layout error") // ErrRender indicates the laid-out graph could not be rendered to SVG. ErrRender = errors.New("mermaid: render error") // ErrUnsupported indicates a diagram type or feature not yet implemented. ErrUnsupported = errors.New("mermaid: unsupported feature") )
Sentinel errors returned by Render, wrapped around the underlying cause. Match them with errors.Is. For parse failures, errors.As can additionally recover a *parser.ParseError carrying source line/column.
Functions ¶
func DiagramTypes ¶
func DiagramTypes() []string
DiagramTypes returns the diagram header keywords this library can render.
func Render ¶
Render parses a Mermaid source document and returns the rendered SVG.
Errors are wrapped so callers can match the failing stage with errors.Is (ErrParse, ErrLayout, ErrRender, ErrUnsupported) and, for parse failures, recover source position with errors.As(&ParseError{}).
Example ¶
Render a simple flowchart to SVG.
package main
import (
"fmt"
"strings"
mermaid "github.com/zkrebbekx/go-mermaid"
)
func main() {
svg, err := mermaid.Render("graph TD\n A[Start] --> B[End]")
if err != nil {
panic(err)
}
fmt.Println(strings.HasPrefix(string(svg), "<svg"))
}
Output: true
Example (ErrorHandling) ¶
Recover the source position of a parse error.
package main
import (
"errors"
"fmt"
mermaid "github.com/zkrebbekx/go-mermaid"
)
func main() {
_, err := mermaid.Render("graph TD\n A[unterminated")
fmt.Println(errors.Is(err, mermaid.ErrParse))
var pe *mermaid.ParseError
if errors.As(err, &pe) {
fmt.Printf("error on line %d\n", pe.Line)
}
}
Output: true error on line 2
Example (Options) ¶
Configure rendering with functional options.
package main
import (
"fmt"
"strings"
mermaid "github.com/zkrebbekx/go-mermaid"
)
func main() {
svg, _ := mermaid.Render("graph LR\n A --> B",
mermaid.WithTheme(mermaid.Dark),
mermaid.WithPadding(24),
)
fmt.Println(strings.Contains(string(svg), "#1e1e1e")) // dark background
}
Output: true
Example (Sequence) ¶
Render a sequence diagram; the diagram type is detected from the header.
package main
import (
"fmt"
"strings"
mermaid "github.com/zkrebbekx/go-mermaid"
)
func main() {
svg, _ := mermaid.Render("sequenceDiagram\n Alice->>Bob: Hello")
fmt.Println(strings.Contains(string(svg), ">Alice<"))
}
Output: true
Types ¶
type Option ¶
type Option func(*config)
Option configures a Render call. Options are applied in order; later options override earlier ones.
func WithBackground ¶
WithBackground overrides the diagram background color (any CSS color).
func WithCurvedEdges ¶
WithCurvedEdges renders flowchart edges as smooth curves instead of straight orthogonal segments.
func WithCustomTheme ¶
WithCustomTheme registers a palette under name and selects it for this render. Subsequent renders can also reference the name via WithTheme.
func WithPadding ¶
WithPadding sets the outer padding (in pixels) around the diagram.
func WithSpacing ¶
WithSpacing sets the gap between sibling nodes (nodeSep) and between ranks (rankSep), in pixels.
func WithTransparentBackground ¶
func WithTransparentBackground() Option
WithTransparentBackground removes the background rect so the diagram blends with whatever it is embedded in.
type ParseError ¶
ParseError reports a lexing or parsing failure with its source position (Line, Col). When Render fails during the parse stage, the returned error wraps a *ParseError recoverable with errors.As.
type Theme ¶
type Theme string
Theme selects a built-in color palette for rendering.
const ( // Default is the light theme used when no theme is set. Default Theme = "default" // Dark is a dark-background palette. Dark Theme = "dark" // Neutral is a grayscale palette suitable for print. Neutral Theme = "neutral" // Forest is a green palette. Forest Theme = "forest" // Base is a muted neutral palette. Base Theme = "base" )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
mermaid
command
Command mermaid renders Mermaid diagrams to SVG.
|
Command mermaid renders Mermaid diagrams to SVG. |
|
internal
|
|
|
block
Package block parses and renders Mermaid block-beta diagrams to SVG as a column grid of labeled blocks.
|
Package block parses and renders Mermaid block-beta diagrams to SVG as a column grid of labeled blocks. |
|
c4
Package c4 parses and renders Mermaid C4 diagrams (C4Context, C4Container, …) to SVG, reusing the shared layered layout engine.
|
Package c4 parses and renders Mermaid C4 diagrams (C4Context, C4Container, …) to SVG, reusing the shared layered layout engine. |
|
class
Package class parses and renders Mermaid class diagrams to SVG.
|
Package class parses and renders Mermaid class diagrams to SVG. |
|
domain
Package domain holds the pure diagram model: the types every other stage produces or consumes.
|
Package domain holds the pure diagram model: the types every other stage produces or consumes. |
|
er
Package er parses and renders Mermaid entity-relationship diagrams (erDiagram) to SVG, reusing the shared layered layout engine.
|
Package er parses and renders Mermaid entity-relationship diagrams (erDiagram) to SVG, reusing the shared layered layout engine. |
|
gantt
Package gantt parses and renders Mermaid gantt charts to SVG.
|
Package gantt parses and renders Mermaid gantt charts to SVG. |
|
git
Package git parses and renders Mermaid gitGraph diagrams to SVG.
|
Package git parses and renders Mermaid gitGraph diagrams to SVG. |
|
journey
Package journey parses and renders Mermaid user-journey diagrams to SVG.
|
Package journey parses and renders Mermaid user-journey diagrams to SVG. |
|
kanban
Package kanban parses and renders Mermaid kanban diagrams to SVG as columns of cards, derived from indentation.
|
Package kanban parses and renders Mermaid kanban diagrams to SVG as columns of cards, derived from indentation. |
|
layout
Package layout assigns coordinates to a domain.Graph using a layered (Sugiyama-style) approach: make the graph acyclic, rank nodes into layers, insert dummy nodes so long edges can bend, order within layers to reduce crossings, then assign positions with a barycenter heuristic.
|
Package layout assigns coordinates to a domain.Graph using a layered (Sugiyama-style) approach: make the graph acyclic, rank nodes into layers, insert dummy nodes so long edges can bend, order within layers to reduce crossings, then assign positions with a barycenter heuristic. |
|
lexer
Package lexer turns Mermaid flowchart source into a flat token stream.
|
Package lexer turns Mermaid flowchart source into a flat token stream. |
|
mindmap
Package mindmap parses and renders Mermaid mindmaps to SVG using an indentation-based hierarchy drawn as a left-to-right tree.
|
Package mindmap parses and renders Mermaid mindmaps to SVG using an indentation-based hierarchy drawn as a left-to-right tree. |
|
packet
Package packet parses and renders Mermaid packet-beta diagrams to SVG as a bit/byte field table.
|
Package packet parses and renders Mermaid packet-beta diagrams to SVG as a bit/byte field table. |
|
parser
Package parser turns a token stream into a domain.Graph.
|
Package parser turns a token stream into a domain.Graph. |
|
pie
Package pie parses and renders Mermaid pie charts to SVG.
|
Package pie parses and renders Mermaid pie charts to SVG. |
|
quadrant
Package quadrant parses and renders Mermaid quadrant charts to SVG.
|
Package quadrant parses and renders Mermaid quadrant charts to SVG. |
|
radar
Package radar parses and renders Mermaid radar-beta charts to SVG as a polar plot with one polygon per data curve.
|
Package radar parses and renders Mermaid radar-beta charts to SVG as a polar plot with one polygon per data curve. |
|
render
Package render turns a laid-out graph into SVG bytes.
|
Package render turns a laid-out graph into SVG bytes. |
|
requirement
Package requirement parses and renders Mermaid requirement diagrams to SVG, reusing the shared layered layout engine.
|
Package requirement parses and renders Mermaid requirement diagrams to SVG, reusing the shared layered layout engine. |
|
sankey
Package sankey parses and renders Mermaid sankey-beta diagrams to SVG as proportional flow bands between nodes arranged in columns.
|
Package sankey parses and renders Mermaid sankey-beta diagrams to SVG as proportional flow bands between nodes arranged in columns. |
|
sequence
Package sequence parses, lays out, and renders Mermaid sequence diagrams to SVG.
|
Package sequence parses, lays out, and renders Mermaid sequence diagrams to SVG. |
|
state
Package state parses and renders Mermaid state diagrams (stateDiagram-v2) to SVG, reusing the shared layered layout engine.
|
Package state parses and renders Mermaid state diagrams (stateDiagram-v2) to SVG, reusing the shared layered layout engine. |
|
svgutil
Package svgutil holds small formatting helpers shared by the diagram renderers: deterministic number formatting and XML text escaping.
|
Package svgutil holds small formatting helpers shared by the diagram renderers: deterministic number formatting and XML text escaping. |
|
syntax
Package syntax defines the positional error type shared by the lexer and parser.
|
Package syntax defines the positional error type shared by the lexer and parser. |
|
theme
Package theme holds the color palettes shared by all diagram renderers.
|
Package theme holds the color palettes shared by all diagram renderers. |
|
timeline
Package timeline parses and renders Mermaid timeline diagrams to SVG.
|
Package timeline parses and renders Mermaid timeline diagrams to SVG. |
|
xychart
Package xychart parses and renders Mermaid xychart-beta diagrams (bar and line charts) to SVG.
|
Package xychart parses and renders Mermaid xychart-beta diagrams (bar and line charts) to SVG. |
|
Package raster rasterizes go-mermaid SVG output to PNG.
|
Package raster rasterizes go-mermaid SVG output to PNG. |








