refactor: Separate templates into layout/body
1 file changed, 28 insertions(+), 11 deletions(-)
changed files
M src/index.js → src/index.js
@@ -20,12 +20,21 @@ const Posts = require('./modules/posts.js') const posts = Posts.getFolder(config.posts.folder) -const index = () => fs.createReadStream(`${__dirname}/templates/index.html`) +const templateReader = template => () => + fs.createReadStream(`${__dirname}/templates/${template}.html`) +const templates = { + layout: templateReader('layout'), + home: templateReader('home'), + post: templateReader('post'), + taxon: templateReader('taxon') +} function setTitle (pageTitle) { - return rheo.template(s => - s.inner('title', rheo((pageTitle ? ' · ' : '') + config.site.title)) - ) + return rheo.template(function (s) { + return s + .inner('title', rheo((pageTitle ? ' · ' : '') + config.site.title)) + .inner('body header h1', rheo(config.site.title)) + }) } function renderPostListItem (template, [, post]) {@@ -38,13 +47,19 @@ function toArrayStream (iterator) { return streamify(Array.from(iterator.entries())) } +function showPage (name) { + return function (els) { + return rheo(templates[name]()) + } +} + const postsStream = toArrayStream(posts) router.get('home', '/', async function (ctx, next) { ctx.set('Content-Type', 'text/html') - ctx.body = index() + ctx.body = templates + .layout() .pipe(rheo()) - .inner('body', pages => pages.find('main.homepage')) - .inner('h1', rheo(config.site.title)) + .outer('main', showPage('home')) .inner('.posts', function (postsTemplate) { return postsStream.pipe(postsTemplate.map(renderPostListItem)) })@@ -58,9 +73,10 @@ const post = posts.get(ctx.params.filename) post.body = Posts.render(post) ctx.set('Content-Type', 'text/html') - ctx.body = index() + ctx.body = templates + .layout() .pipe(rheo()) - .inner('body', pages => pages.find('main.post')) + .inner('main', showPage('post')) .inner('article h1', rheo(post.data.get('title'))) .inner('article main', rheo(post.body)) .pipe(setTitle())@@ -78,9 +94,10 @@ `Could not find ${term} ${value}` ) ctx.set('Content-Type', 'text/html') - ctx.body = index() + ctx.body = templates + .layout() .pipe(rheo()) - .inner('body', pages => pages.find('main.taxon')) + .inner('main', showPage('taxon')) .inner('h1', rheo(config.site.title)) .inner('.posts', function (postsTemplate) { return toArrayStream(items.get(value)).pipe(