GitHub - lukilabs/beautiful-mermaid
Key Points
- 1Beautiful-Mermaid is a fast, dependency-free tool that renders Mermaid diagrams into customizable SVGs for rich UIs and ASCII/Unicode art for terminal environments, supporting five common diagram types.
- 2It addresses shortcomings of the default Mermaid renderer by offering a flexible two-color theming system with live switching via CSS variables, 15 built-in themes, and Shiki compatibility for extensive customization.
- 3Designed for the AI era, this pure TypeScript library provides dual output, ultra-fast rendering, and works everywhere from browsers to CLI tools, making complex visual concepts instantly graspable.
beautiful-mermaid is an open-source library designed to render Mermaid diagrams as aesthetically pleasing SVGs or ASCII art. Developed by Craft, it addresses limitations of the default Mermaid renderer, such as aesthetics, complex theming, lack of terminal output, and heavy dependencies, particularly for use in AI-assisted programming environments and CLI tools.
The library offers dual output capabilities: SVG for rich user interfaces and ASCII/Unicode for terminal environments, providing ultra-fast rendering (over 100 diagrams in under 500ms) with zero DOM dependencies, making it suitable for various platforms. It supports five core diagram types: Flowcharts, State Diagrams, Sequence Diagrams, Class Diagrams, and ER Diagrams.
The core methodology of beautiful-mermaid revolves around a sophisticated yet flexible theming system. It is built on a "Two-Color Foundation" in what is termed "Mono Mode." In this mode, only two fundamental colors are required: bg (background) and fg (foreground). All other diagram element colors are automatically derived from these two using CSS's color-mix() function. For instance:
- Text:
fgat 100% - Secondary text:
fgat 60% mixed intobg - Edge labels:
fgat 40% mixed intobg - Connectors:
fgat 30% mixed intobg - Arrow heads:
fgat 50% mixed intobg - Node fill:
fgat 3% mixed intobg - Node stroke:
fgat 20% mixed intobg
For richer themes, an "Enriched Mode" allows users to provide optional override colors for specific diagram roles. These enrichment colors include line (edge/connector), accent (arrow heads, highlights), muted (secondary text, labels), surface (node fill), and border (node stroke). If an enrichment color is not provided, the system gracefully falls back to the color-mix() derivation from bg and fg.
A key architectural feature of the theming system is its reliance on CSS custom properties (CSS variables). All colors are exposed as CSS variables on the SVG element (e.g., --bg, --fg). This enables dynamic, live theme switching without requiring re-rendering of the SVG; simply updating the CSS variables on the SVG element will instantly update the diagram's appearance. The library ships with 15 built-in themes, which can be accessed via THEMES object, or users can define custom themes by providing bg, fg, and optional enrichment colors.
For advanced theming, beautiful-mermaid offers full compatibility with Shiki, a syntax highlighter built on VS Code themes. The fromShikiTheme() function allows direct extraction of diagram colors from any Shiki theme object. This function intelligently maps VS Code editor theme properties to diagram roles:
editor.backgroundbgeditor.foregroundfgeditorLineNumber.foregroundlinefocusBorder/keyword tokenaccentcomment tokenmutededitor.selectionBackgroundsurfaceeditorWidget.borderborder
The library provides two primary rendering functions:
renderMermaid(text, options?): An asynchronous function that takes Mermaid source code and optionalRenderOptions(includingbg,fg, and enrichment colors, font, transparent background) to produce an SVG string.renderMermaidAscii(text, options?): A synchronous function that renders Mermaid source code to ASCII or Unicode text. This engine is based onmermaid-asciiby Alexander Grooff, ported from Go to TypeScript and extended with support for Sequence, Class, and ER diagrams, Unicode box-drawing characters, and configurable spacing.AsciiRenderOptionsincludeuseAscii(boolean,truefor pure ASCII,falsefor Unicode),paddingX,paddingY(horizontal/vertical node spacing), andboxBorderPadding(inner padding within node boxes).
The library is distributed via npm, bun, and pnpm, and also available for browser environments via CDN script tags, exposing renderMermaid, renderMermaidAscii, THEMES, DEFAULTS, and fromShikiTheme on the global beautifulMermaid object.