Expand description
Unified syntax highlighting for arborium.
This crate provides the core highlighting engine that works with both:
- Statically linked Rust grammars: For CLI tools and servers
- Dynamically loaded WASM plugins: For browser contexts
§Why Async in a Highlighting Library?
You might wonder why a syntax highlighting library has async code. The answer is browser support.
-
Parsing is synchronous: Tree-sitter parsing cannot be async—it’s a fundamentally synchronous operation that walks the syntax tree.
-
Getting a grammar can be async: In browser contexts, grammar plugins are loaded from a CDN via JavaScript’s dynamic
import(). This is inherently async since it involves network requests and WASM instantiation.
In native Rust, grammars are statically linked, so the provider returns immediately. But the trait is async to support both use cases with the same code.
§Architecture
The highlighting system is built around two key traits:
Grammar: What a grammar can do — parse text and return spansGrammarProvider: How grammars are obtained — this is where sync vs async differs
§The Sync-in-Async-Clothing Pattern
The core highlighting logic (including injection handling) is written once
as async code in HighlighterCore. Two wrappers provide the sync and async APIs:
-
SyncHighlighter: Polls the async future once and panics if it yields. This is safe for native Rust where providers return immediately. -
AsyncHighlighter: Actually awaits provider calls. Use this for browser/WASM contexts where grammar loading involves network requests.
This design ensures both environments share the exact same injection-handling logic, avoiding subtle bugs from duplicated code.
§When to Use Which
| Context | Highlighter | Provider Example |
|---|---|---|
| Native Rust | SyncHighlighter | StaticProvider (grammars compiled in) |
| Browser WASM | AsyncHighlighter | JsGrammarProvider (loads from CDN) |
§Quick Start
use arborium_highlight::{SyncHighlighter, Grammar, GrammarProvider, ParseResult, Span};
use arborium_highlight::{HighlightConfig, HtmlFormat};
// Define your grammar (implements Grammar trait)
struct MyGrammar { /* ... */ }
impl Grammar for MyGrammar {
fn parse(&mut self, text: &str) -> ParseResult {
// Parse and return spans + injections
ParseResult::default()
}
}
// Define your provider (implements GrammarProvider trait)
struct MyProvider { /* ... */ }
impl GrammarProvider for MyProvider {
type Grammar = MyGrammar;
async fn get(&mut self, language: &str) -> Option<&mut Self::Grammar> {
// Return grammar for language
None
}
}
// Use with default configuration (custom elements: <a-k>, <a-f>, etc.)
let mut highlighter = SyncHighlighter::new(MyProvider { /* ... */ });
let html = highlighter.highlight("rust", "fn main() {}");
// Output: <a-k>fn</a-k> <a-f>main</a-f>() {}
// Or use class-based output for compatibility with existing CSS
let config = HighlightConfig {
html_format: HtmlFormat::ClassNames,
..Default::default()
};
let mut highlighter = SyncHighlighter::with_config(MyProvider { /* ... */ }, config);
let html = highlighter.highlight("rust", "fn main() {}");
// Output: <span class="keyword">fn</span> <span class="function">main</span>() {}§HTML Output Formats
Arborium supports multiple HTML output formats via HtmlFormat:
CustomElements(default): Compact custom elements like<a-k>,<a-f>, etc.CustomElementsWithPrefix(prefix): Custom elements with your prefix, e.g.,<code-k>ClassNames: Traditional<span class="keyword">for compatibilityClassNamesWithPrefix(prefix): Namespaced classes like<span class="arb-keyword">
See HtmlFormat for examples and use cases.
Structs§
- Ansi
Options - Options controlling ANSI rendering behavior.
- Async
Highlighter - Asynchronous highlighter for WASM/browser contexts.
- Highlight
Config - Configuration for highlighting.
- Injection
- An injection point for embedded languages.
- Parse
Result - Result of parsing a document with a grammar.
- Span
- A span of highlighted text.
- Sync
Highlighter - Synchronous highlighter for Rust contexts.
- Themed
Span - A span with a theme style index for rendering.
Enums§
- Highlight
Error - Errors that can occur during highlighting.
- Html
Format - HTML output format for syntax highlighting.
Traits§
- Grammar
- A grammar that can parse text and produce highlight spans.
- Grammar
Provider - Provides grammars for languages.
Functions§
- html_
escape - Escape HTML special characters.
- spans_
to_ ansi - Deduplicate spans and convert to ANSI-colored text using a theme.
- spans_
to_ ansi_ with_ options - ANSI rendering with additional configuration options.
- spans_
to_ html - Deduplicate spans and convert to HTML.
- spans_
to_ themed - Convert raw spans to themed spans by resolving capture names to theme indices.
- write_
spans_ as_ ansi - Write spans as ANSI-colored text to a writer.
- write_
spans_ as_ html - Write spans as HTML to a writer.