From e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 3 Jul 2017 21:39:43 +0200 Subject: feat: Add code block highlighting Theme is configurable --- src/domain/posts.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'src/domain/posts.js') diff --git a/src/domain/posts.js b/src/domain/posts.js index fd4fb3d..98488ba 100644 --- a/src/domain/posts.js +++ b/src/domain/posts.js @@ -1,9 +1,12 @@ "use strict"; +const h = require("highland"); const fs = require("fs"); const path = require("path"); +const { promisify } = require("util"); const matter = require("gray-matter"); const markdown = require("../modules/markdown.js"); +const predentation = require("predentation"); const { indentForTemplate, postIndentLevel } = require("../responders.js"); const grayMatterOptions = { @@ -30,26 +33,35 @@ function getTitle(file) { return path.basename(file.path, path.extname(file.path)); } -function get(getURL, filename) { +async function indentBody(content) { + return await h( + h + .of(content) + .map(markdown) + .map(html => indentForTemplate(html, postIndentLevel)) + .pipe(predentation()) + ) + .invoke("toString", ["utf-8"]) + .toPromise(Promise); +} + +async function get(getURL, filename) { const fileMatter = matter.read(filename, grayMatterOptions); fileMatter.basename = getTitle(fileMatter); delete fileMatter.orig; - fileMatter.body = indentForTemplate( - markdown(fileMatter.content), - postIndentLevel - ); + fileMatter.body = await indentBody(fileMatter.content); fileMatter.url = getURL(fileMatter.basename); - return canonicaliseMetadata(fileMatter); + return Promise.resolve(canonicaliseMetadata(fileMatter)); } -function getFolder(folder, getURL) { - return new Map( - fs - .readdirSync(folder) - .map(f => path.resolve(folder, f)) - .map(get.bind(this, getURL)) - .map(f => [getTitle(f), f]) +async function getFolder(folder, getURL) { + const files = (await promisify(fs.readdir)(folder)).map(f => + path.resolve(folder, f) ); + + const posts = await Promise.all(files.map(get.bind(this, getURL))); + + return new Map(posts.map(f => [getTitle(f), f])); } function taxonomise(taxonomies, posts) { @@ -71,8 +83,8 @@ function taxonomise(taxonomies, posts) { return taxons; } -module.exports = function(config, getURL) { - const posts = getFolder(config.folder, getURL); +module.exports = async function(config, getURL) { + const posts = await getFolder(config.folder, getURL); const taxonomies = taxonomise(config.taxonomies, posts); return { posts, -- cgit 1.4.1