Skip to content

Commit d136e22

Browse files
lbenieyyx990803
authored andcommitted
feat: support toml config (#138)
* feat: support toml config * remove config.toml * fix: proper iteration over meta tags
1 parent 802a97d commit d136e22

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

lib/prepare.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const fs = require('fs-extra')
33
const globby = require('globby')
44
const yamlParser = require('js-yaml')
5+
const tomlParser = require('toml')
56
const yaml = require('yaml-front-matter')
67
const createMarkdown = require('./markdown')
78
const tempPath = path.resolve(__dirname, 'app/.temp')
@@ -69,12 +70,14 @@ async function resolveOptions (sourceDir) {
6970
const vuepressDir = path.resolve(sourceDir, '.vuepress')
7071
const configPath = path.resolve(vuepressDir, 'config.js')
7172
const configYmlPath = path.resolve(vuepressDir, 'config.yml')
73+
const configTomlPath = path.resolve(vuepressDir, 'config.toml')
7274

7375
delete require.cache[configPath]
7476
let siteConfig = {}
7577
if (fs.existsSync(configYmlPath)) {
76-
const content = await fs.readFile(configYmlPath, 'utf-8')
77-
siteConfig = yamlParser.safeLoad(content)
78+
siteConfig = await parseConfig(configYmlPath)
79+
} else if (fs.existsSync(configTomlPath)) {
80+
siteConfig = await parseConfig(configTomlPath)
7881
} else if (fs.existsSync(configPath)) {
7982
siteConfig = require(configPath)
8083
}
@@ -283,3 +286,31 @@ function sort (arr) {
283286
return 0
284287
})
285288
}
289+
290+
async function parseConfig (file) {
291+
const content = await fs.readFile(file, 'utf-8')
292+
const [extension] = /.\w+$/.exec(file)
293+
let data
294+
295+
switch (extension) {
296+
case '.yml':
297+
case '.yaml':
298+
data = yamlParser.safeLoad(content)
299+
break
300+
301+
case '.toml':
302+
data = tomlParser.parse(content)
303+
// reformat to match config since TOML does not allow different data type
304+
// https://github.com/toml-lang/toml#array
305+
const format = []
306+
Object.keys(data.head).forEach(meta => {
307+
data.head[meta].forEach(values => {
308+
format.push([meta, values])
309+
})
310+
})
311+
data.head = format
312+
break
313+
}
314+
315+
return data || {}
316+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"semver": "^5.5.0",
7979
"stylus": "^0.54.5",
8080
"stylus-loader": "^3.0.2",
81+
"toml": "^2.3.3",
8182
"url-loader": "^1.0.1",
8283
"vue": "^2.5.16",
8384
"vue-loader": "^15.0.0-rc.1",

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -6035,6 +6035,10 @@ to-regex@^3.0.1, to-regex@^3.0.2:
60356035
regex-not "^1.0.2"
60366036
safe-regex "^1.1.0"
60376037

6038+
toml@^2.3.3:
6039+
version "2.3.3"
6040+
resolved "https://registry.yarnpkg.com/toml/-/toml-2.3.3.tgz#8d683d729577cb286231dfc7a8affe58d31728fb"
6041+
60386042
60396043
version "2.0.2"
60406044
resolved "https://registry.yarnpkg.com/topo/-/topo-2.0.2.tgz#cd5615752539057c0dc0491a621c3bc6fbe1d182"

0 commit comments

Comments
 (0)