mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-29 00:47:03 +02:00
feat: throw error if there is wrong field on siteConfig.js
This commit is contained in:
parent
ef17da741a
commit
9965eec798
4 changed files with 41 additions and 4 deletions
|
@ -3,7 +3,7 @@ const chalk = require('chalk');
|
||||||
const escapeHtml = require('escape-html');
|
const escapeHtml = require('escape-html');
|
||||||
|
|
||||||
export default (str, rawLang) => {
|
export default (str, rawLang) => {
|
||||||
if (rawLang === 'text') {
|
if (rawLang === 'text' || !rawLang) {
|
||||||
return escapeHtml(str);
|
return escapeHtml(str);
|
||||||
}
|
}
|
||||||
const lang = rawLang.toLowerCase();
|
const lang = rawLang.toLowerCase();
|
||||||
|
|
|
@ -18,9 +18,27 @@ module.exports = function loadConfig(siteDir, deleteCache = true) {
|
||||||
'projectName',
|
'projectName',
|
||||||
'baseUrl'
|
'baseUrl'
|
||||||
];
|
];
|
||||||
|
const optionalFields = [
|
||||||
|
'customDocsPath',
|
||||||
|
'themePath',
|
||||||
|
'highlight',
|
||||||
|
'markdownPlugins'
|
||||||
|
];
|
||||||
const missingFields = requiredFields.filter(field => !config[field]);
|
const missingFields = requiredFields.filter(field => !config[field]);
|
||||||
if (missingFields && missingFields.length > 0) {
|
if (missingFields && missingFields.length > 0) {
|
||||||
throw new Error(`${missingFields.join(', ')} are missing in siteConfig.js`);
|
throw new Error(
|
||||||
|
`${missingFields.join(', ')} fields are missing in siteConfig.js`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const uselessFields = Object.keys(config).filter(
|
||||||
|
field => ![...requiredFields, ...optionalFields].includes(field)
|
||||||
|
);
|
||||||
|
if (uselessFields && uselessFields.length > 0) {
|
||||||
|
throw new Error(
|
||||||
|
`${uselessFields.join(', ')} fields are useless in siteConfig.js`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
10
test/load/__fixtures__/wrong-site/siteConfig.js
Normal file
10
test/load/__fixtures__/wrong-site/siteConfig.js
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
module.exports = {
|
||||||
|
title: 'Hello',
|
||||||
|
tagline: 'Hello World',
|
||||||
|
organizationName: 'endiliey',
|
||||||
|
projectName: 'hello',
|
||||||
|
baseUrl: '/',
|
||||||
|
useLessField: 'what',
|
||||||
|
superman: 'lol',
|
||||||
|
admin: 'endi'
|
||||||
|
};
|
|
@ -20,7 +20,16 @@ describe('loadConfig', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
loadConfig(siteDir);
|
loadConfig(siteDir);
|
||||||
}).toThrowErrorMatchingInlineSnapshot(
|
}).toThrowErrorMatchingInlineSnapshot(
|
||||||
`"tagline, organizationName, projectName are missing in siteConfig.js"`
|
`"tagline, organizationName, projectName fields are missing in siteConfig.js"`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('website with useless field (wrong field) in siteConfig', () => {
|
||||||
|
const siteDir = path.join(__dirname, '__fixtures__', 'wrong-site');
|
||||||
|
expect(() => {
|
||||||
|
loadConfig(siteDir);
|
||||||
|
}).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"useLessField, superman, admin fields are useless in siteConfig.js"`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -29,7 +38,7 @@ describe('loadConfig', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
loadConfig(siteDir);
|
loadConfig(siteDir);
|
||||||
}).toThrowErrorMatchingInlineSnapshot(
|
}).toThrowErrorMatchingInlineSnapshot(
|
||||||
`"title, tagline, organizationName, projectName, baseUrl are missing in siteConfig.js"`
|
`"title, tagline, organizationName, projectName, baseUrl fields are missing in siteConfig.js"`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue