- Rust 100%
| src | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| README.md | ||
| test_document.md | ||
md: Terminal Markdown Renderer
md is a high-performance, native Rust CLI tool for rendering Markdown directly in your terminal. It goes beyond standard text viewers by leveraging modern terminal protocols and pure Rust layout engines to render syntax-highlighted code blocks, mathematical formulas (LaTeX), and diagram workflows (Mermaid) inline.
Features
- 🎨 Theme Aware: Automatically aligns with your terminal colors or applies custom palettes, with built-in premium support for Catppuccin Mocha (dark) and Catppuccin Latte (light).
- 📐 Self-Contained LaTeX Math: Parses
$ ... $(inline) and$$ ... $$(block) LaTeX equations and renders them inline using the pure-Rustratexlayout engine andresvgrasterization, with embedded KaTeX fonts for zero system dependencies. - 🧜 Mermaid Diagrams: Compiles
mermaidcode blocks into SVGs natively viamermaid-rs-rendererand renders them directly inside the terminal. - 🖼️ Kitty Graphics Protocol: Outputs high-resolution images for math formulas and diagrams using the Kitty terminal graphics protocol (supported by Ghostty, WezTerm, Kitty, and others).
- 🧱 Aligned Unicode Tables: Dynamically calculates column widths and draws neat table borders with proper text alignment (left, right, center).
- 📜 Stream Piping: Fully supports Unix pipes and redirection (e.g.,
cat doc.md | md).
Installation & Compilation
Ensure you have Rust and Cargo installed, then build the release binary:
cargo build --release
The compiled binary will be available at ./target/release/md. You can install it to your cargo bin directory using:
cargo install --path .
Command-Line Usage
md: A CLI markdown renderer for the terminal
Usage: md [OPTIONS] [FILE]
Arguments:
[FILE] Markdown file to render. If not provided, reads from stdin.
Options:
-t, --theme <THEME> Color/style theme to use (options: terminal, mocha, latte) [default: terminal]
-w, --width <WIDTH> Width to wrap text to (defaults to terminal width)
--no-images Disable rendering of diagrams (Mermaid) and math formulas (LaTeX) as images
-h, --help Print help
-V, --version Print version
Examples
Render a local file with the default terminal theme:
md document.md
Pipe text from another command and wrap it to 80 characters with the Catppuccin Mocha theme:
echo "# Hello World\nThis is **bold** text." | md -w 80 -t mocha
View a file with math and diagrams on WezTerm or Ghostty:
md research_notes.md
If your terminal does not support graphics or you want to output raw text, use --no-images to print the LaTeX and Mermaid source inside clean text boxes:
md research_notes.md --no-images
How It Works Under the Hood
- Math Preprocessing: Before parsing,
mdscans the document and wraps math delimiters into CommonMark code structures (math:inline tags ormathblock fences) while safely avoiding existing code blocks. - Markdown Parsing: Uses
pulldown-cmarkto generate a stream of structural events. - Word Wrapping & Layout: Words and formatting chunks are grouped and wrapped dynamically to fit your terminal columns, allowing styled text (bold, italic, links) to flow seamlessly.
- SVG Generation:
- Math equations are compiled using
ratex-parserandratex-layoutinto a display list, then serialized to SVG usingratex-svgin standalone mode (drawing glyph paths). - Mermaid diagrams are compiled using the
mermaid-rs-renderercrate into SVGs.
- Math equations are compiled using
- Rasterization: SVGs are rasterized into high-resolution PNGs at the terminal aspect ratio using
resvgandtiny-skia. - Kitty Graphics Transfer: PNGs are base64-encoded and outputted chunk-by-chunk using Kitty Graphics Protocol escape sequences (
\x1b_G...), reserving exactly the calculated cell width/height and advancing the cursor dynamically.