all repos — archive/homestead @ e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f

My future indieweb platform

feat: Add code block highlighting Theme is configurable

Alan Pearce
commit

e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f

parent

a67e38d1a82c95db5bd24183e81b31438f60dd2c

1 file changed, 27 insertions(+), 15 deletions(-)

changed files
M src/domain/posts.jssrc/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 @@
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,