about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--components/Posts/PostLink.vue4
-rw-r--r--pages/kronika/_year/_month/_day/_title/index.vue66
-rwxr-xr-xscripts/generateApi.js56
-rwxr-xr-xscripts/postbuild.sh2
4 files changed, 96 insertions, 32 deletions
diff --git a/components/Posts/PostLink.vue b/components/Posts/PostLink.vue
index 3f80bae..0361b80 100644
--- a/components/Posts/PostLink.vue
+++ b/components/Posts/PostLink.vue
@@ -1,6 +1,6 @@
 <template>
   <div class="post-link">
-    <a :href="route">
+    <nuxt-link :to="route">
       <div>
         <div class="image" :style="`background-image: url('${image}')`" />
         <div class="post-container">
@@ -8,7 +8,7 @@
           <p class="post-description">{{ shortenedDescription }}...</p>
         </div>
       </div>
-    </a>
+    </nuxt-link>
   </div>
 </template>
 
diff --git a/pages/kronika/_year/_month/_day/_title/index.vue b/pages/kronika/_year/_month/_day/_title/index.vue
index 331ed7a..9326ff8 100644
--- a/pages/kronika/_year/_month/_day/_title/index.vue
+++ b/pages/kronika/_year/_month/_day/_title/index.vue
@@ -17,47 +17,55 @@
 </template>
 
 <script>
-const md = require('markdown-it')()
-import frontmatter from 'front-matter'
-import k from '~/api'
+import axios from 'axios'
 
 export default {
   head() {
-    return {
-      meta: [
-        {
-          hid: 'og:title',
-          property: 'og:title',
-          content: this.attributes.title,
-        },
-        {
-          hid: 'og:type',
-          property: 'og:type',
-          content: 'article',
-        },
-        {
-          hid: 'og:article:author',
-          property: 'og:article:author',
-          content: this.attributes.author,
-        },
-        {
-          hid: 'og:image',
-          property: 'og:image',
-          content: this.attributes.image,
-        },
-      ],
+    if (this.attributes) {
+      return {
+        meta: [
+          {
+            hid: 'og:title',
+            property: 'og:title',
+            content: this.title,
+          },
+          {
+            hid: 'og:type',
+            property: 'og:type',
+            content: 'article',
+          },
+          {
+            hid: 'og:article:author',
+            property: 'og:article:author',
+            content: this.author,
+          },
+          {
+            hid: 'og:image',
+            property: 'og:image',
+            content: this.attributes.image,
+          },
+        ],
+      }
     }
   },
   async asyncData({ params }) {
     const { year, month, day, title } = params
-    const post = k.getPost(year, month, day, title)
+    const response = await axios
+      .get(
+        `https://puszcza.netlify.com/api/posts/${year}/${month}/${day}/${title}.json`
+      )
+      .catch((err) => ({ notFound: true }))
+
+    const post = response.data
 
     if (post === undefined) return { notFound: true }
 
     return {
       params,
-      attributes: post.content.meta,
-      content: post.content.html,
+      title: post.title,
+      author: post.author,
+      attributes: post.meta,
+      content: post.content,
     }
   },
   data() {
diff --git a/scripts/generateApi.js b/scripts/generateApi.js
index 4b7426a..ad266cc 100755
--- a/scripts/generateApi.js
+++ b/scripts/generateApi.js
@@ -1,8 +1,64 @@
 import fs from 'fs'
 import k from '../api'
+import path from 'path'
+
+function mkDirByPathSync(targetDir, { isRelativeToScript = false } = {}) {
+  const sep = path.sep
+  const initDir = path.isAbsolute(targetDir) ? sep : ''
+  const baseDir = isRelativeToScript ? __dirname : '.'
+
+  return targetDir.split(sep).reduce((parentDir, childDir) => {
+    const curDir = path.resolve(baseDir, parentDir, childDir)
+    try {
+      fs.mkdirSync(curDir)
+    } catch (err) {
+      if (err.code === 'EEXIST') {
+        // curDir already exists!
+        return curDir
+      }
+
+      // To avoid `EISDIR` error on Mac and `EACCES`-->`ENOENT` and `EPERM` on Windows.
+      if (err.code === 'ENOENT') {
+        // Throw the original parentDir error on curDir `ENOENT` failure.
+        throw new Error(`EACCES: permission denied, mkdir '${parentDir}'`)
+      }
+
+      const caughtErr = ['EACCES', 'EPERM', 'EISDIR'].indexOf(err.code) > -1
+      if (!caughtErr || (caughtErr && targetDir === curDir)) {
+        throw err // Throw if it's just the last created dir.
+      }
+    }
+
+    return curDir
+  }, initDir)
+}
 
 let posts = k.getPosts()
 
+posts.forEach(post => {
+  const { year, month, day } = post.date
+  const dir = `api/posts/${year}/${month}/${day}`
+  mkDirByPathSync(`./dist/${dir}`)
+  mkDirByPathSync(`./static/${dir}`)
+  console.log(`${dir}/${post.title}.json`)
+  const apiEntry = {
+    title: post.title,
+    date: `${year}-${month}-${day}`,
+    content: post.content.html,
+    meta: post.content.meta
+  }
+  fs.writeFile(
+    `./dist/${dir}/${post.title}.json`,
+    JSON.stringify(apiEntry),
+    err => (err ? console.log(err) : null)
+  )
+  fs.writeFile(
+    `./static/${dir}/${post.title}.json`,
+    JSON.stringify(apiEntry),
+    err => (err ? console.log(err) : null)
+  )
+})
+
 posts = posts.map(({ date, title, content, route }) => {
   const { year, month, day } = date
   const { description, meta } = content
diff --git a/scripts/postbuild.sh b/scripts/postbuild.sh
index 6da3659..7b7b1e5 100755
--- a/scripts/postbuild.sh
+++ b/scripts/postbuild.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 
-mkdir ./dist/api
+mkdir -p ./dist/api
 mkdir ./static/api
 
 node -r esm ./scripts/generateApi.js