update parse-content && config.yml

This commit is contained in:
Jinksi 2017-11-12 12:18:04 +10:00
parent 4f077cf372
commit feac99e067
4 changed files with 42 additions and 20 deletions

View file

@ -9,6 +9,7 @@ const util = require('util')
const glob = util.promisify(globCb)
const readFile = util.promisify(fs.readFile)
const matter = require('gray-matter')
const yaml = require('js-yaml')
const options = {
contentDir: './content/',
@ -17,7 +18,9 @@ const options = {
const getCollectionType = filePath => {
const pathParsed = path.parse(filePath)
const objectKey = pathParsed.dir.replace(options.contentDir, '').replace(/\//g, '.')
const objectKey = pathParsed.dir
.replace(options.contentDir, '')
.replace(/\//g, '.')
return `${objectKey}`
}
@ -33,33 +36,45 @@ const getDocumentExt = filePath => {
const parseMarkdown = data => {
data = matter(data)
data = {...data, ...data.data}
data = { ...data, ...data.data }
delete data.data
return JSON.stringify(data)
}
const parseYaml = data => {
try {
data = yaml.safeLoad(data, 'utf8')
return JSON.stringify(data)
} catch (e) {
return console.log(e)
}
}
const getFileContents = filePath => {
return readFile(filePath, 'utf8')
.then(data => {
if (getDocumentExt(filePath) === '.md') {
data = parseMarkdown(data)
}
let documentData = JSON.parse(data)
documentData.name = getDocumentName(filePath)
let obj = {}
_set(obj, getCollectionType(filePath), [documentData])
console.log(`✨ Processed ${filePath}`)
return obj
})
return readFile(filePath, 'utf8').then(data => {
if (getDocumentExt(filePath) === '.md') {
data = parseMarkdown(data)
}
if (['.yaml', '.yml'].includes(getDocumentExt(filePath))) {
data = parseYaml(data)
}
let documentData = JSON.parse(data)
documentData.name = getDocumentName(filePath)
let obj = {}
_set(obj, getCollectionType(filePath), [documentData])
console.log(`✨ Processed ${filePath}`)
return obj
})
}
const readFiles = async paths => Promise.all(paths.map(getFileContents))
const combineJSON = async () => {
// mergeCustomiser concats arrays items
const mergeCustomiser = (objValue, srcValue) => _isArray(objValue) ? objValue.concat(srcValue) : objValue
const mergeCustomiser = (objValue, srcValue) =>
_isArray(objValue) ? objValue.concat(srcValue) : objValue
console.log(`✨ Reading JSON files in ${options.contentDir}`)
const paths = await glob(`${options.contentDir}/**/**.+(json|md)`)
const paths = await glob(`${options.contentDir}/**/**.+(json|md|yaml|yml)`)
const results = await readFiles(paths)
const data = _mergeWith({}, ...results, mergeCustomiser)
return JSON.stringify(data, null, 2)

View file

@ -13,7 +13,8 @@
"eslint-plugin-react": "^7.4.0",
"eslint-plugin-standard": "^3.0.1",
"glob": "^7.1.2",
"gray-matter": "^3.0.8",
"gray-matter": "^3.1.1",
"js-yaml": "^3.10.0",
"react-scripts": "^1.0.10",
"snazzy": "^7.0.0",
"standard": "^10.0.2",

View file

@ -9,6 +9,7 @@ public_folder: "/images/uploads" # The src attribute for uploaded media will beg
collections: # A list of collections the CMS should be able to edit
- name: "settings"
label: "Settings"
delete: false # Prevent users from deleting documents in this collection
editor:
preview: false
files:
@ -17,6 +18,11 @@ collections: # A list of collections the CMS should be able to edit
name: "global-settings"
fields:
- {label: Site Title, name: siteTitle, widget: string}
- file: "content/settings/global-yaml.yml"
label: "Global Settings - Yaml"
name: "global-settings-yaml"
fields:
- {label: Site Title, name: siteTitle, widget: string}
- name: "pages"
label: "Pages"
files:

View file

@ -3273,9 +3273,9 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9:
version "1.0.1"
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"
gray-matter@^3.0.8:
version "3.0.8"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-3.0.8.tgz#44bdaff586ab4e61e3fe2d30d577da0e502afb93"
gray-matter@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-3.1.1.tgz#101f80d9e69eeca6765cdce437705b18f40876ac"
dependencies:
extend-shallow "^2.0.1"
js-yaml "^3.10.0"