Skip to content

Commit

Permalink
feat: expose more utils
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 11, 2023
1 parent 1288ecf commit 533434a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
6 changes: 2 additions & 4 deletions packages/shikiji-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuiltinLanguage, BuiltinTheme>): ThemedToken[][]
Expand Down Expand Up @@ -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
},

/**
Expand Down
24 changes: 12 additions & 12 deletions packages/shikiji/src/core/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<never, never>

export async function getHighlighterCore(options: HighlighterCoreOptions = {}): Promise<HighlighterCore> {
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,
}
}
5 changes: 4 additions & 1 deletion packages/shikiji/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,8 @@ export async function getShikiInternal(options: HighlighterCoreOptions = {}): Pr
loadTheme,
}
}

/**
* @deprecated Use `getShikiInternal` instead.
*/
export const getShikiContext = getShikiInternal

0 comments on commit 533434a

Please sign in to comment.