about summary refs log tree commit diff
path: root/api
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-14 21:57:50 +0200
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-14 21:57:50 +0200
commit4fe5fef70b111ab0e10d91ee7060302a52ca3b76 (patch)
treeb0d93c439bd9050938344cf4534a1ff4b4e3cc55 /api
parentccb84eca016c8b81c074f17f188f4df47fa770ce (diff)
downloadpuszcza-4fe5fef70b111ab0e10d91ee7060302a52ca3b76.tar.gz
puszcza-4fe5fef70b111ab0e10d91ee7060302a52ca3b76.zip
Revert "Add kronikarz"
This reverts commit d400b53bee9ba563810171c217710d4176b7c367.
Diffstat (limited to 'api')
-rw-r--r--api/api.js65
-rw-r--r--api/index.js7
2 files changed, 65 insertions, 7 deletions
diff --git a/api/api.js b/api/api.js
new file mode 100644
index 0000000..9603f3f
--- /dev/null
+++ b/api/api.js
@@ -0,0 +1,65 @@
+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(fileContent) {
+  const post = frontmatter(fileContent)
+
+  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() {
+  const routesArray = []
+  try {
+    const years = fs.readdirSync(`${POSTS_PATH}`)
+    years.forEach(year => {
+      const months = fs.readdirSync(`${POSTS_PATH}/${year}`)
+      months.forEach(month => {
+        const days = fs.readdirSync(`${POSTS_PATH}/${year}/${month}`)
+        days.forEach(day => {
+          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}/${title}/`
+            const fsRoute = `${POSTS_PATH}/${year}/${month}/${day}/${file}`
+
+            const data = getPostAttributes(fs.readFileSync(fsRoute, 'utf-8'))
+
+            routesArray.push({
+              year,
+              month,
+              day,
+              title,
+              data,
+              file,
+              fsRoute,
+              route
+            })
+          })
+        })
+      })
+    })
+  } finally {
+    return routesArray
+  }
+}
+
+function createRoutesArray() {
+  let posts = getPosts()
+  return posts.map(post => post.route)
+}
+
+module.exports = {
+  getPosts,
+  createRoutesArray,
+  getPostAttributes
+}
diff --git a/api/index.js b/api/index.js
deleted file mode 100644
index 5658365..0000000
--- a/api/index.js
+++ /dev/null
@@ -1,7 +0,0 @@
-import Kronikarz from 'kronikarz'
-
-const POSTS_PATH = './content/wpisy'
-
-const k = new Kronikarz(POSTS_PATH)
-
-export default k