useCode

Code
stable

Singleton shiki-powered syntax highlighter. Lazily loads shiki on the first highlight() call to keep the initial bundle small. Bundles only the curated origam language subset and dual light/dark themes so token colours switch via CSS variables without re-tokenising. Caches results in a tiny LRU keyed by (code, lang).

API

Signature

Full TypeScript signature sourced from the composable file.

typescript

The singleton highlighter is module-scoped — every useCode() consumer in the app shares the same instance. Theme switching is CSS-only: shiki emits --shiki-light / --shiki-dark CSS vars on each token, and the SCSS consumes the matching var based on [data-theme].

Consumed interfaces

IUseCodeReturn

API

Return values

Properties and methods exposed by the composable return object.

highlight
Render code to a pre-tokenised HTML string. Cold (~200 ms on first call while shiki loads), instant on subsequent calls thanks to the LRU cache. Unknown languages fall back to plaintext with a one-shot console warning.
prime
Force the shiki highlighter to load. Useful in stories and tests to warm the cache before mounting consumers.
isReady
Returns false on first paint, flips to true once the dynamic import has resolved.
resetCacheForTesting
Wipe the LRU cache. Test-only — production code should never call this.

Usage

Examples

Ready-to-paste usage snippets.

Highlight a snippet on mount

vue

Prime the highlighter eagerly

vue