import { Element, Root } from "hast"; import { FontStyle, IGrammar, IRawGrammar as RawGrammar, IRawTheme as RawTheme, IRawThemeSetting, IRawThemeSetting as RawThemeSetting, OnigScanner, OnigString, StateStack } from "@shikijs/vscode-textmate"; //#region src/utils.d.ts export type Awaitable = T | Promise; export type MaybeGetter = Awaitable> | (() => Awaitable>); export type MaybeModule = T | { default: T; }; export type MaybeArray = T | T[]; export type RequireKeys = Omit & Required>; interface Nothing {} /** * type StringLiteralUnion<'foo'> = 'foo' | string * This has auto completion whereas `'foo' | string` doesn't * Adapted from https://github.com/microsoft/TypeScript/issues/29729 */ export type StringLiteralUnion = T | (U & Nothing); //#endregion //#region src/engines.d.ts export interface PatternScanner extends OnigScanner {} export interface RegexEngineString extends OnigString {} /** * Engine for RegExp matching and scanning. */ export interface RegexEngine { createScanner: (patterns: (string | RegExp)[]) => PatternScanner; createString: (s: string) => RegexEngineString; } export interface WebAssemblyInstantiator { (importObject: Record> | undefined): Promise; } export type WebAssemblyInstance = WebAssembly.WebAssemblyInstantiatedSource | WebAssembly.Instance | WebAssembly.Instance['exports']; export type OnigurumaLoadOptions = { instantiator: WebAssemblyInstantiator; } | { default: WebAssemblyInstantiator; } | { data: ArrayBufferView | ArrayBuffer | Response; }; export type LoadWasmOptionsPlain = OnigurumaLoadOptions | WebAssemblyInstantiator | ArrayBufferView | ArrayBuffer | Response; export type LoadWasmOptions = Awaitable | (() => Awaitable); //#endregion //#region src/textmate.d.ts export interface Grammar extends IGrammar { name: string; } //#endregion //#region src/langs.d.ts export type PlainTextLanguage = 'text' | 'plaintext' | 'txt' | 'plain'; export type AnsiLanguage = 'ansi'; export type SpecialLanguage = PlainTextLanguage | AnsiLanguage; export type LanguageInput = MaybeGetter>; export type ResolveBundleKey = [T] extends [never] ? string : T; export interface LanguageRegistration extends RawGrammar { name: string; scopeName: string; displayName?: string; aliases?: string[]; /** * A list of languages the current language embeds. * If manually specifying languages to load, make sure to load the embedded * languages for each parent language. */ embeddedLangs?: string[]; embeddedLanguages?: string[]; /** * A list of languages that embed the current language. * Unlike `embeddedLangs`, the embedded languages will not be loaded automatically. */ embeddedLangsLazy?: string[]; balancedBracketSelectors?: string[]; unbalancedBracketSelectors?: string[]; foldingStopMarker?: string; foldingStartMarker?: string; /** * Inject this language to other scopes. * Same as `injectTo` in VSCode's `contributes.grammars`. * * @see https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#injection-grammars */ injectTo?: string[]; } export interface BundledLanguageInfo { id: string; name: string; import: DynamicImportLanguageRegistration; aliases?: string[]; } export type DynamicImportLanguageRegistration = () => Promise<{ default: LanguageRegistration[]; }>; //#endregion //#region src/decorations.d.ts export interface DecorationOptions { /** * Custom decorations to wrap highlighted tokens with. */ decorations?: DecorationItem[]; } export interface DecorationItem { /** * Start offset or position of the decoration. */ start: OffsetOrPosition; /** * End offset or position of the decoration. */ end: OffsetOrPosition; /** * Tag name of the element to create. * @default 'span' */ tagName?: string; /** * Properties of the element to create. */ properties?: Element['properties']; /** * A custom function to transform the element after it has been created. */ transform?: (element: Element, type: DecorationTransformType) => Element | void; /** * By default when the decoration contains only one token, the decoration will be applied to the token. * * Set to `true` to always wrap the token with a new element * * @default false */ alwaysWrap?: boolean; } export interface ResolvedDecorationItem extends Omit { start: ResolvedPosition; end: ResolvedPosition; } export type DecorationTransformType = 'wrapper' | 'line' | 'token'; export interface Position { line: number; character: number; } export type Offset = number; export type OffsetOrPosition = Position | Offset; export interface ResolvedPosition extends Position { offset: Offset; } //#endregion //#region src/themes.d.ts export type SpecialTheme = 'none'; export type ThemeInput = MaybeGetter; export interface ThemeRegistrationRaw extends RawTheme, Partial> {} export interface ThemeRegistration extends Partial {} export interface ThemeRegistrationResolved extends RawTheme { /** * Theme name */ name: string; /** * Display name * * @field shiki custom property */ displayName?: string; /** * Light/dark theme * * @field shiki custom property */ type: 'light' | 'dark'; /** * Token rules */ settings: RawThemeSetting[]; /** * Same as `settings`, will use as fallback if `settings` is not present. */ tokenColors?: RawThemeSetting[]; /** * Default foreground color * * @field shiki custom property */ fg: string; /** * Background color * * @field shiki custom property */ bg: string; /** * A map of color names to new color values. * * The color key starts with '#' and should be lowercased. * * @field shiki custom property */ colorReplacements?: Record; /** * Color map of VS Code options * * Will be used by shiki on `lang: 'ansi'` to find ANSI colors, and to find the default foreground/background colors. */ colors?: Record; /** * JSON schema path * * @field not used by shiki */ $schema?: string; /** * Enable semantic highlighting * * @field not used by shiki */ semanticHighlighting?: boolean; /** * Tokens for semantic highlighting * * @field not used by shiki */ semanticTokenColors?: Record; } export type ThemeRegistrationAny = ThemeRegistrationRaw | ThemeRegistration | ThemeRegistrationResolved; export type DynamicImportThemeRegistration = () => Promise<{ default: ThemeRegistration; }>; export interface BundledThemeInfo { id: string; displayName: string; type: 'light' | 'dark'; import: DynamicImportThemeRegistration; } //#endregion //#region src/tokens.d.ts /** * GrammarState is a special reference object that holds the state of a grammar. * * It's used to highlight code snippets that are part of the target language. */ export interface GrammarState { readonly lang: string; readonly theme: string; readonly themes: string[]; /** * @internal */ getInternalStack: (theme?: string) => StateStack | undefined; getScopes: (theme?: string) => string[] | undefined; } export interface CodeToTokensBaseOptions extends TokenizeWithThemeOptions { lang?: Languages | SpecialLanguage; theme?: Themes | ThemeRegistrationAny | SpecialTheme; } export type CodeToTokensOptions = Omit, 'theme'> & CodeOptionsThemes; export interface CodeToTokensWithThemesOptions extends TokenizeWithThemeOptions { lang?: Languages | SpecialLanguage; /** * A map of color names to themes. * * `light` and `dark` are required, and arbitrary color names can be added. * * @example * ```ts * themes: { * light: 'vitesse-light', * dark: 'vitesse-dark', * soft: 'nord', * // custom colors * } * ``` */ themes: Partial>; } export interface ThemedTokenScopeExplanation { scopeName: string; themeMatches?: IRawThemeSetting[]; } export interface ThemedTokenExplanation { content: string; scopes: ThemedTokenScopeExplanation[]; } /** * A single token with color, and optionally with explanation. * * For example: * * ```json * { * "content": "shiki", * "color": "#D8DEE9", * "explanation": [ * { * "content": "shiki", * "scopes": [ * { * "scopeName": "source.js", * "themeMatches": [] * }, * { * "scopeName": "meta.objectliteral.js", * "themeMatches": [] * }, * { * "scopeName": "meta.object.member.js", * "themeMatches": [] * }, * { * "scopeName": "meta.array.literal.js", * "themeMatches": [] * }, * { * "scopeName": "variable.other.object.js", * "themeMatches": [ * { * "name": "Variable", * "scope": "variable.other", * "settings": { * "foreground": "#D8DEE9" * } * }, * { * "name": "[JavaScript] Variable Other Object", * "scope": "source.js variable.other.object", * "settings": { * "foreground": "#D8DEE9" * } * } * ] * } * ] * } * ] * } * ``` */ export interface ThemedToken extends TokenStyles, TokenBase {} export interface TokenBase { /** * The content of the token */ content: string; /** * The start offset of the token, relative to the input code. 0-indexed. */ offset: number; /** * The type of the token * 0 - other * 1 - comment * 2 - string * 3 - regex */ type?: number; /** * Explanation of * * - token text's matching scopes * - reason that token text is given a color (one matching scope matches a rule (scope -> color) in the theme) */ explanation?: ThemedTokenExplanation[]; } export interface TokenStyles { /** * 6 or 8 digit hex code representation of the token's color */ color?: string; /** * 6 or 8 digit hex code representation of the token's background color */ bgColor?: string; /** * Font style of token. Can be None/Italic/Bold/Underline */ fontStyle?: FontStyle; /** * Override with custom inline style for HTML renderer. * When specified, `color` and `fontStyle` will be ignored. * Prefer use object style for merging with other styles. */ htmlStyle?: Record; /** * Extra HTML attributes for the token. */ htmlAttrs?: Record; } export interface ThemedTokenWithVariants extends TokenBase { /** * An object of color name to token styles */ variants: Record; } export interface TokenizeWithThemeOptions { /** * Include explanation of why a token is given a color. * * You can optionally pass `scopeName` to only include explanation for scopes, * which is more performant than full explanation. * * @default false */ includeExplanation?: boolean | 'scopeName' | 'tokenType'; /** * A map of color names to new color values. * * The color key starts with '#' and should be lowercased. * * This will be merged with theme's `colorReplacements` if any. */ colorReplacements?: Record>; /** * Lines above this length will not be tokenized for performance reasons. * * @default 0 (no limit) */ tokenizeMaxLineLength?: number; /** * Time limit in milliseconds for tokenizing a single line. * * @default 500 (0.5s) */ tokenizeTimeLimit?: number; /** * Represent the state of the grammar, allowing to continue tokenizing from a intermediate grammar state. * * You can get the grammar state from `getLastGrammarState`. */ grammarState?: GrammarState; /** * The code context of the grammar. * Consider it a prepended code to the input code, that only participate the grammar inference but not presented in the final output. * * This will be ignored if `grammarState` is provided. */ grammarContextCode?: string; } /** * Result of `codeToTokens`, an object with 2D array of tokens and meta info like background and foreground color. */ export interface TokensResult { /** * 2D array of tokens, first dimension is lines, second dimension is tokens in a line. */ tokens: ThemedToken[][]; /** * Foreground color of the code. */ fg?: string; /** * Background color of the code. */ bg?: string; /** * A string representation of themes applied to the token. */ themeName?: string; /** * Custom style string to be applied to the root `
` element.
   * When specified, `fg` and `bg` will be ignored.
   */
  rootStyle?: string | false;
  /**
   * The last grammar state of the code snippet.
   */
  grammarState?: GrammarState;
}
//#endregion
//#region src/transformers.d.ts
export interface TransformerOptions {
  /**
   * Transformers for the Shiki pipeline.
   */
  transformers?: ShikiTransformer[];
}
export interface ShikiTransformerContextMeta {}
/**
 * Common transformer context for all transformers hooks
 */
export interface ShikiTransformerContextCommon {
  meta: ShikiTransformerContextMeta;
  options: CodeToHastOptions;
  codeToHast: (code: string, options: CodeToHastOptions) => Root;
  codeToTokens: (code: string, options: CodeToTokensOptions) => TokensResult;
}
export interface ShikiTransformerContextSource extends ShikiTransformerContextCommon {
  readonly source: string;
}
/**
 * Transformer context for HAST related hooks
 */
export interface ShikiTransformerContext extends ShikiTransformerContextSource {
  readonly tokens: ThemedToken[][];
  readonly root: Root;
  readonly pre: Element;
  readonly code: Element;
  readonly lines: Element[];
  readonly structure: CodeToHastOptions['structure'];
  /**
   * Utility to append class to a hast node
   *
   * If the `property.class` is a string, it will be splitted by space and converted to an array.
   */
  addClassToHast: (hast: Element, className: string | string[]) => Element;
}
export interface ShikiTransformer {
  /**
   * Name of the transformer
   */
  name?: string;
  /**
   * Enforce plugin invocation tier similar to webpack loaders. Hooks ordering
   * is still subject to the `order` property in the hook object.
   *
   * Plugin invocation order:
   * - `enforce: 'pre'` plugins
   * - normal plugins
   * - `enforce: 'post'` plugins
   * - shiki post plugins
   */
  enforce?: 'pre' | 'post';
  /**
   * Transform the raw input code before passing to the highlighter.
   */
  preprocess?: (this: ShikiTransformerContextCommon, code: string, options: CodeToHastOptions) => string | void;
  /**
   * Transform the full tokens list before converting to HAST.
   * Return a new tokens list will replace the original one.
   */
  tokens?: (this: ShikiTransformerContextSource, tokens: ThemedToken[][]) => ThemedToken[][] | void;
  /**
   * Transform the entire generated HAST tree. Return a new Node will replace the original one.
   */
  root?: (this: ShikiTransformerContext, hast: Root) => Root | void;
  /**
   * Transform the `
