From 533434a6549b467bb3db0b5482e346b5dd880d42 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 11 Dec 2023 22:31:33 +0100 Subject: [PATCH] feat: expose more utils --- packages/shikiji-compat/src/index.ts | 6 ++--- packages/shikiji/src/core/highlighter.ts | 24 +++++++++---------- packages/shikiji/src/core/index.ts | 5 +++- .../src/core/{context.ts => internal.ts} | 5 ++++ 4 files changed, 23 insertions(+), 17 deletions(-) rename packages/shikiji/src/core/{context.ts => internal.ts} (96%) diff --git a/packages/shikiji-compat/src/index.ts b/packages/shikiji-compat/src/index.ts index 89e285723..9d96fb2e8 100644 --- a/packages/shikiji-compat/src/index.ts +++ b/packages/shikiji-compat/src/index.ts @@ -34,8 +34,6 @@ export async function getHighlighter(options: HighlighterOptions = {}) { langs, }) - const context = shikiji.getInternalContext() - const defaultTheme = shikiji.getLoadedThemes()[0] function codeToThemedTokens(code: string, options: CodeToThemedTokensOptions): ThemedToken[][] @@ -99,10 +97,10 @@ export async function getHighlighter(options: HighlighterOptions = {}) { }) }, getBackgroundColor(theme: BuiltinTheme | ThemeRegistration | string) { - return context.getTheme(theme).bg + return shikiji.getTheme(theme).bg }, getForegroundColor(theme: BuiltinTheme | ThemeRegistration | string) { - return context.getTheme(theme).fg + return shikiji.getTheme(theme).fg }, /** diff --git a/packages/shikiji/src/core/highlighter.ts b/packages/shikiji/src/core/highlighter.ts index 80b2ba896..6f16581a9 100644 --- a/packages/shikiji/src/core/highlighter.ts +++ b/packages/shikiji/src/core/highlighter.ts @@ -2,28 +2,28 @@ import type { HighlighterCoreOptions, HighlighterGeneric } from '../types' import { codeToHtml } from './renderer-html' import { codeToTokensWithThemes } from './renderer-html-themes' import { codeToThemedTokens } from './tokenizer' -import { getShikiInternal } from './context' +import { getShikiInternal } from './internal' import { codeToHast } from './renderer-hast' export type HighlighterCore = HighlighterGeneric export async function getHighlighterCore(options: HighlighterCoreOptions = {}): Promise { - const context = await getShikiInternal(options) + const internal = await getShikiInternal(options) return { - codeToThemedTokens: (code, options) => codeToThemedTokens(context, code, options), - codeToTokensWithThemes: (code, options) => codeToTokensWithThemes(context, code, options), - codeToHast: (code, options) => codeToHast(context, code, options), - codeToHtml: (code, options) => codeToHtml(context, code, options), + codeToThemedTokens: (code, options) => codeToThemedTokens(internal, code, options), + codeToTokensWithThemes: (code, options) => codeToTokensWithThemes(internal, code, options), + codeToHast: (code, options) => codeToHast(internal, code, options), + codeToHtml: (code, options) => codeToHtml(internal, code, options), - loadLanguage: context.loadLanguage, - loadTheme: context.loadTheme, + loadLanguage: internal.loadLanguage, + loadTheme: internal.loadTheme, - getTheme: context.getTheme, + getTheme: internal.getTheme, - getLoadedThemes: context.getLoadedThemes, - getLoadedLanguages: context.getLoadedLanguages, + getLoadedThemes: internal.getLoadedThemes, + getLoadedLanguages: internal.getLoadedLanguages, - getInternalContext: () => context, + getInternalContext: () => internal, } } diff --git a/packages/shikiji/src/core/index.ts b/packages/shikiji/src/core/index.ts index 1704c12e2..add88fb49 100644 --- a/packages/shikiji/src/core/index.ts +++ b/packages/shikiji/src/core/index.ts @@ -1,10 +1,13 @@ export { loadWasm } from '../oniguruma' -export * from './context' export * from './highlighter' export * from './bundle-factory' export * from './utils' +export { getShikiInternal, getShikiContext } from './internal' export { codeToThemedTokens } from './tokenizer' export { tokenizeAnsiWithTheme } from './tokenizer-ansi' +export { codeToHast } from './renderer-hast' +export { codeToHtml } from './renderer-html' +export { codeToTokensWithThemes } from './renderer-html-themes' export { toShikiTheme } from './normalize' diff --git a/packages/shikiji/src/core/context.ts b/packages/shikiji/src/core/internal.ts similarity index 96% rename from packages/shikiji/src/core/context.ts rename to packages/shikiji/src/core/internal.ts index e6dfe89ea..3705fe51e 100644 --- a/packages/shikiji/src/core/context.ts +++ b/packages/shikiji/src/core/internal.ts @@ -105,3 +105,8 @@ export async function getShikiInternal(options: HighlighterCoreOptions = {}): Pr loadTheme, } } + +/** + * @deprecated Use `getShikiInternal` instead. + */ +export const getShikiContext = getShikiInternal