about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/domain/posts.js6
-rw-r--r--src/responders.js37
2 files changed, 36 insertions, 7 deletions
diff --git a/src/domain/posts.js b/src/domain/posts.js
index aedf58e..ea81729 100644
--- a/src/domain/posts.js
+++ b/src/domain/posts.js
@@ -4,6 +4,7 @@ const fs = require('fs')
 const path = require('path')
 const matter = require('gray-matter')
 const markdown = require('../modules/markdown.js')
+const { indentForTemplate, postIndentLevel } = require('../responders.js')
 
 const grayMatterOptions = {
   lang: 'toml',
@@ -33,7 +34,10 @@ function get (filename) {
   const fileMatter = matter.read(filename, grayMatterOptions)
   fileMatter.basename = getTitle(fileMatter)
   delete fileMatter.orig
-  fileMatter.body = markdown(fileMatter.content)
+  fileMatter.body = indentForTemplate(
+    markdown(fileMatter.content),
+    postIndentLevel
+  )
   return canonicaliseMetadata(fileMatter)
 }
 
diff --git a/src/responders.js b/src/responders.js
index 778580c..824195b 100644
--- a/src/responders.js
+++ b/src/responders.js
@@ -1,15 +1,36 @@
 'use strict'
 
+const h = require('highland')
 const fs = require('fs')
 const rheo = require('rheo')
+const indent = require('indent-string')
+
+const toLines = string =>
+  string.split('\n').map((s, i, arr) => (i === arr.length - 1 ? s : s + '\n'))
+
+const getTemplate = name =>
+  fs.readFileSync(`${__dirname}/templates/${name}.html`, 'utf8')
+
+const findMain = /^(\s+)<main/m
+const baseIndentLevel = findMain.exec(getTemplate('layout'))[1].length
+const postIndentLevel =
+  baseIndentLevel + findMain.exec(getTemplate('post'))[1].length
+
+function indentForTemplate (text, indentLevel) {
+  return indent(text, indentLevel).slice(indentLevel).replace(/\n+$/, '')
+}
+
+function templateReader (template, indentLevel) {
+  const content = toLines(indentForTemplate(getTemplate(template), indentLevel))
+  console.log(template, content)
+  return () => h(content)
+}
 
-const templateReader = template => () =>
-  fs.createReadStream(`${__dirname}/templates/${template}.html`)
 const templates = {
   layout: templateReader('layout'),
-  home: templateReader('home'),
-  post: templateReader('post'),
-  taxon: templateReader('taxon')
+  home: templateReader('home', baseIndentLevel),
+  post: templateReader('post', baseIndentLevel),
+  taxon: templateReader('taxon', baseIndentLevel)
 }
 
 function setTitle (siteTitle, pageTitle) {
@@ -35,6 +56,10 @@ function showPage (name) {
 }
 
 module.exports = {
+  baseIndentLevel,
+  postIndentLevel,
+  indentForTemplate,
+
   home (ctx, config, postsStream) {
     ctx.type = 'html'
     ctx.body = templates
@@ -55,7 +80,7 @@ module.exports = {
       .pipe(rheo())
       .outer('main', showPage('post'))
       .inner('article h1', rheo(post.data.get('title')))
-      .inner('article main', rheo(post.body))
+      .outer('article main', rheo(post.body))
       .pipe(setTitle(config.site.title, post.data.get('title')))
       .render()
   },