Skip to content

Add grok-mermaid: Mermaid-to-Unicode box-art renderer#293

Merged
simonw merged 1 commit into
mainfrom
claude/mermaid-unicode-wasm-l39o3c
Jul 16, 2026
Merged

Add grok-mermaid: Mermaid-to-Unicode box-art renderer#293
simonw merged 1 commit into
mainfrom
claude/mermaid-unicode-wasm-l39o3c

Conversation

@simonw

@simonw simonw commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Clone https://github.com/xai-org/grok-build to /tmp

Look at the code in crates/codegen/xai-grok-markdown/src/mermaid.rs which renders some chart types as "Unicode box-drawing art"

Figure out how to run that Rust code independent of the Rust of that codebase. Then figure out how to compile it to WebAssembly and build grok-mermaid.html as a browser-based playground for trying out the code, with several example mermaid charts you can try out.

Put your Rust code in a subfolder - with an appropriate license note, and the build scripts for turning that into the WASM blob

Demo: https://claude-mermaid-unicode-wasm.tools-b1q.pages.dev/grok-mermaid

Summary

This PR adds grok-mermaid, an in-browser playground for rendering Mermaid diagram source as terminal-style Unicode box-drawing art. The implementation extracts and adapts the renderer from xai-org/grok-build, making it available as a standalone WebAssembly module with a web UI.

Key Changes

  • New Rust library (grok-mermaid/):

    • src/mermaid.rs: Core renderer (copied from xai-org/grok-build under Apache-2.0)
    • src/lib.rs: Safe Rust entry points (render_plain, render_html) and WebAssembly FFI surface
    • src/shim.rs: Minimal stand-ins for ratatui types (Style, Modifier, Span, Line) to avoid pulling in the full ratatui dependency
    • Cargo.toml: Package configuration for wasm32 target
    • build_wasm.sh: Build script to compile to WebAssembly
  • Web UI (grok-mermaid.html):

    • Interactive playground with live rendering
    • Example buttons for quick testing
    • Syntax highlighting and error display
    • Styled output with CSS classes mirroring the original TUI theme (b for border, n for node text, e for edge, el for edge label, t for title, i for italic)
    • Copy-to-clipboard functionality
  • Testing (tests/test_grok_mermaid.py):

    • Playwright tests validating rendering via the WebAssembly module
    • Tests for initial render, example loading, and error handling
  • Build artifacts:

    • grok-mermaid.wasm: Compiled WebAssembly module
    • LICENSE: Apache-2.0 license (matching source)
    • README.md: Documentation on the origin and rebuild process
  • Configuration: Updated .gitignore to exclude Rust build output

Implementation Details

  • The renderer is wrapped in a minimal shim layer that replaces ratatui's styling system with CSS class names, enabling HTML output without external UI dependencies
  • The WebAssembly module uses a simple FFI surface (wasm_alloc, wasm_render_html, wasm_result_ptr) for browser integration, avoiding wasm-bindgen overhead
  • HTML output wraps styled text runs in <span> tags with semantic class names, allowing the page to apply custom theming

https://claude.ai/code/session_01NHbXcjun9Rg1yn55N4P497

Extracts the Mermaid-to-Unicode-box-drawing renderer from xai-org/grok-build
(crates/codegen/xai-grok-markdown/src/mermaid.rs, Apache-2.0) and compiles it
to WebAssembly for a browser playground.

- grok-mermaid/: standalone Rust crate. src/mermaid.rs is the upstream file
  unmodified except two import lines, which now point at src/shim.rs, a small
  stand-in for the four ratatui types it uses (Style carries a CSS class name
  instead of terminal colors). src/lib.rs adds render_plain/render_html and a
  wasm-bindgen-free FFI. Includes the upstream Apache-2.0 LICENSE; all 148
  upstream unit tests pass unchanged via cargo test.
- grok-mermaid/build_wasm.sh: cargo build --target wasm32-unknown-unknown
  (plus optional wasm-opt) producing grok-mermaid.wasm (~190 KB).
- grok-mermaid.html: playground with editable source, seven example diagrams
  (flowchart, subgraphs, sequence, state, class, ER, unsupported fallback),
  max-width control, copy-as-text and shareable URL fragment.
- tests/test_grok_mermaid.py: Playwright coverage for initial render, example
  buttons, live editing and URL-fragment loading.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NHbXcjun9Rg1yn55N4P497
@claude

claude Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Claude encountered an error —— View job


I'll analyze this and get back to you.

@simonw simonw merged commit 6cd0aca into main Jul 16, 2026
1 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants