Skip to content

How it works

The package exists because raw Mermaid HTML is only the first half of the job. A real docs site still needs hydration, rerendering, export, zoom, fullscreen, and route-aware lifecycle control

The problem it solves

markdown-it turns a fence into HTML. It does not keep browser state, rerender on route changes, or provide a toolbar

markdown-it-mermaid-enhanced fills that gap in two stages

  1. The markdown-it plugin converts each Mermaid fence into a runtime-friendly wrapper
  2. The browser runtime upgrades that wrapper into an interactive diagram shell

The pipeline

Loading diagram…

Package architecture

Loading diagram…

This split keeps compile-time HTML generation browser-side interactivity shared config resolution and VitePress integration clearly separated

Rendering workflow

Loading diagram…

The key boundary is that the markdown-it side only serializes state while the runtime side owns async rendering and interaction state

What the plugin emits

Each diagram becomes a wrapper with three important parts

  • A hidden <pre> that stores the original Mermaid source
  • A data-mermaid-config payload that captures the resolved config
  • A toolbar with zoom, copy, export, and fullscreen actions

That wrapper format matters because rerendering reads from the same DOM node instead of replacing the whole block

Runtime model

The browser runtime upgrades wrappers into Vue apps on demand

  • initMermaidIt() finds unmounted wrappers and mounts them once
  • rerenderMermaidIt() rereads wrapper state and rerenders in place
  • Pan and zoom state live beside the wrapper, so config-only updates can preserve the current viewport

Runtime design

Loading diagram…

Each wrapper keeps a small runtime state bag so rerenders can swap SVG content in place instead of destroying the host node

Why route changes need help

Single-page docs frameworks replace page content without reloading the browser runtime. That is why the VitePress integration waits for DOM updates, retries wrapper discovery, and then mounts new diagrams after navigation

Key source files

  • src/markdown-it/plugin.ts handles fences and wrapper HTML
  • src/runtime.ts exposes the public runtime entry points
  • src/runtime/MermaidWrapperRuntime.vue owns the interactive shell
  • src/runtime/core.ts handles rendering, export, SVG mounting, and pan or zoom
  • src/vitepress.ts adapts the runtime to VitePress route changes