` element. Return a new Node will replace the original one.
   */
  pre?: (this: ShikiTransformerContext, hast: Element) => Element | void;
  /**
   * Transform the `` element. Return a new Node will replace the original one.
   */
  code?: (this: ShikiTransformerContext, hast: Element) => Element | void;
  /**
   * Transform each line `` element.
   *
   * @param hast
   * @param line 1-based line number
   */
  line?: (this: ShikiTransformerContext, hast: Element, line: number) => Element | void;
  /**
   * Transform each token `` element.
   */
  span?: (this: ShikiTransformerContext, hast: Element, line: number, col: number, lineElement: Element, token: ThemedToken) => Element | void;
  /**
   * Transform the generated HTML string before returning.
   * This hook will only be called with `codeToHtml`.
   */
  postprocess?: (this: ShikiTransformerContextCommon, html: string, options: CodeToHastOptions) => string | void;
}
//#endregion
//#region src/options.d.ts
export interface HighlighterCoreOptions {
  /**
   * Custom RegExp engine.
   */
  engine: Sync extends true ? RegexEngine : Awaitable;
  /**
   * Theme names, or theme registration objects to be loaded upfront.
   */
  themes?: Sync extends true ? MaybeArray[] : ThemeInput[];
  /**
   * Language names, or language registration objects to be loaded upfront.
   */
  langs?: Sync extends true ? MaybeArray[] : LanguageInput[];
  /**
   * Alias of languages
   * @example { 'my-lang': 'javascript' }
   */
  langAlias?: Record;
  /**
   * Emit console warnings to alert users of potential issues.
   * @default true
   */
  warnings?: boolean;
}
export interface BundledHighlighterOptions extends Pick {
  /**
   * Custom RegExp engine.
   */
  engine?: Awaitable;
  /**
   * Theme registation
   *
   * @default []
   */
  themes: (ThemeInput | StringLiteralUnion | SpecialTheme)[];
  /**
   * Language registation
   *
   * @default []
   */
  langs: (LanguageInput | StringLiteralUnion | SpecialLanguage)[];
  /**
   * Alias of languages
   * @example { 'my-lang': 'javascript' }
   */
  langAlias?: Record>;
}
export interface CodeOptionsSingleTheme {
  theme: ThemeRegistrationAny | StringLiteralUnion;
}
export interface CodeOptionsMultipleThemes {
  /**
   * A map of color names to themes.
   * This allows you to specify multiple themes for the generated code.
   *
   * ```ts
   * highlighter.codeToHtml(code, {
   *   lang: 'js',
   *   themes: {
   *     light: 'vitesse-light',
   *     dark: 'vitesse-dark',
   *   }
   * })
   * ```
   *
   * Will generate:
   *
   * ```html
   * code
   * ```
   *
   * @see https://shiki.style/guide/dual-themes
   */
  themes: Partial>>;
  /**
   * The default theme applied to the code (via inline `color` style).
   * The rest of the themes are applied via CSS variables, and toggled by CSS overrides.
   *
   * For example, if `defaultColor` is `light`, then `light` theme is applied to the code,
   * and the `dark` theme and other custom themes are applied via CSS variables:
   *
   * ```html
   * code
   * ```
   *
   * When set to `false`, no default styles will be applied, and totally up to users to apply the styles:
   *
   * ```html
   * code
   * ```
   *
   * When set to `light-dark()`, the default color will be rendered as `light-dark(#{light}, #{dark})`.
   *
   * ```html
   * code
   * ```
   *
   * @default 'light'
   */
  defaultColor?: StringLiteralUnion<'light' | 'dark'> | 'light-dark()' | false;
  /**
   * The strategy to render multiple colors.
   *
   * - `css-vars`: Render the colors via CSS variables.
   * - `none`: Do not render the colors, only use the default color.
   *
   * @default 'css-vars'
   */
  colorsRendering?: 'css-vars' | 'none';
  /**
   * Prefix of CSS variables used to store the color of the other theme.
   *
   * @default '--shiki-'
   */
  cssVariablePrefix?: string;
}
export type CodeOptionsThemes = CodeOptionsSingleTheme | CodeOptionsMultipleThemes;
export type CodeToHastOptions = CodeToHastOptionsCommon & CodeOptionsThemes & CodeOptionsMeta;
export interface CodeToHastOptionsCommon extends TransformerOptions, DecorationOptions, Pick {
  /**
   * Data to be added to the root `
` element.
   */
  data?: Record;
  /**
   * The grammar name for the code.
   */
  lang: StringLiteralUnion;
  /**
   * Custom style string to be applied to the root `
` element.
   *
   * When set to `false`, no style will be applied.
   */
  rootStyle?: string | false;
  /**
   * Merge whitespace tokens to saving extra ``.
   *
   * When set to true, it will merge whitespace tokens with the next token.
   * When set to false, it keep the output as-is.
   * When set to `never`, it will force to separate leading and trailing spaces from tokens.
   *
   * @default true
   */
  mergeWhitespaces?: boolean | 'never';
  /**
   * Merge consecutive tokens with the same style to reduce the number of DOM nodes.
   * This can improve rendering performance but may affect the structure of the output.
   *
   * @default false
   */
  mergeSameStyleTokens?: boolean;
  /**
   * The structure of the generated HAST and HTML.
   *
   * - `classic`: The classic structure with `
` and `` elements, each line wrapped with a `` element.
   * - `inline`: All tokens are rendered as ``, line breaks are rendered as `
`. No `
` or `` elements. Default forground and background colors are not applied.
   *
   * @default 'classic'
   */
  structure?: 'classic' | 'inline';
  /**
   * Tab index of the root `
` element.
   *
   * Set to `false` to disable tab index.
   *
   * @default 0
   */
  tabindex?: number | string | false;
}
export interface CodeOptionsMeta {
  /**
   * Meta data passed to Shiki, usually used by plugin integrations to pass the code block header.
   *
   * Key values in meta will be serialized to the attributes of the root `
` element.
   *
   * Keys starting with `_` will be ignored.
   *
   * A special key `__raw` key will be used to pass the raw code block header (if the integration supports it).
   */
  meta?: {
    /**
     * Raw string of the code block header.
     */
    __raw?: string;
    [key: string]: any;
  };
}
export interface CodeToHastRenderOptionsCommon extends TransformerOptions, Omit {
  lang?: string;
  langId?: string;
}
export type CodeToHastRenderOptions = CodeToHastRenderOptionsCommon & CodeToHastOptions;
//#endregion
//#region src/highlighter.d.ts
/**
 * Type of object that can be bound to a grammar state
 */
