|
| 1 | +import type { BuiltinLanguage, BuiltinTheme, BundledHighlighterOptions, CodeToHastOptions, CodeToThemedTokensOptions, LineOption, StringLiteralUnion, ThemedToken } from 'shikiji' |
| 2 | +import { bundledLanguages, bundledThemes, getHighlighter as getShikiji } from 'shikiji' |
| 3 | + |
| 4 | +export const BUNDLED_LANGUAGES = bundledLanguages |
| 5 | +export const BUNDLED_THEMES = bundledThemes |
| 6 | + |
| 7 | +export * from './stub' |
| 8 | + |
| 9 | +export interface AnsiToHtmlOptions { |
| 10 | + theme?: StringLiteralUnion<BuiltinTheme> |
| 11 | + lineOptions?: LineOption[] |
| 12 | +} |
| 13 | + |
| 14 | +export interface HighlighterOptions extends BundledHighlighterOptions<BuiltinLanguage, BuiltinTheme> { |
| 15 | + theme?: BuiltinTheme |
| 16 | +} |
| 17 | + |
| 18 | +export async function getHighlighter(options: HighlighterOptions = {}) { |
| 19 | + const themes = options.themes || [] |
| 20 | + const langs = options.langs || [] |
| 21 | + |
| 22 | + if (options.theme) |
| 23 | + themes.unshift(options.theme) |
| 24 | + if (!themes.length) |
| 25 | + themes.push('nord') |
| 26 | + |
| 27 | + if (!langs.length) |
| 28 | + langs.push(...Object.keys(bundledLanguages) as BuiltinLanguage[]) |
| 29 | + |
| 30 | + const shikiji = await getShikiji({ |
| 31 | + ...options, |
| 32 | + themes, |
| 33 | + langs, |
| 34 | + }) |
| 35 | + |
| 36 | + const defaultTheme = shikiji.getLoadedThemes()[0] |
| 37 | + |
| 38 | + function codeToThemedTokens(code: string, options: CodeToThemedTokensOptions<BuiltinLanguage, BuiltinTheme>): ThemedToken[][] |
| 39 | + function codeToThemedTokens(code: string, lang: BuiltinLanguage, theme?: BuiltinTheme): ThemedToken[][] |
| 40 | + function codeToThemedTokens(code: string, lang: BuiltinLanguage | CodeToThemedTokensOptions<BuiltinLanguage, BuiltinTheme>, theme?: BuiltinTheme): ThemedToken[][] { |
| 41 | + if (typeof lang === 'string') { |
| 42 | + return shikiji.codeToThemedTokens(code, { |
| 43 | + lang, |
| 44 | + theme: (theme || defaultTheme) as BuiltinTheme, |
| 45 | + }) |
| 46 | + } |
| 47 | + return shikiji.codeToThemedTokens(code, lang) |
| 48 | + } |
| 49 | + |
| 50 | + function codeToHtml(code: string, options: Partial<CodeToHastOptions<BuiltinLanguage, BuiltinTheme>>): string |
| 51 | + function codeToHtml(code: string, lang: BuiltinLanguage, theme?: BuiltinTheme): string |
| 52 | + function codeToHtml(code: string, lang: any, theme?: BuiltinTheme): string { |
| 53 | + if (typeof lang === 'string') { |
| 54 | + return shikiji.codeToHtml(code, { |
| 55 | + lang, |
| 56 | + theme: (theme || defaultTheme), |
| 57 | + }) |
| 58 | + } |
| 59 | + return shikiji.codeToHtml(code, { |
| 60 | + ...lang, |
| 61 | + theme: (lang.theme || defaultTheme), |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + return { |
| 66 | + ...shikiji, |
| 67 | + codeToThemedTokens, |
| 68 | + codeToHtml, |
| 69 | + ansiToHtml(code: string, options?: AnsiToHtmlOptions) { |
| 70 | + return shikiji.codeToHtml(code, { |
| 71 | + lang: 'ansi', |
| 72 | + ...options, |
| 73 | + theme: options?.theme || defaultTheme, |
| 74 | + }) |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
0 commit comments