summary refs log tree commit diff stats
path: root/src/index.js
blob: 93d61a40b63ef829ea51956455b03cf45826ab90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict'

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')

const PORT = process.env.PORT || config.server.port

const Router = require('koa-router')
const router = new Router()

const rheo = require('rheo')

const Posts = require('./modules/posts.js')
const posts = Posts.getFolder(config.posts.folder)

const index = () => fs.createReadStream(`${__dirname}/templates/index.html`)

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, next) {
  ctx.assert(posts.has(ctx.params.filename), 404, 'Post not found')
  const post = posts.get(ctx.params.filename)
  post.body = Posts.render(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, next) {
    const value = ctx.params.value
    ctx.assert(
      items.has(ctx.params.value),
      404,
      `Could not find ${term} ${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()
  })
}

app.use(router.routes()).use(router.allowedMethods())

const prefix = /^\/static\//
app.use(async function (ctx) {
  if (prefix.test(ctx.path)) {
    await send(ctx, ctx.path.replace(prefix, ''), {
      root: './static'
    })
  }
})

module.exports = app

if (require.main === module) {
  app.listen(PORT, () => {
    console.log(`App listening on port ${PORT}`)
  })
}