@shikijs/core 1.15.2 → 1.16.1

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.
@@ -1,4 +1,32 @@
1
+ import { OnigScanner, OnigString } from '@shikijs/vscode-textmate';
2
+
1
3
  type Awaitable<T> = T | Promise<T>;
4
+ type MaybeGetter<T> = Awaitable<MaybeModule<T>> | (() => Awaitable<MaybeModule<T>>);
5
+ type MaybeModule<T> = T | {
6
+ default: T;
7
+ };
8
+ type MaybeArray<T> = T | T[];
9
+ type RequireKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
10
+ interface Nothing {
11
+ }
12
+ /**
13
+ * type StringLiteralUnion<'foo'> = 'foo' | string
14
+ * This has auto completion whereas `'foo' | string` doesn't
15
+ * Adapted from https://github.com/microsoft/TypeScript/issues/29729
16
+ */
17
+ type StringLiteralUnion<T extends U, U = string> = T | (U & Nothing);
18
+
19
+ interface PatternScanner extends OnigScanner {
20
+ }
21
+ interface RegexEngineString extends OnigString {
22
+ }
23
+ /**
24
+ * Engine for RegExp matching and scanning.
25
+ */
26
+ interface RegexEngine {
27
+ createScanner: (patterns: string[]) => PatternScanner;
28
+ createString: (s: string) => RegexEngineString;
29
+ }
2
30
  interface WebAssemblyInstantiator {
3
31
  (importObject: Record<string, Record<string, WebAssembly.ImportValue>> | undefined): Promise<WebAssemblyInstance>;
4
32
  }
@@ -23,4 +51,4 @@ interface JavaScriptRegexEngineOptions {
23
51
  cache?: Map<string, RegExp | Error>;
24
52
  }
25
53
 
