From e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 3 Jul 2017 21:39:43 +0200 Subject: feat: Add code block highlighting Theme is configurable --- src/actions.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/actions.js') diff --git a/src/actions.js b/src/actions.js index 8a04671..7c7482f 100644 --- a/src/actions.js +++ b/src/actions.js @@ -1,5 +1,7 @@ "use strict"; +const fs = require("fs"); +const path = require("path"); const send = require("koa-send"); const responders = require("./responders"); @@ -10,6 +12,25 @@ function home(config, posts) { }; } +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, posts) { return async function(ctx, next) { ctx.assert(posts.has(ctx.params.filename), 404, "Post not found"); @@ -41,6 +62,7 @@ async function serveFiles(ctx) { module.exports = { home, + highlightTheme, post, taxonGenerator, serveFiles -- cgit 1.4.1