diff --git a/packages/shikiji/src/core/renderer-hast.ts b/packages/shikiji/src/core/renderer-hast.ts index c4180cb8b..c085a6431 100644 --- a/packages/shikiji/src/core/renderer-hast.ts +++ b/packages/shikiji/src/core/renderer-hast.ts @@ -19,6 +19,7 @@ export function codeToHast( options: CodeToHastOptions, transformerContext: ShikijiTransformerContextCommon = { meta: {}, + codeToHast: (_code, _options) => codeToHast(internal, _code, _options), }, ) { let bg: string @@ -138,9 +139,7 @@ function flattenToken( export function tokensToHast( tokens: ThemedToken[][], options: HtmlRendererOptions, - transformerContext: ShikijiTransformerContextCommon = { - meta: {}, - }, + transformerContext: ShikijiTransformerContextCommon, ) { const { mergeWhitespaces = true, diff --git a/packages/shikiji/src/core/renderer-html-themes.ts b/packages/shikiji/src/core/renderer-html-themes.ts index d0b5667a1..ed2c0b31c 100644 --- a/packages/shikiji/src/core/renderer-html-themes.ts +++ b/packages/shikiji/src/core/renderer-html-themes.ts @@ -10,7 +10,7 @@ import { codeToThemedTokens } from './tokenizer' * Get tokens with multiple themes */ export function codeToTokensWithThemes( - context: ShikiInternal, + internal: ShikiInternal, code: string, options: CodeToTokensWithThemesOptions, ) { @@ -19,7 +19,7 @@ export function codeToTokensWithThemes( .map(i => ({ color: i[0], theme: i[1]! })) const tokens = syncThemesTokenization( - ...themes.map(t => codeToThemedTokens(context, code, { + ...themes.map(t => codeToThemedTokens(internal, code, { ...options, theme: t.theme, includeExplanation: false, diff --git a/packages/shikiji/src/core/renderer-html.ts b/packages/shikiji/src/core/renderer-html.ts index ad084cde1..248a24268 100644 --- a/packages/shikiji/src/core/renderer-html.ts +++ b/packages/shikiji/src/core/renderer-html.ts @@ -12,6 +12,7 @@ export function codeToHtml( ): string { const context: ShikijiTransformerContextCommon = { meta: {}, + codeToHast: (_code, _options) => codeToHast(internal, _code, _options), } let intput = code diff --git a/packages/shikiji/src/core/tokenizer.ts b/packages/shikiji/src/core/tokenizer.ts index a5432a2d0..47a034c04 100644 --- a/packages/shikiji/src/core/tokenizer.ts +++ b/packages/shikiji/src/core/tokenizer.ts @@ -1,8 +1,6 @@ /* --------------------------------------------------------- * Copyright (C) Microsoft Corporation. All rights reserved. *-------------------------------------------------------- */ -'use strict' - import type { IGrammar, IRawTheme } from 'vscode-textmate' import { INITIAL } from 'vscode-textmate' import type { CodeToThemedTokensOptions, ShikiInternal, ThemedToken, ThemedTokenScopeExplanation } from '../types' @@ -16,13 +14,13 @@ export interface TokenizeWithThemeOptions { } export function codeToThemedTokens( - context: ShikiInternal, + internal: ShikiInternal, code: string, options: CodeToThemedTokensOptions = {}, ): ThemedToken[][] { const { lang = 'text', - theme: themeName = context.getLoadedThemes()[0], + theme: themeName = internal.getLoadedThemes()[0], includeExplanation = true, } = options @@ -31,12 +29,12 @@ export function codeToThemedTokens( return [...lines.map(line => [{ content: line }])] } - const { theme, colorMap } = context.setTheme(themeName) + const { theme, colorMap } = internal.setTheme(themeName) if (lang === 'ansi') return tokenizeAnsiWithTheme(theme, code) - const _grammar = context.getLangGrammar(lang) + const _grammar = internal.getLangGrammar(lang) return tokenizeWithTheme(code, _grammar, theme, colorMap, { includeExplanation, }) diff --git a/packages/shikiji/src/types.ts b/packages/shikiji/src/types.ts index 9346bac21..364e36ffa 100644 --- a/packages/shikiji/src/types.ts +++ b/packages/shikiji/src/types.ts @@ -360,6 +360,7 @@ export interface ShikijiTransformerContextMeta {} export interface ShikijiTransformerContextCommon { meta: ShikijiTransformerContextMeta + codeToHast: (code: string, options: CodeToHastOptions) => Root } export interface ShikijiTransformerContext extends ShikijiTransformerContextCommon {