26
- export type { JavaScriptRegexEngineOptions as J, LoadWasmOptions as L, WebAssemblyInstantiator as W, WebAssemblyInstance as a };
54
+ export type { Awaitable as A, JavaScriptRegexEngineOptions as J, LoadWasmOptions as L, MaybeArray as M, OnigurumaLoadOptions as O, PatternScanner as P, RequireKeys as R, StringLiteralUnion as S, WebAssemblyInstantiator as W, RegexEngine as a, MaybeGetter as b, MaybeModule as c, RegexEngineString as d, WebAssemblyInstance as e, LoadWasmOptionsPlain as f };
package/dist/index.d.mts CHANGED
@@ -1,18 +1,27 @@
1
- import { HighlighterCore, HighlighterGeneric, ShikiInternal } from './types.mjs';
2
- export { Grammar } from './types.mjs';
3
- import { H as HighlighterCoreOptions, B as BundledHighlighterOptions, L as LanguageInput, T as ThemeInput, C as CodeToHastOptions, a as CodeToTokensOptions, b as TokensResult, R as RequireKeys, c as CodeToTokensBaseOptions, d as ThemedToken, e as CodeToTokensWithThemesOptions, f as ThemedTokenWithVariants, G as GrammarState, M as MaybeArray, P as PlainTextLanguage, S as SpecialLanguage, g as SpecialTheme, h as ThemeRegistrationAny, i as TokenizeWithThemeOptions, j as TokenStyles, k as Position, l as RegexEngine, I as IGrammar, m as ThemeRegistrationResolved, n as ShikiTransformerContextCommon, o as CodeToHastRenderOptions, p as ShikiTransformerContextSource, q as ShikiTransformer } from './chunk-tokens.mjs';
4
- export { A as AnsiLanguage, $ as Awaitable, t as BundledLanguageInfo, Q as BundledThemeInfo, y as CodeOptionsMeta, v as CodeOptionsMultipleThemes, u as CodeOptionsSingleTheme, w as CodeOptionsThemes, x as CodeToHastOptionsCommon, z as CodeToHastRenderOptionsCommon, a4 as DecorationItem, a3 as DecorationOptions, a6 as DecorationTransformType, D as DynamicImportLanguageRegistration, O as DynamicImportThemeRegistration, X as FontStyle, s as LanguageRegistration, a0 as MaybeGetter, a1 as MaybeModule, a7 as Offset, a8 as OffsetOrPosition, E as RawGrammar, F as RawTheme, J as RawThemeSetting, r as ResolveBundleKey, a5 as ResolvedDecorationItem, a9 as ResolvedPosition, _ as ShikiTransformerContext, Z as ShikiTransformerContextMeta, a2 as StringLiteralUnion, N as ThemeRegistration, K as ThemeRegistrationRaw, V as ThemedTokenExplanation, U as ThemedTokenScopeExplanation, W as TokenBase, Y as TransformerOptions } from './chunk-tokens.mjs';
5
- import { Root, Element, RootContent, Nodes } from 'hast';
6
- import { L as LoadWasmOptions, J as JavaScriptRegexEngineOptions } from './chunk-engines.mjs';
7
- export { a as WebAssemblyInstance, W as WebAssemblyInstantiator } from './chunk-engines.mjs';
1
+ import { HighlighterCoreOptions, HighlighterCore, BundledHighlighterOptions, HighlighterGeneric, CreatedBundledHighlighterOptions, LanguageInput, ThemeInput, CodeToHastOptions, CodeToTokensOptions, TokensResult, CodeToTokensBaseOptions, ThemedToken, CodeToTokensWithThemesOptions, ThemedTokenWithVariants, GrammarState, ShikiInternal, ThemeRegistrationAny, ThemeRegistrationResolved, TokenizeWithThemeOptions, ShikiTransformerContextCommon, CodeToHastRenderOptions, ShikiTransformerContextSource, PlainTextLanguage, SpecialLanguage, SpecialTheme, TokenStyles, Position, ShikiTransformer } from './types.mjs';
2
+ export { AnsiLanguage, BundledLanguageInfo, BundledThemeInfo, CodeOptionsMeta, CodeOptionsMultipleThemes, CodeOptionsSingleTheme, CodeOptionsThemes, CodeToHastOptionsCommon, CodeToHastRenderOptionsCommon, DecorationItem, DecorationOptions, DecorationTransformType, DynamicImportLanguageRegistration, DynamicImportThemeRegistration, FontStyle, Grammar, LanguageRegistration, Offset, OffsetOrPosition, ResolveBundleKey, ResolvedDecorationItem, ResolvedPosition, ShikiTransformerContext, ShikiTransformerContextMeta, ThemeRegistration, ThemeRegistrationRaw, ThemedTokenExplanation, ThemedTokenScopeExplanation, TokenBase, TransformerOptions } from './types.mjs';
3
+ import { R as RequireKeys, L as LoadWasmOptions, a as RegexEngine, J as JavaScriptRegexEngineOptions, M as MaybeArray, b as MaybeGetter } from './chunk-engines.mjs';
4
+ export { A as Awaitable, f as LoadWasmOptionsPlain, c as MaybeModule, O as OnigurumaLoadOptions, P as PatternScanner, d as RegexEngineString, S as StringLiteralUnion, e as WebAssemblyInstance, W as WebAssemblyInstantiator } from './chunk-engines.mjs';
5
+ import { Root, RootContent, Nodes, Element } from 'hast';
6
+ import { IGrammar } from '@shikijs/vscode-textmate';
7
+ export { IRawGrammar as RawGrammar, IRawTheme as RawTheme, IRawThemeSetting as RawThemeSetting, EncodedTokenMetadata as StackElementMetadata } from '@shikijs/vscode-textmate';
8
8
 
9
9
  /**
10
10
  * Create a Shiki core highlighter instance, with no languages or themes bundled.
11
11
  * Wasm and each language and theme must be loaded manually.
12
12
  *
13
- * @see http://shiki.style/guide/install#fine-grained-bundle
13
+ * @see http://shiki.style/guide/bundles#fine-grained-bundle
14
14
  */
15
15
  declare function createHighlighterCore(options?: HighlighterCoreOptions): Promise<HighlighterCore>;
