@shikijs/core 1.16.2 → 1.16.3
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-engines.d.mts +1 -1
- package/dist/index.d.mts +59 -59
- package/dist/index.mjs +4751 -5106
- package/dist/types.d.mts +300 -300
- package/package.json +4 -3
package/dist/types.d.mts
CHANGED
|
@@ -1,8 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Element, Root } from 'hast';
|
|
2
|
+
import { b as MaybeGetter, M as MaybeArray, a as RegexEngine, A as Awaitable, L as LoadWasmOptions, S as StringLiteralUnion } from './chunk-engines.mjs';
|
|
3
|
+
export { J as JavaScriptRegexEngineOptions, e as LoadWasmOptionsPlain, f as MaybeModule, O as OnigurumaLoadOptions, P as PatternScanner, c as RegexEngineString, R as RequireKeys, d as WebAssemblyInstance, W as WebAssemblyInstantiator } from './chunk-engines.mjs';
|
|
2
4
|
import { IGrammar, IRawGrammar, IRawTheme, IRawThemeSetting, StateStack } from '@shikijs/vscode-textmate';
|
|
3
5
|
export { IRawGrammar as RawGrammar, IRawTheme as RawTheme, IRawThemeSetting as RawThemeSetting } from '@shikijs/vscode-textmate';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
|
|
7
|
+
interface DecorationOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Custom decorations to wrap highlighted tokens with.
|
|
10
|
+
*/
|
|
11
|
+
decorations?: DecorationItem[];
|
|
12
|
+
}
|
|
13
|
+
interface DecorationItem {
|
|
14
|
+
/**
|
|
15
|
+
* Start offset or position of the decoration.
|
|
16
|
+
*/
|
|
17
|
+
start: OffsetOrPosition;
|
|
18
|
+
/**
|
|
19
|
+
* End offset or position of the decoration.
|
|
20
|
+
*/
|
|
21
|
+
end: OffsetOrPosition;
|
|
22
|
+
/**
|
|
23
|
+
* Tag name of the element to create.
|
|
24
|
+
* @default 'span'
|
|
25
|
+
*/
|
|
26
|
+
tagName?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Properties of the element to create.
|
|
29
|
+
*/
|
|
30
|
+
properties?: Element['properties'];
|
|
31
|
+
/**
|
|
32
|
+
* A custom function to transform the element after it has been created.
|
|
33
|
+
*/
|
|
34
|
+
transform?: (element: Element, type: DecorationTransformType) => Element | void;
|
|
35
|
+
/**
|
|
36
|
+
* By default when the decoration contains only one token, the decoration will be applied to the token.
|
|
37
|
+
*
|
|
38
|
+
* Set to `true` to always wrap the token with a new element
|
|
39
|
+
*
|
|
40
|
+
* @default false
|
|
41
|
+
*/
|
|
42
|
+
alwaysWrap?: boolean;
|
|
43
|
+
}
|
|
44
|
+
interface ResolvedDecorationItem extends Omit<DecorationItem, 'start' | 'end'> {
|
|
45
|
+
start: ResolvedPosition;
|
|
46
|
+
end: ResolvedPosition;
|
|
47
|
+
}
|
|
48
|
+
type DecorationTransformType = 'wrapper' | 'line' | 'token';
|
|
49
|
+
interface Position {
|
|
50
|
+
line: number;
|
|
51
|
+
character: number;
|
|
52
|
+
}
|
|
53
|
+
type Offset = number;
|
|
54
|
+
type OffsetOrPosition = Position | Offset;
|
|
55
|
+
interface ResolvedPosition extends Position {
|
|
56
|
+
offset: Offset;
|
|
57
|
+
}
|
|
6
58
|
|
|
7
59
|
interface Grammar extends IGrammar {
|
|
8
60
|
name: string;
|
|
@@ -142,306 +194,23 @@ interface BundledThemeInfo {
|
|
|
142
194
|
* GrammarState is a special reference object that holds the state of a grammar.
|
|
143
195
|
*
|
|
144
196
|
* It's used to highlight code snippets that are part of the target language.
|
|
145
|
-
*/
|
|
146
|
-
declare class GrammarState {
|
|
147
|
-
private readonly _stack;
|
|
148
|
-
readonly lang: string;
|
|
149
|
-
readonly theme: string;
|
|
150
|
-
/**
|
|
151
|
-
* Static method to create a initial grammar state.
|
|
152
|
-
*/
|
|
153
|
-
static initial(lang: string, theme: string): GrammarState;
|
|
154
|
-
constructor(_stack: StateStack, lang: string, theme: string);
|
|
155
|
-
get scopes(): string[];
|
|
156
|
-
toJSON(): {
|
|
157
|
-
lang: string;
|
|
158
|
-
theme: string;
|
|
159
|
-
scopes: string[];
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
interface TransformerOptions {
|
|
164
|
-
/**
|
|
165
|
-
* Transformers for the Shiki pipeline.
|
|
166
|
-
*/
|
|
167
|
-
transformers?: ShikiTransformer[];
|
|
168
|
-
}
|
|
169
|
-
interface ShikiTransformerContextMeta {
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Common transformer context for all transformers hooks
|
|
173
|
-
*/
|
|
174
|
-
interface ShikiTransformerContextCommon {
|
|
175
|
-
meta: ShikiTransformerContextMeta;
|
|
176
|
-
options: CodeToHastOptions;
|
|
177
|
-
codeToHast: (code: string, options: CodeToHastOptions) => Root;
|
|
178
|
-
codeToTokens: (code: string, options: CodeToTokensOptions) => TokensResult;
|
|
179
|
-
}
|
|
180
|
-
interface ShikiTransformerContextSource extends ShikiTransformerContextCommon {
|
|
181
|
-
readonly source: string;
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Transformer context for HAST related hooks
|
|
185
|
-
*/
|
|
186
|
-
interface ShikiTransformerContext extends ShikiTransformerContextSource {
|
|
187
|
-
readonly tokens: ThemedToken[][];
|
|
188
|
-
readonly root: Root;
|
|
189
|
-
readonly pre: Element;
|
|
190
|
-
readonly code: Element;
|
|
191
|
-
readonly lines: Element[];
|
|
192
|
-
readonly structure: CodeToHastOptions['structure'];
|
|
193
|
-
/**
|
|
194
|
-
* Utility to append class to a hast node
|
|
195
|
-
*
|
|
196
|
-
* If the `property.class` is a string, it will be splitted by space and converted to an array.
|
|
197
|
-
*/
|
|
198
|
-
addClassToHast: (hast: Element, className: string | string[]) => Element;
|
|
199
|
-
}
|
|
200
|
-
interface ShikiTransformer {
|
|
201
|
-
/**
|
|
202
|
-
* Name of the transformer
|
|
203
|
-
*/
|
|
204
|
-
name?: string;
|
|
205
|
-
/**
|
|
206
|
-
* Transform the raw input code before passing to the highlighter.
|
|
207
|
-
*/
|
|
208
|
-
preprocess?: (this: ShikiTransformerContextCommon, code: string, options: CodeToHastOptions) => string | void;
|
|
209
|
-
/**
|
|
210
|
-
* Transform the full tokens list before converting to HAST.
|
|
211
|
-
* Return a new tokens list will replace the original one.
|
|
212
|
-
*/
|
|
213
|
-
tokens?: (this: ShikiTransformerContextSource, tokens: ThemedToken[][]) => ThemedToken[][] | void;
|
|
214
|
-
/**
|
|
215
|
-
* Transform the entire generated HAST tree. Return a new Node will replace the original one.
|
|
216
|
-
*/
|
|
217
|
-
root?: (this: ShikiTransformerContext, hast: Root) => Root | void;
|
|
218
|
-
/**
|
|
219
|
-
* Transform the `<pre>` element. Return a new Node will replace the original one.
|
|
220
|
-
*/
|
|
221
|
-
pre?: (this: ShikiTransformerContext, hast: Element) => Element | void;
|
|
222
|
-
/**
|
|
223
|
-
* Transform the `<code>` element. Return a new Node will replace the original one.
|
|
224
|
-
*/
|
|
225
|
-
code?: (this: ShikiTransformerContext, hast: Element) => Element | void;
|
|
226
|
-
/**
|
|
227
|
-
* Transform each line `<span class="line">` element.
|
|
228
|
-
*
|
|
229
|
-
* @param hast
|
|
230
|
-
* @param line 1-based line number
|
|
231
|
-
*/
|
|
232
|
-
line?: (this: ShikiTransformerContext, hast: Element, line: number) => Element | void;
|
|
233
|
-
/**
|
|
234
|
-
* Transform each token `<span>` element.
|
|
235
|
-
*/
|
|
236
|
-
span?: (this: ShikiTransformerContext, hast: Element, line: number, col: number, lineElement: Element) => Element | void;
|
|
237
|
-
/**
|
|
238
|
-
* Transform the generated HTML string before returning.
|
|
239
|
-
* This hook will only be called with `codeToHtml`.
|
|
240
|
-
*/
|
|
241
|
-
postprocess?: (this: ShikiTransformerContextCommon, html: string, options: CodeToHastOptions) => string | void;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
interface DecorationOptions {
|
|
245
|
-
/**
|
|
246
|
-
* Custom decorations to wrap highlighted tokens with.
|
|
247
|
-
*/
|
|
248
|
-
decorations?: DecorationItem[];
|
|
249
|
-
}
|
|
250
|
-
interface DecorationItem {
|
|
251
|
-
/**
|
|
252
|
-
* Start offset or position of the decoration.
|
|
253
|
-
*/
|
|
254
|
-
start: OffsetOrPosition;
|
|
255
|
-
/**
|
|
256
|
-
* End offset or position of the decoration.
|
|
257
|
-
*/
|
|
258
|
-
end: OffsetOrPosition;
|
|
259
|
-
/**
|
|
260
|
-
* Tag name of the element to create.
|
|
261
|
-
* @default 'span'
|
|
262
|
-
*/
|
|
263
|
-
tagName?: string;
|
|
264
|
-
/**
|
|
265
|
-
* Properties of the element to create.
|
|
266
|
-
*/
|
|
267
|
-
properties?: Element['properties'];
|
|
268
|
-
/**
|
|
269
|
-
* A custom function to transform the element after it has been created.
|
|
270
|
-
*/
|
|
271
|
-
transform?: (element: Element, type: DecorationTransformType) => Element | void;
|
|
272
|
-
/**
|
|
273
|
-
* By default when the decoration contains only one token, the decoration will be applied to the token.
|
|
274
|
-
*
|
|
275
|
-
* Set to `true` to always wrap the token with a new element
|
|
276
|
-
*
|
|
277
|
-
* @default false
|
|
278
|
-
*/
|
|
279
|
-
alwaysWrap?: boolean;
|
|
280
|
-
}
|
|
281
|
-
interface ResolvedDecorationItem extends Omit<DecorationItem, 'start' | 'end'> {
|
|
282
|
-
start: ResolvedPosition;
|
|
283
|
-
end: ResolvedPosition;
|
|
284
|
-
}
|
|
285
|
-
type DecorationTransformType = 'wrapper' | 'line' | 'token';
|
|
286
|
-
interface Position {
|
|
287
|
-
line: number;
|
|
288
|
-
character: number;
|
|
289
|
-
}
|
|
290
|
-
type Offset = number;
|
|
291
|
-
type OffsetOrPosition = Position | Offset;
|
|
292
|
-
interface ResolvedPosition extends Position {
|
|
293
|
-
offset: Offset;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
interface HighlighterCoreOptions<Sync extends boolean = false> {
|
|
297
|
-
/**
|
|
298
|
-
* Custom RegExp engine.
|
|
299
|
-
*/
|
|
300
|
-
engine?: Sync extends true ? RegexEngine : Awaitable<RegexEngine>;
|
|
301
|
-
/**
|
|
302
|
-
* Theme names, or theme registration objects to be loaded upfront.
|
|
303
|
-
*/
|
|
304
|
-
themes?: Sync extends true ? MaybeArray<ThemeRegistrationAny>[] : ThemeInput[];
|
|
305
|
-
/**
|
|
306
|
-
* Language names, or language registration objects to be loaded upfront.
|
|
307
|
-
*/
|
|
308
|
-
langs?: Sync extends true ? MaybeArray<LanguageRegistration>[] : LanguageInput[];
|
|
309
|
-
/**
|
|
310
|
-
* Alias of languages
|
|
311
|
-
* @example { 'my-lang': 'javascript' }
|
|
312
|
-
*/
|
|
313
|
-
langAlias?: Record<string, string>;
|
|
314
|
-
/**
|
|
315
|
-
* Emit console warnings to alert users of potential issues.
|
|
316
|
-
* @default true
|
|
317
|
-
*/
|
|
318
|
-
warnings?: boolean;
|
|
319
|
-
/**
|
|
320
|
-
* Load wasm file from a custom path or using a custom function.
|
|
321
|
-
*/
|
|
322
|
-
loadWasm?: Sync extends true ? never : LoadWasmOptions;
|
|
323
|
-
}
|
|
324
|
-
interface BundledHighlighterOptions<L extends string, T extends string> extends Pick<HighlighterCoreOptions, 'warnings' | 'engine'> {
|
|
325
|
-
/**
|
|
326
|
-
* Theme registation
|
|
327
|
-
*
|
|
328
|
-
* @default []
|
|
329
|
-
*/
|
|
330
|
-
themes: (ThemeInput | StringLiteralUnion<T> | SpecialTheme)[];
|
|
331
|
-
/**
|
|
332
|
-
* Language registation
|
|
333
|
-
*
|
|
334
|
-
* @default []
|
|
335
|
-
*/
|
|
336
|
-
langs: (LanguageInput | StringLiteralUnion<L> | SpecialLanguage)[];
|
|
337
|
-
/**
|
|
338
|
-
* Alias of languages
|
|
339
|
-
* @example { 'my-lang': 'javascript' }
|
|
340
|
-
*/
|
|
341
|
-
langAlias?: Record<string, StringLiteralUnion<L>>;
|
|
342
|
-
}
|
|
343
|
-
interface CodeOptionsSingleTheme<Themes extends string = string> {
|
|
344
|
-
theme: ThemeRegistrationAny | StringLiteralUnion<Themes>;
|
|
345
|
-
}
|
|
346
|
-
interface CodeOptionsMultipleThemes<Themes extends string = string> {
|
|
347
|
-
/**
|
|
348
|
-
* A map of color names to themes.
|
|
349
|
-
* This allows you to specify multiple themes for the generated code.
|
|
350
|
-
*
|
|
351
|
-
* ```ts
|
|
352
|
-
* highlighter.codeToHtml(code, {
|
|
353
|
-
* lang: 'js',
|
|
354
|
-
* themes: {
|
|
355
|
-
* light: 'vitesse-light',
|
|
356
|
-
* dark: 'vitesse-dark',
|
|
357
|
-
* }
|
|
358
|
-
* })
|
|
359
|
-
* ```
|
|
360
|
-
*
|
|
361
|
-
* Will generate:
|
|
362
|
-
*
|
|
363
|
-
* ```html
|
|
364
|
-
* <span style="color:#111;--shiki-dark:#fff;">code</span>
|
|
365
|
-
* ```
|
|
366
|
-
*
|
|
367
|
-
* @see https://github.com/shikijs/shiki#lightdark-dual-themes
|
|
368
|
-
*/
|
|
369
|
-
themes: Partial<Record<string, ThemeRegistrationAny | StringLiteralUnion<Themes>>>;
|
|
370
|
-
/**
|
|
371
|
-
* The default theme applied to the code (via inline `color` style).
|
|
372
|
-
* The rest of the themes are applied via CSS variables, and toggled by CSS overrides.
|
|
373
|
-
*
|
|
374
|
-
* For example, if `defaultColor` is `light`, then `light` theme is applied to the code,
|
|
375
|
-
* and the `dark` theme and other custom themes are applied via CSS variables:
|
|
376
|
-
*
|
|
377
|
-
* ```html
|
|
378
|
-
* <span style="color:#{light};--shiki-dark:#{dark};--shiki-custom:#{custom};">code</span>
|
|
379
|
-
* ```
|
|
380
|
-
*
|
|
381
|
-
* When set to `false`, no default styles will be applied, and totally up to users to apply the styles:
|
|
382
|
-
*
|
|
383
|
-
* ```html
|
|
384
|
-
* <span style="--shiki-light:#{light};--shiki-dark:#{dark};--shiki-custom:#{custom};">code</span>
|
|
385
|
-
* ```
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
* @default 'light'
|
|
389
|
-
*/
|
|
390
|
-
defaultColor?: StringLiteralUnion<'light' | 'dark'> | false;
|
|
391
|
-
/**
|
|
392
|
-
* Prefix of CSS variables used to store the color of the other theme.
|
|
393
|
-
*
|
|
394
|
-
* @default '--shiki-'
|
|
395
|
-
*/
|
|
396
|
-
cssVariablePrefix?: string;
|
|
397
|
-
}
|
|
398
|
-
type CodeOptionsThemes<Themes extends string = string> = CodeOptionsSingleTheme<Themes> | CodeOptionsMultipleThemes<Themes>;
|
|
399
|
-
type CodeToHastOptions<Languages extends string = string, Themes extends string = string> = CodeToHastOptionsCommon<Languages> & CodeOptionsThemes<Themes> & CodeOptionsMeta;
|
|
400
|
-
interface CodeToHastOptionsCommon<Languages extends string = string> extends TransformerOptions, DecorationOptions, Pick<TokenizeWithThemeOptions, 'colorReplacements' | 'tokenizeMaxLineLength' | 'tokenizeTimeLimit' | 'grammarState' | 'grammarContextCode'> {
|
|
401
|
-
lang: StringLiteralUnion<Languages | SpecialLanguage>;
|
|
402
|
-
/**
|
|
403
|
-
* Merge whitespace tokens to saving extra `<span>`.
|
|
404
|
-
*
|
|
405
|
-
* When set to true, it will merge whitespace tokens with the next token.
|
|
406
|
-
* When set to false, it keep the output as-is.
|
|
407
|
-
* When set to `never`, it will force to separate leading and trailing spaces from tokens.
|
|
408
|
-
*
|
|
409
|
-
* @default true
|
|
410
|
-
*/
|
|
411
|
-
mergeWhitespaces?: boolean | 'never';
|
|
412
|
-
/**
|
|
413
|
-
* The structure of the generated HAST and HTML.
|
|
414
|
-
*
|
|
415
|
-
* - `classic`: The classic structure with `<pre>` and `<code>` elements, each line wrapped with a `<span class="line">` element.
|
|
416
|
-
* - `inline`: All tokens are rendered as `<span>`, line breaks are rendered as `<br>`. No `<pre>` or `<code>` elements. Default forground and background colors are not applied.
|
|
417
|
-
*
|
|
418
|
-
* @default 'classic'
|
|
419
|
-
*/
|
|
420
|
-
structure?: 'classic' | 'inline';
|
|
421
|
-
}
|
|
422
|
-
interface CodeOptionsMeta {
|
|
423
|
-
/**
|
|
424
|
-
* Meta data passed to Shiki, usually used by plugin integrations to pass the code block header.
|
|
425
|
-
*
|
|
426
|
-
* Key values in meta will be serialized to the attributes of the root `<pre>` element.
|
|
427
|
-
*
|
|
428
|
-
* Keys starting with `_` will be ignored.
|
|
429
|
-
*
|
|
430
|
-
* A special key `__raw` key will be used to pass the raw code block header (if the integration supports it).
|
|
197
|
+
*/
|
|
198
|
+
declare class GrammarState {
|
|
199
|
+
private readonly _stack;
|
|
200
|
+
readonly lang: string;
|
|
201
|
+
readonly theme: string;
|
|
202
|
+
/**
|
|
203
|
+
* Static method to create a initial grammar state.
|
|
431
204
|
*/
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
205
|
+
static initial(lang: string, theme: string): GrammarState;
|
|
206
|
+
constructor(_stack: StateStack, lang: string, theme: string);
|
|
207
|
+
get scopes(): string[];
|
|
208
|
+
toJSON(): {
|
|
209
|
+
lang: string;
|
|
210
|
+
theme: string;
|
|
211
|
+
scopes: string[];
|
|
438
212
|
};
|
|
439
213
|
}
|
|
440
|
-
interface CodeToHastRenderOptionsCommon extends TransformerOptions, Omit<TokensResult, 'tokens'> {
|
|
441
|
-
lang?: string;
|
|
442
|
-
langId?: string;
|
|
443
|
-
}
|
|
444
|
-
type CodeToHastRenderOptions = CodeToHastRenderOptionsCommon & CodeToHastOptions;
|
|
445
214
|
|
|
446
215
|
interface CodeToTokensBaseOptions<Languages extends string = string, Themes extends string = string> extends TokenizeWithThemeOptions {
|
|
447
216
|
lang?: Languages | SpecialLanguage;
|
|
@@ -651,6 +420,237 @@ declare enum FontStyle {
|
|
|
651
420
|
Underline = 4
|
|
652
421
|
}
|
|
653
422
|
|
|
423
|
+
interface TransformerOptions {
|
|
424
|
+
/**
|
|
425
|
+
* Transformers for the Shiki pipeline.
|
|
426
|
+
*/
|
|
427
|
+
transformers?: ShikiTransformer[];
|
|
428
|
+
}
|
|
429
|
+
interface ShikiTransformerContextMeta {
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Common transformer context for all transformers hooks
|
|
433
|
+
*/
|
|
434
|
+
interface ShikiTransformerContextCommon {
|
|
435
|
+
meta: ShikiTransformerContextMeta;
|
|
436
|
+
options: CodeToHastOptions;
|
|
437
|
+
codeToHast: (code: string, options: CodeToHastOptions) => Root;
|
|
438
|
+
codeToTokens: (code: string, options: CodeToTokensOptions) => TokensResult;
|
|
439
|
+
}
|
|
440
|
+
interface ShikiTransformerContextSource extends ShikiTransformerContextCommon {
|
|
441
|
+
readonly source: string;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Transformer context for HAST related hooks
|
|
445
|
+
*/
|
|
446
|
+
interface ShikiTransformerContext extends ShikiTransformerContextSource {
|
|
447
|
+
readonly tokens: ThemedToken[][];
|
|
448
|
+
readonly root: Root;
|
|
449
|
+
readonly pre: Element;
|
|
450
|
+
readonly code: Element;
|
|
451
|
+
readonly lines: Element[];
|
|
452
|
+
readonly structure: CodeToHastOptions['structure'];
|
|
453
|
+
/**
|
|
454
|
+
* Utility to append class to a hast node
|
|
455
|
+
*
|
|
456
|
+
* If the `property.class` is a string, it will be splitted by space and converted to an array.
|
|
457
|
+
*/
|
|
458
|
+
addClassToHast: (hast: Element, className: string | string[]) => Element;
|
|
459
|
+
}
|
|
460
|
+
interface ShikiTransformer {
|
|
461
|
+
/**
|
|
462
|
+
* Name of the transformer
|
|
463
|
+
*/
|
|
464
|
+
name?: string;
|
|
465
|
+
/**
|
|
466
|
+
* Transform the raw input code before passing to the highlighter.
|
|
467
|
+
*/
|
|
468
|
+
preprocess?: (this: ShikiTransformerContextCommon, code: string, options: CodeToHastOptions) => string | void;
|
|
469
|
+
/**
|
|
470
|
+
* Transform the full tokens list before converting to HAST.
|
|
471
|
+
* Return a new tokens list will replace the original one.
|
|
472
|
+
*/
|
|
473
|
+
tokens?: (this: ShikiTransformerContextSource, tokens: ThemedToken[][]) => ThemedToken[][] | void;
|
|
474
|
+
/**
|
|
475
|
+
* Transform the entire generated HAST tree. Return a new Node will replace the original one.
|
|
476
|
+
*/
|
|
477
|
+
root?: (this: ShikiTransformerContext, hast: Root) => Root | void;
|
|
478
|
+
/**
|
|
479
|
+
* Transform the `<pre>` element. Return a new Node will replace the original one.
|
|
480
|
+
*/
|
|
481
|
+
pre?: (this: ShikiTransformerContext, hast: Element) => Element | void;
|
|
482
|
+
/**
|
|
483
|
+
* Transform the `<code>` element. Return a new Node will replace the original one.
|
|
484
|
+
*/
|
|
485
|
+
code?: (this: ShikiTransformerContext, hast: Element) => Element | void;
|
|
486
|
+
/**
|
|
487
|
+
* Transform each line `<span class="line">` element.
|
|
488
|
+
*
|
|
489
|
+
* @param hast
|
|
490
|
+
* @param line 1-based line number
|
|
491
|
+
*/
|
|
492
|
+
line?: (this: ShikiTransformerContext, hast: Element, line: number) => Element | void;
|
|
493
|
+
/**
|
|
494
|
+
* Transform each token `<span>` element.
|
|
495
|
+
*/
|
|
496
|
+
span?: (this: ShikiTransformerContext, hast: Element, line: number, col: number, lineElement: Element) => Element | void;
|
|
497
|
+
/**
|
|
498
|
+
* Transform the generated HTML string before returning.
|
|
499
|
+
* This hook will only be called with `codeToHtml`.
|
|
500
|
+
*/
|
|
501
|
+
postprocess?: (this: ShikiTransformerContextCommon, html: string, options: CodeToHastOptions) => string | void;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
interface HighlighterCoreOptions<Sync extends boolean = false> {
|
|
505
|
+
/**
|
|
506
|
+
* Custom RegExp engine.
|
|
507
|
+
*/
|
|
508
|
+
engine?: Sync extends true ? RegexEngine : Awaitable<RegexEngine>;
|
|
509
|
+
/**
|
|
510
|
+
* Theme names, or theme registration objects to be loaded upfront.
|
|
511
|
+
*/
|
|
512
|
+
themes?: Sync extends true ? MaybeArray<ThemeRegistrationAny>[] : ThemeInput[];
|
|
513
|
+
/**
|
|
514
|
+
* Language names, or language registration objects to be loaded upfront.
|
|
515
|
+
*/
|
|
516
|
+
langs?: Sync extends true ? MaybeArray<LanguageRegistration>[] : LanguageInput[];
|
|
517
|
+
/**
|
|
518
|
+
* Alias of languages
|
|
519
|
+
* @example { 'my-lang': 'javascript' }
|
|
520
|
+
*/
|
|
521
|
+
langAlias?: Record<string, string>;
|
|
522
|
+
/**
|
|
523
|
+
* Emit console warnings to alert users of potential issues.
|
|
524
|
+
* @default true
|
|
525
|
+
*/
|
|
526
|
+
warnings?: boolean;
|
|
527
|
+
/**
|
|
528
|
+
* Load wasm file from a custom path or using a custom function.
|
|
529
|
+
*/
|
|
530
|
+
loadWasm?: Sync extends true ? never : LoadWasmOptions;
|
|
531
|
+
}
|
|
532
|
+
interface BundledHighlighterOptions<L extends string, T extends string> extends Pick<HighlighterCoreOptions, 'warnings' | 'engine'> {
|
|
533
|
+
/**
|
|
534
|
+
* Theme registation
|
|
535
|
+
*
|
|
536
|
+
* @default []
|
|
537
|
+
*/
|
|
538
|
+
themes: (ThemeInput | StringLiteralUnion<T> | SpecialTheme)[];
|
|
539
|
+
/**
|
|
540
|
+
* Language registation
|
|
541
|
+
*
|
|
542
|
+
* @default []
|
|
543
|
+
*/
|
|
544
|
+
langs: (LanguageInput | StringLiteralUnion<L> | SpecialLanguage)[];
|
|
545
|
+
/**
|
|
546
|
+
* Alias of languages
|
|
547
|
+
* @example { 'my-lang': 'javascript' }
|
|
548
|
+
*/
|
|
549
|
+
langAlias?: Record<string, StringLiteralUnion<L>>;
|
|
550
|
+
}
|
|
551
|
+
interface CodeOptionsSingleTheme<Themes extends string = string> {
|
|
552
|
+
theme: ThemeRegistrationAny | StringLiteralUnion<Themes>;
|
|
553
|
+
}
|
|
554
|
+
interface CodeOptionsMultipleThemes<Themes extends string = string> {
|
|
555
|
+
/**
|
|
556
|
+
* A map of color names to themes.
|
|
557
|
+
* This allows you to specify multiple themes for the generated code.
|
|
558
|
+
*
|
|
559
|
+
* ```ts
|
|
560
|
+
* highlighter.codeToHtml(code, {
|
|
561
|
+
* lang: 'js',
|
|
562
|
+
* themes: {
|
|
563
|
+
* light: 'vitesse-light',
|
|
564
|
+
* dark: 'vitesse-dark',
|
|
565
|
+
* }
|
|
566
|
+
* })
|
|
567
|
+
* ```
|
|
568
|
+
*
|
|
569
|
+
* Will generate:
|
|
570
|
+
*
|
|
571
|
+
* ```html
|
|
572
|
+
* <span style="color:#111;--shiki-dark:#fff;">code</span>
|
|
573
|
+
* ```
|
|
574
|
+
*
|
|
575
|
+
* @see https://github.com/shikijs/shiki#lightdark-dual-themes
|
|
576
|
+
*/
|
|
577
|
+
themes: Partial<Record<string, ThemeRegistrationAny | StringLiteralUnion<Themes>>>;
|
|
578
|
+
/**
|
|
579
|
+
* The default theme applied to the code (via inline `color` style).
|
|
580
|
+
* The rest of the themes are applied via CSS variables, and toggled by CSS overrides.
|
|
581
|
+
*
|
|
582
|
+
* For example, if `defaultColor` is `light`, then `light` theme is applied to the code,
|
|
583
|
+
* and the `dark` theme and other custom themes are applied via CSS variables:
|
|
584
|
+
*
|
|
585
|
+
* ```html
|
|
586
|
+
* <span style="color:#{light};--shiki-dark:#{dark};--shiki-custom:#{custom};">code</span>
|
|
587
|
+
* ```
|
|
588
|
+
*
|
|
589
|
+
* When set to `false`, no default styles will be applied, and totally up to users to apply the styles:
|
|
590
|
+
*
|
|
591
|
+
* ```html
|
|
592
|
+
* <span style="--shiki-light:#{light};--shiki-dark:#{dark};--shiki-custom:#{custom};">code</span>
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
*
|
|
596
|
+
* @default 'light'
|
|
597
|
+
*/
|
|
598
|
+
defaultColor?: StringLiteralUnion<'light' | 'dark'> | false;
|
|
599
|
+
/**
|
|
600
|
+
* Prefix of CSS variables used to store the color of the other theme.
|
|
601
|
+
*
|
|
602
|
+
* @default '--shiki-'
|
|
603
|
+
*/
|
|
604
|
+
cssVariablePrefix?: string;
|
|
605
|
+
}
|
|
606
|
+
type CodeOptionsThemes<Themes extends string = string> = CodeOptionsSingleTheme<Themes> | CodeOptionsMultipleThemes<Themes>;
|
|
607
|
+
type CodeToHastOptions<Languages extends string = string, Themes extends string = string> = CodeToHastOptionsCommon<Languages> & CodeOptionsThemes<Themes> & CodeOptionsMeta;
|
|
608
|
+
interface CodeToHastOptionsCommon<Languages extends string = string> extends TransformerOptions, DecorationOptions, Pick<TokenizeWithThemeOptions, 'colorReplacements' | 'tokenizeMaxLineLength' | 'tokenizeTimeLimit' | 'grammarState' | 'grammarContextCode'> {
|
|
609
|
+
lang: StringLiteralUnion<Languages | SpecialLanguage>;
|
|
610
|
+
/**
|
|
611
|
+
* Merge whitespace tokens to saving extra `<span>`.
|
|
612
|
+
*
|
|
613
|
+
* When set to true, it will merge whitespace tokens with the next token.
|
|
614
|
+
* When set to false, it keep the output as-is.
|
|
615
|
+
* When set to `never`, it will force to separate leading and trailing spaces from tokens.
|
|
616
|
+
*
|
|
617
|
+
* @default true
|
|
618
|
+
*/
|
|
619
|
+
mergeWhitespaces?: boolean | 'never';
|
|
620
|
+
/**
|
|
621
|
+
* The structure of the generated HAST and HTML.
|
|
622
|
+
*
|
|
623
|
+
* - `classic`: The classic structure with `<pre>` and `<code>` elements, each line wrapped with a `<span class="line">` element.
|
|
624
|
+
* - `inline`: All tokens are rendered as `<span>`, line breaks are rendered as `<br>`. No `<pre>` or `<code>` elements. Default forground and background colors are not applied.
|
|
625
|
+
*
|
|
626
|
+
* @default 'classic'
|
|
627
|
+
*/
|
|
628
|
+
structure?: 'classic' | 'inline';
|
|
629
|
+
}
|
|
630
|
+
interface CodeOptionsMeta {
|
|
631
|
+
/**
|
|
632
|
+
* Meta data passed to Shiki, usually used by plugin integrations to pass the code block header.
|
|
633
|
+
*
|
|
634
|
+
* Key values in meta will be serialized to the attributes of the root `<pre>` element.
|
|
635
|
+
*
|
|
636
|
+
* Keys starting with `_` will be ignored.
|
|
637
|
+
*
|
|
638
|
+
* A special key `__raw` key will be used to pass the raw code block header (if the integration supports it).
|
|
639
|
+
*/
|
|
640
|
+
meta?: {
|
|
641
|
+
/**
|
|
642
|
+
* Raw string of the code block header.
|
|
643
|
+
*/
|
|
644
|
+
__raw?: string;
|
|
645
|
+
[key: string]: any;
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
interface CodeToHastRenderOptionsCommon extends TransformerOptions, Omit<TokensResult, 'tokens'> {
|
|
649
|
+
lang?: string;
|
|
650
|
+
langId?: string;
|
|
651
|
+
}
|
|
652
|
+
type CodeToHastRenderOptions = CodeToHastRenderOptionsCommon & CodeToHastOptions;
|
|
653
|
+
|
|
654
654
|
/**
|
|
655
655
|
* Internal context of Shiki, core textmate logic
|
|
656
656
|
*/
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shikijs/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.16.
|
|
4
|
+
"version": "1.16.3",
|
|
5
5
|
"description": "Core of Shiki",
|
|
6
6
|
"author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -61,11 +61,12 @@
|
|
|
61
61
|
],
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"@shikijs/vscode-textmate": "^9.2.0",
|
|
64
|
-
"@types/hast": "^3.0.4"
|
|
64
|
+
"@types/hast": "^3.0.4",
|
|
65
|
+
"oniguruma-to-js": "0.3.3",
|
|
66
|
+
"regex": "4.3.2"
|
|
65
67
|
},
|
|
66
68
|
"devDependencies": {
|
|
67
69
|
"hast-util-to-html": "^9.0.2",
|
|
68
|
-
"oniguruma-to-js": "^0.3.1",
|
|
69
70
|
"vscode-oniguruma": "^1.7.0"
|
|
70
71
|
},
|
|
71
72
|
"scripts": {
|