Skip to content

Commit

Permalink
feat: support toml config (#138)
Browse files Browse the repository at this point in the history
* feat: support toml config

* remove config.toml

* fix: proper iteration over meta tags
  • Loading branch information
lbenie authored and yyx990803 committed Apr 23, 2018
1 parent 802a97d commit d136e22
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
35 changes: 33 additions & 2 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 || {}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

[email protected]:
version "2.0.2"
resolved "https://registry.yarnpkg.com/topo/-/topo-2.0.2.tgz#cd5615752539057c0dc0491a621c3bc6fbe1d182"
Expand Down

0 comments on commit d136e22

Please sign in to comment.