feat: beautify HTML output Indent child templates according to the their place in the parent template, and likewise for rendered posts Needs refactoring
1 file changed, 31 insertions(+), 6 deletions(-)
changed files
M src/responders.js → 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 templateReader = template => () => - fs.createReadStream(`${__dirname}/templates/${template}.html`) +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 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 @@ } } module.exports = { + baseIndentLevel, + postIndentLevel, + indentForTemplate, + home (ctx, config, postsStream) { ctx.type = 'html' ctx.body = templates@@ -55,7 +80,7 @@ .layout() .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() },