Why highlighter
highlighter turns source code into highlighted output using TextMate grammars — the same
.tmLanguage.json files that drive VS Code. Point it at a grammar, hand it a snippet, and it
gives you back highlighted HTML, or the raw tokens to render however you like.
import io.github.edadma.highlighter.*
val Right(hl) = Highlighter.fromJson(grammarJson, InlineMode(Theme.OneDark)): @unchecked
println(hl.highlight("val x = 42"))
// <span style="color:#c678dd">val</span> x = <span style="color:#d19a66">42</span>
The grammar engine is a full regex state machine — begin/end spans, captures, repository
includes, $self references, cycle detection — running on a pure-Scala port of Oniguruma, so
the same grammars work identically on every platform, with no native dependency.
A Scala snippet, highlighted by this site at build time from highlighter’s own Scala grammar:
// quicksort
def sort(xs: List[Int]): List[Int] = xs match
case Nil => Nil
case pivot :: rest =>
val (less, more) = rest.partition(_ < pivot)
sort(less) ++ (pivot :: sort(more))
val nums = List(5, 2, 8, 1, 9)
println(sort(nums)) // List(1, 2, 5, 8, 9)
What’s here
- Getting Started — add the dependency and highlight your first snippet.
- Guide — loading grammars, the HTML render modes, themes, and consuming tokens directly.
What it does
- TextMate grammar engine —
matchandbegin/endpatterns,captures,contentName,repositoryrule sets,#rule/$selfincludes, with cycle detection. - Two HTML render modes — CSS classes with a configurable prefix, or inline
stylecolours from a theme. - Built-in themes — OneDark, OneLight, and Monokai, or your own colour scheme.
- Tokens, not just HTML — take
List[List[Token]](one list per source line) and render to a PDF, a terminal, or a GUI against your own palette. - Three platforms — the JVM, Scala.js, and Scala Native, all from one pure-Scala codebase.