highlight_fork

Telosnex's Dart port of highlight.js 11, based on upstream master 696d9a4. The default registry contains 38 grammars and preserves all 78 existing fence aliases. Dart 3.8 or later is required.

Highlight code

import 'package:highlight_fork/highlight.dart';

void main() {
  ensureInitialized();
  final result = hljs.highlight('print("hello");', language: 'dart');
  print(result.toHtml());
}

hljs.highlightAuto(code) detects a language. An unknown explicit language throws ArgumentError. The result contains a DataNode tree at result.emitter.root. Each child is a text string or another DataNode with an optional scope. Scopes can contain dots, for example title.function and title.class. Embedded language scopes use language:javascript.

For a custom registry, import highlight_core.dart and create a Highlight instance. Register a grammar with engine.registerLanguage('dart', dart) from languages/dart.dart. The default registry does not load the full generated grammar set, including the 1C grammar.

Resume a parse

final first = hljs.highlight('/* first line\n', language: 'cpp');
final second = hljs.highlight(
  'second line */ int n = 1;\n',
  language: 'cpp',
  continuation: first.continuation,
);

HighlightContinuation is an opaque, reusable checkpoint. It retains open modes, callback data, and named embedded-language states. A checkpoint belongs to its engine and registered language definition. Aliases for that definition work. Resuming a checkpoint does not mutate it. This supports repeated parses of a partial trailing line.

Continuation does not guarantee that arbitrary chunks match a full parse. A chunk boundary can divide a token, delimiter, escape, or grammar lookahead. Even newline boundaries can change colours: JavaScript recognises hello\n() as a function call only when both lines are available. Concatenated HTML also has different span boundaries when a scope spans chunks. Tests compare scope paths per character instead.

Validation

From this directory:

dart pub get
dart test
dart test -p chrome
dart test -p chrome -c dart2wasm

The native integration tests use the highlight.js 11 fixtures in vendor/highlight.js. Initialize that submodule from the repository root before the tests. Browser tests cover the limited registry, continuation, vendor grammar ports, and public streaming benchmarks.

Reproduce the evaluation

dart run tool/evaluate.dart --bundle /path/to/hleval --output /tmp/highlight-evaluation
dart run tool/benchmark.dart
# Generate local browser fixtures, without committing private inputs:
dart run tool/evaluate.dart --bundle /path/to/hleval --output build/evaluation --skip-stream --emit-browser-test --emit-browser-stream-test
dart test build/evaluation --platform chrome --compiler dart2js
dart test build/evaluation --platform chrome --compiler dart2wasm

The evaluator reads the original bundle locally and writes fresh token runs, detection results, quality comparisons, and streaming timings. The public benchmark generates its own 20 KB inputs and can compile with dart2js or dart2wasm. Timing results include worst-chunk duration and scope differences from a full parse. Do not commit evaluation outputs: they can contain private source text.

Migration from 0.7.x

  • Replace highlight.parse with hljs.highlight, or hljs.highlightAuto for detection.
  • Call ensureInitialized() when using the default registry.
  • Replace Result.nodes / Node with HighlightResult.emitter.root / DataNode.
  • Replace parseWithContinuation and result.top with highlight(..., continuation: ...) and result.continuation.
  • Theme keyword replaces legacy meta-keyword. Many built-in types now use type.
  • Resolve dotted theme scopes explicitly or fall back to their parent scope.

License

MIT. Generated grammars retain the upstream highlight.js definitions.

Libraries

highlight
Telosnex's limited registry: 38 grammars, with all existing fence aliases. Import highlight_core.dart instead to create an isolated custom registry.
highlight_core
Core highlight.js engine for Dart — no languages registered by default.
languages/1c
languages/_all
languages/abnf
languages/accesslog
languages/actionscript
languages/ada
languages/angelscript
languages/apache
languages/applescript
languages/arcade
languages/arduino
languages/armasm
languages/asciidoc
languages/aspectj
languages/autohotkey
languages/autoit
languages/avrasm
languages/awk
languages/axapta
languages/bash
languages/basic
languages/bnf
languages/brainfuck
languages/c
languages/cal
languages/capnproto
languages/ceylon
languages/clean
languages/clojure
languages/clojure_repl
languages/cmake
languages/coffeescript
languages/coq
languages/cos
languages/cpp
languages/crmsh
languages/crystal
languages/csharp
languages/csp
languages/css
languages/d
languages/dart
languages/delphi
languages/diff
languages/django
languages/dns
languages/dockerfile
languages/dos
languages/dsconfig
languages/dts
languages/dust
languages/ebnf
languages/elixir
languages/elm
languages/erb
languages/erlang
languages/erlang_repl
languages/excel
languages/fix
languages/flix
languages/fortran
languages/fsharp
languages/gams
languages/gauss
languages/gcode
languages/gherkin
languages/glsl
languages/gml
languages/go
languages/golo
languages/gradle
languages/graphql
languages/groovy
languages/haml
languages/handlebars
languages/haskell
languages/haxe
languages/hsp
languages/http
languages/hy
languages/inform7
languages/ini
languages/irpf90
languages/isbl
languages/java
languages/javascript
languages/jboss_cli
languages/json
languages/julia
languages/julia_repl
languages/kotlin
languages/lasso
languages/latex
languages/ldif
languages/leaf
languages/less
languages/lisp
languages/livecodeserver
languages/livescript
languages/llvm
languages/lsl
languages/lua
languages/makefile
languages/markdown
languages/mathematica
languages/matlab
languages/maxima
languages/mel
languages/mercury
languages/mipsasm
languages/mizar
languages/mojolicious
languages/monkey
languages/moonscript
languages/n1ql
languages/nestedtext
languages/nginx
languages/nim
languages/nix
languages/node_repl
languages/nsis
languages/objectivec
languages/ocaml
languages/openscad
languages/oxygene
languages/parser3
languages/perl
languages/pf
languages/pgsql
languages/php
languages/php_template
languages/plaintext
languages/pony
languages/powershell
languages/processing
languages/profile
languages/prolog
languages/properties
languages/protobuf
languages/puppet
languages/purebasic
languages/python
languages/python_repl
languages/q
languages/qml
languages/r
languages/reasonml
languages/rib
languages/roboconf
languages/routeros
languages/rsl
languages/ruby
languages/ruleslanguage
languages/rust
languages/sas
languages/scala
languages/scheme
languages/scilab
languages/scss
languages/shell
languages/smali
languages/smalltalk
languages/sml
languages/solidity
languages/sqf
languages/sql
languages/stan
languages/stata
languages/step21
languages/stylus
languages/subunit
languages/swift
languages/taggerscript
languages/tap
languages/tcl
languages/thrift
languages/tp
languages/twig
languages/typescript
languages/vala
languages/vbnet
languages/vbscript
languages/vbscript_html
languages/verilog
languages/vhdl
languages/vim
languages/vue
languages/wasm
languages/wren
languages/x86asm
languages/xl
languages/xml
languages/xquery
languages/yaml
languages/zephir