16
+ /**
17
+ * Create a Shiki core highlighter instance, with no languages or themes bundled.
18
+ * Wasm and each language and theme must be loaded manually.
19
+ *
20
+ * Synchronous version of `createHighlighterCore`, which requires to provide the engine and all themes and languages upfront.
21
+ *
22
+ * @see http://shiki.style/guide/bundles#fine-grained-bundle
23
+ */
24
+ declare function createHighlighterCoreSync(options?: HighlighterCoreOptions<true>): HighlighterCore;
16
25
  declare function makeSingletonHighlighterCore(createHighlighter: typeof createHighlighterCore): (options?: Partial<HighlighterCoreOptions>) => Promise<HighlighterCore>;
17
26
  declare const getSingletonHighlighterCore: (options?: Partial<HighlighterCoreOptions>) => Promise<HighlighterCore>;
18
27
  /**
@@ -21,12 +30,31 @@ declare const getSingletonHighlighterCore: (options?: Partial<HighlighterCoreOpt
21
30
  declare function getHighlighterCore(options?: HighlighterCoreOptions): Promise<HighlighterCore>;
22
31
 
23
32
  type CreateHighlighterFactory<L extends string, T extends string> = (options: BundledHighlighterOptions<L, T>) => Promise<HighlighterGeneric<L, T>>;
33
+ /**
34
+ * Create a `createHighlighter` function with bundled themes, languages, and engine.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const createHighlighter = createdBundledHighlighter({
39
+ * langs: {
40
+ * typescript: () => import('shiki/langs/typescript.mjs'),
41
+ * // ...
42
+ * },
43
+ * themes: {
44
+ * nord: () => import('shiki/themes/nord.mjs'),
45
+ * // ...
46
+ * },
47
+ * engine: () => createWasmOnigEngine(), // or createJavaScriptRegexEngine()
48
+ * })
49
+ * ```
50
+ *
51
+ * @param options
52
+ */
53
+ declare function createdBundledHighlighter<BundledLangs extends string, BundledThemes extends string>(options: CreatedBundledHighlighterOptions<BundledLangs, BundledThemes>): CreateHighlighterFactory<BundledLangs, BundledThemes>;
24
54
  /**
25
55
  * Create a `createHighlighter` function with bundled themes and languages.
26
56
  *
27
- * @param bundledLanguages
28
- * @param bundledThemes
29
- * @param loadWasm
57
+ * @deprecated Use `createdBundledHighlighter({ langs, themes, engine })` signature instead.
30
58
  */
31
59
  declare function createdBundledHighlighter<BundledLangs extends string, BundledThemes extends string>(bundledLanguages: Record<BundledLangs, LanguageInput>, bundledThemes: Record<BundledThemes, ThemeInput>, loadWasm: HighlighterCoreOptions['loadWasm']): CreateHighlighterFactory<BundledLangs, BundledThemes>;
