forked from facebook/docusaurus
/
validateThemeConfig.ts
449 lines (424 loc) · 13.8 KB
/
validateThemeConfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import defaultPrismTheme from 'prism-react-renderer/themes/palenight';
import {Joi, URISchema} from '@docusaurus/utils-validation';
import type {ThemeConfig} from '@docusaurus/theme-common';
import type {ThemeConfigValidationContext} from '@docusaurus/types';
const DEFAULT_DOCS_CONFIG: ThemeConfig['docs'] = {
versionPersistence: 'localStorage',
sidebar: {
hideable: false,
autoCollapseCategories: false,
},
};
const DocsSchema = Joi.object({
versionPersistence: Joi.string()
.equal('localStorage', 'none')
.default(DEFAULT_DOCS_CONFIG.versionPersistence),
sidebar: Joi.object({
hideable: Joi.bool().default(DEFAULT_DOCS_CONFIG.sidebar.hideable),
autoCollapseCategories: Joi.bool().default(
DEFAULT_DOCS_CONFIG.sidebar.autoCollapseCategories,
),
}).default(DEFAULT_DOCS_CONFIG.sidebar),
}).default(DEFAULT_DOCS_CONFIG);
const DEFAULT_COLOR_MODE_CONFIG: ThemeConfig['colorMode'] = {
defaultMode: 'light',
disableSwitch: false,
respectPrefersColorScheme: false,
};
export const DEFAULT_CONFIG: ThemeConfig = {
colorMode: DEFAULT_COLOR_MODE_CONFIG,
docs: DEFAULT_DOCS_CONFIG,
metadata: [],
prism: {
additionalLanguages: [],
theme: defaultPrismTheme,
magicComments: [
{
className: 'theme-code-block-highlighted-line',
line: 'highlight-next-line',
block: {start: 'highlight-start', end: 'highlight-end'},
},
],
},
navbar: {
hideOnScroll: false,
items: [],
},
tableOfContents: {
minHeadingLevel: 2,
maxHeadingLevel: 3,
},
};
const NavbarItemPosition = Joi.string().equal('left', 'right').default('left');
const NavbarItemBaseSchema = Joi.object({
label: Joi.string(),
html: Joi.string(),
className: Joi.string(),
})
.nand('html', 'label')
// We allow any unknown attributes on the links (users may need additional
// attributes like target, aria-role, data-customAttribute...)
.unknown();
const DefaultNavbarItemSchema = NavbarItemBaseSchema.append({
to: Joi.string(),
href: URISchema,
activeBasePath: Joi.string(),
activeBaseRegex: Joi.string(),
prependBaseUrlToHref: Joi.bool(),
// This is only triggered in case of a nested dropdown
items: Joi.forbidden().messages({
'any.unknown': 'Nested dropdowns are not allowed',
}),
})
.xor('href', 'to')
.messages({
'object.xor': 'One and only one between "to" and "href" should be provided',
});
const DocsVersionNavbarItemSchema = NavbarItemBaseSchema.append({
type: Joi.string().equal('docsVersion').required(),
to: Joi.string(),
docsPluginId: Joi.string(),
});
const DocItemSchema = NavbarItemBaseSchema.append({
type: Joi.string().equal('doc').required(),
docId: Joi.string().required(),
docsPluginId: Joi.string(),
});
const DocSidebarItemSchema = NavbarItemBaseSchema.append({
type: Joi.string().equal('docSidebar').required(),
sidebarId: Joi.string().required(),
docsPluginId: Joi.string(),
});
const HtmlNavbarItemSchema = Joi.object({
className: Joi.string(),
type: Joi.string().equal('html').required(),
value: Joi.string().required(),
});
// A temporary workaround to allow users to add custom navbar items
// See https://github.com/facebook/docusaurus/issues/7227
const CustomNavbarItemRegexp = /custom-.*/;
const CustomNavbarItemSchema = Joi.object({
type: Joi.string().regex(CustomNavbarItemRegexp).required(),
}).unknown();
const itemWithType = (type: string | RegExp | undefined) => {
// Because equal(undefined) is not supported :/
const typeSchema =
// eslint-disable-next-line no-nested-ternary
type instanceof RegExp
? Joi.string().required().regex(type)
: type
? Joi.string().required().equal(type)
: Joi.string().forbidden();
return Joi.object({
type: typeSchema,
})
.unknown()
.required();
};
const DropdownSubitemSchema = Joi.object({
position: Joi.forbidden(),
}).when('.', {
switch: [
{
is: itemWithType('docsVersion'),
then: DocsVersionNavbarItemSchema,
},
{
is: itemWithType('doc'),
then: DocItemSchema,
},
{
is: itemWithType('docSidebar'),
then: DocSidebarItemSchema,
},
{
is: itemWithType(undefined),
then: DefaultNavbarItemSchema,
},
{
is: itemWithType('html'),
then: HtmlNavbarItemSchema,
},
{
is: itemWithType(CustomNavbarItemRegexp),
then: CustomNavbarItemSchema,
},
{
is: Joi.alternatives().try(
itemWithType('dropdown'),
itemWithType('docsVersionDropdown'),
itemWithType('localeDropdown'),
itemWithType('search'),
),
then: Joi.forbidden().messages({
'any.unknown': 'Nested dropdowns are not allowed',
}),
},
],
otherwise: Joi.forbidden().messages({
'any.unknown': 'Bad navbar item type {.type}',
}),
});
const DropdownNavbarItemSchema = NavbarItemBaseSchema.append({
items: Joi.array().items(DropdownSubitemSchema).required(),
});
const DocsVersionDropdownNavbarItemSchema = NavbarItemBaseSchema.append({
type: Joi.string().equal('docsVersionDropdown').required(),
docsPluginId: Joi.string(),
dropdownActiveClassDisabled: Joi.boolean(),
dropdownItemsBefore: Joi.array().items(DropdownSubitemSchema).default([]),
dropdownItemsAfter: Joi.array().items(DropdownSubitemSchema).default([]),
});
const LocaleDropdownNavbarItemSchema = NavbarItemBaseSchema.append({
type: Joi.string().equal('localeDropdown').required(),
dropdownItemsBefore: Joi.array().items(DropdownSubitemSchema).default([]),
dropdownItemsAfter: Joi.array().items(DropdownSubitemSchema).default([]),
});
const SearchItemSchema = Joi.object({
type: Joi.string().equal('search').required(),
className: Joi.string(),
});
const NavbarItemSchema = Joi.object({
position: NavbarItemPosition,
}).when('.', {
switch: [
{
is: itemWithType('docsVersion'),
then: DocsVersionNavbarItemSchema,
},
{
is: itemWithType('dropdown'),
then: DropdownNavbarItemSchema,
},
{
is: itemWithType('docsVersionDropdown'),
then: DocsVersionDropdownNavbarItemSchema,
},
{
is: itemWithType('doc'),
then: DocItemSchema,
},
{
is: itemWithType('docSidebar'),
then: DocSidebarItemSchema,
},
{
is: itemWithType('localeDropdown'),
then: LocaleDropdownNavbarItemSchema,
},
{
is: itemWithType('search'),
then: SearchItemSchema,
},
{
is: itemWithType('html'),
then: HtmlNavbarItemSchema,
},
{
is: itemWithType(CustomNavbarItemRegexp),
then: CustomNavbarItemSchema,
},
{
is: itemWithType(undefined),
then: Joi.object().when('.', {
// Dropdown item can be specified without type field
is: Joi.object({
items: Joi.array().required(),
}).unknown(),
then: DropdownNavbarItemSchema,
otherwise: DefaultNavbarItemSchema,
}),
},
],
otherwise: Joi.forbidden().messages({
'any.unknown': 'Bad navbar item type {.type}',
}),
});
const ColorModeSchema = Joi.object({
defaultMode: Joi.string()
.equal('dark', 'light')
.default(DEFAULT_COLOR_MODE_CONFIG.defaultMode),
disableSwitch: Joi.bool().default(DEFAULT_COLOR_MODE_CONFIG.disableSwitch),
respectPrefersColorScheme: Joi.bool().default(
DEFAULT_COLOR_MODE_CONFIG.respectPrefersColorScheme,
),
switchConfig: Joi.any().forbidden().messages({
'any.unknown':
'colorMode.switchConfig is deprecated. If you want to customize the icons for light and dark mode, swizzle IconLightMode, IconDarkMode, or ColorModeToggle instead.',
}),
}).default(DEFAULT_COLOR_MODE_CONFIG);
const HtmlMetadataSchema = Joi.object({
id: Joi.string(),
name: Joi.string(),
property: Joi.string(),
content: Joi.string(),
itemprop: Joi.string(),
}).unknown();
const FooterLinkItemSchema = Joi.object({
to: Joi.string(),
href: URISchema,
html: Joi.string(),
label: Joi.string(),
})
.xor('to', 'href', 'html')
.with('to', 'label')
.with('href', 'label')
.nand('html', 'label')
// We allow any unknown attributes on the links (users may need additional
// attributes like target, aria-role, data-customAttribute...)
.unknown();
const CustomCssSchema = Joi.alternatives()
.try(Joi.array().items(Joi.string().required()), Joi.string().required())
.optional();
export const ThemeConfigSchema = Joi.object<ThemeConfig>({
// TODO temporary (@alpha-58)
// @ts-expect-error: forbidden
disableDarkMode: Joi.any().forbidden().messages({
'any.unknown':
'disableDarkMode theme config is deprecated. Please use the new colorMode attribute. You likely want: config.themeConfig.colorMode.disableSwitch = true',
}),
// TODO temporary (@alpha-58)
defaultDarkMode: Joi.any().forbidden().messages({
'any.unknown':
'defaultDarkMode theme config is deprecated. Please use the new colorMode attribute. You likely want: config.themeConfig.colorMode.defaultMode = "dark"',
}),
customCss: CustomCssSchema,
colorMode: ColorModeSchema,
image: Joi.string(),
docs: DocsSchema,
metadata: Joi.array()
.items(HtmlMetadataSchema)
.default(DEFAULT_CONFIG.metadata),
// cSpell:ignore metadatas
metadatas: Joi.any().forbidden().messages({
'any.unknown':
// cSpell:ignore metadatas
'themeConfig.metadatas has been renamed as themeConfig.metadata. See https://github.com/facebook/docusaurus/pull/5871',
}),
announcementBar: Joi.object({
id: Joi.string().default('announcement-bar'),
content: Joi.string().required(),
backgroundColor: Joi.string(),
textColor: Joi.string(),
isCloseable: Joi.bool().default(true),
}).optional(),
navbar: Joi.object({
style: Joi.string().equal('dark', 'primary'),
hideOnScroll: Joi.bool().default(DEFAULT_CONFIG.navbar.hideOnScroll),
// TODO temporary (@alpha-58)
links: Joi.any().forbidden().messages({
'any.unknown':
'themeConfig.navbar.links has been renamed as themeConfig.navbar.items',
}),
items: Joi.array()
.items(NavbarItemSchema)
.default(DEFAULT_CONFIG.navbar.items),
title: Joi.string().allow('', null),
logo: Joi.object({
alt: Joi.string().allow(''),
src: Joi.string().required(),
srcDark: Joi.string(),
width: Joi.alternatives().try(Joi.string(), Joi.number()),
height: Joi.alternatives().try(Joi.string(), Joi.number()),
href: Joi.string(),
target: Joi.string(),
}),
}).default(DEFAULT_CONFIG.navbar),
footer: Joi.object({
style: Joi.string().equal('dark', 'light').default('light'),
logo: Joi.object({
alt: Joi.string().allow(''),
src: Joi.string().required(),
srcDark: Joi.string(),
// TODO infer this from reading the image
width: Joi.alternatives().try(Joi.string(), Joi.number()),
height: Joi.alternatives().try(Joi.string(), Joi.number()),
href: Joi.string(),
target: Joi.string(),
}),
copyright: Joi.string(),
links: Joi.alternatives(
Joi.array().items(
Joi.object({
title: Joi.string().allow(null).default(null),
items: Joi.array().items(FooterLinkItemSchema).default([]),
}),
),
Joi.array().items(FooterLinkItemSchema),
)
.messages({
'alternatives.match': `The footer must be either simple or multi-column, and not a mix of the two. See: https://docusaurus.io/docs/api/themes/configuration#footer-links`,
})
.default([]),
}).optional(),
prism: Joi.object({
theme: Joi.object({
plain: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
styles: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
}).default(DEFAULT_CONFIG.prism.theme),
darkTheme: Joi.object({
plain: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
styles: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
}),
defaultLanguage: Joi.string(),
additionalLanguages: Joi.array()
.items(Joi.string())
.default(DEFAULT_CONFIG.prism.additionalLanguages),
magicComments: Joi.array()
.items(
Joi.object({
className: Joi.string().required(),
line: Joi.string(),
block: Joi.object({
start: Joi.string().required(),
end: Joi.string().required(),
}),
}).or('line', 'block'),
)
.default(DEFAULT_CONFIG.prism.magicComments),
})
.default(DEFAULT_CONFIG.prism)
.unknown(),
hideableSidebar: Joi.forbidden().messages({
'any.unknown':
'themeConfig.hideableSidebar has been moved to themeConfig.docs.sidebar.hideable.',
}),
autoCollapseSidebarCategories: Joi.forbidden().messages({
'any.unknown':
'themeConfig.autoCollapseSidebarCategories has been moved to themeConfig.docs.sidebar.autoCollapseCategories.',
}),
sidebarCollapsible: Joi.forbidden().messages({
'any.unknown':
'The themeConfig.sidebarCollapsible has been moved to docs plugin options. See: https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-docs',
}),
tableOfContents: Joi.object({
minHeadingLevel: Joi.number()
.default(DEFAULT_CONFIG.tableOfContents.minHeadingLevel)
.when('maxHeadingLevel', {
is: Joi.exist(),
then: Joi.number()
.integer()
.min(2)
.max(6)
.max(Joi.ref('maxHeadingLevel')),
otherwise: Joi.number().integer().min(2).max(6),
}),
maxHeadingLevel: Joi.number()
.integer()
.min(2)
.max(6)
.default(DEFAULT_CONFIG.tableOfContents.maxHeadingLevel),
}).default(DEFAULT_CONFIG.tableOfContents),
});
export function validateThemeConfig({
validate,
themeConfig,
}: ThemeConfigValidationContext<ThemeConfig>): ThemeConfig {
return validate(ThemeConfigSchema, themeConfig);
}