|
| 1 | +import type { |
| 2 | + HighlighterCoreOptions, |
| 3 | + LanguageInput, |
| 4 | + LanguageRegistration, |
| 5 | + MaybeArray, |
| 6 | + ShikiInternal, |
| 7 | + SpecialLanguage, |
| 8 | + SpecialTheme, |
| 9 | + ThemeInput, |
| 10 | + ThemeRegistrationAny, |
| 11 | + ThemeRegistrationResolved, |
| 12 | +} from '../types' |
| 13 | +import { Registry } from '../textmate/registry' |
| 14 | +import { Resolver } from '../textmate/resolver' |
| 15 | +import { normalizeTheme } from '../textmate/normalize-theme' |
| 16 | +import { ShikiError } from '../error' |
| 17 | +import { resolveLangs, resolveThemes } from '../textmate/getters-resolve' |
| 18 | + |
| 19 | +let instancesCount = 0 |
| 20 | + |
| 21 | +/** |
| 22 | + * Get the minimal shiki context for rendering. |
| 23 | + * |
| 24 | + * Synchronous version of `createShikiInternal`, which requires to provide the engine and all themes and languages upfront. |
| 25 | + */ |
| 26 | +export function createShikiInternalSync(options: HighlighterCoreOptions<true>): ShikiInternal { |
| 27 | + instancesCount += 1 |
| 28 | + if (options.warnings !== false && instancesCount >= 10 && instancesCount % 10 === 0) |
| 29 | + console.warn(`[Shiki] ${instancesCount} instances have been created. Shiki is supposed to be used as a singleton, consider refactoring your code to cache your highlighter instance; Or call \`highlighter.dispose()\` to release unused instances.`) |
| 30 | + |
| 31 | + let isDisposed = false |
| 32 | + |
| 33 | + if (!options.engine) |
| 34 | + throw new ShikiError('`engine` option is required for synchronous mode') |
| 35 | + |
| 36 | + const langs = (options.langs || []).flat(1) |
| 37 | + const themes = (options.themes || []).flat(1).map(normalizeTheme) |
| 38 | + |
| 39 | + const resolver = new Resolver(options.engine, langs) |
| 40 | + const _registry = new Registry(resolver, themes, langs, options.langAlias) |
| 41 | + |
| 42 | + let _lastTheme: string | ThemeRegistrationAny |
| 43 | + |
| 44 | + function getLanguage(name: string | LanguageRegistration) { |
| 45 | + ensureNotDisposed() |
| 46 | + const _lang = _registry.getGrammar(typeof name === 'string' ? name : name.name) |
| 47 | + if (!_lang) |
| 48 | + throw new ShikiError(`Language \`${name}\` not found, you may need to load it first`) |
| 49 | + return _lang |
| 50 | + } |
| 51 | + |
| 52 | + function getTheme(name: string | ThemeRegistrationAny): ThemeRegistrationResolved { |
| 53 | + if (name === 'none') |
| 54 | + return { bg: '', fg: '', name: 'none', settings: [], type: 'dark' } |
| 55 | + ensureNotDisposed() |
| 56 | + const _theme = _registry.getTheme(name) |
| 57 | + if (!_theme) |
| 58 | + throw new ShikiError(`Theme \`${name}\` not found, you may need to load it first`) |
| 59 | + return _theme |
| 60 | + } |
| 61 | + |
| 62 | + function setTheme(name: string | ThemeRegistrationAny) { |
| 63 | + ensureNotDisposed() |
| 64 | + const theme = getTheme(name) |
| 65 | + if (_lastTheme !== name) { |
| 66 | + _registry.setTheme(theme) |
| 67 | + _lastTheme = name |
| 68 | + } |
| 69 | + const colorMap = _registry.getColorMap() |
| 70 | + return { |
| 71 | + theme, |
| 72 | + colorMap, |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + function getLoadedThemes() { |
| 77 | + ensureNotDisposed() |
| 78 | + return _registry.getLoadedThemes() |
| 79 | + } |
| 80 | + |
| 81 | + function getLoadedLanguages() { |
| 82 | + ensureNotDisposed() |
| 83 | + return _registry.getLoadedLanguages() |
| 84 | + } |
| 85 | + |
| 86 | + function loadLanguageSync(...langs: MaybeArray<LanguageRegistration>[]) { |
| 87 | + ensureNotDisposed() |
| 88 | + _registry.loadLanguages(langs.flat(1)) |
| 89 | + } |
| 90 | + |
| 91 | + async function loadLanguage(...langs: (LanguageInput | SpecialLanguage)[]) { |
| 92 | + return loadLanguageSync(await resolveLangs(langs)) |
| 93 | + } |
| 94 | + |
| 95 | + async function loadThemeSync(...themes: MaybeArray<ThemeRegistrationAny>[]) { |
| 96 | + ensureNotDisposed() |
| 97 | + for (const theme of themes.flat(1)) { |
| 98 | + _registry.loadTheme(theme) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + async function loadTheme(...themes: (ThemeInput | SpecialTheme)[]) { |
| 103 | + ensureNotDisposed() |
| 104 | + return loadThemeSync(await resolveThemes(themes)) |
| 105 | + } |
| 106 | + |
| 107 | + function ensureNotDisposed() { |
| 108 | + if (isDisposed) |
| 109 | + throw new ShikiError('Shiki instance has been disposed') |
| 110 | + } |
| 111 | + |
| 112 | + function dispose() { |
| 113 | + if (isDisposed) |
| 114 | + return |
| 115 | + isDisposed = true |
| 116 | + _registry.dispose() |
| 117 | + instancesCount -= 1 |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + setTheme, |
| 122 | + getTheme, |
| 123 | + getLanguage, |
| 124 | + getLoadedThemes, |
| 125 | + getLoadedLanguages, |
| 126 | + loadLanguage, |
| 127 | + loadLanguageSync, |
| 128 | + loadTheme, |
| 129 | + loadThemeSync, |
| 130 | + dispose, |
| 131 | + [Symbol.dispose]: dispose, |
| 132 | + } |
| 133 | +} |
0 commit comments