@shikijs/core 1.7.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-tokens.d.mts +1 -1
- package/dist/index.d.mts +17 -8
- package/dist/index.mjs +126 -45
- package/dist/types.d.mts +8 -0
- package/package.json +1 -1
package/dist/chunk-tokens.d.mts
CHANGED
|
@@ -1017,7 +1017,7 @@ interface BundledHighlighterOptions<L extends string, T extends string> {
|
|
|
1017
1017
|
*
|
|
1018
1018
|
* @default []
|
|
1019
1019
|
*/
|
|
1020
|
-
themes: (ThemeInput | StringLiteralUnion<T>)[];
|
|
1020
|
+
themes: (ThemeInput | StringLiteralUnion<T> | SpecialTheme)[];
|
|
1021
1021
|
/**
|
|
1022
1022
|
* Language registation
|
|
1023
1023
|
*
|
package/dist/index.d.mts
CHANGED
|
@@ -10,17 +10,23 @@ export { W as WebAssemblyInstantiator, l as loadWasm } from './chunk-index.mjs';
|
|
|
10
10
|
*
|
|
11
11
|
* @see http://shiki.style/guide/install#fine-grained-bundle
|
|
12
12
|
*/
|
|
13
|
+
declare function createHighlighterCore(options?: HighlighterCoreOptions): Promise<HighlighterCore>;
|
|
14
|
+
declare function makeSingletonHighlighterCore(createHighlighter: typeof createHighlighterCore): (options?: Partial<HighlighterCoreOptions>) => Promise<HighlighterCore>;
|
|
15
|
+
declare const getSingletonHighlighterCore: (options?: Partial<HighlighterCoreOptions>) => Promise<HighlighterCore>;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Use `createHighlighterCore` or `getSingletonHighlighterCore` instead.
|
|
18
|
+
*/
|
|
13
19
|
declare function getHighlighterCore(options?: HighlighterCoreOptions): Promise<HighlighterCore>;
|
|
14
20
|
|
|
15
|
-
type
|
|
21
|
+
type CreateHighlighterFactory<L extends string, T extends string> = (options: BundledHighlighterOptions<L, T>) => Promise<HighlighterGeneric<L, T>>;
|
|
16
22
|
/**
|
|
17
|
-
* Create a `
|
|
23
|
+
* Create a `createHighlighter` function with bundled themes and languages.
|
|
18
24
|
*
|
|
19
25
|
* @param bundledLanguages
|
|
20
26
|
* @param bundledThemes
|
|
21
27
|
* @param loadWasm
|
|
22
28
|
*/
|
|
23
|
-
declare function createdBundledHighlighter<BundledLangs extends string, BundledThemes extends string>(bundledLanguages: Record<BundledLangs, LanguageInput>, bundledThemes: Record<BundledThemes, ThemeInput>, loadWasm: HighlighterCoreOptions['loadWasm']):
|
|
29
|
+
declare function createdBundledHighlighter<BundledLangs extends string, BundledThemes extends string>(bundledLanguages: Record<BundledLangs, LanguageInput>, bundledThemes: Record<BundledThemes, ThemeInput>, loadWasm: HighlighterCoreOptions['loadWasm']): CreateHighlighterFactory<BundledLangs, BundledThemes>;
|
|
24
30
|
interface ShorthandsBundle<L extends string, T extends string> {
|
|
25
31
|
/**
|
|
26
32
|
* Shorthand for `codeToHtml` with auto-loaded theme and language.
|
|
@@ -59,12 +65,11 @@ interface ShorthandsBundle<L extends string, T extends string> {
|
|
|
59
65
|
codeToTokensWithThemes: (code: string, options: RequireKeys<CodeToTokensWithThemesOptions<L, T>, 'themes' | 'lang'>) => Promise<ThemedTokenWithVariants[][]>;
|
|
60
66
|
/**
|
|
61
67
|
* Get internal singleton highlighter.
|
|
62
|
-
*
|
|
63
|
-
* @internal
|
|
64
68
|
*/
|
|
65
|
-
getSingletonHighlighter: () => Promise<HighlighterGeneric<L, T>>;
|
|
69
|
+
getSingletonHighlighter: (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
|
|
66
70
|
}
|
|
67
|
-
declare function
|
|
71
|
+
declare function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
|
|
72
|
+
declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
|
|
68
73
|
|
|
69
74
|
declare function toArray<T>(x: MaybeArray<T>): T[];
|
|
70
75
|
/**
|
|
@@ -135,6 +140,10 @@ declare function setDefaultWasmLoader(_loader: LoadWasmOptions): void;
|
|
|
135
140
|
/**
|
|
136
141
|
* Get the minimal shiki context for rendering.
|
|
137
142
|
*/
|
|
143
|
+
declare function createShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
|
|
144
|
+
/**
|
|
145
|
+
* @deprecated Use `createShikiInternal` instead.
|
|
146
|
+
*/
|
|
138
147
|
declare function getShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
|
|
139
148
|
|
|
140
149
|
/**
|
|
@@ -401,4 +410,4 @@ declare class ShikiError extends Error {
|
|
|
401
410
|
constructor(message: string);
|
|
402
411
|
}
|
|
403
412
|
|
|
404
|
-
export { BundledHighlighterOptions, CodeToHastOptions, CodeToHastRenderOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type
|
|
413
|
+
export { BundledHighlighterOptions, CodeToHastOptions, CodeToHastRenderOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type CreateHighlighterFactory, IGrammar as Grammar, HighlighterCore, HighlighterCoreOptions, HighlighterGeneric, LanguageInput, MaybeArray, PlainTextLanguage, Position, RequireKeys, ShikiError, ShikiInternal, ShikiTransformer, ShikiTransformerContextCommon, ShikiTransformerContextSource, type ShorthandsBundle, SpecialLanguage, SpecialTheme, ThemeInput, ThemeRegistrationAny, ThemeRegistrationResolved, ThemedToken, ThemedTokenWithVariants, TokenStyles, TokenizeWithThemeOptions, TokensResult, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createHighlighterCore, createPositionConverter, createShikiInternal, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
|
package/dist/index.mjs
CHANGED
|
@@ -5199,9 +5199,9 @@ class Registry extends Registry$1 {
|
|
|
5199
5199
|
_themes;
|
|
5200
5200
|
_langs;
|
|
5201
5201
|
_alias;
|
|
5202
|
-
_resolvedThemes =
|
|
5203
|
-
_resolvedGrammars =
|
|
5204
|
-
_langMap =
|
|
5202
|
+
_resolvedThemes = new Map();
|
|
5203
|
+
_resolvedGrammars = new Map();
|
|
5204
|
+
_langMap = new Map();
|
|
5205
5205
|
_langGraph = new Map();
|
|
5206
5206
|
_textmateThemeCache = new WeakMap();
|
|
5207
5207
|
_loadedThemesCache = null;
|
|
@@ -5217,14 +5217,14 @@ class Registry extends Registry$1 {
|
|
|
5217
5217
|
}
|
|
5218
5218
|
getTheme(theme) {
|
|
5219
5219
|
if (typeof theme === 'string')
|
|
5220
|
-
return this._resolvedThemes
|
|
5220
|
+
return this._resolvedThemes.get(theme);
|
|
5221
5221
|
else
|
|
5222
5222
|
return this.loadTheme(theme);
|
|
5223
5223
|
}
|
|
5224
5224
|
loadTheme(theme) {
|
|
5225
5225
|
const _theme = normalizeTheme(theme);
|
|
5226
5226
|
if (_theme.name) {
|
|
5227
|
-
this._resolvedThemes
|
|
5227
|
+
this._resolvedThemes.set(_theme.name, _theme);
|
|
5228
5228
|
// Reset cache
|
|
5229
5229
|
this._loadedThemesCache = null;
|
|
5230
5230
|
}
|
|
@@ -5232,7 +5232,7 @@ class Registry extends Registry$1 {
|
|
|
5232
5232
|
}
|
|
5233
5233
|
getLoadedThemes() {
|
|
5234
5234
|
if (!this._loadedThemesCache)
|
|
5235
|
-
this._loadedThemesCache =
|
|
5235
|
+
this._loadedThemesCache = [...this._resolvedThemes.keys()];
|
|
5236
5236
|
return this._loadedThemesCache;
|
|
5237
5237
|
}
|
|
5238
5238
|
// Override and re-implement this method to cache the textmate themes as `TextMateTheme.createFromRawTheme`
|
|
@@ -5259,12 +5259,13 @@ class Registry extends Registry$1 {
|
|
|
5259
5259
|
resolved.add(name);
|
|
5260
5260
|
}
|
|
5261
5261
|
}
|
|
5262
|
-
return this._resolvedGrammars
|
|
5262
|
+
return this._resolvedGrammars.get(name);
|
|
5263
5263
|
}
|
|
5264
5264
|
async loadLanguage(lang) {
|
|
5265
5265
|
if (this.getGrammar(lang.name))
|
|
5266
5266
|
return;
|
|
5267
|
-
const embeddedLazilyBy = new Set(
|
|
5267
|
+
const embeddedLazilyBy = new Set([...this._langMap.values()]
|
|
5268
|
+
.filter(i => i.embeddedLangsLazy?.includes(lang.name)));
|
|
5268
5269
|
this._resolver.addLanguage(lang);
|
|
5269
5270
|
const grammarConfig = {
|
|
5270
5271
|
balancedBracketSelectors: lang.balancedBracketSelectors || ['*'],
|
|
@@ -5273,7 +5274,7 @@ class Registry extends Registry$1 {
|
|
|
5273
5274
|
// @ts-expect-error Private members, set this to override the previous grammar (that can be a stub)
|
|
5274
5275
|
this._syncRegistry._rawGrammars.set(lang.scopeName, lang);
|
|
5275
5276
|
const g = await this.loadGrammarWithConfiguration(lang.scopeName, 1, grammarConfig);
|
|
5276
|
-
this._resolvedGrammars
|
|
5277
|
+
this._resolvedGrammars.set(lang.name, g);
|
|
5277
5278
|
if (lang.aliases) {
|
|
5278
5279
|
lang.aliases.forEach((alias) => {
|
|
5279
5280
|
this._alias[alias] = lang.name;
|
|
@@ -5284,14 +5285,14 @@ class Registry extends Registry$1 {
|
|
|
5284
5285
|
// If there is a language that embeds this language lazily, we need to reload it
|
|
5285
5286
|
if (embeddedLazilyBy.size) {
|
|
5286
5287
|
for (const e of embeddedLazilyBy) {
|
|
5287
|
-
|
|
5288
|
+
this._resolvedGrammars.delete(e.name);
|
|
5288
5289
|
// Reset cache
|
|
5289
5290
|
this._loadedLanguagesCache = null;
|
|
5290
5291
|
// @ts-expect-error clear cache
|
|
5291
5292
|
this._syncRegistry?._injectionGrammars?.delete(e.scopeName);
|
|
5292
5293
|
// @ts-expect-error clear cache
|
|
5293
5294
|
this._syncRegistry?._grammars?.delete(e.scopeName);
|
|
5294
|
-
await this.loadLanguage(this._langMap
|
|
5295
|
+
await this.loadLanguage(this._langMap.get(e.name));
|
|
5295
5296
|
}
|
|
5296
5297
|
}
|
|
5297
5298
|
}
|
|
@@ -5299,6 +5300,14 @@ class Registry extends Registry$1 {
|
|
|
5299
5300
|
this._themes.map(t => this.loadTheme(t));
|
|
5300
5301
|
await this.loadLanguages(this._langs);
|
|
5301
5302
|
}
|
|
5303
|
+
dispose() {
|
|
5304
|
+
super.dispose();
|
|
5305
|
+
this._resolvedThemes.clear();
|
|
5306
|
+
this._resolvedGrammars.clear();
|
|
5307
|
+
this._langMap.clear();
|
|
5308
|
+
this._langGraph.clear();
|
|
5309
|
+
this._loadedThemesCache = null;
|
|
5310
|
+
}
|
|
5302
5311
|
async loadLanguages(langs) {
|
|
5303
5312
|
for (const lang of langs)
|
|
5304
5313
|
this.resolveEmbeddedLanguages(lang);
|
|
@@ -5316,16 +5325,19 @@ class Registry extends Registry$1 {
|
|
|
5316
5325
|
await this.loadLanguage(lang);
|
|
5317
5326
|
}
|
|
5318
5327
|
getLoadedLanguages() {
|
|
5319
|
-
if (!this._loadedLanguagesCache)
|
|
5320
|
-
this._loadedLanguagesCache =
|
|
5328
|
+
if (!this._loadedLanguagesCache) {
|
|
5329
|
+
this._loadedLanguagesCache = [
|
|
5330
|
+
...new Set([...this._resolvedGrammars.keys(), ...Object.keys(this._alias)]),
|
|
5331
|
+
];
|
|
5332
|
+
}
|
|
5321
5333
|
return this._loadedLanguagesCache;
|
|
5322
5334
|
}
|
|
5323
5335
|
resolveEmbeddedLanguages(lang) {
|
|
5324
|
-
this._langMap
|
|
5336
|
+
this._langMap.set(lang.name, lang);
|
|
5325
5337
|
this._langGraph.set(lang.name, lang);
|
|
5326
5338
|
if (lang.embeddedLangs) {
|
|
5327
5339
|
for (const embeddedLang of lang.embeddedLangs)
|
|
5328
|
-
this._langGraph.set(embeddedLang, this._langMap
|
|
5340
|
+
this._langGraph.set(embeddedLang, this._langMap.get(embeddedLang));
|
|
5329
5341
|
}
|
|
5330
5342
|
}
|
|
5331
5343
|
}
|
|
@@ -5387,10 +5399,11 @@ let instancesCount = 0;
|
|
|
5387
5399
|
/**
|
|
5388
5400
|
* Get the minimal shiki context for rendering.
|
|
5389
5401
|
*/
|
|
5390
|
-
async function
|
|
5402
|
+
async function createShikiInternal(options = {}) {
|
|
5391
5403
|
instancesCount += 1;
|
|
5392
5404
|
if (options.warnings !== false && instancesCount >= 10 && instancesCount % 10 === 0)
|
|
5393
|
-
console.warn(`[Shiki] ${instancesCount} instances have been created. Shiki is supposed to be used as a singleton, consider refactoring your code to cache your highlighter instance.`);
|
|
5405
|
+
console.warn(`[Shiki] ${instancesCount} instances have been created. Shiki is supposed to be used as a singleton, consider refactoring your code to cache your highlighter instance; Or call \`highlighter.dispose()\` to release unused instances.`);
|
|
5406
|
+
let isDisposed = false;
|
|
5394
5407
|
async function normalizeGetter(p) {
|
|
5395
5408
|
return Promise.resolve(typeof p === 'function' ? p() : p).then(r => r.default || r);
|
|
5396
5409
|
}
|
|
@@ -5417,6 +5430,7 @@ async function getShikiInternal(options = {}) {
|
|
|
5417
5430
|
await _registry.init();
|
|
5418
5431
|
let _lastTheme;
|
|
5419
5432
|
function getLanguage(name) {
|
|
5433
|
+
ensureNotDisposed();
|
|
5420
5434
|
const _lang = _registry.getGrammar(typeof name === 'string' ? name : name.name);
|
|
5421
5435
|
if (!_lang)
|
|
5422
5436
|
throw new ShikiError(`Language \`${name}\` not found, you may need to load it first`);
|
|
@@ -5425,12 +5439,14 @@ async function getShikiInternal(options = {}) {
|
|
|
5425
5439
|
function getTheme(name) {
|
|
5426
5440
|
if (name === 'none')
|
|
5427
5441
|
return { bg: '', fg: '', name: 'none', settings: [], type: 'dark' };
|
|
5442
|
+
ensureNotDisposed();
|
|
5428
5443
|
const _theme = _registry.getTheme(name);
|
|
5429
5444
|
if (!_theme)
|
|
5430
5445
|
throw new ShikiError(`Theme \`${name}\` not found, you may need to load it first`);
|
|
5431
5446
|
return _theme;
|
|
5432
5447
|
}
|
|
5433
5448
|
function setTheme(name) {
|
|
5449
|
+
ensureNotDisposed();
|
|
5434
5450
|
const theme = getTheme(name);
|
|
5435
5451
|
if (_lastTheme !== name) {
|
|
5436
5452
|
_registry.setTheme(theme);
|
|
@@ -5443,19 +5459,34 @@ async function getShikiInternal(options = {}) {
|
|
|
5443
5459
|
};
|
|
5444
5460
|
}
|
|
5445
5461
|
function getLoadedThemes() {
|
|
5462
|
+
ensureNotDisposed();
|
|
5446
5463
|
return _registry.getLoadedThemes();
|
|
5447
5464
|
}
|
|
5448
5465
|
function getLoadedLanguages() {
|
|
5466
|
+
ensureNotDisposed();
|
|
5449
5467
|
return _registry.getLoadedLanguages();
|
|
5450
5468
|
}
|
|
5451
5469
|
async function loadLanguage(...langs) {
|
|
5470
|
+
ensureNotDisposed();
|
|
5452
5471
|
await _registry.loadLanguages(await resolveLangs(langs));
|
|
5453
5472
|
}
|
|
5454
5473
|
async function loadTheme(...themes) {
|
|
5474
|
+
ensureNotDisposed();
|
|
5455
5475
|
await Promise.all(themes.map(async (theme) => isSpecialTheme(theme)
|
|
5456
5476
|
? null
|
|
5457
5477
|
: _registry.loadTheme(await normalizeGetter(theme))));
|
|
5458
5478
|
}
|
|
5479
|
+
function ensureNotDisposed() {
|
|
5480
|
+
if (isDisposed)
|
|
5481
|
+
throw new ShikiError('Shiki instance has been disposed');
|
|
5482
|
+
}
|
|
5483
|
+
function dispose() {
|
|
5484
|
+
if (isDisposed)
|
|
5485
|
+
return;
|
|
5486
|
+
isDisposed = true;
|
|
5487
|
+
_registry.dispose();
|
|
5488
|
+
instancesCount -= 1;
|
|
5489
|
+
}
|
|
5459
5490
|
return {
|
|
5460
5491
|
setTheme,
|
|
5461
5492
|
getTheme,
|
|
@@ -5464,8 +5495,17 @@ async function getShikiInternal(options = {}) {
|
|
|
5464
5495
|
getLoadedLanguages,
|
|
5465
5496
|
loadLanguage,
|
|
5466
5497
|
loadTheme,
|
|
5498
|
+
dispose,
|
|
5499
|
+
[Symbol.dispose]: dispose,
|
|
5467
5500
|
};
|
|
5468
5501
|
}
|
|
5502
|
+
/**
|
|
5503
|
+
* @deprecated Use `createShikiInternal` instead.
|
|
5504
|
+
*/
|
|
5505
|
+
function getShikiInternal(options = {}) {
|
|
5506
|
+
// TODO: next: console.warn('`getShikiInternal` is deprecated. Use `createShikiInternal` instead.')
|
|
5507
|
+
return createShikiInternal(options);
|
|
5508
|
+
}
|
|
5469
5509
|
|
|
5470
5510
|
/**
|
|
5471
5511
|
* Create a Shiki core highlighter instance, with no languages or themes bundled.
|
|
@@ -5473,8 +5513,8 @@ async function getShikiInternal(options = {}) {
|
|
|
5473
5513
|
*
|
|
5474
5514
|
* @see http://shiki.style/guide/install#fine-grained-bundle
|
|
5475
5515
|
*/
|
|
5476
|
-
async function
|
|
5477
|
-
const internal = await
|
|
5516
|
+
async function createHighlighterCore(options = {}) {
|
|
5517
|
+
const internal = await createShikiInternal(options);
|
|
5478
5518
|
return {
|
|
5479
5519
|
codeToTokensBase: (code, options) => codeToTokensBase(internal, code, options),
|
|
5480
5520
|
codeToTokensWithThemes: (code, options) => codeToTokensWithThemes(internal, code, options),
|
|
@@ -5485,16 +5525,47 @@ async function getHighlighterCore(options = {}) {
|
|
|
5485
5525
|
getInternalContext: () => internal,
|
|
5486
5526
|
};
|
|
5487
5527
|
}
|
|
5528
|
+
function makeSingletonHighlighterCore(createHighlighter) {
|
|
5529
|
+
let _shiki;
|
|
5530
|
+
async function getSingletonHighlighterCore(options = {}) {
|
|
5531
|
+
if (!_shiki) {
|
|
5532
|
+
_shiki = createHighlighter({
|
|
5533
|
+
...options,
|
|
5534
|
+
themes: options.themes || [],
|
|
5535
|
+
langs: options.langs || [],
|
|
5536
|
+
});
|
|
5537
|
+
return _shiki;
|
|
5538
|
+
}
|
|
5539
|
+
else {
|
|
5540
|
+
const s = await _shiki;
|
|
5541
|
+
await Promise.all([
|
|
5542
|
+
s.loadTheme(...(options.themes || [])),
|
|
5543
|
+
s.loadLanguage(...(options.langs || [])),
|
|
5544
|
+
]);
|
|
5545
|
+
return s;
|
|
5546
|
+
}
|
|
5547
|
+
}
|
|
5548
|
+
return getSingletonHighlighterCore;
|
|
5549
|
+
}
|
|
5550
|
+
const getSingletonHighlighterCore = /* @__PURE__ */ makeSingletonHighlighterCore(createHighlighterCore);
|
|
5551
|
+
/**
|
|
5552
|
+
* @deprecated Use `createHighlighterCore` or `getSingletonHighlighterCore` instead.
|
|
5553
|
+
*/
|
|
5554
|
+
/* v8 ignore next 5 */
|
|
5555
|
+
function getHighlighterCore(options = {}) {
|
|
5556
|
+
// TODO: next: console.warn('`getHighlighterCore` is deprecated. Use `createHighlighterCore` or `getSingletonHighlighterCore` instead.')
|
|
5557
|
+
return createHighlighterCore(options);
|
|
5558
|
+
}
|
|
5488
5559
|
|
|
5489
5560
|
/**
|
|
5490
|
-
* Create a `
|
|
5561
|
+
* Create a `createHighlighter` function with bundled themes and languages.
|
|
5491
5562
|
*
|
|
5492
5563
|
* @param bundledLanguages
|
|
5493
5564
|
* @param bundledThemes
|
|
5494
5565
|
* @param loadWasm
|
|
5495
5566
|
*/
|
|
5496
5567
|
function createdBundledHighlighter(bundledLanguages, bundledThemes, loadWasm) {
|
|
5497
|
-
async function
|
|
5568
|
+
async function createHighlighter(options) {
|
|
5498
5569
|
function resolveLang(lang) {
|
|
5499
5570
|
if (typeof lang === 'string') {
|
|
5500
5571
|
if (isSpecialLang(lang))
|
|
@@ -5520,7 +5591,7 @@ function createdBundledHighlighter(bundledLanguages, bundledThemes, loadWasm) {
|
|
|
5520
5591
|
const _themes = (options.themes ?? []).map(i => resolveTheme(i));
|
|
5521
5592
|
const langs = (options.langs ?? [])
|
|
5522
5593
|
.map(i => resolveLang(i));
|
|
5523
|
-
const core = await
|
|
5594
|
+
const core = await createHighlighterCore({
|
|
5524
5595
|
...options,
|
|
5525
5596
|
themes: _themes,
|
|
5526
5597
|
langs,
|
|
@@ -5536,62 +5607,72 @@ function createdBundledHighlighter(bundledLanguages, bundledThemes, loadWasm) {
|
|
|
5536
5607
|
},
|
|
5537
5608
|
};
|
|
5538
5609
|
}
|
|
5539
|
-
return
|
|
5610
|
+
return createHighlighter;
|
|
5540
5611
|
}
|
|
5541
|
-
function
|
|
5612
|
+
function makeSingletonHighlighter(createHighlighter) {
|
|
5542
5613
|
let _shiki;
|
|
5543
|
-
async function
|
|
5614
|
+
async function getSingletonHighlighter(options = {}) {
|
|
5544
5615
|
if (!_shiki) {
|
|
5545
|
-
_shiki =
|
|
5546
|
-
|
|
5547
|
-
|
|
5616
|
+
_shiki = createHighlighter({
|
|
5617
|
+
...options,
|
|
5618
|
+
themes: options.themes || [],
|
|
5619
|
+
langs: options.langs || [],
|
|
5548
5620
|
});
|
|
5549
5621
|
return _shiki;
|
|
5550
5622
|
}
|
|
5551
5623
|
else {
|
|
5552
5624
|
const s = await _shiki;
|
|
5553
5625
|
await Promise.all([
|
|
5554
|
-
s.loadTheme(...
|
|
5555
|
-
s.loadLanguage(...
|
|
5626
|
+
s.loadTheme(...(options.themes || [])),
|
|
5627
|
+
s.loadLanguage(...(options.langs || [])),
|
|
5556
5628
|
]);
|
|
5557
5629
|
return s;
|
|
5558
5630
|
}
|
|
5559
5631
|
}
|
|
5632
|
+
return getSingletonHighlighter;
|
|
5633
|
+
}
|
|
5634
|
+
function createSingletonShorthands(createHighlighter) {
|
|
5635
|
+
const getSingletonHighlighter = makeSingletonHighlighter(createHighlighter);
|
|
5560
5636
|
return {
|
|
5561
|
-
getSingletonHighlighter
|
|
5637
|
+
getSingletonHighlighter(options) {
|
|
5638
|
+
return getSingletonHighlighter(options);
|
|
5639
|
+
},
|
|
5562
5640
|
async codeToHtml(code, options) {
|
|
5563
|
-
const shiki = await
|
|
5564
|
-
|
|
5565
|
-
|
|
5641
|
+
const shiki = await getSingletonHighlighter({
|
|
5642
|
+
langs: [options.lang],
|
|
5643
|
+
themes: ('theme' in options ? [options.theme] : Object.values(options.themes)),
|
|
5566
5644
|
});
|
|
5567
5645
|
return shiki.codeToHtml(code, options);
|
|
5568
5646
|
},
|
|
5569
5647
|
async codeToHast(code, options) {
|
|
5570
|
-
const shiki = await
|
|
5571
|
-
|
|
5572
|
-
|
|
5648
|
+
const shiki = await getSingletonHighlighter({
|
|
5649
|
+
langs: [options.lang],
|
|
5650
|
+
themes: ('theme' in options ? [options.theme] : Object.values(options.themes)),
|
|
5573
5651
|
});
|
|
5574
5652
|
return shiki.codeToHast(code, options);
|
|
5575
5653
|
},
|
|
5576
5654
|
async codeToTokens(code, options) {
|
|
5577
|
-
const shiki = await
|
|
5578
|
-
|
|
5579
|
-
|
|
5655
|
+
const shiki = await getSingletonHighlighter({
|
|
5656
|
+
langs: [options.lang],
|
|
5657
|
+
themes: ('theme' in options ? [options.theme] : Object.values(options.themes)),
|
|
5580
5658
|
});
|
|
5581
5659
|
return shiki.codeToTokens(code, options);
|
|
5582
5660
|
},
|
|
5583
5661
|
async codeToTokensBase(code, options) {
|
|
5584
|
-
const shiki = await
|
|
5662
|
+
const shiki = await getSingletonHighlighter({
|
|
5663
|
+
langs: [options.lang],
|
|
5664
|
+
themes: [options.theme],
|
|
5665
|
+
});
|
|
5585
5666
|
return shiki.codeToTokensBase(code, options);
|
|
5586
5667
|
},
|
|
5587
5668
|
async codeToTokensWithThemes(code, options) {
|
|
5588
|
-
const shiki = await
|
|
5589
|
-
|
|
5590
|
-
|
|
5669
|
+
const shiki = await getSingletonHighlighter({
|
|
5670
|
+
langs: [options.lang],
|
|
5671
|
+
themes: Object.values(options.themes).filter(Boolean),
|
|
5591
5672
|
});
|
|
5592
5673
|
return shiki.codeToTokensWithThemes(code, options);
|
|
5593
5674
|
},
|
|
5594
5675
|
};
|
|
5595
5676
|
}
|
|
5596
5677
|
|
|
5597
|
-
export { FontStyle, ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createPositionConverter, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
|
|
5678
|
+
export { FontStyle, ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createHighlighterCore, createPositionConverter, createShikiInternal, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
|
package/dist/types.d.mts
CHANGED
|
@@ -42,6 +42,14 @@ interface ShikiInternal<BundledLangKeys extends string = never, BundledThemeKeys
|
|
|
42
42
|
* Special-handled themes like `none` are not included.
|
|
43
43
|
*/
|
|
44
44
|
getLoadedThemes: () => string[];
|
|
45
|
+
/**
|
|
46
|
+
* Dispose the internal registry and release resources
|
|
47
|
+
*/
|
|
48
|
+
dispose: () => void;
|
|
49
|
+
/**
|
|
50
|
+
* Dispose the internal registry and release resources
|
|
51
|
+
*/
|
|
52
|
+
[Symbol.dispose]: () => void;
|
|
45
53
|
}
|
|
46
54
|
/**
|
|
47
55
|
* Generic instance interface of Shiki
|