Crate md_kroki

source ·
Expand description

Crates.io Docs.rs 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.

Usage

Creating a renderer

You can create a default renderer easily:

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

renderer.render(my_markdown_string).await

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.
   .endpoint("http://localhost/")

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

   .build();

renderer.render(my_markdown_string).await

Inlining diagrams

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

<kroki type="erd">
  [Person]
  *name
  height
  weight
  +birth_location_id

  [Location]
  *id
  city
  state
  country

  Person *--1 Location
</kroki>

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.

```kroki-mermaid
graph TD
  A[ Anyone ] -->|Can help | B( Go to github.com/yuzutech/kroki )
  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.

Structs