mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-12 15:52:39 +02:00
fix(core): configValidation should allow inline theme functions (#6496)
This commit is contained in:
parent
c99026c524
commit
f5f598a921
3 changed files with 175 additions and 24 deletions
|
@ -108,7 +108,76 @@ exports[`normalizeConfig should throw error if scripts doesn't have src 1`] = `
|
||||||
"
|
"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`normalizeConfig should throw error if themes is not a string and it's not an array #1 for the input of: [123] 1`] = `
|
||||||
|
" => Bad Docusaurus theme value as path [themes,0].
|
||||||
|
Example valid theme config:
|
||||||
|
{
|
||||||
|
themes: [
|
||||||
|
[\\"@docusaurus/theme-classic\\",options],
|
||||||
|
\\"./myTheme\\",
|
||||||
|
[\\"./myTheme\\",{someOption: 42}],
|
||||||
|
function myTheme() { },
|
||||||
|
[function myTheme() { },options]
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`normalizeConfig should throw error if themes is not an array of [string, object][] #1 for the input of: [[Array]] 1`] = `
|
||||||
|
" => Bad Docusaurus theme value as path [themes,0].
|
||||||
|
Example valid theme config:
|
||||||
|
{
|
||||||
|
themes: [
|
||||||
|
[\\"@docusaurus/theme-classic\\",options],
|
||||||
|
\\"./myTheme\\",
|
||||||
|
[\\"./myTheme\\",{someOption: 42}],
|
||||||
|
function myTheme() { },
|
||||||
|
[function myTheme() { },options]
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`normalizeConfig should throw error if themes is not an array of [string, object][] #2 for the input of: [[Array]] 1`] = `
|
||||||
|
" => Bad Docusaurus theme value as path [themes,0].
|
||||||
|
Example valid theme config:
|
||||||
|
{
|
||||||
|
themes: [
|
||||||
|
[\\"@docusaurus/theme-classic\\",options],
|
||||||
|
\\"./myTheme\\",
|
||||||
|
[\\"./myTheme\\",{someOption: 42}],
|
||||||
|
function myTheme() { },
|
||||||
|
[function myTheme() { },options]
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`normalizeConfig should throw error if themes is not an array of [string, object][] #3 for the input of: [[Array]] 1`] = `
|
||||||
|
" => Bad Docusaurus theme value as path [themes,0].
|
||||||
|
Example valid theme config:
|
||||||
|
{
|
||||||
|
themes: [
|
||||||
|
[\\"@docusaurus/theme-classic\\",options],
|
||||||
|
\\"./myTheme\\",
|
||||||
|
[\\"./myTheme\\",{someOption: 42}],
|
||||||
|
function myTheme() { },
|
||||||
|
[function myTheme() { },options]
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`normalizeConfig should throw error if themes is not array 1`] = `
|
exports[`normalizeConfig should throw error if themes is not array 1`] = `
|
||||||
"\\"themes\\" must be an array
|
"\\"themes\\" must be an array
|
||||||
"
|
"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`normalizeConfig should throw error if themes is not array for the input of: {} 1`] = `
|
||||||
|
"\\"themes\\" must be an array
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
|
@ -117,6 +117,32 @@ describe('normalizeConfig', () => {
|
||||||
}).toThrowErrorMatchingSnapshot();
|
}).toThrowErrorMatchingSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
['should throw error if themes is not array', {}],
|
||||||
|
[
|
||||||
|
"should throw error if themes is not a string and it's not an array #1",
|
||||||
|
[123],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'should throw error if themes is not an array of [string, object][] #1',
|
||||||
|
[['example/path', 'wrong parameter here']],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'should throw error if themes is not an array of [string, object][] #2',
|
||||||
|
[[{}, 'example/path']],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'should throw error if themes is not an array of [string, object][] #3',
|
||||||
|
[[{}, {}]],
|
||||||
|
],
|
||||||
|
])(`%s for the input of: %p`, (_message, themes) => {
|
||||||
|
expect(() => {
|
||||||
|
normalizeConfig({
|
||||||
|
themes,
|
||||||
|
});
|
||||||
|
}).toThrowErrorMatchingSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
test.each([
|
test.each([
|
||||||
['should accept [string] for plugins', ['plain/string']],
|
['should accept [string] for plugins', ['plain/string']],
|
||||||
[
|
[
|
||||||
|
@ -155,6 +181,44 @@ describe('normalizeConfig', () => {
|
||||||
}).not.toThrowError();
|
}).not.toThrowError();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
['should accept [string] for themes', ['plain/string']],
|
||||||
|
[
|
||||||
|
'should accept string[] for themes',
|
||||||
|
['plain/string', 'another/plain/string/path'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'should accept [string, object] for themes',
|
||||||
|
[['plain/string', {it: 'should work'}]],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'should accept [string, object][] for themes',
|
||||||
|
[
|
||||||
|
['plain/string', {it: 'should work'}],
|
||||||
|
['this/should/work', {too: 'yes'}],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'should accept ([string, object]|string)[] for themes',
|
||||||
|
[
|
||||||
|
'plain/string',
|
||||||
|
['plain', {it: 'should work'}],
|
||||||
|
['this/should/work', {too: 'yes'}],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
['should accept function for theme', [function (_context, _options) {}]],
|
||||||
|
[
|
||||||
|
'should accept [function, object] for theme',
|
||||||
|
[[function (_context, _options) {}, {it: 'should work'}]],
|
||||||
|
],
|
||||||
|
])(`%s for the input of: %p`, (_message, themes) => {
|
||||||
|
expect(() => {
|
||||||
|
normalizeConfig({
|
||||||
|
themes,
|
||||||
|
});
|
||||||
|
}).not.toThrowError();
|
||||||
|
});
|
||||||
|
|
||||||
test('should throw error if themes is not array', () => {
|
test('should throw error if themes is not array', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
normalizeConfig({
|
normalizeConfig({
|
||||||
|
|
|
@ -55,21 +55,33 @@ export const DEFAULT_CONFIG: Pick<
|
||||||
staticDirectories: [STATIC_DIR_NAME],
|
staticDirectories: [STATIC_DIR_NAME],
|
||||||
};
|
};
|
||||||
|
|
||||||
const PluginSchema = Joi.alternatives()
|
function createPluginSchema(theme: boolean = false) {
|
||||||
.try(
|
return (
|
||||||
Joi.function(),
|
Joi.alternatives()
|
||||||
Joi.array().ordered(Joi.function().required(), Joi.object().required()),
|
.try(
|
||||||
Joi.string(),
|
Joi.function(),
|
||||||
Joi.array()
|
Joi.array().ordered(Joi.function().required(), Joi.object().required()),
|
||||||
.ordered(Joi.string().required(), Joi.object().required())
|
Joi.string(),
|
||||||
.length(2),
|
Joi.array()
|
||||||
Joi.bool().equal(false), // In case of conditional adding of plugins.
|
.ordered(Joi.string().required(), Joi.object().required())
|
||||||
)
|
.length(2),
|
||||||
// @ts-expect-error: bad lib def, doesn't recognize an array of reports
|
Joi.bool().equal(false), // In case of conditional adding of plugins.
|
||||||
.error((errors) => {
|
)
|
||||||
errors.forEach((error) => {
|
// @ts-expect-error: bad lib def, doesn't recognize an array of reports
|
||||||
error.message = ` => Bad Docusaurus plugin value as path [${error.path}].
|
.error((errors) => {
|
||||||
Example valid plugin config:
|
errors.forEach((error) => {
|
||||||
|
const validConfigExample = theme
|
||||||
|
? `Example valid theme config:
|
||||||
|
{
|
||||||
|
themes: [
|
||||||
|
["@docusaurus/theme-classic",options],
|
||||||
|
"./myTheme",
|
||||||
|
["./myTheme",{someOption: 42}],
|
||||||
|
function myTheme() { },
|
||||||
|
[function myTheme() { },options]
|
||||||
|
],
|
||||||
|
};`
|
||||||
|
: `Example valid plugin config:
|
||||||
{
|
{
|
||||||
plugins: [
|
plugins: [
|
||||||
["@docusaurus/plugin-content-docs",options],
|
["@docusaurus/plugin-content-docs",options],
|
||||||
|
@ -78,16 +90,22 @@ Example valid plugin config:
|
||||||
function myPlugin() { },
|
function myPlugin() { },
|
||||||
[function myPlugin() { },options]
|
[function myPlugin() { },options]
|
||||||
],
|
],
|
||||||
};
|
};`;
|
||||||
`;
|
|
||||||
});
|
|
||||||
return errors;
|
|
||||||
});
|
|
||||||
|
|
||||||
const ThemeSchema = Joi.alternatives().try(
|
error.message = ` => Bad Docusaurus ${
|
||||||
Joi.string(),
|
theme ? 'theme' : 'plugin'
|
||||||
Joi.array().items(Joi.string().required(), Joi.object().required()).length(2),
|
} value as path [${error.path}].
|
||||||
);
|
${validConfigExample}
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
return errors;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const PluginSchema = createPluginSchema(false);
|
||||||
|
|
||||||
|
const ThemeSchema = createPluginSchema(true);
|
||||||
|
|
||||||
const PresetSchema = Joi.alternatives().try(
|
const PresetSchema = Joi.alternatives().try(
|
||||||
Joi.string(),
|
Joi.string(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue