about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--api/api.js65
-rw-r--r--api/index.js7
-rw-r--r--nuxt.config.js4
-rw-r--r--package-lock.json38
-rw-r--r--package.json2
-rw-r--r--pages/kronika/_year/_month/_day/_title/index.vue17
-rwxr-xr-xscripts/generateApi.js12
-rwxr-xr-xscripts/postbuild.sh2
8 files changed, 63 insertions, 84 deletions
diff --git a/api/api.js b/api/api.js
deleted file mode 100644
index 9603f3f..0000000
--- a/api/api.js
+++ /dev/null
@@ -1,65 +0,0 @@
-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
new file mode 100644
index 0000000..5658365
--- /dev/null
+++ b/api/index.js
@@ -0,0 +1,7 @@
+import Kronikarz from 'kronikarz'
+
+const POSTS_PATH = './content/wpisy'
+
+const k = new Kronikarz(POSTS_PATH)
+
+export default k
diff --git a/nuxt.config.js b/nuxt.config.js
index 016f39f..6ef4e09 100644
--- a/nuxt.config.js
+++ b/nuxt.config.js
@@ -1,4 +1,4 @@
-import { createRoutesArray } from './api/api.js'
+import k from './api'
 
 export default {
   mode: 'universal',
@@ -34,7 +34,7 @@ export default {
     ]
   },
   generate: {
-    routes: createRoutesArray()
+    routes: k.getPosts().map(post => post.route)
   },
   /*
    ** Customize the progress-bar color
diff --git a/package-lock.json b/package-lock.json
index 1609932..ef76df3 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1944,6 +1944,28 @@
         "@types/istanbul-lib-report": "*"
       }
     },
+    "@types/jsdom": {
+      "version": "12.2.4",
+      "resolved": "https://registry.npmjs.org/@types/jsdom/-/jsdom-12.2.4.tgz",
+      "integrity": "sha512-q+De3S/Ri6U9uPx89YA1XuC+QIBgndIfvBaaJG0pRT8Oqa75k4Mr7G9CRZjIvlbLGIukO/31DFGFJYlQBmXf/A==",
+      "requires": {
+        "@types/node": "*",
+        "@types/tough-cookie": "*",
+        "parse5": "^4.0.0"
+      },
+      "dependencies": {
+        "parse5": {
+          "version": "4.0.0",
+          "resolved": "https://registry.npmjs.org/parse5/-/parse5-4.0.0.tgz",
+          "integrity": "sha512-VrZ7eOd3T1Fk4XWNXMgiGBK/z0MG48BWG2uQNU4I72fkQuKUTZpl+u9k+CxEG0twMVzSmXEEz12z5Fnw1jIQFA=="
+        }
+      }
+    },
+    "@types/node": {
+      "version": "12.7.12",
+      "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.12.tgz",
+      "integrity": "sha512-KPYGmfD0/b1eXurQ59fXD1GBzhSQfz6/lKBxkaHX9dKTzjXbK68Zt7yGUxUsCS1jeTy/8aL+d9JEr+S54mpkWQ=="
+    },
     "@types/q": {
       "version": "1.5.2",
       "resolved": "https://registry.npmjs.org/@types/q/-/q-1.5.2.tgz",
@@ -1955,6 +1977,11 @@
       "integrity": "sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==",
       "dev": true
     },
+    "@types/tough-cookie": {
+      "version": "2.3.5",
+      "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-2.3.5.tgz",
+      "integrity": "sha512-SCcK7mvGi3+ZNz833RRjFIxrn4gI1PPR3NtuIS+6vMkvmsGjosqTJwRt5bAEFLRz+wtJMWv8+uOnZf2hi2QXTg=="
+    },
     "@types/yargs": {
       "version": "13.0.2",
       "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-13.0.2.tgz",
@@ -7593,6 +7620,17 @@
       "integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==",
       "dev": true
     },
+    "kronikarz": {
+      "version": "0.1.3",
+      "resolved": "https://registry.npmjs.org/kronikarz/-/kronikarz-0.1.3.tgz",
+      "integrity": "sha512-isj1P/hI1Sd4Lp/rug8Tb3TrFbnSKUdiamAFvFBAuDQhe2xkm2mjMoHiGjSvYCUHdBPYKllCpthEoHfsJhYRHA==",
+      "requires": {
+        "@types/jsdom": "^12.2.4",
+        "front-matter": "^3.0.2",
+        "jsdom": "^15.1.1",
+        "markdown-it": "^10.0.0"
+      }
+    },
     "last-call-webpack-plugin": {
       "version": "3.0.0",
       "resolved": "https://registry.npmjs.org/last-call-webpack-plugin/-/last-call-webpack-plugin-3.0.0.tgz",
diff --git a/package.json b/package.json
index 5e9991c..e6ceb92 100644
--- a/package.json
+++ b/package.json
@@ -15,8 +15,10 @@
   "dependencies": {
     "@nuxtjs/axios": "^5.3.6",
     "@nuxtjs/pwa": "^3.0.0-0",
+    "esm": "^3.2.25",
     "front-matter": "^3.0.2",
     "jsdom": "^15.1.1",
+    "kronikarz": "^0.1.3",
     "markdown-it": "^10.0.0",
     "nuxt": "^2.0.0",
     "raw-loader": "^3.1.0"
diff --git a/pages/kronika/_year/_month/_day/_title/index.vue b/pages/kronika/_year/_month/_day/_title/index.vue
index f4d5cc0..a8a086a 100644
--- a/pages/kronika/_year/_month/_day/_title/index.vue
+++ b/pages/kronika/_year/_month/_day/_title/index.vue
@@ -15,24 +15,19 @@
 <script>
 const md = require('markdown-it')()
 import frontmatter from 'front-matter'
-import { getPostAttributes } from '~/api/api'
+import k from '~/api'
 
 export default {
   async asyncData({ params }) {
-    const fileContent = await import(
-      `~/content/wpisy/${params.year}/${params.month}/${
-        params.day
-      }/${params.title.toLowerCase()}.md`
-    ).catch(e => console.log(e))
+    const { year, month, day, title } = params
+    const post = k.getPost(year, month, day, title)
 
-    if (fileContent === undefined) return { notFound: true }
-
-    const post = getPostAttributes(fileContent.default)
+    if (post === undefined) return { notFound: true }
 
     return {
       params,
-      attributes: post.attributes,
-      content: post.body
+      attributes: post.content.meta,
+      content: post.content.html
     }
   },
   data() {
diff --git a/scripts/generateApi.js b/scripts/generateApi.js
index 128387c..f72745e 100755
--- a/scripts/generateApi.js
+++ b/scripts/generateApi.js
@@ -1,14 +1,16 @@
-const fs = require('fs')
+import fs from 'fs'
+import k from '../api'
 
-let { getPosts } = require('../api/api')
+let posts = k.getPosts()
 
-let posts = getPosts()
+posts = posts.map(({ date, title, content, route }) => {
+  const { year, month, day } = date
+  const { description, meta } = content
 
-posts = posts.map(({ year, month, day, title, data, route }) => {
   return {
     date: `${year}-${month}-${day}`,
     title,
-    data,
+    content: { description, meta },
     route
   }
 })
diff --git a/scripts/postbuild.sh b/scripts/postbuild.sh
index 749bfdd..85e35f9 100755
--- a/scripts/postbuild.sh
+++ b/scripts/postbuild.sh
@@ -2,5 +2,5 @@
 
 mkdir ./dist/api
 
-node ./scripts/generateApi.js
+node -r esm ./scripts/generateApi.js