提交 d136e222 编写于 作者: L Lucien Bénié 提交者: Evan You

feat: support toml config (#138)

* feat: support toml config

* remove config.toml

* fix: proper iteration over meta tags
上级 802a97d2
......@@ -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 || {}
}
......@@ -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"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册