export type GrammarStateMapKey = Root | ThemedToken[][];
/**
 * Internal context of Shiki, core textmate logic
 */
export interface ShikiPrimitive {
  /**
   * Load a theme to the highlighter, so later it can be used synchronously.
   */
  loadTheme: (...themes: (ThemeInput | BundledThemeKeys | SpecialTheme)[]) => Promise;
  /**
   * Load a theme registration synchronously.
   */
  loadThemeSync: (...themes: MaybeArray[]) => void;
  /**
   * Load a language to the highlighter, so later it can be used synchronously.
   */
  loadLanguage: (...langs: (LanguageInput | BundledLangKeys | SpecialLanguage)[]) => Promise;
  /**
   * Load a language registration synchronously.
   */
  loadLanguageSync: (...langs: MaybeArray[]) => void;
  /**
   * Get the registered theme object
   */
  getTheme: (name: string | ThemeRegistrationAny) => ThemeRegistrationResolved;
  /**
   * Get the registered language object
   */
  getLanguage: (name: string | LanguageRegistration) => Grammar;
  /**
   * Set the current theme and get the resolved theme object and color map.
   * @internal
   */
  setTheme: (themeName: string | ThemeRegistrationAny) => {
    theme: ThemeRegistrationResolved;
    colorMap: string[];
  };
  /**
   * Resolve a language alias
   */
  resolveLangAlias: (lang: string) => string;
  /**
   * Get the names of loaded languages
   *
   * Special-handled languages like `text`, `plain` and `ansi` are not included.
   */
  getLoadedLanguages: () => string[];
  /**
   * Get the names of loaded themes
   *
   * Special-handled themes like `none` are not included.
   */
  getLoadedThemes: () => string[];
  /**
   * Dispose the internal registry and release resources
   */
  dispose: () => void;
  /**
   * Dispose the internal registry and release resources
   */
  [Symbol.dispose]: () => void;
}
/**
 * Generic instance interface of Shiki
 */
