@@ -134,6 +134,12 @@ export function createdBundledHighlighter<BundledLangs extends string, BundledTh
134
134
loadTheme ( ...themes ) {
135
135
return core . loadTheme ( ...themes . map ( resolveTheme ) )
136
136
} ,
137
+ getBundledLanguages ( ) {
138
+ return bundledLanguages
139
+ } ,
140
+ getBundledThemes ( ) {
141
+ return bundledThemes
142
+ } ,
137
143
}
138
144
}
139
145
@@ -224,53 +230,58 @@ export function makeSingletonHighlighter<L extends string, T extends string>(
224
230
return getSingletonHighlighter
225
231
}
226
232
233
+ export interface CreateSingletonShorthandsOptions < L extends string , T extends string > {
234
+ /**
235
+ * A custom function to guess embedded languages to be loaded.
236
+ */
237
+ guessEmbeddedLanguages ?: ( code : string , lang : string | undefined , highlighter : HighlighterGeneric < L , T > ) => Awaitable < string [ ] | undefined >
238
+ }
239
+
227
240
export function createSingletonShorthands < L extends string , T extends string > (
228
241
createHighlighter : CreateHighlighterFactory < L , T > ,
242
+ config ?: CreateSingletonShorthandsOptions < L , T > ,
229
243
) : ShorthandsBundle < L , T > {
230
244
const getSingletonHighlighter = makeSingletonHighlighter ( createHighlighter )
231
245
246
+ async function get ( code : string , options : CodeToTokensOptions < L , T > | CodeToHastOptions < L , T > ) : Promise < HighlighterGeneric < L , T > > {
247
+ const shiki = await getSingletonHighlighter ( {
248
+ langs : [ options . lang as L ] ,
249
+ themes : ( 'theme' in options ? [ options . theme ] : Object . values ( options . themes ) ) as T [ ] ,
250
+ } )
251
+ const langs = await config ?. guessEmbeddedLanguages ?.( code , options . lang , shiki ) as L [ ]
252
+ if ( langs ) {
253
+ await shiki . loadLanguage ( ...langs )
254
+ }
255
+ return shiki
256
+ }
257
+
232
258
return {
233
259
getSingletonHighlighter ( options ) {
234
260
return getSingletonHighlighter ( options )
235
261
} ,
236
262
237
263
async codeToHtml ( code , options ) {
238
- const shiki = await getSingletonHighlighter ( {
239
- langs : [ options . lang as L ] ,
240
- themes : ( 'theme' in options ? [ options . theme ] : Object . values ( options . themes ) ) as T [ ] ,
241
- } )
264
+ const shiki = await get ( code , options )
242
265
return shiki . codeToHtml ( code , options )
243
266
} ,
244
267
245
268
async codeToHast ( code , options ) {
246
- const shiki = await getSingletonHighlighter ( {
247
- langs : [ options . lang as L ] ,
248
- themes : ( 'theme' in options ? [ options . theme ] : Object . values ( options . themes ) ) as T [ ] ,
249
- } )
269
+ const shiki = await get ( code , options )
250
270
return shiki . codeToHast ( code , options )
251
271
} ,
252
272
253
273
async codeToTokens ( code , options ) {
254
- const shiki = await getSingletonHighlighter ( {
255
- langs : [ options . lang as L ] ,
256
- themes : ( 'theme' in options ? [ options . theme ] : Object . values ( options . themes ) ) as T [ ] ,
257
- } )
274
+ const shiki = await get ( code , options )
258
275
return shiki . codeToTokens ( code , options )
259
276
} ,
260
277
261
278
async codeToTokensBase ( code , options ) {
262
- const shiki = await getSingletonHighlighter ( {
263
- langs : [ options . lang as L ] ,
264
- themes : [ options . theme as T ] ,
265
- } )
279
+ const shiki = await get ( code , options )
266
280
return shiki . codeToTokensBase ( code , options )
267
281
} ,
268
282
269
283
async codeToTokensWithThemes ( code , options ) {
270
- const shiki = await getSingletonHighlighter ( {
271
- langs : [ options . lang as L ] ,
272
- themes : Object . values ( options . themes ) . filter ( Boolean ) as T [ ] ,
273
- } )
284
+ const shiki = await get ( code , options )
274
285
return shiki . codeToTokensWithThemes ( code , options )
275
286
} ,
276
287
0 commit comments