返回 slidev
config.ts
根目录 / packages / parser / src / config.ts
1 import type { DrawingsOptions, FontOptions, ResolvedDrawingsOptions, ResolvedExportOptions, ResolvedFontOptions, SlidevConfig, SlidevThemeMeta } from '@slidev/types'
2 import { toArray, uniq } from '@antfu/utils'
3 import { parseAspectRatio } from './utils'
4
5 const RE_QUOTED_STRING = /^(['"]).*\1$/
6 const RE_FILENAME_STEM = /([^\\/]+?)(?:\.\w+)?$/
7
8 export function getDefaultConfig(): SlidevConfig {
9 return {
10 theme: 'default',
11 title: 'Slidev',
12 titleTemplate: '%s - Slidev',
13 addons: [],
14 remoteAssets: false,
15 monaco: true,
16 monacoTypesSource: 'local',
17 monacoTypesAdditionalPackages: [],
18 monacoTypesIgnorePackages: [],
19 monacoRunAdditionalDeps: [],
20 monacoRunUseStrict: true,
21 download: false,
22 export: {} as ResolvedExportOptions,
23 info: false,
24 highlighter: 'shiki',
25 twoslash: true,
26 lineNumbers: false,
27 colorSchema: 'auto',
28 routerMode: 'history',
29 advanceDebounce: 0,
30 aspectRatio: 16 / 9,
31 canvasWidth: 980,
32 exportFilename: '',
33 selectable: false,
34 themeConfig: {},
35 fonts: {} as ResolvedFontOptions,
36 favicon: 'https://cdn.jsdelivr.net/gh/slidevjs/slidev/assets/favicon.png',
37 drawings: {} as ResolvedDrawingsOptions,
38 plantUmlServer: 'https://www.plantuml.com/plantuml',
39 codeCopy: true,
40 magicMoveCopy: true,
41 author: '',
42 record: 'dev',
43 css: 'unocss',
44 presenter: true,
45 browserExporter: 'dev',
46 htmlAttrs: {},
47 transition: null,
48 editor: true,
49 mcp: true,
50 contextMenu: null,
51 wakeLock: true,
52 pwa: false,
53 remote: false,
54 mdc: false,
55 comark: false,
56 seoMeta: {},
57 notesAutoRuby: {},
58 duration: '30min',
59 timer: 'stopwatch',
60 magicMoveDuration: 800,
61 shiki: {},
62 preloadImages: true,
63 clickAnimation: '',
64 }
65 }
66
67 export function resolveConfig(headmatter: any, themeMeta: SlidevThemeMeta = {}, filepath?: string, verify = false) {
68 const themeHightlighter = ['prism', 'shiki', 'shikiji'].includes(themeMeta.highlighter || '')
69 ? themeMeta.highlighter as 'shiki'
70 : undefined
71 const themeColorSchema = ['light', 'dark'].includes(themeMeta.colorSchema || '')
72 ? themeMeta.colorSchema as 'light' | 'dark'
73 : undefined
74
75 const defaultConfig = getDefaultConfig()
76
77 const config: SlidevConfig = {
78 ...defaultConfig,
79 highlighter: themeHightlighter || defaultConfig.highlighter,
80 colorSchema: themeColorSchema || defaultConfig.colorSchema,
81 ...themeMeta.defaults,
82 ...headmatter.config,
83 ...headmatter,
84 fonts: resolveFonts({
85 ...defaultConfig.fonts,
86 ...themeMeta.defaults?.fonts,
87 ...headmatter.config?.fonts,
88 ...headmatter?.fonts,
89 }),
90 drawings: resolveDrawings({
91 ...themeMeta.defaults?.drawings,
92 ...headmatter.config?.drawings,
93 ...headmatter?.drawings,
94 }, filepath),
95 htmlAttrs: {
96 ...defaultConfig.htmlAttrs,
97 ...themeMeta.defaults?.htmlAttrs,
98 ...headmatter.config?.htmlAttrs,
99 ...headmatter?.htmlAttrs,
100 },
101 themeConfig: {
102 ...defaultConfig.themeConfig,
103 ...themeMeta.defaults?.themeConfig,
104 ...headmatter.config?.themeConfig,
105 ...headmatter?.themeConfig,
106 },
107 }
108
109 // @ts-expect-error compat
110 if (config.highlighter === 'shikiji') {
111 console.warn(`[slidev] "shikiji" is merged back to "shiki", you can safely change it "highlighter: shiki"`)
112 config.highlighter = 'shiki'
113 }
114
115 // @ts-expect-error compat
116 if (config.highlighter === 'prism')
117 throw new Error(`[slidev] "prism" support has been dropped. Please use "highlighter: shiki" instead`)
118
119 if (![
120 'dark',
121 'light',
122 'auto',
123 'light dark',
124 'dark light',
125 ].includes(config.colorSchema)) {
126 config.colorSchema = 'auto'
127 }
128 if (themeColorSchema && config.colorSchema === 'auto')
129 config.colorSchema = themeColorSchema
130
131 config.aspectRatio = parseAspectRatio(config.aspectRatio)
132
133 if (verify)
134 verifyConfig(config, themeMeta)
135
136 return config
137 }
138
139 export function verifyConfig(
140 config: SlidevConfig,
141 themeMeta: SlidevThemeMeta = {},
142 warn = (v: string) => console.warn(`[slidev] ${v}`),
143 ) {
144 const themeHightlighter = themeMeta.highlighter === 'shiki'
145 ? themeMeta.highlighter as 'shiki'
146 : undefined
147 const themeColorSchema = ['light', 'dark'].includes(themeMeta.colorSchema || '')
148 ? themeMeta.colorSchema as 'light' | 'dark'
149 : undefined
150
151 if (themeColorSchema && config.colorSchema !== themeColorSchema)
152 warn(`Color schema "${config.colorSchema}" is not supported by the theme`)
153
154 if (themeHightlighter && config.highlighter !== themeHightlighter)
155 warn(`Syntax highlighter "${config.highlighter}" does not supported by the theme`)
156
157 if (config.css !== 'unocss') {
158 warn(`Unsupported Atomic CSS engine "${config.css}", fallback to UnoCSS`)
159 config.css = 'unocss'
160 }
161 }
162
163 export function resolveFonts(fonts: FontOptions = {}): ResolvedFontOptions {
164 const {
165 fallbacks = true,
166 italic = false,
167 provider = 'google',
168 body = 'sans',
169 } = fonts
170 let sans = toArray(fonts.sans).flatMap(i => i.split(',')).map(i => i.trim())
171 let serif = toArray(fonts.serif).flatMap(i => i.split(',')).map(i => i.trim())
172 let mono = toArray(fonts.mono).flatMap(i => i.split(',')).map(i => i.trim())
173 const weights = toArray(fonts.weights || '200,400,600').flatMap(i => i.toString().split(',')).map(i => i.trim())
174 const custom = toArray(fonts.custom).flatMap(i => i.split(',')).map(i => i.trim())
175
176 const local = toArray(fonts.local).flatMap(i => i.split(',')).map(i => i.trim())
177 const webfonts = fonts.webfonts
178 ? fonts.webfonts
179 : fallbacks
180 ? uniq([
181 ...sans,
182 ...serif,
183 ...mono,
184 ...custom,
185 ]).filter(i => !local.includes(i))
186 : []
187
188 function toQuoted(font: string) {
189 if (RE_QUOTED_STRING.test(font))
190 return font
191 return `"${font}"`
192 }
193
194 if (fallbacks) {
195 sans = uniq([
196 ...sans.map(toQuoted),
197 'ui-sans-serif',
198 'system-ui',
199 '-apple-system',
200 'BlinkMacSystemFont',
201 '"Segoe UI"',
202 'Roboto',
203 '"Helvetica Neue"',
204 'Arial',
205 '"Noto Sans"',
206 'sans-serif',
207 '"Apple Color Emoji"',
208 '"Segoe UI Emoji"',
209 '"Segoe UI Symbol"',
210 '"Noto Color Emoji"',
211 ])
212 serif = uniq([
213 ...serif.map(toQuoted),
214 'ui-serif',
215 'Georgia',
216 'Cambria',
217 '"Times New Roman"',
218 'Times',
219 'serif',
220 ])
221 mono = uniq([
222 ...mono.map(toQuoted),
223 'ui-monospace',
224 'SFMono-Regular',
225 'Menlo',
226 'Monaco',
227 'Consolas',
228 '"Liberation Mono"',
229 '"Courier New"',
230 'monospace',
231 ])
232 }
233
234 return {
235 sans,
236 serif,
237 mono,
238 body,
239 webfonts,
240 provider,
241 local,
242 italic,
243 weights,
244 }
245 }
246
247 function resolveDrawings(options: DrawingsOptions = {}, filepath?: string): ResolvedDrawingsOptions {
248 const {
249 enabled = true,
250 persist = false,
251 presenterOnly = false,
252 syncAll = true,
253 } = options
254
255 const persistPath = typeof persist === 'string'
256 ? persist
257 : persist
258 ? `.slidev/drawings${filepath
259 ? `/${filepath.match(RE_FILENAME_STEM)?.[1]}`
260 : ''}`
261 : false
262
263 return {
264 enabled,
265 persist: persistPath,
266 presenterOnly,
267 syncAll,
268 }
269 }
270
270 lines TYPESCRIPT