Open source · MIT

swift-mermaid

A pure-Swift Mermaid diagram renderer for Apple platforms — no JavaScript engine, no WKWebView, no network access. Built to be embedded in sandboxed apps that need to render Markdown previews containing ```mermaid fenced blocks without dragging WebKit (or its com.apple.security.network.client entitlement) along.

What it does

The pipeline mirrors Mermaid's own: source → preprocess → detect type → type-specific parser → type-specific layout → renderer → MermaidScene (geometry + style IR) → SVG, CGImage, or PDF.

Determinism is a hard requirement: same (source, theme, OS) → byte-identical SVG output.

Supported diagram types

Diagram typeStatus
flowchart / graphv1
sequenceDiagramv1
piev1
architecture-betav1 (built-in icons only — no custom iconify packs)
classDiagram(-v2)v1 (notes & namespaces parsed-and-skipped)
Other diagram types throw .unsupportedDiagramType cleanly so hosts can fall back to the raw source.

Usage

// Package.swift
dependencies: [
    .package(url: "https://github.com/australware/swift-mermaid.git", from: "0.1.0")
],
targets: [
    .target(name: "YourApp", dependencies: [
        .product(name: "Mermaid", package: "swift-mermaid")
    ])
]
import Mermaid

let scene = try Mermaid.render("""
flowchart TD
    Start([Start]) --> Check{Valid?}
    Check -->|yes| Process[Process data]
    Check -->|no| Error[/Show error/]
    Process --> Save[(Database)]
    Save --> Done([End])
    Error --> Done
""", theme: .dark)

let svg: String   = scene.svgString()
let png: CGImage? = scene.cgImage(scale: 2)
let pdf: Data     = scene.pdfData()

Platforms

macOS 14+. Uses Core Text for deterministic text measurement.

License

MIT. The package vendors a copy of dagre-swift (also MIT) as an internal target — see the third-party notices.

Contributing & issues

Bug reports and pull requests on GitHub. For private inquiries: support@australware.io.