@shikijs/core 1.0.0-beta.1 → 1.0.0-beta.2

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.
@@ -681,18 +681,26 @@ interface HighlighterGeneric<BundledLangKeys extends string, BundledThemeKeys ex
681
681
  */
682
682
  codeToHast(code: string, options: CodeToHastOptions<ResolveBundleKey<BundledLangKeys>, ResolveBundleKey<BundledThemeKeys>>): Root;
683
683
  /**
684
- * Get highlighted code in tokens.
684
+ * Get highlighted code in tokens. Uses `codeToTokensWithThemes` or `codeToTokensBase` based on the options.
685
+ */
686
+ codeToTokens(code: string, options: CodeToTokensOptions<ResolveBundleKey<BundledLangKeys>, ResolveBundleKey<BundledThemeKeys>>): TokensResult;
687
+ /**
688
+ * Get highlighted code in tokens with a single theme.
685
689
  * @returns A 2D array of tokens, first dimension is lines, second dimension is tokens in a line.
686
690
  */
687
- codeToThemedTokens(code: string, options: CodeToThemedTokensOptions<ResolveBundleKey<BundledLangKeys>, ResolveBundleKey<BundledThemeKeys>>): ThemedToken[][];
691
+ codeToTokensBase(code: string, options: CodeToTokensBaseOptions<ResolveBundleKey<BundledLangKeys>, ResolveBundleKey<BundledThemeKeys>>): ThemedToken[][];
688
692
  /**
689
693
  * Get highlighted code in tokens with multiple themes.
690
694
  *
691
- * Different from `codeToThemedTokens`, each token will have a `variants` property consisting of an object of color name to token styles.
695
+ * Different from `codeToTokensBase`, each token will have a `variants` property consisting of an object of color name to token styles.
692
696
  *
693
697
  * @returns A 2D array of tokens, first dimension is lines, second dimension is tokens in a line.
694
698
  */
695
699
  codeToTokensWithThemes(code: string, options: CodeToTokensWithThemesOptions<ResolveBundleKey<BundledLangKeys>, ResolveBundleKey<BundledThemeKeys>>): ThemedTokenWithVariants[][];
700
+ /**
701
+ * @deprecated Renamed to `codeToTokensBase`, or use high-level `codeToTokens` directly.
702
+ */
703
+ codeToThemedTokens(code: string, options: CodeToTokensBaseOptions<ResolveBundleKey<BundledLangKeys>, ResolveBundleKey<BundledThemeKeys>>): ThemedToken[][];
696
704
  /**
697
705
  * Load a theme to the highlighter, so later it can be used synchronously.
698
706
  */
@@ -799,10 +807,11 @@ interface LanguageRegistration extends IRawGrammar {
799
807
  */
800
808
  injectTo?: string[];
801
809
  }
