MDExMermex

Hex.pmHexdocs.pmLicense: MIT

An MDEx plugin that renders Mermaid diagrams server-side using Mermex (Rust NIF). Each diagram is wrapped in an interactive container with zoom, pan, and fullscreen controls.

Installation

Add mdex_mermex to your list of dependencies in mix.exs:

def deps do
  [
    {:mdex_mermex, "~> 0.1.1"}
  ]
end

Then run:

mix deps.get

Usage

Pass MDExMermex as a plugin to MDEx.to_html!/2:

markdown = """
```mermaid
flowchart LR
  A-->B-->C
```
"""

MDEx.to_html!(markdown, plugins: [MDExMermex])

This renders the Mermaid diagram server-side and outputs an interactive HTML container with the SVG inline. CSS and JS are injected once per document by default.

Options

Option Default Description
:output:inline_svg:inline_svg for raw SVG, :img_base64 for base64 <img> tag
:classnil Additional CSS class(es) appended to the wrapper <div>
:inject_csstrue Set to false to skip per-document <style> injection
:inject_jstrue Set to false to skip per-document <script> injection
:css_layernil Wraps injected CSS in a @layer rule (e.g. "components")
# Base64 image output
MDEx.to_html!(markdown, plugins: [{MDExMermex, output: :img_base64}])

# Custom CSS class — produces: <div class="mdex-mermex my-diagram" tabindex="0">
MDEx.to_html!(markdown, plugins: [{MDExMermex, class: "my-diagram"}])

# Wrap injected CSS in a cascade layer
MDEx.to_html!(markdown, plugins: [{MDExMermex, css_layer: "components"}])

Including Assets Manually

When injection is disabled (e.g. for Phoenix LiveView where inline <script> tags won't execute via DOM patches), import the assets directly from the package:

MDEx.to_html!(markdown, plugins: [{MDExMermex, inject_css: false, inject_js: false}])

The JS uses a MutationObserver to automatically initialize diagrams added to the DOM after page load, making it compatible with LiveView and other frameworks that patch the DOM.

Features

License

MIT — see LICENSE for details.