diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/index.js | 82 | ||||
-rw-r--r-- | src/templates/index.html | 36 | ||||
-rw-r--r-- | src/views/index.njk | 17 | ||||
-rw-r--r-- | src/views/layouts/main.njk | 13 | ||||
-rw-r--r-- | src/views/post.njk | 8 | ||||
-rw-r--r-- | src/views/term.njk | 7 |
6 files changed, 93 insertions, 70 deletions
diff --git a/src/index.js b/src/index.js index fe0351d..93d61a4 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,9 @@ const Koa = require('koa') const app = new Koa() +const fs = require('fs') +const streamify = require('stream-array') + const send = require('koa-send') const config = require('./modules/config.js') @@ -12,51 +15,80 @@ const PORT = process.env.PORT || config.server.port const Router = require('koa-router') const router = new Router() -const view = require('koa-nunjucks-next') +const rheo = require('rheo') const Posts = require('./modules/posts.js') const posts = Posts.getFolder(config.posts.folder) -app.use( - view(`${__dirname}/views`, { - extname: 'njk', - globals: { - site: config.site, - url: (...args) => router.url(...args) - } - }) -) +const index = () => fs.createReadStream(`${__dirname}/templates/index.html`) -const postsArray = Array.from(posts.entries()) -router.get('home', '/', async function (ctx) { - await ctx.render('index', { - posts: postsArray - }) +function setTitle (pageTitle) { + return rheo.template(s => + s.inner('title', rheo((pageTitle ? ' · ' : '') + config.site.title)) + ) +} + +function renderPostListItem (template, [, post]) { + return template + .attribute('a', 'href', () => router.url('post', post.basename)) + .inner('a', () => rheo(post.data.get('title'))) +} + +function toArrayStream (iterator) { + return streamify(Array.from(iterator.entries())) +} + +const postsStream = toArrayStream(posts) +router.get('home', '/', async function (ctx, next) { + ctx.set('Content-Type', 'text/html') + ctx.body = index() + .pipe(rheo()) + .inner('body', pages => pages.find('main.homepage')) + .inner('h1', rheo(config.site.title)) + .inner('.posts', function (postsTemplate) { + return postsStream.pipe(postsTemplate.map(renderPostListItem)) + }) + .pipe(setTitle()) + .render() }) -router.get('post', '/post/:filename', async function (ctx) { +router.get('post', '/post/:filename', async function (ctx, next) { ctx.assert(posts.has(ctx.params.filename), 404, 'Post not found') const post = posts.get(ctx.params.filename) post.body = Posts.render(post) - await ctx.render('post', { - post: post - }) + + ctx.set('Content-Type', 'text/html') + ctx.body = index() + .pipe(rheo()) + .inner('body', pages => pages.find('main.post')) + .inner('article h1', rheo(post.data.get('title'))) + .inner('article main', rheo(post.body)) + .pipe(setTitle()) + .render() }) const taxonomies = Posts.taxonomise(config.taxonomies, posts) for (let [term, items] of taxonomies) { - router.get(`taxon-${term}`, `/${term}/:value`, async function (ctx) { + router.get(`taxon-${term}`, `/${term}/:value`, async function (ctx, next) { const value = ctx.params.value ctx.assert( items.has(ctx.params.value), 404, `Could not find ${term} ${value}` ) - await ctx.render('term', { - term: term, - [term]: value, - posts: items.get(ctx.params.value) - }) + + ctx.set('Content-Type', 'text/html') + ctx.body = index() + .pipe(rheo()) + .inner('body', pages => pages.find('main.taxon')) + .inner('h1', rheo(config.site.title)) + .inner('.posts', function (postsTemplate) { + return toArrayStream(items.get(value)).pipe( + postsTemplate.map(renderPostListItem) + ) + }) + .pipe(setTitle()) + .render() }) } diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..412b4ec --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,36 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"/> + <title></title> + </head> + <body> + <main class="homepage"> + <h1>hello world</h1> + + <ul class="posts"> + <li class="post"> + <a href="/">Test post please ignore</a> + </li> + </ul> + </main> + <main class="post"> + <article> + <h1>post title</h1> + <main> + Fringilla ut morbi tincidunt augue interdum velit euismod! + Interdum velit laoreet id donec ultrices tincidunt arcu, non + sodales neque sodales ut etiam sit amet nisl purus, in + mollis nunc sed. + </main> + </article> + </main> + <main class="taxon"> + <ul class="posts"> + <li class="post"> + <a href="/">Test post please ignore</a> + </li> + </ul> + </main> + </body> +</html> diff --git a/src/views/index.njk b/src/views/index.njk deleted file mode 100644 index 9a7cc80..0000000 --- a/src/views/index.njk +++ /dev/null @@ -1,17 +0,0 @@ -{% extends "layouts/main.njk" %} - -{% block body %} - - <h1>hello world</h1> - - <ul> - {% for filename, post in posts %} - <li> - <a href="{{ router.url('post', post.basename)}}"> - {{ post.data.get('title') }} - </a> - </li> - {% endfor %} - </ul> - -{% endblock %} diff --git a/src/views/layouts/main.njk b/src/views/layouts/main.njk deleted file mode 100644 index 1fe1a93..0000000 --- a/src/views/layouts/main.njk +++ /dev/null @@ -1,13 +0,0 @@ -<!doctype html> -<html lang="en"> - <head> - <meta charset="utf-8"/> - <title>{% block title %}{{ site.title }}{% endblock %}</title> - </head> - <body> - <main> - {% block body %} - {% endblock %} - </main> - </body> -</html> diff --git a/src/views/post.njk b/src/views/post.njk deleted file mode 100644 index d6a4f92..0000000 --- a/src/views/post.njk +++ /dev/null @@ -1,8 +0,0 @@ -{% extends "layouts/main.njk" %} - -{% block body %} - -{{ post.data.title }} - -{{ post.body | safe }} -{% endblock %} diff --git a/src/views/term.njk b/src/views/term.njk deleted file mode 100644 index 6ac932e..0000000 --- a/src/views/term.njk +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "layouts/main.njk" %} - -{% block body %} -{% for post in posts %} - {{ post.data.get('title') }} -{% endfor %} -{% endblock %} |