From 293388bf6c338fb5a2bc2d05ff526750f28bb4bc Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Fri, 11 Oct 2019 20:25:43 +0200 Subject: Add attribute parsing --- api/api.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'api/api.js') 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 -- cgit 1.4.1