diff --git a/lib/prepare.js b/lib/prepare.js index a3b12d684e..63a0dae7e1 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -2,6 +2,7 @@ const path = require('path') const fs = require('fs-extra') const globby = require('globby') const yamlParser = require('js-yaml') +const tomlParser = require('toml') const yaml = require('yaml-front-matter') const createMarkdown = require('./markdown') const tempPath = path.resolve(__dirname, 'app/.temp') @@ -69,12 +70,14 @@ async function resolveOptions (sourceDir) { const vuepressDir = path.resolve(sourceDir, '.vuepress') const configPath = path.resolve(vuepressDir, 'config.js') const configYmlPath = path.resolve(vuepressDir, 'config.yml') + const configTomlPath = path.resolve(vuepressDir, 'config.toml') delete require.cache[configPath] let siteConfig = {} if (fs.existsSync(configYmlPath)) { - const content = await fs.readFile(configYmlPath, 'utf-8') - siteConfig = yamlParser.safeLoad(content) + siteConfig = await parseConfig(configYmlPath) + } else if (fs.existsSync(configTomlPath)) { + siteConfig = await parseConfig(configTomlPath) } else if (fs.existsSync(configPath)) { siteConfig = require(configPath) } @@ -283,3 +286,31 @@ function sort (arr) { return 0 }) } + +async function parseConfig (file) { + const content = await fs.readFile(file, 'utf-8') + const [extension] = /.\w+$/.exec(file) + let data + + switch (extension) { + case '.yml': + case '.yaml': + data = yamlParser.safeLoad(content) + break + + case '.toml': + data = tomlParser.parse(content) + // reformat to match config since TOML does not allow different data type + // https://github.com/toml-lang/toml#array + const format = [] + Object.keys(data.head).forEach(meta => { + data.head[meta].forEach(values => { + format.push([meta, values]) + }) + }) + data.head = format + break + } + + return data || {} +} diff --git a/package.json b/package.json index 4c77eb9df8..6fbc2553c9 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "semver": "^5.5.0", "stylus": "^0.54.5", "stylus-loader": "^3.0.2", + "toml": "^2.3.3", "url-loader": "^1.0.1", "vue": "^2.5.16", "vue-loader": "^15.0.0-rc.1", diff --git a/yarn.lock b/yarn.lock index a30b0c56a2..29be7b94a9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6035,6 +6035,10 @@ to-regex@^3.0.1, to-regex@^3.0.2: regex-not "^1.0.2" safe-regex "^1.1.0" +toml@^2.3.3: + version "2.3.3" + resolved "https://registry.yarnpkg.com/toml/-/toml-2.3.3.tgz#8d683d729577cb286231dfc7a8affe58d31728fb" + topo@2.x.x: version "2.0.2" resolved "https://registry.yarnpkg.com/topo/-/topo-2.0.2.tgz#cd5615752539057c0dc0491a621c3bc6fbe1d182"