@@ -8,13 +8,15 @@ import {
88 type HighlighterGeneric ,
99 isSpecialLang ,
1010 type LanguageRegistration ,
11+ type RegexEngine ,
1112 type ShikiTransformer ,
1213 type ThemeRegistration ,
1314 type ThemeRegistrationRaw ,
1415} from 'shiki' ;
1516import { globalShikiStyleCollector } from './shiki-style-collector.js' ;
1617import { transformerStyleToClass } from './transformers/style-to-class.js' ;
1718import type { ThemePresets } from './types.js' ;
19+ import { loadShikiEngine } from '#shiki-engine' ;
1820
1921export interface ShikiHighlighter {
2022 codeToHast (
@@ -34,11 +36,6 @@ export interface CreateShikiHighlighterOptions {
3436 theme ?: ThemePresets | ThemeRegistration | ThemeRegistrationRaw ;
3537 themes ?: Record < string , ThemePresets | ThemeRegistration | ThemeRegistrationRaw > ;
3638 langAlias ?: HighlighterCoreOptions [ 'langAlias' ] ;
37- /**
38- * Custom regex engine for Shiki.
39- * Use JavaScript engine for environments that can't load WASM files (e.g. Cloudflare Workers).
40- */
41- engine ?: HighlighterCoreOptions [ 'engine' ] ;
4239}
4340
4441export interface ShikiHighlighterHighlightOptions {
@@ -81,20 +78,25 @@ const cssVariablesTheme = () =>
8178// Caches Promise<ShikiHighlighter> for reuse when the same theme and langs are provided
8279const cachedHighlighters = new Map ( ) ;
8380
81+ let shikiEngine : RegexEngine | undefined = undefined ;
82+
8483export async function createShikiHighlighter ( {
8584 langs = [ ] ,
8685 theme = 'github-dark' ,
8786 themes = { } ,
8887 langAlias = { } ,
89- engine,
9088} : CreateShikiHighlighterOptions = { } ) : Promise < ShikiHighlighter > {
9189 theme = theme === 'css-variables' ? cssVariablesTheme ( ) : theme ;
9290
91+ if ( shikiEngine === undefined ) {
92+ shikiEngine = await loadShikiEngine ( ) ;
93+ }
94+
9395 const highlighterOptions = {
9496 langs : [ 'plaintext' , ...langs ] ,
9597 langAlias,
9698 themes : Object . values ( themes ) . length ? Object . values ( themes ) : [ theme ] ,
97- ... ( engine ? { engine } : { } ) ,
99+ engine : shikiEngine ,
98100 } ;
99101
100102 const key = JSON . stringify ( highlighterOptions , Object . keys ( highlighterOptions ) . sort ( ) ) ;
0 commit comments