From 5cfbd5b71d149419b0b80056e652ca94d26f1a95 Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Sat, 12 Oct 2019 16:54:05 +0200 Subject: Move to single api --- api/api.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'api') diff --git a/api/api.js b/api/api.js index 1675d7e..9603f3f 100644 --- a/api/api.js +++ b/api/api.js @@ -1,13 +1,20 @@ const fs = require('fs') const frontmatter = require('front-matter') +const md = require('markdown-it')() +const { JSDOM } = require('jsdom') const POSTS_PATH = './content/wpisy' -function getPostAttributes(filePath) { - const fileContent = fs.readFileSync(filePath, 'utf-8') - +function getPostAttributes(fileContent) { const post = frontmatter(fileContent) - return post.attributes + + const { document } = new JSDOM(`${md.render(post.body)}`).window + const element = document.getElementsByTagName('p') + + post.body = `
${md.render(post.body)}
` + post.description = element[1].textContent + + return post } function getPosts() { @@ -25,14 +32,14 @@ function getPosts() { const route = `/kronika/${year}/${month}/${day}/${title}/` const fsRoute = `${POSTS_PATH}/${year}/${month}/${day}/${file}` - const attributes = getPostAttributes(fsRoute) + const data = getPostAttributes(fs.readFileSync(fsRoute, 'utf-8')) routesArray.push({ year, month, day, title, - attributes, + data, file, fsRoute, route @@ -53,5 +60,6 @@ function createRoutesArray() { module.exports = { getPosts, - createRoutesArray + createRoutesArray, + getPostAttributes } -- cgit 1.4.1