diff options
author | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2019-10-11 20:25:43 +0200 |
---|---|---|
committer | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2019-10-11 20:25:43 +0200 |
commit | 293388bf6c338fb5a2bc2d05ff526750f28bb4bc (patch) | |
tree | 98065e7cdd518f095634be903d48a6f898df4348 | |
parent | c29fcbdf765e36023985996334ad711f4bc2b6fa (diff) | |
download | puszcza-293388bf6c338fb5a2bc2d05ff526750f28bb4bc.tar.gz puszcza-293388bf6c338fb5a2bc2d05ff526750f28bb4bc.zip |
Add attribute parsing
-rw-r--r-- | api/api.js | 16 | ||||
-rw-r--r-- | package-lock.json | 10 | ||||
-rw-r--r-- | package.json | 7 | ||||
-rwxr-xr-x | scripts/generateApi.js | 8 | ||||
-rwxr-xr-x | scripts/postbuild.sh | 2 |
5 files changed, 33 insertions, 10 deletions
diff --git a/api/api.js b/api/api.js index 34a53a0..a704912 100644 --- a/api/api.js +++ b/api/api.js @@ -1,7 +1,15 @@ const fs = require('fs') +const frontmatter = require('front-matter') const POSTS_PATH = './kronika/wpisy' +function getPostAttributes(filePath) { + const fileContent = fs.readFileSync(filePath, "utf-8") + + const post = frontmatter(fileContent); + return post.attributes +} + function getPosts() { const routesArray = [] try { @@ -14,13 +22,17 @@ function getPosts() { const files = fs.readdirSync(`${POSTS_PATH}/${year}/${month}/${day}`) files.forEach((file) => { const title = file.substr(0, file.lastIndexOf('.')) - const route = `/kronika/${year}/${month}/${day}/${name}` + const route = `/kronika/${year}/${month}/${day}/${title}` const fsRoute = `${POSTS_PATH}/${year}/${month}/${day}/${file}` + + const attributes = getPostAttributes(fsRoute) + routesArray.push({ year, month, day, title, + attributes, file, fsRoute, route @@ -40,8 +52,6 @@ function createRoutesArray() { return posts.map(post => post.route) } -createRoutesArray() - module.exports = { getPosts, createRoutesArray diff --git a/package-lock.json b/package-lock.json index 538b1bb..18ec963 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "puszcza", + "name": "Puszcza", "version": "1.0.0", "lockfileVersion": 1, "requires": true, @@ -5334,6 +5334,14 @@ } } }, + "front-matter": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/front-matter/-/front-matter-3.0.2.tgz", + "integrity": "sha512-iBGZaWyzqgsrPGsqrXZP6N4hp5FzSKDi18nfAoYpgz3qK5sAwFv/ojmn3VS60SOgLvq6CtojNqy0y6ZNz05IzQ==", + "requires": { + "js-yaml": "^3.13.1" + } + }, "fs-extra": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz", diff --git a/package.json b/package.json index e99735d..f85c87c 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,10 @@ "test": "jest" }, "dependencies": { - "nuxt": "^2.0.0", "@nuxtjs/axios": "^5.3.6", - "@nuxtjs/pwa": "^3.0.0-0" + "@nuxtjs/pwa": "^3.0.0-0", + "front-matter": "^3.0.2", + "nuxt": "^2.0.0" }, "devDependencies": { "@vue/test-utils": "^1.0.0-beta.27", @@ -44,4 +45,4 @@ "jest-serializer-vue" ] } -} \ No newline at end of file +} diff --git a/scripts/generateApi.js b/scripts/generateApi.js index f439252..82bd583 100755 --- a/scripts/generateApi.js +++ b/scripts/generateApi.js @@ -1,13 +1,17 @@ +const fs = require('fs') + let { getPosts } = require('../api/api') let posts = getPosts() -posts = posts.map(({ year, month, day, title, route }) => { +posts = posts.map(({ year, month, day, title, attributes, route }) => { return { date: `${year}-${month}-${day}`, title, + attributes, route } }) -console.log(posts) \ No newline at end of file +fs.writeFile('./dist/api/posts.json', JSON.stringify(posts), (err) => err ? console.log(err) : null) +console.log(posts) diff --git a/scripts/postbuild.sh b/scripts/postbuild.sh index 53a1702..749bfdd 100755 --- a/scripts/postbuild.sh +++ b/scripts/postbuild.sh @@ -2,5 +2,5 @@ mkdir ./dist/api -node ./scripts/generateApi.js > ./dist/api/posts.json +node ./scripts/generateApi.js |