From 0b9b8b74108bf1d3fb972d546a2dd63446cb28ce Mon Sep 17 00:00:00 2001 From: Jinksi Date: Sat, 14 Oct 2017 16:04:09 +1000 Subject: [PATCH] Refactor content parse --- {utils => functions}/parse-content.js | 7 ++- package.json | 2 +- src/App.js | 16 +++++-- utils/parse-markdown.js | 61 --------------------------- 4 files changed, 16 insertions(+), 70 deletions(-) rename {utils => functions}/parse-content.js (92%) delete mode 100644 utils/parse-markdown.js diff --git a/utils/parse-content.js b/functions/parse-content.js similarity index 92% rename from utils/parse-content.js rename to functions/parse-content.js index b7713c6..72778ac 100644 --- a/utils/parse-content.js +++ b/functions/parse-content.js @@ -20,7 +20,7 @@ const getCollectionType = filePath => { return `${objectKey}` } -const getDocumentKey = filePath => { +const getDocumentName = filePath => { const pathParsed = path.parse(filePath) return `${pathParsed.name}` } @@ -28,10 +28,9 @@ const getDocumentKey = filePath => { const getFileContents = filePath => { return readFile(filePath, 'utf8') .then(data => { + let documentData = JSON.parse(data) + documentData.name = getDocumentName(filePath) let obj = {} - let documentData = { - [getDocumentKey(filePath)]: JSON.parse(data) - } _set(obj, getCollectionType(filePath), [documentData]) console.log(`✨ Processed ${filePath}`) return obj diff --git a/package.json b/package.json index 5c8c7f0..1adb2d3 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "scripts": { "start": "npm run parse-content && react-scripts start", "build": "npm run parse-content && react-scripts build && react-snapshot && npm run sw", - "parse-content": "node ./utils/parse-content.js", + "parse-content": "node ./functions/parse-content.js", "sw": "sw-precache --config='sw-precache-config.js'", "test": "standard | snazzy && react-scripts test --env=jsdom", "eject": "react-scripts eject" diff --git a/src/App.js b/src/App.js index ac45eaa..95e9289 100644 --- a/src/App.js +++ b/src/App.js @@ -11,17 +11,25 @@ import Nav from './components/Nav' import GithubCorner from './components/GithubCorner' import ServiceWorkerNotifications from './components/ServiceWorkerNotifications' import globalStyles from './globalStyles' - import data from './data.json' export const siteTitle = 'HyperStatic' class App extends Component { + state = { + data + } + componentWillMount () { globalStyles() import('./netlifyIdentity') } + getDocument = (collection, name) => + this.state.data[collection] && this.state.data[collection].filter(page => page.name === name)[0] + + getDocuments = (collection) => this.state.data[collection] + render () { return ( @@ -33,13 +41,13 @@ class App extends Component {