diff --git a/content/pages/about.json b/content/pages/about.json deleted file mode 100644 index e1c16ea..0000000 --- a/content/pages/about.json +++ /dev/null @@ -1 +0,0 @@ -{"title":"About page title","subtitle":"","body":"#### This is the about page body!\n\n`inline-code`\n","section1":"## Hello World!\n\nA sem vel nec sodales mi vivamus senectus sed potenti a parturient nascetur tincidunt nisi pulvinar rhoncus a. Risus imperdiet taciti suspendisse facilisi a per metus cubilia varius a nostra adipiscing amet ultrices quisque ac mi a. Dictumst a ultrices mi a dignissim ad fermentum eget a nam et a blandit scelerisque. Taciti lorem tempor quam vestibulum dis habitasse vestibulum diam vel est ut proin dis auctor. Suscipit scelerisque orci magna interdum vel bibendum duis netus a consectetur dui magnis ac aliquet sem posuere tincidunt vestibulum.\n","section2":"## This is a skinny center-aligned \n\nA sem vel nec sodales mi vivamus senectus sed potenti a parturient nascetur tincidunt nisi pulvinar rhoncus a. Risus imperdiet taciti suspendisse facilisi a per metus cubilia varius a nostra adipiscing amet ultrices quisque ac mi a. Dictumst a ultrices mi a dignissim ad fermentum eget a nam et a blandit scelerisque. Taciti lorem tempor quam vestibulum dis habitasse vestibulum diam vel est ut proin dis auctor. Suscipit scelerisque orci magna interdum vel bibendum duis netus a consectetur dui magnis ac aliquet sem posuere tincidunt vestibulum."} \ No newline at end of file diff --git a/content/pages/about.md b/content/pages/about.md new file mode 100644 index 0000000..1ca7afe --- /dev/null +++ b/content/pages/about.md @@ -0,0 +1,9 @@ +--- +title: About page title +subtitle: +section1: "## Hello World!\n\nNetlify CMS works with both `.md` and `.json`.\n" +section2: "## This is a skinny center-aligned \n\nA sem vel nec sodales mi vivamus senectus sed potenti a parturient nascetur tincidunt nisi pulvinar rhoncus a. Risus imperdiet taciti suspendisse facilisi a per metus cubilia varius a nostra adipiscing amet ultrices quisque ac mi a. Dictumst a ultrices mi a dignissim ad fermentum eget a nam et a blandit scelerisque. Taciti lorem tempor quam vestibulum dis habitasse vestibulum diam vel est ut proin dis auctor. Suscipit scelerisque orci magna interdum vel bibendum duis netus a consectetur dui magnis ac aliquet sem posuere tincidunt vestibulum." +--- +#### This is the about page body! + +`inline-code` diff --git a/functions/parse-content.js b/functions/parse-content.js index 72778ac..495a784 100644 --- a/functions/parse-content.js +++ b/functions/parse-content.js @@ -8,6 +8,7 @@ const util = require('util') const glob = util.promisify(globCb) const readFile = util.promisify(fs.readFile) +const matter = require('gray-matter') const options = { contentDir: './content/', @@ -25,9 +26,24 @@ const getDocumentName = filePath => { return `${pathParsed.name}` } +const getDocumentExt = filePath => { + const pathParsed = path.parse(filePath) + return `${pathParsed.ext}` +} + +const parseMarkdown = data => { + data = matter(data) + data = {...data, ...data.data} + delete data.data + return JSON.stringify(data) +} + 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 = {} @@ -43,7 +59,7 @@ const combineJSON = async () => { // mergeCustomiser concats arrays items 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`) + const paths = await glob(`${options.contentDir}/**/**.+(json|md)`) 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 1adb2d3..09e13c3 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "eslint-plugin-react": "^7.4.0", "eslint-plugin-standard": "^3.0.1", "glob": "^7.1.2", + "gray-matter": "^3.0.8", "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 8e6cda2..30a8fcb 100644 --- a/public/admin/config.yml +++ b/public/admin/config.yml @@ -27,13 +27,13 @@ collections: # A list of collections the CMS should be able to edit - {label: Title, name: title, widget: string} - {label: Subtitle, name: subtitle, widget: string} - {label: Body, name: body, widget: markdown} - - file: "content/pages/about.json" + # markdown files are ok + - file: "content/pages/about.md" label: "About Page" name: "about-page" fields: - {label: Title, name: title, widget: string} - {label: Subtitle, name: subtitle, widget: string} - - {label: Body, name: body, widget: markdown} - {label: Section 1, name: section1, widget: markdown} - {label: Section 2, name: section2, widget: markdown} - file: "content/pages/contact.json" diff --git a/yarn.lock b/yarn.lock index 9c8c0b4..b54ed3e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2811,6 +2811,12 @@ express@^4.13.3, express@^4.15.2: utils-merge "1.0.0" vary "~1.1.1" +extend-shallow@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f" + dependencies: + is-extendable "^0.1.0" + extend@~3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.0.tgz#5a474353b9f3353ddd8176dfd37b91c83a46f1d4" @@ -3267,6 +3273,15 @@ 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" + dependencies: + extend-shallow "^2.0.1" + js-yaml "^3.10.0" + kind-of "^5.0.2" + strip-bom-string "^1.0.0" + growly@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081" @@ -3728,7 +3743,7 @@ is-equal-shallow@^0.1.3: dependencies: is-primitive "^2.0.0" -is-extendable@^0.1.1: +is-extendable@^0.1.0, is-extendable@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89" @@ -4225,6 +4240,13 @@ js-tokens@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7" +js-yaml@^3.10.0, js-yaml@^3.9.1: + version "3.10.0" + resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc" + dependencies: + argparse "^1.0.7" + esprima "^4.0.0" + js-yaml@^3.4.3, js-yaml@^3.5.1, js-yaml@^3.7.0, js-yaml@~3.7.0: version "3.7.0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80" @@ -4232,13 +4254,6 @@ js-yaml@^3.4.3, js-yaml@^3.5.1, js-yaml@^3.7.0, js-yaml@~3.7.0: argparse "^1.0.7" esprima "^2.6.0" -js-yaml@^3.9.1: - version "3.10.0" - resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc" - dependencies: - argparse "^1.0.7" - esprima "^4.0.0" - jsbn@~0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.0.tgz#650987da0dd74f4ebf5a11377a2aa2d273e97dfd" @@ -4378,6 +4393,10 @@ kind-of@^3.0.2: dependencies: is-buffer "^1.0.2" +kind-of@^5.0.2: + version "5.1.0" + resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-5.1.0.tgz#729c91e2d857b7a419a1f9aa65685c4c33f5845d" + klaw@^1.0.0: version "1.3.1" resolved "https://registry.yarnpkg.com/klaw/-/klaw-1.3.1.tgz#4088433b46b3b1ba259d78785d8e96f73ba02439" @@ -6728,6 +6747,10 @@ strip-ansi@^4.0.0: dependencies: ansi-regex "^3.0.0" +strip-bom-string@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/strip-bom-string/-/strip-bom-string-1.0.0.tgz#e5211e9224369fbb81d633a2f00044dc8cedad92" + strip-bom@3.0.0, strip-bom@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"