Maven Central Last Commit GitHub Scala Version ScalaJS Version Scala Native Version
A cross-platform Scala syntax highlighter that uses VS Code-compatible TextMate grammar JSON files to tokenize code and render highlighted HTML.
- TextMate grammar engine — regex state machine with begin/end patterns, captures, repository includes,
$selfreferences, and cycle detection - Two rendering modes — CSS classes with configurable prefix, or inline styles with theme colors
- Cross-platform — compiles to JVM, JavaScript (Scala.js), and Native (Scala Native)
- Built-in themes — OneDark and OneLight presets, or pass your own color scheme
libraryDependencies += "io.github.edadma" %%% "highlighter" % "0.0.1"
import io.github.edadma.highlighter.* val grammar = """{ "scopeName": "source.example", "patterns": [ { "match": "\\b(val|def|if|else)\\b", "name": "keyword.control" }, { "match": "\\b\\d+\\b", "name": "constant.numeric" }, { "begin": "\"", "end": "\"", "name": "string.quoted.double" }, { "match": "//.*$", "name": "comment.line" } ] }""" // CSS class mode (default) val Right(hl) = Highlighter.fromJson(grammar, ClassMode("hl-")): @unchecked println(hl.highlight("val x = 42")) // <span class="hl-keyword">val</span> x = <span class="hl-number">42</span> // Inline style mode val Right(hl2) = Highlighter.fromJson(grammar, InlineMode(Theme.OneDark)): @unchecked println(hl2.highlight("val x = 42")) // <span style="color:#c678dd">val</span> x = <span style="color:#d19a66">42</span>
Any VS Code-compatible .tmLanguage.json file works:
val grammarJson = scala.io.Source.fromFile("JavaScript.tmLanguage.json").mkString val Right(hl) = Highlighter.fromJson(grammarJson, ClassMode("code-")): @unchecked println(hl.highlight("const x = 42;"))
ClassMode(prefix) emits <span class="prefix-category"> where category is one of: keyword, string, comment, number, type, function, variable, operator, punctuation.
ClassMode("hl-") // <span class="hl-keyword"> ClassMode("code-") // <span class="code-keyword">
InlineMode(theme) emits <span style="color:#hex"> using a color theme.
InlineMode(Theme.OneDark) // dark theme InlineMode(Theme.OneLight) // light theme InlineMode(Theme( // custom colors keyword = "#ff0000", string = "#00ff00", comment = "#888888", // ... ))
matchpatterns withnameandcapturesbegin/endpatterns withbeginCaptures,endCaptures, and nestedpatternscontentNamefor scoping region contentrepositorywith named rule setsincludereferences:#ruleName,$self- Recursive/cyclic includes (with cycle detection)
- POSIX character classes (
[:alpha:],[:digit:], etc.) translated per platform
ISC