package mermaidcdp

import "go.abhg.dev/goldmark/mermaid/mermaidcdp"

Package mermaidcdp implements a server-side compiler for Mermaid diagrams that uses a headless Chromium-based browser to render the diagrams.

It's recommended to use this compiler instead of the CLI compiler for processes that render many diagrams.

Usage

Build the compiler with New, and be sure to [Close] it when you're done.

compiler, err := mermaidcdp.New(&mermaidcdp.Config{
	// ...
})
if err != nil {
	// handle error
}
defer compiler.Close()

Install the compiler into your server-side renderer. Do this by setting the 'Compiler' field on mermaid.Extender or mermaid.ServerRenderer.

mermaid.Extender{
	Compiler: compiler,
	// ...
}

MermaidJS source code

Compiler expects the MermaidJS source code supplied to it. This will typically be a minified version of the source code. You can download it from the MermaidJS GitHub repository or a CDN. For example, https://cdn.jsdelivr.net/npm/mermaid@10.6.0/dist/mermaid.min.js.

It is recommended that you download this once, and embed it into your program with go:embed.

import "embed" // for go:embed

//go:embed mermaid.min.js
var mermaidJSSource string

Then set it on the Config object you pass to New.

mermaidcdp.New(&mermaidcdp.Config{
	JSSource: mermaidJSSource,
})

Downloading MermaidJS source code

As a convenience, you can use DownloadJSSource to download a minified copy of MermaidJS from a CDN programmatically.

mermaidcdp.DownloadJSSource(ctx, "10.6.0")

This is useful if you can't embed it into your program. For most users, embedding it is recommended.

Index

Functions

func DownloadJSSource

func DownloadJSSource(ctx context.Context, version string) (string, error)

DownloadJSSource downloads and returns the specified version of MermaidJS from a CDN. Use this if you cannot bundle MermaidJS with your application.

version is the version of MermaidJS to download, without a "v" prefix. Example values are "10", "10.3.0".

Types

type Compiler

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

Compiler compiles Mermaid diagrams into SVGs.

func New

func New(cfg *Config) (_ *Compiler, err error)

New builds a new Compiler with the provided configuration.

The returned Compiler must be closed with [Close] when it is no longer needed.

func (*Compiler) Close

func (c *Compiler) Close() error

Close stops the compiler and releases any resources it holds. This method must be called when the compiler is no longer needed.

func (*Compiler) Compile

func (c *Compiler) Compile(ctx context.Context, req *mermaid.CompileRequest) (*mermaid.CompileResponse, error)

Compile renders a Mermaid diagram into an SVG. The context controls how long the rendering is allowed to take.

Panics if the Compiler has already been closed.

type Config

type Config struct {
	// JSSource is JavaScript code for MermaidJS as a string.
	//
	// This will normally be the contents of the mermaid.min.js file
	// acquired from https://mermaid.js.org/intro/#cdn.
	//
	// You can use DownloadJSSource if you don't haev it available.
	JSSource string

	// Theme to use for rendering.
	//
	// Values include "dark", "default", "forest", and "neutral".
	// See MermaidJS documentation for a full list.
	Theme string
}

Config specifies the configuration for a Compiler.