summary refs log tree commit diff stats
path: root/src/domain/posts.js
diff options
context:
space:
mode:
authorAlan Pearce2017-07-03 21:39:43 +0200
committerAlan Pearce2017-07-03 21:55:41 +0200
commite7b08b1dfe3f2a2596deb6e2a72bb79805d3708f (patch)
tree027fda29fe96736d4ac641a1dfe0bfe657d3fd33 /src/domain/posts.js
parenta67e38d1a82c95db5bd24183e81b31438f60dd2c (diff)
downloadhomestead-e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f.tar.lz
homestead-e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f.tar.zst
homestead-e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f.zip
feat: Add code block highlighting
Theme is configurable
Diffstat (limited to 'src/domain/posts.js')
-rw-r--r--src/domain/posts.js42
1 files changed, 27 insertions, 15 deletions
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,