Skip to main content

Crate hl_lite

Crate hl_lite 

Source
Expand description

A tiny, dependency-free syntax highlighter.

hl-lite turns a source string into a flat list of Spans, each tagged with a Kind that maps to a CSS class (hl-keyword, hl-string, …). It is built for documentation sites: twelve hand-written byte-level lexers, no regex engine, no C, no dependencies, and a few tens of kilobytes of WebAssembly instead of the megabytes a tree-sitter grammar set costs.

The lexers are lexical, not scope-aware — the same level of fidelity highlight.js or Prism give you, not a parse tree. See the crate README for the per-language table of known gaps.

§Example

use hl_lite::{highlight, Kind, Lang};

let lang = Lang::from_slug("rs").unwrap();
let spans = highlight(lang, "let x = 1;");

assert_eq!(spans[0].kind, Kind::Keyword);
assert_eq!(spans[0].text, "let");
assert_eq!(spans[0].kind.class(), "keyword");

// The spans always reconstruct the input exactly.
let joined: String = spans.iter().map(|s| s.text).collect();
assert_eq!(joined, "let x = 1;");

§Guarantees

  • Concatenating every Span::text reproduces the input byte for byte.
  • Spans never split a char, so non-ASCII input cannot panic.
  • Unterminated strings, comments, heredocs and templates run to the end of the input instead of panicking or looping.
  • Every lexer is a single forward pass: highlighting is O(input length).

Structs§

Span
A classified slice of the input.

Enums§

Kind
What a Span of source is.
Lang
A language hl-lite can highlight.

Functions§

highlight
Highlight src as lang.