From 9965eec798d9ecd78e36430297ebe430ccad628d Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 29 Aug 2018 23:57:26 +0800 Subject: [PATCH] feat: throw error if there is wrong field on siteConfig.js --- lib/core/components/Markdown/highlight.js | 2 +- lib/load/config.js | 20 ++++++++++++++++++- .../__fixtures__/wrong-site/siteConfig.js | 10 ++++++++++ test/load/config.test.js | 13 ++++++++++-- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 test/load/__fixtures__/wrong-site/siteConfig.js diff --git a/lib/core/components/Markdown/highlight.js b/lib/core/components/Markdown/highlight.js index 2b2c4f815d..643856b550 100644 --- a/lib/core/components/Markdown/highlight.js +++ b/lib/core/components/Markdown/highlight.js @@ -3,7 +3,7 @@ const chalk = require('chalk'); const escapeHtml = require('escape-html'); export default (str, rawLang) => { - if (rawLang === 'text') { + if (rawLang === 'text' || !rawLang) { return escapeHtml(str); } const lang = rawLang.toLowerCase(); diff --git a/lib/load/config.js b/lib/load/config.js index b5269b5a2f..92540265e4 100644 --- a/lib/load/config.js +++ b/lib/load/config.js @@ -18,9 +18,27 @@ module.exports = function loadConfig(siteDir, deleteCache = true) { 'projectName', 'baseUrl' ]; + const optionalFields = [ + 'customDocsPath', + 'themePath', + 'highlight', + 'markdownPlugins' + ]; const missingFields = requiredFields.filter(field => !config[field]); 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; }; diff --git a/test/load/__fixtures__/wrong-site/siteConfig.js b/test/load/__fixtures__/wrong-site/siteConfig.js new file mode 100644 index 0000000000..c84b9e4594 --- /dev/null +++ b/test/load/__fixtures__/wrong-site/siteConfig.js @@ -0,0 +1,10 @@ +module.exports = { + title: 'Hello', + tagline: 'Hello World', + organizationName: 'endiliey', + projectName: 'hello', + baseUrl: '/', + useLessField: 'what', + superman: 'lol', + admin: 'endi' +}; diff --git a/test/load/config.test.js b/test/load/config.test.js index f0015a0770..817346229f 100644 --- a/test/load/config.test.js +++ b/test/load/config.test.js @@ -20,7 +20,16 @@ describe('loadConfig', () => { expect(() => { loadConfig(siteDir); }).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(() => { loadConfig(siteDir); }).toThrowErrorMatchingInlineSnapshot( - `"title, tagline, organizationName, projectName, baseUrl are missing in siteConfig.js"` + `"title, tagline, organizationName, projectName, baseUrl fields are missing in siteConfig.js"` ); }); });