# Syntax highlighting in Markdown code fences How rune-kit colours fenced code, and why it uses hand-written scanners instead of a grammar engine. ## What it has to do - Colour the languages chat answers use most: TypeScript/TSX/JavaScript, JSON, Rust, Swift, CSS, HTML, shell, SQL and YAML. Anything else stays plain. - Treat the input as untrusted: model output can be huge or adversarial, so time must be linear in the source with no backtracking, and work per block must be capped. - Stream without layout shift: a fence grows a line at a time, and colouring must not move a glyph. - Build everywhere Rune builds, including wasm and iOS, without adding much to binaries. ## Options **syntect** (Sublime Text grammars). The most accurate of the three for many languages, but it ships a serialized grammar set (hundreds of KB to a few MB, depending on the languages kept), loads it lazily at a noticeable startup cost, and its grammars rely on regexes with lookaround, run by `onig` (C) or `fancy-regex`. Both backtrack, so a crafted line can take super-linear time. Highlighting is per line with carried state, which fits streaming, but the cost and the backtracking risk don't fit untrusted chat text. **tree-sitter**. Real incremental parsing and the best structure, but each language is a generated C parser (the TypeScript and TSX grammars alone are large), the runtime is C, and highlighting needs a query file per language. Every target, wasm and iOS included, then needs a C toolchain, and the size cost is several hundred KB per language. It is the right tool for an editor, more than a chat transcript needs. **Hand-written scanners** (chosen). One small byte scanner per language family (C-like, shell, markup, CSS, SQL, YAML), in the same spirit as the native mermaid renderer. Each only moves forward; lookaheads are bounded (a regex literal at most 512 bytes) or skip bytes the scanner would otherwise visit; embedded code (template `${}`, JSX `{}`, `