diff --git a/functions/parse-content.js b/functions/parse-content.js index 495a784..9d1aad8 100644 --- a/functions/parse-content.js +++ b/functions/parse-content.js @@ -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) diff --git a/package.json b/package.json index 09e13c3..354d9af 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/public/admin/config.yml b/public/admin/config.yml index 30a8fcb..eb19684 100644 --- a/public/admin/config.yml +++ b/public/admin/config.yml @@ -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: diff --git a/yarn.lock b/yarn.lock index b54ed3e..39ad1bf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"