Getting started
Install limu, lay out a diagram, and render it as SVG.
limu is written twice, as limu-rs in Rust, whose crate is limu, and as limu in Go. They lay diagrams out alike, so these pages call both limu where they agree. limu turns Mermaid text into a scene, which records what each part of a diagram is and where it goes. A backend then draws the scene. This page uses the SVG backend, which, like limu itself, needs only the standard library. The examples are in Rust and Go. Pick a language with the switch at the top of the page or above any example, and the whole site follows it.
Installing
1cargo add limu1go get github.com/firede/limulimu-rs needs Rust 1.95 or later. It’s young, and its API will change in any 0.x release.
limu needs Go 1.27 or later.
Drawing a diagram
limu::layout limu.Layout reads a diagram and lays it out, and svg::render svg.Render writes the resulting scene as an SVG document.
1use limu::scene::style::{Look, Theme};2use limu::scene::svg;34fn main() -> Result<(), limu::Error> {5 let scene = limu::layout(6 "flowchart LR7 text[Mermaid text] --> layout[Layout] --> scene[Scene]",8 &Default::default(),9 )?;10 println!(11 "{}",12 svg::render(13 &scene,14 &svg::Options {15 look: Look::new(Theme::KOHU),16 font_family: Some("Arial, Helvetica, sans-serif".into()),17 ..Default::default()18 },19 )20 );21 Ok(())22}1package main23import (4 "fmt"5 "log"67 "github.com/firede/limu"8 "github.com/firede/limu/style"9 "github.com/firede/limu/svg"10)1112func main() {13 sc, err := limu.Layout(`flowchart LR14 text[Mermaid text] --> layout[Layout] --> scene[Scene]`, nil)15 if err != nil {16 log.Fatal(err)17 }18 fmt.Println(svg.Render(sc, &svg.Options{19 Look: style.Look{Theme: style.Kohu()},20 FontFamily: "Arial, Helvetica, sans-serif",21 }))22}By default, layout measures text with a table of Arial’s character widths, limu::layout::Arial text.Arial, so the SVG should set its text in Arial or a font of the same width. If you draw text in other fonts, measure it with those fonts instead. See Measuring text.
The theme sets the diagram’s colors. The SVG backend writes them as CSS variables with the theme’s colors as fallbacks, so a page can change them without rendering the diagram again. See Theming.
Errors
When limu can’t read a diagram, it returns an error, a limu::Error *diagram.Error. Its code says whether the type is unsupported, the text doesn’t parse, or the text is over a limit. Its line and column give the position when there is one.
1match limu::layout(src, &Default::default()) {2 Ok(scene) => draw(&scene),3 Err(e) => println!("{} at line {}: {}", e.code.as_str(), e.line, e.message),4}1sc, err := limu.Layout(src, nil)2var derr *diagram.Error3if errors.As(err, &derr) {4 fmt.Printf("%s at line %d: %s\n", derr.Code, derr.Line, derr.Message)5}Error messages can quote the diagram’s text, so display them as text rather than markup.
Fitting a width
Set the layout’s maximum width, layout::Options::max_width layout.Options.MaxWidth, to the width you have. Diagrams that can get narrower will, so a wide flowchart may switch from running across to running down. Diagrams that can’t get narrower keep the width they need.
1let scene = limu::layout(2 src,3 &limu::layout::Options {4 max_width: 480.0,5 ..Default::default()6 },7)?;1sc, err := limu.Layout(src, &layout.Options{MaxWidth: 480})The SVG is the size of the scene. To let a page scale it down further, give it max-width: 100% and height: auto in CSS.
Laying out again
If you lay out the same diagram more than once, for example when the available width changes, parse it once with limu::parse limu.Parse and lay out the result as often as you need. Layout doesn’t change the parsed diagram, so it’s safe to do from several goroutines or threads at once.
1use limu::layout::Options;23let d = limu::parse(src, &limu::ParseOptions::default())?;4let wide = d.layout(&Options { max_width: 640.0, ..Default::default() })?;5let narrow = d.layout(&Options { max_width: 320.0, ..Default::default() })?;1d, err := limu.Parse(src, diagram.Options{})2if err != nil {3 return err4}5wide, err := d.Layout(&layout.Options{MaxWidth: 640})6narrow, err := d.Layout(&layout.Options{MaxWidth: 320})Changing the theme doesn’t need a new layout at all. Draw the same scene in the new theme.
Diagram types
limu supports every diagram type that Pele draws, which covers every type built into Mermaid. limu::types limu.Types lists them. limu::supports limu.Supports checks a text’s declared type without parsing the rest.
1if !limu::supports(src) {2 // Show the text as code instead.3}1if !limu.Supports(src) {2 // Show the text as code instead.3}The limu crate has a feature for each diagram type, and all of them are on by default. A program that reads only some types can set default-features = false and turn on the features of the types it reads, such as flowchart and sequence. Text of a type whose feature is off is an unsupported diagram.
Examples shows each type, and Compatibility lists the keywords that declare them.
Next steps
- API covers parsing and layout options, limits and the scene.
- Theming covers themes, per-role overrides and custom drawing.
- SVG covers CSS variables, classes and untrusted diagrams.
- The backends draw diagrams natively. In Rust, tiny-skia draws into a bitmap, Core Graphics draws on Apple’s platforms, GPUI draws in a GPUI window, and Terminal draws in a terminal’s cells. In Go, MyGo draws on the GPU and finds the part under the pointer.