Crate md_kroki

source ·
Expand description CI

This crate provides a tool for rendering Kroki diagrams inside markdown strings. The input diagram code can either be inlined in the markdown or referenced via and external file, but for now the output is always inlined back into the markdown.


Creating a renderer

You can create a default renderer easily:

// This default renderer uses the API and only allows inlined diagrams.
let renderer = MdKroki::new();


The renderer also provides a synchronous render_sync method for sync contexts.

You can configure the endpoint and enable external file references with the builder:

let renderer = MdKroki::builder()

   // Use your own deployment of Kroki.

   // Resolve file references and read their contents.
   // See builder docs for more details.
   .path_resolver(|path| Ok(std::fs::read_to_string(path)?))



Inlining diagrams

You can write the diagram code directly in the markdown using the custom <kroki> tag:

<kroki type="erd">


  Person *--1 Location

The type attribute tells kroki what renderer to use and is required.

If you want to use traditional markdown elements, you can inline the diagram source with a fenced code block.

graph TD
  A[ Anyone ] -->|Can help | B( Go to )
  B --> C{ How to contribute? }
  C --> D[ Reporting bugs ]
  C --> E[ Sharing ideas ]
  C --> F[ Advocating ]

Here the code block language takes the place of the type attribute: it must be of the form kroki-<diagram type>. Otherwise it will be treated like a normal code block.

Referencing external files

If the input code of a diagram is too big to inline nicely in your markdown, you can reference an external file:

Using the kroki tag:
<kroki type="excalidraw" path="my/file.excalidraw" />

Or using a traditional markdown image tag:
![my excalidrawing](kroki-excalidraw:my/file.excalidraw)

When using the markdown tag, the path must be prefixed with kroki-<diagram type>:. Otherwise it will be treated like a normal image tag.

You must provide a path resolver to the builder if you want to use file references.