mermaid

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mermaid renders Mermaid diagrams to inline SVG at build time.

Mermaid is a browser library: it lays a diagram out by measuring text in a real DOM, so it cannot run in an embedded JavaScript interpreter the way KaTeX does. The build therefore draws diagrams in headless Chrome — the same browser the PDF is printed with — using the Mermaid release vendored in assets/, which is byte-for-byte the file the site would otherwise load from jsDelivr. A finished book then carries finished pictures: the site and the single-file HTML load nothing from a CDN for diagrams, the PDF does not wait for a script before printing, and an ePub, whose reading systems run no script, shows the diagram instead of its source.

Rendering is local and offline: the render tab blocks every network request. Each diagram is drawn with Mermaid's strict security level and labels as SVG <text> (no HTML in <foreignObject>), then cleaned with the shared SVG sanitizer before it is inlined, so a diagram cannot inject script into the book. Results are cached in memory for the life of the process and, when a cache directory is given, on disk across builds, so an unchanged diagram costs nothing on a rebuild.

When Chrome is missing or cannot start, nothing is rendered: diagrams stay <div class="mermaid"> source blocks, which the site, HTML and PDF still draw in the browser with the CDN loader, exactly as before build-time rendering.

Index

Constants

View Source
const Version = "11.16.0"

Version is the vendored Mermaid release. It must match the version in utils.MermaidCDNURL so a diagram drawn at build time and one drawn by the client-side fallback come from the same library; the tests pin that, and that the embedded file's digest equals utils.MermaidSRI.

Variables

View Source
var ErrUnavailable = errors.New("build-time Mermaid rendering is unavailable")

ErrUnavailable is wrapped by the error returned when diagrams cannot be rendered at all, because Chrome is missing or could not be started.

Functions

func CountDiagrams

func CountDiagrams(htmlContent string) int

CountDiagrams returns the number of unrendered Mermaid blocks in HTML.

func DiagramID

func DiagramID(scope string, index int) string

DiagramID returns the SVG id of the index-th diagram (0-based) of the document named scope.

func License

func License() []byte

License returns the license of the vendored Mermaid bundle.

func Script

func Script() []byte

Script returns the vendored mermaid.min.js.

func Sources

func Sources(htmlContent string) []string

Sources returns the source of each unrendered Mermaid block in HTML.

func Wrap

func Wrap(svg string) string

Wrap returns the markup a rendered diagram is published as.

Types

type DiagramError

type DiagramError struct {
	Message string
}

DiagramError is a problem with one diagram's source, reported by Mermaid (a syntax error, an unknown diagram type) or found in its output.

func (*DiagramError) Error

func (e *DiagramError) Error() string

type Engine

type Engine interface {
	Render(ctx context.Context, id, source string, seed uint32) (string, error)
	Close()
}

Engine draws one diagram and returns Mermaid's raw SVG markup. seed initializes the page's random number generator for that diagram. A problem with the diagram itself is returned as *DiagramError; any other error means the engine is broken and will be replaced.

type EngineFactory

type EngineFactory func(ctx context.Context) (Engine, error)

EngineFactory starts an Engine. ctx bounds the engine's lifetime.

type HTMLResult

type HTMLResult struct {
	// HTML is the input with every diagram that rendered replaced.
	HTML string
	// Diagrams is the number of Mermaid blocks found; Rendered how many of
	// them were replaced with SVG.
	Diagrams, Rendered int
	// Failures holds one *DiagramError-wrapping error per diagram that
	// Mermaid rejected ("diagram 2: …"); those stay source blocks.
	Failures []error
	// Unavailable is set when nothing could be rendered because Chrome is
	// missing or failed to start; every remaining diagram stays a source
	// block.
	Unavailable error
}

HTMLResult is the outcome of RenderHTML.

type Options

type Options struct {
	// CacheDir is where rendered diagrams are kept between builds. Empty
	// disables the disk cache.
	CacheDir string
	// Browser is a running Chrome to open the render tab in. Nil starts one
	// on the first diagram that is not cached; Close stops it.
	Browser *pdf.Browser
	// NewEngine replaces Chrome, for tests.
	NewEngine EngineFactory
	// DiagramTimeout caps one diagram; zero means 30 seconds.
	DiagramTimeout time.Duration
	// StartTimeout caps starting Chrome and loading Mermaid; zero means 60
	// seconds.
	StartTimeout time.Duration
}

Options configures a Renderer.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer converts Mermaid diagrams to SVG. It is safe for concurrent use; diagrams are drawn one at a time in a single browser tab, which is started lazily, so a book whose diagrams are all cached never starts Chrome.

func New

func New(opts Options) *Renderer

New returns a Renderer. It never starts Chrome; that happens on the first diagram that is not cached.

func (*Renderer) Close

func (r *Renderer) Close()

Close stops the render tab and any Chrome the renderer started. The Renderer still serves cached diagrams afterwards but renders nothing new.

func (*Renderer) RenderHTML

func (r *Renderer) RenderHTML(ctx context.Context, htmlContent, scope string) (HTMLResult, error)

RenderHTML replaces each Mermaid source block in htmlContent with <div class="mermaid-diagram"><svg …></svg></div>. scope names the document the HTML belongs to (a chapter); SVG ids are derived from it and the diagram's position, so they are stable across builds and unique across the documents of one book. The returned error is only ever ctx's.

func (*Renderer) RenderSVG

func (r *Renderer) RenderSVG(ctx context.Context, source, id string) (string, error)

RenderSVG renders one diagram and returns its sanitized <svg> element, whose id is id (a DiagramID). Errors wrap ErrUnavailable when no diagram can be rendered, are *DiagramError for a problem with this one, or are ctx's error.

func (*Renderer) Rendered

func (r *Renderer) Rendered() int

Rendered reports how many diagrams were drawn rather than served from a cache.

func (*Renderer) Unavailable

func (r *Renderer) Unavailable() error

Unavailable returns the error that keeps this renderer from drawing diagrams (it wraps ErrUnavailable), or nil when it can, or has not tried.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL