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
- Variables
- func CountDiagrams(htmlContent string) int
- func DiagramID(scope string, index int) string
- func License() []byte
- func Script() []byte
- func Sources(htmlContent string) []string
- func Wrap(svg string) string
- type DiagramError
- type Engine
- type EngineFactory
- type HTMLResult
- type Options
- type Renderer
Constants ¶
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 ¶
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 ¶
CountDiagrams returns the number of unrendered Mermaid blocks in HTML.
func DiagramID ¶
DiagramID returns the SVG id of the index-th diagram (0-based) of the document named scope.
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 ¶
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
// 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 ¶
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 ¶
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 ¶
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 ¶
Rendered reports how many diagrams were drawn rather than served from a cache.
func (*Renderer) Unavailable ¶
Unavailable returns the error that keeps this renderer from drawing diagrams (it wraps ErrUnavailable), or nil when it can, or has not tried.