返回 slidev
shiki-options.ts
根目录 / packages / client / setup / shiki-options.ts
1 // This module also runs in the Node.js environment
2
3 import type { ResolvedSlidevUtils, ShikiContext, ShikiSetupReturn } from '@slidev/types'
4 import type { LanguageInput, ThemeInput, ThemeRegistrationAny } from 'shiki'
5 import { objectMap } from '@antfu/utils'
6 import { red, yellow } from 'ansis'
7 import { bundledLanguages, bundledThemes } from 'shiki'
8
9 export const shikiContext: ShikiContext = {
10 /** @deprecated */
11 loadTheme() {
12 throw new Error('`loadTheme` is no longer supported.')
13 },
14 }
15
16 export function resolveShikiOptions(options: (ShikiSetupReturn | void)[]) {
17 const mergedOptions: Record<string, any> = Object.assign({}, ...options)
18
19 if ('theme' in mergedOptions && 'themes' in mergedOptions)
20 delete mergedOptions.theme
21
22 // Rename theme to themes when provided in multiple themes format, but exclude when it's a theme object.
23 if (mergedOptions.theme && typeof mergedOptions.theme !== 'string' && !mergedOptions.theme.name && !mergedOptions.theme.tokenColors) {
24 mergedOptions.themes = mergedOptions.theme
25 delete mergedOptions.theme
26 }
27
28 // No theme at all, apply the default
29 if (!mergedOptions.theme && !mergedOptions.themes) {
30 mergedOptions.themes = {
31 dark: 'vitesse-dark',
32 light: 'vitesse-light',
33 }
34 }
35
36 if (mergedOptions.themes)
37 mergedOptions.defaultColor = false
38
39 const themeOption = extractThemeName(mergedOptions.theme) || extractThemeNames(mergedOptions.themes || {})
40 const themeNames = typeof themeOption === 'string'
41 ? [themeOption]
42 : Object.values(themeOption)
43
44 const themeInput: Record<string, ThemeInput> = Object.assign({}, bundledThemes)
45 if (typeof mergedOptions.theme === 'object' && mergedOptions.theme?.name) {
46 themeInput[mergedOptions.theme.name] = mergedOptions.theme
47 }
48 if (mergedOptions.themes) {
49 for (const theme of Object.values<ThemeRegistrationAny | string>(mergedOptions.themes)) {
50 if (typeof theme === 'object' && theme?.name) {
51 themeInput[theme.name] = theme
52 }
53 }
54 }
55
56 const languageNames = new Set<string>([
57 'markdown',
58 'vue',
59 'javascript',
60 'typescript',
61 'html',
62 'css',
63 ])
64 const languageInput: Record<string, LanguageInput> = Object.assign({}, bundledLanguages)
65 for (const option of options) {
66 const langs = option?.langs
67 if (langs == null)
68 continue
69 if (Array.isArray(langs)) {
70 for (const lang of langs.flat()) {
71 if (typeof lang === 'function') {
72 console.error(red('[slidev] `langs` option returned by setup/shiki.ts cannot be an array containing functions. Please use the record format (`{ [name]: () => {...} }`) instead.'))
73 }
74 else if (typeof lang === 'string') {
75 // a name of a Shiki built-in language
76 // which can be loaded on demand without overhead, so all built-in languages are available.
77 // Only need to include them explicitly in browser environment.
78 languageNames.add(lang)
79 }
80 else if (lang.name) {
81 // a custom grammar object
82 languageNames.add(lang.name)
83 languageInput[lang.name] = lang
84 for (const alias of lang.aliases || []) {
85 languageNames.add(alias)
86 languageInput[alias] = lang
87 }
88 }
89 else {
90 console.error(red('[slidev] Invalid lang option in shiki setup:'), lang)
91 }
92 }
93 }
94 else if (typeof langs === 'object') {
95 // a map from name to loader or grammar object
96 for (const name of Object.keys(langs))
97 languageNames.add(name)
98 Object.assign(languageInput, langs)
99 }
100 else {
101 console.error(red('[slidev] Invalid langs option in shiki setup:'), langs)
102 }
103 }
104
105 return {
106 options: mergedOptions as ResolvedSlidevUtils['shikiOptions'],
107 themeOption,
108 themeNames,
109 themeInput,
110 languageNames,
111 languageInput,
112 }
113 }
114
115 function extractThemeName(theme?: ThemeRegistrationAny | string): string | undefined {
116 if (!theme)
117 return undefined
118 if (typeof theme === 'string')
119 return theme
120 if (!theme.name)
121 console.warn(yellow('[slidev] Theme'), theme, yellow('does not have a name, which may cause issues.'))
122 return theme.name
123 }
124
125 function extractThemeNames(themes?: Record<string, ThemeRegistrationAny | string>): Record<string, string> {
126 if (!themes)
127 return {}
128 return objectMap(themes, (key, theme) => {
129 const name = extractThemeName(theme)
130 if (!name)
131 return undefined
132 return [key, name]
133 })
134 }
135
135 lines TYPESCRIPT