|
| 1 | +import type { |
| 2 | + CodeOptionsSingleTheme, |
| 3 | + CodeOptionsMultipleThemes, |
| 4 | + BundledTheme, |
| 5 | +} from 'shiki'; |
| 6 | +import type { ThemeRegistrationAny } from 'shiki/core'; |
| 7 | +import type { Theme, Themes } from './types'; |
| 8 | + |
| 9 | +export const DEFAULT_THEMES: Themes = { |
| 10 | + light: 'github-light', |
| 11 | + dark: 'github-dark', |
| 12 | +}; |
| 13 | + |
| 14 | +export interface ThemeResolution { |
| 15 | + isMultiTheme: boolean; |
| 16 | + themeId: Theme; |
| 17 | + multiTheme?: Themes | ThemeRegistrationAny | null; |
| 18 | + singleTheme?: Theme | undefined; |
| 19 | + themesToLoad: Theme[]; |
| 20 | +} |
| 21 | + |
| 22 | +export type ThemeShikiOptions = |
| 23 | + | CodeOptionsSingleTheme<BundledTheme> |
| 24 | + | CodeOptionsMultipleThemes<BundledTheme>; |
| 25 | + |
| 26 | +export function resolveTheme( |
| 27 | + themeInput: Theme | Themes |
| 28 | +): ThemeResolution { |
| 29 | + const isTextmateTheme = |
| 30 | + typeof themeInput === 'object' && |
| 31 | + 'tokenColors' in themeInput && |
| 32 | + Array.isArray(themeInput.tokenColors); |
| 33 | + |
| 34 | + // Non-textmate objects are assumed to be multi-theme configs |
| 35 | + const isMultiThemeConfig = |
| 36 | + typeof themeInput === 'object' && |
| 37 | + themeInput !== null && |
| 38 | + !isTextmateTheme; |
| 39 | + |
| 40 | + const validMultiThemeObj = |
| 41 | + typeof themeInput === 'object' && |
| 42 | + themeInput !== null && |
| 43 | + !isTextmateTheme && |
| 44 | + Object.entries(themeInput).some( |
| 45 | + ([key, value]) => |
| 46 | + key && |
| 47 | + value && |
| 48 | + key.trim() !== '' && |
| 49 | + value !== '' && |
| 50 | + (typeof value === 'string' || isTextmateTheme) |
| 51 | + ); |
| 52 | + |
| 53 | + if (isMultiThemeConfig) { |
| 54 | + const themeId = validMultiThemeObj |
| 55 | + ? `multi-${Object.values(themeInput) |
| 56 | + .map( |
| 57 | + (theme) => |
| 58 | + (typeof theme === 'string' ? theme : theme?.name) || |
| 59 | + 'custom' |
| 60 | + ) |
| 61 | + .sort() |
| 62 | + .join('-')}` |
| 63 | + : 'multi-default'; |
| 64 | + |
| 65 | + // Invalid config returns null; fallback handled in buildShikiOptions |
| 66 | + return { |
| 67 | + isMultiTheme: true, |
| 68 | + themeId, |
| 69 | + multiTheme: validMultiThemeObj ? themeInput : null, |
| 70 | + themesToLoad: validMultiThemeObj ? Object.values(themeInput) : [], |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + return { |
| 75 | + isMultiTheme: false, |
| 76 | + themeId: |
| 77 | + typeof themeInput === 'string' |
| 78 | + ? themeInput |
| 79 | + : themeInput?.name || 'custom', |
| 80 | + singleTheme: themeInput, |
| 81 | + themesToLoad: [themeInput], |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +export const toShikiOptions = ( |
| 86 | + resolution: ThemeResolution |
| 87 | +): ThemeShikiOptions => { |
| 88 | + const { isMultiTheme, multiTheme, singleTheme } = resolution; |
| 89 | + |
| 90 | + if (isMultiTheme) { |
| 91 | + return { |
| 92 | + themes: multiTheme || DEFAULT_THEMES, |
| 93 | + } as CodeOptionsMultipleThemes<BundledTheme>; |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + theme: singleTheme || DEFAULT_THEMES.dark, |
| 98 | + } as CodeOptionsSingleTheme<BundledTheme>; |
| 99 | +}; |
| 100 | + |
| 101 | +export const getMultiThemeOptions = ( |
| 102 | + resolution: ThemeResolution, |
| 103 | + defaultColor?: string | false, |
| 104 | + cssVariablePrefix?: string |
| 105 | +): Record<string, unknown> => { |
| 106 | + if (!resolution.isMultiTheme) return {}; |
| 107 | + return { defaultColor, cssVariablePrefix }; |
| 108 | +}; |
0 commit comments