export interface HighlighterGeneric extends ShikiPrimitive {
  /**
   * Get highlighted code in HTML string
   */
  codeToHtml: (code: string, options: CodeToHastOptions, ResolveBundleKey>) => string;
  /**
   * Get highlighted code in HAST.
   * @see https://github.com/syntax-tree/hast
   */
  codeToHast: (code: string, options: CodeToHastOptions, ResolveBundleKey>) => Root;
  /**
   * Get highlighted code in tokens. Uses `codeToTokensWithThemes` or `codeToTokensBase` based on the options.
   */
  codeToTokens: (code: string, options: CodeToTokensOptions, ResolveBundleKey>) => TokensResult;
  /**
   * Get highlighted code in tokens with a single theme.
   * @returns A 2D array of tokens, first dimension is lines, second dimension is tokens in a line.
   */
  codeToTokensBase: (code: string, options: CodeToTokensBaseOptions, ResolveBundleKey>) => ThemedToken[][];
  /**
   * Get highlighted code in tokens with multiple themes.
   *
   * Different from `codeToTokensBase`, each token will have a `variants` property consisting of an object of color name to token styles.
   *
   * @returns A 2D array of tokens, first dimension is lines, second dimension is tokens in a line.
   */
  codeToTokensWithThemes: (code: string, options: CodeToTokensWithThemesOptions, ResolveBundleKey>) => ThemedTokenWithVariants[][];
  /**
   * Get the last grammar state of a code snippet.
   * You can pass the grammar state to `codeToTokens` as `grammarState` to continue tokenizing from an intermediate state.
   */
  getLastGrammarState: {
    (element: GrammarStateMapKey, options?: never): GrammarState | undefined;
    (code: string, options: CodeToTokensBaseOptions, ResolveBundleKey>): GrammarState;
  };
  /**
   * Get internal context object
   * @internal
   * @deprecated
   */
  getInternalContext: () => ShikiPrimitive;
  /**
   * Get bundled languages object
   */
  getBundledLanguages: () => Record;
  /**
   * Get bundled themes object
   */
  getBundledThemes: () => Record;
}
/**
 * The fine-grained core Shiki highlighter instance.
 */
export type HighlighterCore = HighlighterGeneric;
/**
 * Options for creating a bundled highlighter.
 */
export interface CreateBundledHighlighterOptions {
  langs: Record;
  themes: Record;
  engine: () => Awaitable;
}
/**
 * @deprecated Use `CreateBundledHighlighterOptions` instead.
 */
export interface CreatedBundledHighlighterOptions extends CreateBundledHighlighterOptions {}
//#endregion
//#region src/bundle-factory.d.ts
export type CreateHighlighterFactory = (options: BundledHighlighterOptions) => Promise>;
//#endregion
//#region src/error.d.ts
export declare class ShikiError extends Error {
  constructor(message: string);
}
//#endregion
export type { RawGrammar, RawTheme, RawThemeSetting };