diff options
author | Alan Pearce | 2017-07-03 21:39:43 +0200 |
---|---|---|
committer | Alan Pearce | 2017-07-03 21:55:41 +0200 |
commit | e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f (patch) | |
tree | 027fda29fe96736d4ac641a1dfe0bfe657d3fd33 /src/actions.js | |
parent | a67e38d1a82c95db5bd24183e81b31438f60dd2c (diff) | |
download | homestead-e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f.tar.lz homestead-e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f.tar.zst homestead-e7b08b1dfe3f2a2596deb6e2a72bb79805d3708f.zip |
feat: Add code block highlighting
Theme is configurable
Diffstat (limited to 'src/actions.js')
-rw-r--r-- | src/actions.js | 22 |
1 files changed, 22 insertions, 0 deletions
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 |