802
- interface CodeToThemedTokensOptions<Languages = string, Themes = string> extends TokenizeWithThemeOptions {
810
+ interface CodeToTokensBaseOptions<Languages extends string = string, Themes extends string = string> extends TokenizeWithThemeOptions {
803
811
  lang?: Languages | SpecialLanguage;
804
812
  theme?: Themes | ThemeRegistrationAny | SpecialTheme;
805
813
  }
814
+ type CodeToTokensOptions<Languages extends string = string, Themes extends string = string> = Omit<CodeToTokensBaseOptions<Languages, Themes>, 'theme'> & CodeOptionsThemes<Themes>;
806
815
  interface CodeToHastOptionsCommon<Languages extends string = string> extends TransformerOptions, Pick<TokenizeWithThemeOptions, 'colorReplacements'> {
807
816
  lang: StringLiteralUnion<Languages | SpecialLanguage>;
808
817
  /**
@@ -1000,6 +1009,7 @@ interface ShikiTransformerContextCommon {
1000
1009
  meta: ShikiTransformerContextMeta;
1001
1010
  options: CodeToHastOptions;
1002
1011
  codeToHast: (code: string, options: CodeToHastOptions) => Root;
1012
+ codeToTokens: (code: string, options: CodeToTokensOptions) => TokensResult;
1003
1013
  }
1004
1014
  /**
1005
1015
  * Transformer context for HAST related hooks
@@ -1010,6 +1020,12 @@ interface ShikiTransformerContext extends ShikiTransformerContextCommon {
1010
1020
  readonly pre: Element;
1011
1021
  readonly code: Element;
1012
1022
  readonly lines: Element[];
1023
+ /**
1024
+ * Utility to append class to a hast node
1025
+ *
1026
+ * If the `property.class` is a string, it will be splitted by space and converted to an array.
1027
+ */
1028
+ addClassToHast: (hast: Element, className: string | string[]) => Element;
1013
1029
  }
1014
1030
  interface ShikiTransformer {
1015
1031
  /**
@@ -1058,11 +1074,22 @@ interface ShikiTransformer {
1058
1074
  */
1059
1075
  token?(this: ShikiTransformerContext, hast: Element, line: number, col: number, lineElement: Element): Element | void;
1060
1076
  }
1061
- interface HtmlRendererOptionsCommon extends TransformerOptions {
1062
- lang?: string;
1063
- langId?: string;
1077
+ interface TokensResult {
1078
+ /**
1079
+ * 2D array of tokens, first dimension is lines, second dimension is tokens in a line.
1080
+ */
1081
+ tokens: ThemedToken[][];
1082
+ /**
1083
+ * Foreground color of the code.
1084
+ */
1064
1085
  fg?: string;
1086
+ /**
1087
+ * Background color of the code.
1088
+ */
1065
1089
  bg?: string;
1090
+ /**
1091
+ * A string representation of themes applied to the token.
1092
+ */
1066
1093
  themeName?: string;
1067
1094
  /**
1068
1095
  * Custom style string to be applied to the root `<pre>` element.
@@ -1070,7 +1097,11 @@ interface HtmlRendererOptionsCommon extends TransformerOptions {
1070
1097
  */
1071
1098
  rootStyle?: string;
1072
1099
  }
1073
- type HtmlRendererOptions = HtmlRendererOptionsCommon & CodeToHastOptions;
1100
+ interface CodeToHastRenderOptionsCommon extends TransformerOptions, Omit<TokensResult, 'tokens'> {
1101
+ lang?: string;
1102
+ langId?: string;
1103
+ }
1104
+ type CodeToHastRenderOptions = CodeToHastRenderOptionsCommon & CodeToHastOptions;
1074
1105
  interface ThemedTokenScopeExplanation {
1075
1106
  scopeName: string;
1076
1107
  themeMatches: any[];
@@ -1227,4 +1258,4 @@ interface ShikijiTransformerContext extends ShikiTransformerContext {
1227
1258
  interface ShikijiTransformerContextCommon extends ShikiTransformerContextCommon {
1228
1259
  }
1229
1260
 
1230
- export { type BundledLanguageInfo as $, type AnsiLanguage as A, type BundledHighlighterOptions as B, type CodeToHastOptions as C, type TransformerOptions as D, type Element as E, FontStyle as F, type ThemeRegistrationRaw as G, type HighlighterGeneric as H, type IGrammar as I, type ThemeRegistration as J, type ShikiTransformerContextMeta as K, type LanguageInput as L, type MaybeArray as M, type Nodes as N, type ShikiTransformerContext as O, type PlainTextLanguage as P, type ShikiTransformer as Q, type Root as R, type SpecialTheme as S, type ThemeInput as T, type HtmlRendererOptionsCommon as U, type ThemedTokenScopeExplanation as V, type ThemedTokenExplanation as W, type TokenBase as X, type TokenStyles as Y, type DynamicImportLanguageRegistration as Z, type DynamicImportThemeRegistration as _, type HighlighterCoreOptions as a, type BundledThemeInfo as a0, type ShikijiTransformer as a1, type ShikijiTransformerContext as a2, type ShikijiTransformerContextCommon as a3, Registry as a4, INITIAL as a5, type StateStack as a6, type IRawTheme as a7, type IGrammarConfiguration as a8, type IOnigLib as a9, type RegistryOptions as aa, type IRawThemeSetting as ab, type RequireKeys as b, type CodeToThemedTokensOptions as c, type ThemedToken as d, type CodeToTokensWithThemesOptions as e, type ThemedTokenWithVariants as f, type ShikiInternal as g, type ThemeRegistrationResolved as h, type TokenizeWithThemeOptions as i, type ShikiTransformerContextCommon as j, type HtmlRendererOptions as k, type RootContent as l, type ThemeRegistrationAny as m, type IRawGrammar as n, type SpecialLanguage as o, type Awaitable as p, type MaybeGetter as q, type MaybeModule as r, type StringLiteralUnion as s, type ResolveBundleKey as t, type LanguageRegistration as u, type CodeToHastOptionsCommon as v, type CodeOptionsSingleTheme as w, type CodeOptionsMultipleThemes as x, type CodeOptionsThemes as y, type CodeOptionsMeta as z };
1261
+ export { type DynamicImportLanguageRegistration as $, type AnsiLanguage as A, type BundledHighlighterOptions as B, type CodeToHastOptions as C, type CodeOptionsMultipleThemes as D, type Element as E, FontStyle as F, type CodeOptionsThemes as G, type HighlighterGeneric as H, type IGrammar as I, type CodeOptionsMeta as J, type TransformerOptions as K, type LanguageInput as L, type MaybeArray as M, type Nodes as N, type ThemeRegistrationRaw as O, type PlainTextLanguage as P, type ThemeRegistration as Q, type Root as R, type SpecialTheme as S, type ThemeInput as T, type ShikiTransformerContextMeta as U, type ShikiTransformerContext as V, type ShikiTransformer as W, type CodeToHastRenderOptionsCommon as X, type ThemedTokenScopeExplanation as Y, type ThemedTokenExplanation as Z, type TokenBase as _, type HighlighterCoreOptions as a, type DynamicImportThemeRegistration as a0, type BundledLanguageInfo as a1, type BundledThemeInfo as a2, type ShikijiTransformer as a3, type ShikijiTransformerContext as a4, type ShikijiTransformerContextCommon as a5, Registry as a6, INITIAL as a7, type StateStack as a8, type IRawTheme as a9, type IGrammarConfiguration as aa, type IOnigLib as ab, type RegistryOptions as ac, type IRawThemeSetting as ad, type CodeToTokensOptions as b, type TokensResult as c, type RequireKeys as d, type CodeToTokensBaseOptions as e, type ThemedToken as f, type CodeToTokensWithThemesOptions as g, type ThemedTokenWithVariants as h, type TokenStyles as i, type ShikiInternal as j, type ThemeRegistrationResolved as k, type TokenizeWithThemeOptions as l, type ShikiTransformerContextCommon as m, type CodeToHastRenderOptions as n, type RootContent as o, type ThemeRegistrationAny as p, type IRawGrammar as q, type SpecialLanguage as r, type Awaitable as s, type MaybeGetter as t, type MaybeModule as u, type StringLiteralUnion as v, type ResolveBundleKey as w, type LanguageRegistration as x, type CodeToHastOptionsCommon as y, type CodeOptionsSingleTheme as z };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { H as HighlighterGeneric, a as HighlighterCoreOptions, B as BundledHighlighterOptions, L as LanguageInput, T as ThemeInput, C as CodeToHastOptions, R as Root, b as RequireKeys, c as CodeToThemedTokensOptions, d as ThemedToken, e as CodeToTokensWithThemesOptions, f as ThemedTokenWithVariants, M as MaybeArray, S as SpecialTheme, E as Element, g as ShikiInternal, I as IGrammar, h as ThemeRegistrationResolved, i as TokenizeWithThemeOptions, j as ShikiTransformerContextCommon, k as HtmlRendererOptions, N as Nodes$1, l as RootContent$1, m as ThemeRegistrationAny } from './chunk-types.mjs';
2
- export { A as AnsiLanguage, p as Awaitable, $ as BundledLanguageInfo, a0 as BundledThemeInfo, z as CodeOptionsMeta, x as CodeOptionsMultipleThemes, w as CodeOptionsSingleTheme, y as CodeOptionsThemes, v as CodeToHastOptionsCommon, Z as DynamicImportLanguageRegistration, _ as DynamicImportThemeRegistration, F as FontStyle, U as HtmlRendererOptionsCommon, n as IRawGrammar, u as LanguageRegistration, q as MaybeGetter, r as MaybeModule, P as PlainTextLanguage, n as RawGrammar, t as ResolveBundleKey, Q as ShikiTransformer, O as ShikiTransformerContext, K as ShikiTransformerContextMeta, a1 as ShikijiTransformer, a2 as ShikijiTransformerContext, a3 as ShikijiTransformerContextCommon, o as SpecialLanguage, s as StringLiteralUnion, J as ThemeRegistration, G as ThemeRegistrationRaw, W as ThemedTokenExplanation, V as ThemedTokenScopeExplanation, X as TokenBase, Y as TokenStyles, D as TransformerOptions } from './chunk-types.mjs';
1
+ import { H as HighlighterGeneric, a as HighlighterCoreOptions, B as BundledHighlighterOptions, L as LanguageInput, T as ThemeInput, C as CodeToHastOptions, R as Root, b as CodeToTokensOptions, c as TokensResult, d as RequireKeys, e as CodeToTokensBaseOptions, f as ThemedToken, g as CodeToTokensWithThemesOptions, h as ThemedTokenWithVariants, M as MaybeArray, S as SpecialTheme, E as Element, i as TokenStyles, j as ShikiInternal, I as IGrammar, k as ThemeRegistrationResolved, l as TokenizeWithThemeOptions, m as ShikiTransformerContextCommon, n as CodeToHastRenderOptions, N as Nodes$1, o as RootContent$1, p as ThemeRegistrationAny } from './chunk-types.mjs';
2
+ export { A as AnsiLanguage, s as Awaitable, a1 as BundledLanguageInfo, a2 as BundledThemeInfo, J as CodeOptionsMeta, D as CodeOptionsMultipleThemes, z as CodeOptionsSingleTheme, G as CodeOptionsThemes, y as CodeToHastOptionsCommon, X as CodeToHastRenderOptionsCommon, $ as DynamicImportLanguageRegistration, a0 as DynamicImportThemeRegistration, F as FontStyle, q as IRawGrammar, x as LanguageRegistration, t as MaybeGetter, u as MaybeModule, P as PlainTextLanguage, q as RawGrammar, w as ResolveBundleKey, W as ShikiTransformer, V as ShikiTransformerContext, U as ShikiTransformerContextMeta, a3 as ShikijiTransformer, a4 as ShikijiTransformerContext, a5 as ShikijiTransformerContextCommon, r as SpecialLanguage, v as StringLiteralUnion, Q as ThemeRegistration, O as ThemeRegistrationRaw, Z as ThemedTokenExplanation, Y as ThemedTokenScopeExplanation, _ as TokenBase, K as TransformerOptions } from './chunk-types.mjs';
3
3
  import { L as LoadWasmOptions } from './chunk-index.mjs';
4
4
  export { l as loadWasm } from './chunk-index.mjs';
5
5
 
@@ -37,12 +37,19 @@ interface ShorthandsBundle<L extends string, T extends string> {
37
37
  */
38
38
  codeToHast(code: string, options: CodeToHastOptions<L, T>): Promise<Root>;
39
39
  /**
40
- * Shorthand for `codeToThemedTokens` with auto-loaded theme and language.
40
+ * Shorthand for `codeToTokens` with auto-loaded theme and language.
41
41
  * A singleton highlighter it maintained internally.
42
42
  *
43
- * Differences from `highlighter.codeToThemedTokens()`, this function is async.
43
+ * Differences from `highlighter.codeToTokens()`, this function is async.
44
44
  */
45
- codeToThemedTokens(code: string, options: RequireKeys<CodeToThemedTokensOptions<L, T>, 'theme' | 'lang'>): Promise<ThemedToken[][]>;
45
+ codeToTokens(code: string, options: CodeToTokensOptions<L, T>): Promise<TokensResult>;
46
+ /**
47
+ * Shorthand for `codeToTokensBase` with auto-loaded theme and language.
48
+ * A singleton highlighter it maintained internally.
49
+ *
50
+ * Differences from `highlighter.codeToTokensBase()`, this function is async.
51
+ */
52
+ codeToTokensBase(code: string, options: RequireKeys<CodeToTokensBaseOptions<L, T>, 'theme' | 'lang'>): Promise<ThemedToken[][]>;
46
53
  /**
47
54
  * Shorthand for `codeToTokensWithThemes` with auto-loaded theme and language.
48
55
  * A singleton highlighter it maintained internally.
@@ -56,6 +63,10 @@ interface ShorthandsBundle<L extends string, T extends string> {
56
63
  * @internal
57
64
  */
58
65
  getSingletonHighlighter(): Promise<HighlighterGeneric<L, T>>;
66
+ /**
67
+ * @deprecated Use `codeToTokensBase` instead.
68
+ */
69
+ codeToThemedTokens(code: string, options: RequireKeys<CodeToTokensBaseOptions<L, T>, 'theme' | 'lang'>): Promise<ThemedToken[][]>;
59
70
  }
60
71
  declare function createSingletonShorthands<L extends string, T extends string>(getHighlighter: GetHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
61
72
 
@@ -93,7 +104,7 @@ declare function isSpecialTheme(theme: string | ThemeInput | null | undefined):
93
104
  *
94
105
  * If the `property.class` is a string, it will be splitted by space and converted to an array.
95
106
  */
96
- declare function addClassToHast(node: Element, className: string | string[]): void;
107
+ declare function addClassToHast(node: Element, className: string | string[]): Element;
97
108
  /**
98
109
  * Split a token into multiple tokens by given offsets.
99
110
  *
@@ -105,6 +116,8 @@ declare function applyColorReplacements(color: string, replacements?: Record<str
105
116
  * @deprecated Use `isPlainLang` instead.
106
117
  */
107
118
  declare const isPlaintext: typeof isPlainLang;
119
+ declare function getTokenStyleObject(token: TokenStyles): Record<string, string>;
120
+ declare function stringifyTokenStyle(token: Record<string, string>): string;
108
121
 
109
122
  /**
110
123
  * Set the default wasm loader for `loadWasm`.
@@ -116,13 +129,23 @@ declare function setDefaultWasmLoader(_loader: LoadWasmOptions): void;
116
129
  */
117
130
  declare function getShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
118
131
 
119
- declare function codeToThemedTokens(internal: ShikiInternal, code: string, options?: CodeToThemedTokensOptions): ThemedToken[][];
132
+ /**
133
+ * Code to tokens, with a simple theme.
134
+ */
135
+ declare function codeToTokensBase(internal: ShikiInternal, code: string, options?: CodeToTokensBaseOptions): ThemedToken[][];
120
136
  declare function tokenizeWithTheme(code: string, grammar: IGrammar, theme: ThemeRegistrationResolved, colorMap: string[], options: TokenizeWithThemeOptions): ThemedToken[][];
121
137
 
138
+ /**
139
+ * High-level code-to-tokens API.
140
+ *
141
+ * It will use `codeToTokensWithThemes` or `codeToTokensBase` based on the options.
142
+ */
143
+ declare function codeToTokens(internal: ShikiInternal, code: string, options: CodeToTokensOptions): TokensResult;
144
+
122
145
  declare function tokenizeAnsiWithTheme(theme: ThemeRegistrationResolved, fileContents: string, options?: TokenizeWithThemeOptions): ThemedToken[][];
123
146
 
124
147
  declare function codeToHast(internal: ShikiInternal, code: string, options: CodeToHastOptions, transformerContext?: ShikiTransformerContextCommon): Root;
125
- declare function tokensToHast(tokens: ThemedToken[][], options: HtmlRendererOptions, transformerContext: ShikiTransformerContextCommon): Root;
148
+ declare function tokensToHast(tokens: ThemedToken[][], options: CodeToHastRenderOptions, transformerContext: ShikiTransformerContextCommon): Root;
126
149
 
127
150
  type FormatSmartOptions = {
128
151
  /**
@@ -361,4 +384,4 @@ declare function codeToTokensWithThemes(internal: ShikiInternal, code: string, o
361
384
  */
362
385
  declare function normalizeTheme(rawTheme: ThemeRegistrationAny): ThemeRegistrationResolved;
363
386
 
364
- export { BundledHighlighterOptions, CodeToHastOptions, CodeToThemedTokensOptions, CodeToTokensWithThemesOptions, type GetHighlighterFactory, IGrammar as Grammar, type HighlighterCore, HighlighterCoreOptions, HighlighterGeneric, HtmlRendererOptions, IGrammar, LanguageInput, MaybeArray, RequireKeys, ShikiInternal, ShikiTransformerContextCommon, type ShorthandsBundle, SpecialTheme, ThemeInput, ThemeRegistrationAny, ThemeRegistrationResolved, ThemedToken, ThemedTokenWithVariants, TokenizeWithThemeOptions, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToThemedTokens, codeToTokensWithThemes, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, toHtml as hastToHtml, isNoneTheme, isPlainLang, isPlaintext, isSpecialLang, isSpecialTheme, normalizeTheme, setDefaultWasmLoader, splitLines, splitToken, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast };
387
+ export { BundledHighlighterOptions, CodeToHastOptions, CodeToHastRenderOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type GetHighlighterFactory, IGrammar as Grammar, type HighlighterCore, HighlighterCoreOptions, HighlighterGeneric, IGrammar, LanguageInput, MaybeArray, RequireKeys, ShikiInternal, ShikiTransformerContextCommon, type ShorthandsBundle, SpecialTheme, ThemeInput, ThemeRegistrationAny, ThemeRegistrationResolved, ThemedToken, ThemedTokenWithVariants, TokenStyles, TokenizeWithThemeOptions, TokensResult, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isPlaintext, isSpecialLang, isSpecialTheme, normalizeTheme, setDefaultWasmLoader, splitLines, splitToken, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast };
package/dist/index.mjs CHANGED
@@ -49,7 +49,7 @@ function isSpecialTheme(theme) {
49
49
  */
50
50
  function addClassToHast(node, className) {
51
51
  if (!className)
52
- return;
52
+ return node;
53
53
  node.properties ||= {};
54
54
  node.properties.class ||= [];
55
55
  if (typeof node.properties.class === 'string')
@@ -61,6 +61,7 @@ function addClassToHast(node, className) {
61
61
  if (c && !node.properties.class.includes(c))
62
62
  node.properties.class.push(c);
63
63
  }
64
+ return node;
64
65
  }
65
66
  /**
66
67
  * Split a token into multiple tokens by given offsets.
@@ -96,6 +97,25 @@ function applyColorReplacements(color, replacements) {
96
97
  * @deprecated Use `isPlainLang` instead.
97
98
  */
98
99
  const isPlaintext = isPlainLang;
100
+ function getTokenStyleObject(token) {
101
+ const styles = {};
102
+ if (token.color)
103
+ styles.color = token.color;
104
+ if (token.bgColor)
105
+ styles['background-color'] = token.bgColor;
106
+ if (token.fontStyle) {
107
+ if (token.fontStyle & FontStyle.Italic)
108
+ styles['font-style'] = 'italic';
109
+ if (token.fontStyle & FontStyle.Bold)
110
+ styles['font-weight'] = 'bold';
111
+ if (token.fontStyle & FontStyle.Underline)
112
+ styles['text-decoration'] = 'underline';
113
+ }
114
+ return styles;
115
+ }
116
+ function stringifyTokenStyle(token) {
117
+ return Object.entries(token).map(([key, value]) => `${key}:${value}`).join(';');
118
+ }
99
119
 
100
120
  // src/colors.ts
101
121
  var namedColors = [
@@ -430,7 +450,10 @@ function dimColor(color) {
430
450
  return color;
431
451
  }
432
452
 
433
- function codeToThemedTokens(internal, code, options = {}) {
453
+ /**
454
+ * Code to tokens, with a simple theme.
455
+ */
456
+ function codeToTokensBase(internal, code, options = {}) {
434
457
  const { lang = 'text', theme: themeName = internal.getLoadedThemes()[0], } = options;
435
458
  if (isPlainLang(lang) || isNoneTheme(themeName))
436
459
  return splitLines(code).map(line => [{ content: line[0], offset: line[1] }]);
@@ -570,7 +593,7 @@ function codeToTokensWithThemes(internal, code, options) {
570
593
  const themes = Object.entries(options.themes)
571
594
  .filter(i => i[1])
572
595
  .map(i => ({ color: i[0], theme: i[1] }));
573
- const tokens = syncThemesTokenization(...themes.map(t => codeToThemedTokens(internal, code, {
596
+ const tokens = syncThemesTokenization(...themes.map(t => codeToTokensBase(internal, code, {
574
597
  ...options,
575
598
  theme: t.theme,
576
599
  })));
@@ -638,14 +661,12 @@ function syncThemesTokenization(...themes) {
638
661
  return outThemes;
639
662
  }
640
663
 
641
- function codeToHast(internal, code, options, transformerContext = {
642
- meta: {},
643
- options,
644
- codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
645
- }) {
646
- let input = code;
647
- for (const transformer of options.transformers || [])
648
- input = transformer.preprocess?.call(transformerContext, input, options) || input;
664
+ /**
665
+ * High-level code-to-tokens API.
666
+ *
667
+ * It will use `codeToTokensWithThemes` or `codeToTokensBase` based on the options.
668
+ */
669
+ function codeToTokens(internal, code, options) {
649
670
  let bg;
650
671
  let fg;
651
672
  let tokens;
@@ -659,7 +680,7 @@ function codeToHast(internal, code, options, transformerContext = {
659
680
  .sort((a, b) => a.color === defaultColor ? -1 : b.color === defaultColor ? 1 : 0);
660
681
  if (themes.length === 0)
661
682
  throw new Error('[shiki] `themes` option must not be empty');
662
- const themeTokens = codeToTokensWithThemes(internal, input, options);
683
+ const themeTokens = codeToTokensWithThemes(internal, code, options);
663
684
  if (defaultColor && !themes.find(t => t.color === defaultColor))
664
685
  throw new Error(`[shiki] \`themes\` option must contain the defaultColor key \`${defaultColor}\``);
665
686
  const themeRegs = themes.map(t => internal.getTheme(t.theme));
@@ -672,7 +693,7 @@ function codeToHast(internal, code, options, transformerContext = {
672
693
  rootStyle = defaultColor ? undefined : [fg, bg].join(';');
673
694
  }
674
695
  else if ('theme' in options) {
675
- tokens = codeToThemedTokens(internal, input, options);
696
+ tokens = codeToTokensBase(internal, code, options);
676
697
  const _theme = internal.getTheme(options.theme);
677
698
  bg = _theme.bg;
678
699
  fg = _theme.fg;
@@ -681,20 +702,13 @@ function codeToHast(internal, code, options, transformerContext = {
681
702
  else {
682
703
  throw new Error('[shiki] Invalid options, either `theme` or `themes` must be provided');
683
704
  }
684
- const { mergeWhitespaces = true, } = options;
685
- if (mergeWhitespaces === true)
686
- tokens = mergeWhitespaceTokens(tokens);
687
- else if (mergeWhitespaces === 'never')
688
- tokens = splitWhitespaceTokens(tokens);
689
- for (const transformer of options.transformers || [])
690
- tokens = transformer.tokens?.call(transformerContext, tokens) || tokens;
691
- return tokensToHast(tokens, {
692
- ...options,
705
+ return {
706
+ tokens,
693
707
  fg,
694
708
  bg,
695
709
  themeName,
696
710
  rootStyle,
697
- }, transformerContext);
711
+ };
698
712
  }
699
713
  function mergeToken(merged, variantsOrder, cssVariablePrefix, defaultColor) {
700
714
  const token = {
@@ -727,6 +741,32 @@ function mergeToken(merged, variantsOrder, cssVariablePrefix, defaultColor) {
727
741
  : Object.values(mergedStyles).join(';');
728
742
  return token;
729
743
  }
744
+
745
+ function codeToHast(internal, code, options, transformerContext = {
746
+ meta: {},
747
+ options,
748
+ codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
749
+ codeToTokens: (_code, _options) => codeToTokens(internal, _code, _options),
750
+ }) {
751
+ let input = code;
752
+ for (const transformer of options.transformers || [])
753
+ input = transformer.preprocess?.call(transformerContext, input, options) || input;
754
+ let { tokens, fg, bg, themeName, rootStyle, } = codeToTokens(internal, input, options);
755
+ const { mergeWhitespaces = true, } = options;
756
+ if (mergeWhitespaces === true)
757
+ tokens = mergeWhitespaceTokens(tokens);
758
+ else if (mergeWhitespaces === 'never')
759
+ tokens = splitWhitespaceTokens(tokens);
760
+ for (const transformer of options.transformers || [])
761
+ tokens = transformer.tokens?.call(transformerContext, tokens) || tokens;
762
+ return tokensToHast(tokens, {
763
+ ...options,
764
+ fg,
765
+ bg,
766
+ themeName,
767
+ rootStyle,
768
+ }, transformerContext);
769
+ }
730
770
  function tokensToHast(tokens, options, transformerContext) {
731
771
  const { transformers = [], } = options;
732
772
  const lines = [];
@@ -755,6 +795,7 @@ function tokensToHast(tokens, options, transformerContext) {
755
795
  const lineNodes = [];
756
796
  const context = {
757
797
  ...transformerContext,
798
+ addClassToHast,
758
799
  get tokens() {
759
800
  return tokens;
760
801
  },
@@ -815,25 +856,6 @@ function tokensToHast(tokens, options, transformerContext) {
815
856
  result = transformer?.root?.call(context, result) || result;
816
857
  return result;
817
858
  }
818
- function getTokenStyleObject(token) {
819
- const styles = {};
820
- if (token.color)
821
- styles.color = token.color;
822
- if (token.bgColor)
823
- styles['background-color'] = token.bgColor;
824
- if (token.fontStyle) {
825
- if (token.fontStyle & FontStyle.Italic)
826
- styles['font-style'] = 'italic';
827
- if (token.fontStyle & FontStyle.Bold)
828
- styles['font-weight'] = 'bold';
829
- if (token.fontStyle & FontStyle.Underline)
830
- styles['text-decoration'] = 'underline';
831
- }
832
- return styles;
833
- }
834
- function stringifyTokenStyle(token) {
835
- return Object.entries(token).map(([key, value]) => `${key}:${value}`).join(';');
836
- }
837
859
  function mergeWhitespaceTokens(tokens) {
838
860
  return tokens.map((line) => {
839
861
  const newLine = [];
@@ -4277,6 +4299,7 @@ function codeToHtml(internal, code, options) {
4277
4299
  meta: {},
4278
4300
  options,
4279
4301
  codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
4302
+ codeToTokens: (_code, _options) => codeToTokens(internal, _code, _options),
4280
4303
  };
4281
4304
  let result = toHtml(codeToHast(internal, code, options, context));
4282
4305
  for (const transformer of options.transformers || [])
@@ -5073,8 +5096,9 @@ async function getShikiInternal(options = {}) {
5073
5096
  async function getHighlighterCore(options = {}) {
5074
5097
  const internal = await getShikiInternal(options);
5075
5098
  return {
5076
- codeToThemedTokens: (code, options) => codeToThemedTokens(internal, code, options),
5099
+ codeToTokensBase: (code, options) => codeToTokensBase(internal, code, options),
5077
5100
  codeToTokensWithThemes: (code, options) => codeToTokensWithThemes(internal, code, options),
5101
+ codeToTokens: (code, options) => codeToTokens(internal, code, options),
5078
5102
  codeToHast: (code, options) => codeToHast(internal, code, options),
5079
5103
  codeToHtml: (code, options) => codeToHtml(internal, code, options),
5080
5104
  loadLanguage: internal.loadLanguage,
@@ -5085,6 +5109,8 @@ async function getHighlighterCore(options = {}) {
5085
5109
  getLoadedThemes: internal.getLoadedThemes,
5086
5110
  getLoadedLanguages: internal.getLoadedLanguages,
5087
5111
  getInternalContext: () => internal,
5112
+ /** @deprecated */
5113
+ codeToThemedTokens: (code, options) => codeToTokensBase(internal, code, options),
5088
5114
  };
5089
5115
  }
5090
5116
 
@@ -5175,6 +5201,20 @@ function createSingletonShorthands(getHighlighter) {
5175
5201
  });
5176
5202
  return shiki.codeToHast(code, options);
5177
5203
  },
5204
+ async codeToTokens(code, options) {
5205
+ const shiki = await _getHighlighter({
5206
+ lang: options.lang,
5207
+ theme: ('theme' in options ? [options.theme] : Object.values(options.themes)),
5208
+ });
5209
+ return shiki.codeToTokens(code, options);
5210
+ },
5211
+ async codeToTokensBase(code, options) {
5212
+ const shiki = await _getHighlighter(options);
5213
+ return shiki.codeToTokensBase(code, options);
5214
+ },
5215
+ /**
5216
+ * @deprecated Use `codeToTokensBase` instead.
5217
+ */
5178
5218
  async codeToThemedTokens(code, options) {
5179
5219
  const shiki = await _getHighlighter(options);
5180
5220
  return shiki.codeToThemedTokens(code, options);
@@ -5189,4 +5229,4 @@ function createSingletonShorthands(getHighlighter) {
5189
5229
  };
5190
5230
  }
5191
5231
 
5192
- export { FontStyle, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToThemedTokens, codeToTokensWithThemes, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, toHtml as hastToHtml, isNoneTheme, isPlainLang, isPlaintext, isSpecialLang, isSpecialTheme, loadWasm, normalizeTheme, setDefaultWasmLoader, splitLines, splitToken, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast };
5232
+ export { FontStyle, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isPlaintext, isSpecialLang, isSpecialTheme, loadWasm, normalizeTheme, setDefaultWasmLoader, splitLines, splitToken, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast };
@@ -1,5 +1,5 @@
1
1
  import { F as FontStyle } from './chunk-types.mjs';
2
- export { I as IGrammar, a8 as IGrammarConfiguration, a5 as INITIAL, a9 as IOnigLib, n as IRawGrammar, a7 as IRawTheme, ab as IRawThemeSetting, a4 as Registry, aa as RegistryOptions, a6 as StateStack } from './chunk-types.mjs';
2
+ export { I as IGrammar, aa as IGrammarConfiguration, a7 as INITIAL, ab as IOnigLib, q as IRawGrammar, a9 as IRawTheme, ad as IRawThemeSetting, a6 as Registry, ac as RegistryOptions, a8 as StateStack } from './chunk-types.mjs';
3
3
  import './chunk-index.mjs';
4
4
 
5
5
  declare const enum TemporaryStandardTokenType {
package/dist/types.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export { A as AnsiLanguage, p as Awaitable, B as BundledHighlighterOptions, $ as BundledLanguageInfo, a0 as BundledThemeInfo, z as CodeOptionsMeta, x as CodeOptionsMultipleThemes, w as CodeOptionsSingleTheme, y as CodeOptionsThemes, C as CodeToHastOptions, v as CodeToHastOptionsCommon, c as CodeToThemedTokensOptions, e as CodeToTokensWithThemesOptions, Z as DynamicImportLanguageRegistration, _ as DynamicImportThemeRegistration, F as FontStyle, I as Grammar, a as HighlighterCoreOptions, H as HighlighterGeneric, k as HtmlRendererOptions, U as HtmlRendererOptionsCommon, I as IGrammar, n as IRawGrammar, L as LanguageInput, u as LanguageRegistration, M as MaybeArray, q as MaybeGetter, r as MaybeModule, P as PlainTextLanguage, n as RawGrammar, b as RequireKeys, t as ResolveBundleKey, g as ShikiInternal, Q as ShikiTransformer, O as ShikiTransformerContext, j as ShikiTransformerContextCommon, K as ShikiTransformerContextMeta, a1 as ShikijiTransformer, a2 as ShikijiTransformerContext, a3 as ShikijiTransformerContextCommon, o as SpecialLanguage, S as SpecialTheme, s as StringLiteralUnion, T as ThemeInput, J as ThemeRegistration, m as ThemeRegistrationAny, G as ThemeRegistrationRaw, h as ThemeRegistrationResolved, d as ThemedToken, W as ThemedTokenExplanation, V as ThemedTokenScopeExplanation, f as ThemedTokenWithVariants, X as TokenBase, Y as TokenStyles, i as TokenizeWithThemeOptions, D as TransformerOptions } from './chunk-types.mjs';
1
+ export { A as AnsiLanguage, s as Awaitable, B as BundledHighlighterOptions, a1 as BundledLanguageInfo, a2 as BundledThemeInfo, J as CodeOptionsMeta, D as CodeOptionsMultipleThemes, z as CodeOptionsSingleTheme, G as CodeOptionsThemes, C as CodeToHastOptions, y as CodeToHastOptionsCommon, n as CodeToHastRenderOptions, X as CodeToHastRenderOptionsCommon, e as CodeToTokensBaseOptions, b as CodeToTokensOptions, g as CodeToTokensWithThemesOptions, $ as DynamicImportLanguageRegistration, a0 as DynamicImportThemeRegistration, F as FontStyle, I as Grammar, a as HighlighterCoreOptions, H as HighlighterGeneric, I as IGrammar, q as IRawGrammar, L as LanguageInput, x as LanguageRegistration, M as MaybeArray, t as MaybeGetter, u as MaybeModule, P as PlainTextLanguage, q as RawGrammar, d as RequireKeys, w as ResolveBundleKey, j as ShikiInternal, W as ShikiTransformer, V as ShikiTransformerContext, m as ShikiTransformerContextCommon, U as ShikiTransformerContextMeta, a3 as ShikijiTransformer, a4 as ShikijiTransformerContext, a5 as ShikijiTransformerContextCommon, r as SpecialLanguage, S as SpecialTheme, v as StringLiteralUnion, T as ThemeInput, Q as ThemeRegistration, p as ThemeRegistrationAny, O as ThemeRegistrationRaw, k as ThemeRegistrationResolved, f as ThemedToken, Z as ThemedTokenExplanation, Y as ThemedTokenScopeExplanation, h as ThemedTokenWithVariants, _ as TokenBase, i as TokenStyles, l as TokenizeWithThemeOptions, c as TokensResult, K as TransformerOptions } from './chunk-types.mjs';
2
2
  import './chunk-index.mjs';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.1",
4
+ "version": "1.0.0-beta.2",
5
5
  "description": "Core of Shiki",
6
6
  "author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",