highlight

suspend fun highlight(code: String, language: String, theme: HighlightTheme): Result<HighlightResult>

Full pipeline: tokenise → apply theme → convert to HighlightResult.

Combines highlightToHtml with colour-map application to produce a ready-to-render AnnotatedString. A HighlightResult.spanCount of 0 indicates that no syntax tokens were styled (e.g. unrecognized or blank language, empty code, or plaintext); HighlightResult.annotated still contains plain text so callers can always render something.

engine.highlight(code, "kotlin", theme).onSuccess { result ->
display(result.annotated)
if (result.spanCount == 0) log("no tokens - language may be unsupported")
log("highlighted in ${result.durationMs} ms")
}

Return

Result wrapping a HighlightResult, or Result.failure with a HighlightException on error.

Parameters

code

The source code to highlight.

language

Highlight.js language identifier (e.g. "kotlin", "python"). If blank or unrecognized by Highlight.js, returns plain unhighlighted text without running automatic language detection.

theme

The HighlightTheme whose colour map will be applied.