@@ -5,14 +5,13 @@ import {
55 type CodeOptionsMeta ,
66 type HighlighterCoreOptions ,
77 type CodeToHastOptionsCommon ,
8- type Highlighter ,
9- createHighlighter ,
108 getSingletonHighlighter ,
119} from 'shiki' ;
1210import type { BundledTheme } from 'shiki/themes' ;
1311import { type Components , toJsxRuntime } from 'hast-util-to-jsx-runtime' ;
1412import { Fragment , type ReactNode } from 'react' ;
1513import { jsx , jsxs } from 'react/jsx-runtime' ;
14+ import { createOnigurumaEngine } from 'shiki/engine/oniguruma' ;
1615
1716export function createStyleTransformer ( ) : ShikiTransformer {
1817 return {
@@ -41,46 +40,27 @@ export type HighlightOptions = CodeToHastOptionsCommon<BundledLanguage> &
4140 components ?: Partial < Components > ;
4241 } ;
4342
44- let instance : Promise < Highlighter > | undefined ;
45-
4643export async function highlight (
4744 code : string ,
4845 options : HighlightOptions ,
4946) : Promise < ReactNode > {
5047 const { lang, components, engine, ...rest } = options ;
5148
52- if ( ! instance ) {
53- if ( ! engine ) {
54- // use existing one if not specified
55- instance = getSingletonHighlighter ( ) ;
56- } else {
57- instance = createHighlighter ( {
58- langs : [ ] ,
59- themes : [ ] ,
60- engine,
61- } ) ;
62- }
63- }
64-
6549 let themes : CodeOptionsThemes < BundledTheme > = { themes : defaultThemes } ;
6650 if ( 'theme' in options && options . theme ) {
6751 themes = { theme : options . theme } ;
6852 } else if ( 'themes' in options && options . themes ) {
6953 themes = { themes : options . themes } ;
7054 }
7155
72- const highlighter = await instance ;
73- await Promise . all ( [
74- highlighter . loadLanguage ( lang as BundledLanguage ) ,
75-
76- 'theme' in themes
77- ? highlighter . loadTheme ( themes . theme as BundledTheme )
78- : highlighter . loadTheme (
79- ...( Object . values ( themes . themes ) . filter (
80- ( v ) => v !== undefined ,
81- ) as BundledTheme [ ] ) ,
82- ) ,
83- ] ) ;
56+ const highlighter = await getSingletonHighlighter ( {
57+ langs : [ lang ] ,
58+ engine : engine ?? createOnigurumaEngine ( ( ) => import ( 'shiki/wasm' ) ) ,
59+ themes :
60+ 'theme' in themes
61+ ? [ themes . theme ]
62+ : Object . values ( themes . themes ) . filter ( ( v ) => v !== undefined ) ,
63+ } ) ;
8464
8565 const hast = highlighter . codeToHast ( code , {
8666 lang,
0 commit comments