"use strict"; const fs = require("fs"); const path = require("path"); const send = require("koa-send"); function home(config, responder, posts) { const postsArray = Array.from(posts.values()); return async function(ctx, next) { responder(ctx, config, { posts: postsArray }); }; } function posts(config, responder, posts) { const postsArray = Array.from(posts.values()); return async function(ctx, next) { responder(ctx, config, { listType: null, listName: "Posts", posts: postsArray }); }; } function highlightTheme(config) { const theme = config.posts.code.theme; const themeFile = path.resolve( __dirname, `../node_modules/highlight.js/styles/${theme}.css` ); if (!fs.existsSync(themeFile)) { throw new Error(`Couldn't find highlight theme ${theme}`); } const css = fs.readFileSync(themeFile, "utf-8"); return async function(ctx, next) { ctx.type = "css"; ctx.body = css; }; } function post(config, responder, posts) { return async function(ctx, next) { ctx.assert(posts.has(ctx.params.filename), 404, "Post not found"); const post = posts.get(ctx.params.filename); responder(ctx, config, { post }); }; } function taxonGenerator(config, responder, term, items) { return async function(ctx, next) { const value = ctx.params.value; ctx.assert(items.has(ctx.params.value), 404, `${term} ${value} not found`); const taxonItems = items.get(value); responder(ctx, config, { listType: term, listName: value, posts: taxonItems }); }; } const prefix = /^\/static\//; async function serveFiles(ctx) { if (prefix.test(ctx.path)) { await send(ctx, ctx.path.replace(prefix, ""), { root: "./static" }); } } module.exports = { home, posts, highlightTheme, post, taxonGenerator, serveFiles };