32
60
  interface ShorthandsBundle<L extends string, T extends string> {
@@ -78,68 +106,26 @@ interface ShorthandsBundle<L extends string, T extends string> {
78
106
  declare function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
79
107
  declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
80
108
 
81
- declare function toArray<T>(x: MaybeArray<T>): T[];
82
- /**
83
- * Split a string into lines, each line preserves the line ending.
84
- */
85
- declare function splitLines(code: string, preserveEnding?: boolean): [string, number][];
86
- /**
87
- * Check if the language is plaintext that is ignored by Shiki.
88
- *
89
- * Hard-coded plain text languages: `plaintext`, `txt`, `text`, `plain`.
90
- */
91
- declare function isPlainLang(lang: string | null | undefined): lang is PlainTextLanguage;
92
- /**
93
- * Check if the language is specially handled or bypassed by Shiki.
94
- *
95
- * Hard-coded languages: `ansi` and plaintexts like `plaintext`, `txt`, `text`, `plain`.
96
- */
97
- declare function isSpecialLang(lang: any): lang is SpecialLanguage;
98
109
  /**
99
- * Check if the theme is specially handled or bypassed by Shiki.
100
- *
101
- * Hard-coded themes: `none`.
102
- */
103
- declare function isNoneTheme(theme: string | ThemeInput | null | undefined): theme is 'none';
104
- /**
105
- * Check if the theme is specially handled or bypassed by Shiki.
106
- *
107
- * Hard-coded themes: `none`.
108
- */
109
- declare function isSpecialTheme(theme: string | ThemeInput | null | undefined): theme is SpecialTheme;
110
- /**
111
- * Utility to append class to a hast node
110
+ * Get the minimal shiki context for rendering.
112
111
  *
113
- * If the `property.class` is a string, it will be splitted by space and converted to an array.
112
+ * Synchronous version of `createShikiInternal`, which requires to provide the engine and all themes and languages upfront.
114
113
  */
115
- declare function addClassToHast(node: Element, className: string | string[]): Element;
114
+ declare function createShikiInternalSync(options: HighlighterCoreOptions<true>): ShikiInternal;
115
+
116
116
  /**
117
- * Split a token into multiple tokens by given offsets.
118
- *
119
- * The offsets are relative to the token, and should be sorted.
117
+ * Set the default wasm loader for `loadWasm`.
118
+ * @internal
120
119
  */
121
- declare function splitToken<T extends Pick<ThemedToken, 'content' | 'offset'>>(token: T, offsets: number[]): T[];
120
+ declare function setDefaultWasmLoader(_loader: LoadWasmOptions): void;
122
121
  /**
123
- * Split 2D tokens array by given breakpoints.
122
+ * Get the minimal shiki context for rendering.
124
123
  */
125
- declare function splitTokens<T extends Pick<ThemedToken, 'content' | 'offset'>>(tokens: T[][], breakpoints: number[] | Set<number>): T[][];
126
- declare function resolveColorReplacements(theme: ThemeRegistrationAny | string, options?: TokenizeWithThemeOptions): {
127
- [x: string]: string;
128
- };
129
- declare function applyColorReplacements(color: string, replacements?: Record<string, string>): string;
130
- declare function applyColorReplacements(color?: string | undefined, replacements?: Record<string, string>): string | undefined;
131
- declare function getTokenStyleObject(token: TokenStyles): Record<string, string>;
132
- declare function stringifyTokenStyle(token: Record<string, string>): string;
124
+ declare function createShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
133
125
  /**
134
- * Creates a converter between index and position in a code block.
135
- *
136
- * Overflow/underflow are unchecked.
126
+ * @deprecated Use `createShikiInternal` instead.
137
127
  */
138
- declare function createPositionConverter(code: string): {
139
- lines: string[];
140
- indexToPos: (index: number) => Position;
141
- posToIndex: (line: number, character: number) => number;
142
- };
128
+ declare function getShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
143
129
 
144
130
  declare function loadWasm(options: LoadWasmOptions): Promise<void>;
145
131
 
@@ -157,18 +143,9 @@ declare function createWasmOnigEngine(options?: LoadWasmOptions | null): Promise
157
143
  declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
158
144
 
159
145
  /**
160
- * Set the default wasm loader for `loadWasm`.
161
- * @internal
162
- */
163
- declare function setDefaultWasmLoader(_loader: LoadWasmOptions): void;
164
- /**
165
- * Get the minimal shiki context for rendering.
166
- */
167
- declare function createShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
168
- /**
169
- * @deprecated Use `createShikiInternal` instead.
146
+ * Normalize a textmate theme to shiki theme
170
147
  */
171
- declare function getShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
148
+ declare function normalizeTheme(rawTheme: ThemeRegistrationAny): ThemeRegistrationResolved;
172
149
 
173
150
  /**
174
151
  * Code to tokens, with a simple theme.
@@ -417,10 +394,72 @@ declare function codeToHtml(internal: ShikiInternal, code: string, options: Code
417
394
  */
418
395
  declare function codeToTokensWithThemes(internal: ShikiInternal, code: string, options: CodeToTokensWithThemesOptions): ThemedTokenWithVariants[][];
419
396
 
397
+ declare function toArray<T>(x: MaybeArray<T>): T[];
420
398
  /**
421
- * Normalize a textmate theme to shiki theme
399
+ * Split a string into lines, each line preserves the line ending.
422
400
  */
423
- declare function normalizeTheme(rawTheme: ThemeRegistrationAny): ThemeRegistrationResolved;
401
+ declare function splitLines(code: string, preserveEnding?: boolean): [string, number][];
402
+ /**
403
+ * Check if the language is plaintext that is ignored by Shiki.
404
+ *
405
+ * Hard-coded plain text languages: `plaintext`, `txt`, `text`, `plain`.
406
+ */
407
+ declare function isPlainLang(lang: string | null | undefined): lang is PlainTextLanguage;
408
+ /**
409
+ * Check if the language is specially handled or bypassed by Shiki.
410
+ *
411
+ * Hard-coded languages: `ansi` and plaintexts like `plaintext`, `txt`, `text`, `plain`.
412
+ */
413
+ declare function isSpecialLang(lang: any): lang is SpecialLanguage;
414
+ /**
415
+ * Check if the theme is specially handled or bypassed by Shiki.
416
+ *
417
+ * Hard-coded themes: `none`.
418
+ */
419
+ declare function isNoneTheme(theme: string | ThemeInput | null | undefined): theme is 'none';
420
+ /**
421
+ * Check if the theme is specially handled or bypassed by Shiki.
422
+ *
423
+ * Hard-coded themes: `none`.
424
+ */
425
+ declare function isSpecialTheme(theme: string | ThemeInput | null | undefined): theme is SpecialTheme;
426
+ /**
427
+ * Utility to append class to a hast node
428
+ *
429
+ * If the `property.class` is a string, it will be splitted by space and converted to an array.
430
+ */
431
+ declare function addClassToHast(node: Element, className: string | string[]): Element;
432
+ /**
433
+ * Split a token into multiple tokens by given offsets.
434
+ *
435
+ * The offsets are relative to the token, and should be sorted.
436
+ */
437
+ declare function splitToken<T extends Pick<ThemedToken, 'content' | 'offset'>>(token: T, offsets: number[]): T[];
438
+ /**
439
+ * Split 2D tokens array by given breakpoints.
440
+ */
441
+ declare function splitTokens<T extends Pick<ThemedToken, 'content' | 'offset'>>(tokens: T[][], breakpoints: number[] | Set<number>): T[][];
442
+ /**
443
+ * Normalize a getter to a promise.
444
+ */
445
+ declare function normalizeGetter<T>(p: MaybeGetter<T>): Promise<T>;
446
+ declare function resolveColorReplacements(theme: ThemeRegistrationAny | string, options?: TokenizeWithThemeOptions): {
447
+ [x: string]: string;
448
+ };
449
+ declare function applyColorReplacements(color: string, replacements?: Record<string, string>): string;
450
+ declare function applyColorReplacements(color?: string | undefined, replacements?: Record<string, string>): string | undefined;
451
+ declare function getTokenStyleObject(token: TokenStyles): Record<string, string>;
452
+ declare function stringifyTokenStyle(token: Record<string, string>): string;
453
+ /**
454
+ * Creates a converter between index and position in a code block.
455
+ *
456
+ * Overflow/underflow are unchecked.
457
+ */
458
+ declare function createPositionConverter(code: string): {
459
+ lines: string[];
460
+ indexToPos: (index: number) => Position;
461
+ posToIndex: (line: number, character: number) => number;
462
+ };
424
463
 
425
464
  /**
426
465
  * A built-in transformer to add decorations to the highlighted code.
@@ -431,4 +470,4 @@ declare class ShikiError extends Error {
431
470
  constructor(message: string);
432
471
  }
433
472
 
434
- export { BundledHighlighterOptions, CodeToHastOptions, CodeToHastRenderOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type CreateHighlighterFactory, GrammarState, HighlighterCore, HighlighterCoreOptions, HighlighterGeneric, LanguageInput, LoadWasmOptions, 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, createJavaScriptRegexEngine, createPositionConverter, createShikiInternal, createSingletonShorthands, createWasmOnigEngine, 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 };
473
+ export { BundledHighlighterOptions, CodeToHastOptions, CodeToHastRenderOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type CreateHighlighterFactory, CreatedBundledHighlighterOptions, GrammarState, HighlighterCore, HighlighterCoreOptions, HighlighterGeneric, JavaScriptRegexEngineOptions, LanguageInput, LoadWasmOptions, MaybeArray, MaybeGetter, PlainTextLanguage, Position, RegexEngine, 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, createHighlighterCoreSync, createJavaScriptRegexEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };