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

Signature TypeScript complète extraite du fichier du composable.

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].

Interfaces consommées

IUseCodeReturn

API

Valeurs de retour

Propriétés et méthodes exposées par l'objet retourné par le composable.

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

Exemples

Extraits de code prêts à coller.

Highlight a snippet on mount

vue

Prime the highlighter eagerly

vue