rememberHighlightedCodeBothThemes

fun rememberHighlightedCodeBothThemes(code: String, language: String, lightTheme: HighlightTheme = LocalLightHighlightTheme.current, darkTheme: HighlightTheme = LocalDarkHighlightTheme.current, onHighlightComplete: (ThemedHighlightResult) -> Unit? = null, onError: (HighlightException) -> Unit? = null): State<ThemedHighlightResult?>

Pre-highlights code for both light and dark themes in a single JS call.

Unlike calling rememberHighlightedCode twice, this runs the JavaScript tokeniser once and applies two colour maps to the same HTML output. Theme switching after the result is available is instant - no re-highlighting is needed.

Returns null while highlighting is in progress or if it failed.

Usage inside a HighlightThemeProvider

When called inside a HighlightThemeProvider, light and dark themes are picked up automatically from LocalLightHighlightTheme and LocalDarkHighlightTheme:

HighlightThemeProvider {
val result by rememberHighlightedCodeBothThemes(code = code, language = "kotlin")
val text = if (isDark) result?.dark else result?.light
Text(text = text ?: AnnotatedString(code))
}

Usage outside a provider (explicit themes)

@Composable
fun CodeSnippet(code: String, isDark: Boolean) {
val result by rememberHighlightedCodeBothThemes(
code = code,
language = "kotlin",
lightTheme = rememberTomorrowTheme(),
darkTheme = rememberTomorrowNightTheme(),
)
val text = if (isDark) result?.dark else result?.light
Text(text = text ?: AnnotatedString(code))
}

Return

A State holding a ThemedHighlightResult with both variants (including ThemedHighlightResult.durationMs for timing), or null while loading / on error.

Parameters

code

The source code to highlight.

language

The Highlight.js language identifier (e.g. "python", "kotlin").

lightTheme

Theme to apply for the light variant. Defaults to LocalLightHighlightTheme (available inside HighlightThemeProvider). Create inside remember to avoid re-parsing CSS on every recomposition.

darkTheme

Theme to apply for the dark variant. Defaults to LocalDarkHighlightTheme (available inside HighlightThemeProvider). Create inside remember to avoid re-parsing CSS on every recomposition.

onHighlightComplete

Optional callback invoked with a ThemedHighlightResult when highlighting succeeds. Fires after the State is updated. Not called on failure. Use result.durationMs for timing, result.light.spanStyles.size for span count.

onError

Optional callback invoked with the HighlightException when highlighting fails. The plain-text fallback is always displayed regardless - this callback is purely observational.