feat(posts): render posts as markdown
Alan Pearce alan@alanpearce.eu
Sun, 18 Jun 2017 17:42:21 +0200
6 files changed, 49 insertions(+), 5 deletions(-)
M package.json → package.json
@@ -29,9 +29,10 @@ "standard": "^10.0.2", "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" } }
M src/index.js → src/index.js
@@ -24,8 +24,10 @@ }) 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 }) })
M src/modules/posts.js → src/modules/posts.js
@@ -3,11 +3,19 @@ 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) { @@ -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 @@ module.exports = { get, getFolder, - toTags + toTags, + render }
M src/views/post.html → src/views/post.html
@@ -1,3 +1,3 @@ {{ post.data.title }} -{{ post.content }} +{{ post.body | safe }}
M test/index.test.js → 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()) })
M yarn.lock → yarn.lock
@@ -1263,6 +1263,10 @@ dependencies: 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 @@ dependencies: 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" @@ -2816,6 +2836,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" @@ -4195,6 +4219,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"