arborium-highlight
Core syntax highlighting engine for arborium.
Overview
arborium-highlight provides:
- Grammar trait: Parse text, return spans + injections
- GrammarProvider trait: Get grammars by language (sync or async)
- SyncHighlighter: For Rust native (statically linked grammars)
- AsyncHighlighter: For WASM browser (dynamically loaded plugins)
- HTML rendering:
spans_to_html()outputs<a-k>,<a-s>, etc.
Architecture
┌─────────────────────────────────────────────────────────────────┐
│ GrammarProvider │
│ │
│ async fn get(&mut self, lang: &str) -> Option<&mut Grammar> │
└─────────────────────────────────────────────────────────────────┘
│ │
│ Rust: returns immediately │ WASM: awaits JS Promise
│ (statically linked) │ (wasm-bindgen-futures)
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ SyncHighlighter │ │ AsyncHighlighter│
│ │ │ │
│ Polls once, │ │ Actually awaits │
│ panics if │ │ │
│ Pending │ │ │
└─────────────────┘ └─────────────────┘
Rust Usage
use Highlighter;
let mut highlighter = new;
let html = highlighter.highlight?;
// Output: <a-k>fn</a-k> <a-f>main</a-f>() {}
WASM Usage
The host uses wasm-bindgen with wasm-bindgen-futures for async:
// In arborium-host (wasm-bindgen crate)
pub async
import init from 'arborium-host';
await ;
const html = await ;
The WasmPluginProvider::get() calls into JS to load grammar plugins.
JS returns a Promise, Rust awaits it via wasm-bindgen-futures.
Grammar plugins are WIT components loaded on demand.
Key Types
Injection Handling
When parsing HTML like <style>h1 { color: red; }</style>:
- HTML grammar returns spans for tags + injection
{7..25, "css"} - Highlighter calls
provider.get("css").await - CSS grammar parses
h1 { color: red; } - Spans are offset-adjusted and merged
- Recurse for any nested injections
- Render combined spans to HTML
Crate Structure
crates/
├── arborium-highlight/ # This crate - core engine
├── arborium/ # Rust API (SyncHighlighter + StaticProvider)
├── arborium-host/ # WASM host (wasm-bindgen, AsyncHighlighter)
├── arborium-theme/ # Capture → tag mapping
└── lang-*/ # Grammar plugins (WIT components)