summary refs log tree commit diff stats
path: root/src/domain
diff options
context:
space:
mode:
Diffstat (limited to 'src/domain')
-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,