about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2017-06-18 17:42:21 +0200
committerAlan Pearce2017-06-18 17:42:21 +0200
commit33171a749162685e42650f82041cd2af0136718d (patch)
treebd6970a89c47db7ea13698ca378eae0df605af0d
parent1a36d4dc311a86246fe854da888af01f8195368e (diff)
downloadhomestead-33171a749162685e42650f82041cd2af0136718d.tar.lz
homestead-33171a749162685e42650f82041cd2af0136718d.tar.zst
homestead-33171a749162685e42650f82041cd2af0136718d.zip
feat(posts): render posts as markdown
-rw-r--r--package.json3
-rw-r--r--src/index.js4
-rw-r--r--src/modules/posts.js15
-rw-r--r--src/views/post.html2
-rw-r--r--test/index.test.js2
-rw-r--r--yarn.lock28
6 files changed, 49 insertions, 5 deletions
diff --git a/package.json b/package.json
index 03179cd..e25d390 100644
--- a/package.json
+++ b/package.json
@@ -29,9 +29,10 @@
     "supertest": "^3.0.0"
   },
   "dependencies": {
+    "gray-matter": "^2.1.1",
     "koa": "^2.2.0",
     "koa-nunjucks-next": "^1.1.3",
     "koa-router": "^7.2.1",
-    "gray-matter": "^2.1.1"
+    "markdown-it": "^8.3.1"
   }
 }
diff --git a/src/index.js b/src/index.js
index 07d7249..fce4c09 100644
--- a/src/index.js
+++ b/src/index.js
@@ -24,8 +24,10 @@ router.get('/', async function (ctx) {
 
 router.get('/post/:filename', async function (ctx) {
   ctx.assert(posts.has(ctx.params.filename), 404, 'Post not found')
+  const post = posts.get(ctx.params.filename)
+  post.body = Posts.render(post)
   await ctx.render('post', {
-    post: posts.get(ctx.params.filename)
+    post: post
   })
 })
 
diff --git a/src/modules/posts.js b/src/modules/posts.js
index 011292b..cba2372 100644
--- a/src/modules/posts.js
+++ b/src/modules/posts.js
@@ -3,12 +3,20 @@
 const fs = require('fs')
 const path = require('path')
 const matter = require('gray-matter')
+const Markdown = require('markdown-it')
 
 const grayMatterOptions = {
   lang: 'toml',
   delims: '+++'
 }
 
+const markdownOptions = {
+  html: true,
+  typographer: true
+}
+
+const markdown = new Markdown(markdownOptions)
+
 function* lowercaseKeys (iterator) {
   for (let [k, v] of iterator) {
     yield [String(k).toLowerCase(), v]
@@ -28,6 +36,10 @@ function getTitle (file) {
   return path.basename(file.path, path.extname(file.path))
 }
 
+function render (post) {
+  return markdown.render(post.content)
+}
+
 function get (filename) {
   const fileMatter = matter.read(filename, grayMatterOptions)
   fileMatter.basename = getTitle(fileMatter)
@@ -59,5 +71,6 @@ function toTags (posts) {
 module.exports = {
   get,
   getFolder,
-  toTags
+  toTags,
+  render
 }
diff --git a/src/views/post.html b/src/views/post.html
index fcc4dbd..81f7e3d 100644
--- a/src/views/post.html
+++ b/src/views/post.html
@@ -1,3 +1,3 @@
 {{ post.data.title }}
 
-{{ post.content }}
+{{ post.body | safe }}
diff --git a/test/index.test.js b/test/index.test.js
index a81dbb6..e13e52c 100644
--- a/test/index.test.js
+++ b/test/index.test.js
@@ -18,7 +18,7 @@ test('post', t => {
   return request(app.listen())
     .get('/post/testfile')
     .expect(200)
-    .expect(/Lorem ipsum/)
+    .expect(/<h1>Lorem ipsum<\/h1>/)
     .then(() => t.pass())
 })
 
diff --git a/yarn.lock b/yarn.lock
index f92a4f8..297ce30 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1263,6 +1263,10 @@ empower-core@^0.6.1:
     call-signature "0.0.2"
     core-js "^2.0.0"
 
+entities@~1.1.1:
+  version "1.1.1"
+  resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0"
+
 equal-length@^1.0.0:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/equal-length/-/equal-length-1.0.1.tgz#21ca112d48ab24b4e1e7ffc0e5339d31fdfc274c"
@@ -2596,6 +2600,12 @@ levn@^0.3.0, levn@~0.3.0:
     prelude-ls "~1.1.2"
     type-check "~0.3.2"
 
+linkify-it@^2.0.0:
+  version "2.0.3"
+  resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-2.0.3.tgz#d94a4648f9b1c179d64fa97291268bdb6ce9434f"
+  dependencies:
+    uc.micro "^1.0.1"
+
 lint-staged@^3.6.1:
   version "3.6.1"
   resolved "https://registry.yarnpkg.com/lint-staged/-/lint-staged-3.6.1.tgz#24423c8b7bd99d96e15acd1ac8cb392a78e58582"
@@ -2795,6 +2805,16 @@ map-obj@^1.0.0, map-obj@^1.0.1:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-1.0.1.tgz#d933ceb9205d82bdcf4886f6742bdc2b4dea146d"
 
+markdown-it@^8.3.1:
+  version "8.3.1"
+  resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-8.3.1.tgz#2f4b622948ccdc193d66f3ca2d43125ac4ac7323"
+  dependencies:
+    argparse "^1.0.7"
+    entities "~1.1.1"
+    linkify-it "^2.0.0"
+    mdurl "^1.0.1"
+    uc.micro "^1.0.3"
+
 matcher@^0.1.1:
   version "0.1.2"
   resolved "https://registry.yarnpkg.com/matcher/-/matcher-0.1.2.tgz#ef20cbde64c24c50cc61af5b83ee0b1b8ff00101"
@@ -2817,6 +2837,10 @@ md5-o-matic@^0.1.1:
   version "0.1.1"
   resolved "https://registry.yarnpkg.com/md5-o-matic/-/md5-o-matic-0.1.1.tgz#822bccd65e117c514fab176b25945d54100a03c3"
 
+mdurl@^1.0.1:
+  version "1.0.1"
+  resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e"
+
 media-typer@0.3.0:
   version "0.3.0"
   resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
@@ -4196,6 +4220,10 @@ typedarray@^0.0.6:
   version "0.0.6"
   resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
 
+uc.micro@^1.0.1, uc.micro@^1.0.3:
+  version "1.0.3"
+  resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.3.tgz#7ed50d5e0f9a9fb0a573379259f2a77458d50192"
+
 uid-number@^0.0.6:
   version "0.0.6"
   resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"