parse-content (JSON)
This commit is contained in:
parent
bd130d0f96
commit
1cf9fa1a24
2 changed files with 44 additions and 45 deletions
44
utils/parse-content.js
Normal file
44
utils/parse-content.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const _set = require('lodash/set')
|
||||
const _merge = require('lodash/merge')
|
||||
const globCb = require('glob')
|
||||
const util = require('util')
|
||||
|
||||
const glob = util.promisify(globCb)
|
||||
const readFile = util.promisify(fs.readFile)
|
||||
|
||||
const options = {
|
||||
contentDir: './content/',
|
||||
outputFile: './src/data.json'
|
||||
}
|
||||
|
||||
const getNestedKey = filePath => {
|
||||
const pathParsed = path.parse(filePath)
|
||||
const objectKey = pathParsed.dir.replace(options.contentDir, '').replace(/\//g, '.')
|
||||
return `${objectKey}.${pathParsed.name}`
|
||||
}
|
||||
|
||||
const getFileContents = filePath =>
|
||||
readFile(filePath, 'utf8')
|
||||
.then(data => {
|
||||
let obj = {}
|
||||
_set(obj, getNestedKey(filePath), data)
|
||||
return obj
|
||||
})
|
||||
|
||||
const readFiles = async paths => Promise.all(paths.map(getFileContents))
|
||||
|
||||
const combineJSON = async () => {
|
||||
const paths = await glob(`${options.contentDir}/**/**.json`)
|
||||
const results = await readFiles(paths)
|
||||
const data = _merge({}, ...results)
|
||||
return JSON.stringify(data, null, 2)
|
||||
}
|
||||
|
||||
const writeJSON = async () => {
|
||||
const json = await combineJSON()
|
||||
fs.writeFileSync(options.outputFile, json)
|
||||
}
|
||||
|
||||
writeJSON()
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const globCb = require('glob')
|
||||
const util = require('util')
|
||||
|
||||
const glob = util.promisify(globCb)
|
||||
const readFile = util.promisify(fs.readFileSync)
|
||||
|
||||
const options = {
|
||||
contentDir: './content/',
|
||||
outputFile: './src/data.json'
|
||||
}
|
||||
|
||||
const getFileContents = async file => {
|
||||
const result = await readFile(file, 'utf8')
|
||||
return result
|
||||
}
|
||||
|
||||
const readFiles = async paths => {
|
||||
const results = await Promise.all(paths.map(getFileContents))
|
||||
return results
|
||||
}
|
||||
|
||||
const makeJSON = async () => {
|
||||
const paths = await glob(`${options.contentDir}/**/**.json`)
|
||||
const results = await parseFiles(paths)
|
||||
return console.log(results)
|
||||
let data = {}
|
||||
results.map(item => {
|
||||
const pathParsed = path.parse(item.history[0])
|
||||
const objectKey = pathParsed.dir.replace(options.markdownDir, '').replace(/\//g, '.')
|
||||
const nest = `${objectKey}.${pathParsed.name}`
|
||||
_set(data, nest, {...item})
|
||||
})
|
||||
return JSON.stringify(data, null, 2)
|
||||
}
|
||||
|
||||
const markdownToJson = async () => {
|
||||
const json = await makeJSON()
|
||||
return console.log(json)
|
||||
fs.writeFileSync(options.outputFile, json)
|
||||
}
|
||||
|
||||
markdownToJson()
|
||||
Loading…
Add table
Reference in a new issue