about summary refs log tree commit diff
path: root/api
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-12 16:54:05 +0200
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-12 16:54:05 +0200
commit5cfbd5b71d149419b0b80056e652ca94d26f1a95 (patch)
tree4f61dd2e8822cec3613c0ef7f17f694afc42182e /api
parentd92f1a2a1bac3039434a16c504cc1d54c6d0409a (diff)
downloadpuszcza-5cfbd5b71d149419b0b80056e652ca94d26f1a95.tar.gz
puszcza-5cfbd5b71d149419b0b80056e652ca94d26f1a95.zip
Move to single api
Diffstat (limited to 'api')
-rw-r--r--api/api.js22
1 files changed, 15 insertions, 7 deletions
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(`<body>${md.render(post.body)}</body>`).window
+  const element = document.getElementsByTagName('p')
+
+  post.body = `<div>${md.render(post.body)}</div>`
+  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
 }