feat: Add code block highlighting Theme is configurable
1 file changed, 22 insertions(+), 0 deletions(-)
changed files
M src/actions.js → 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");@@ -7,6 +9,25 @@ function home(config, posts) { const postsArray = Array.from(posts.values()); return async function(ctx, next) { responders.home(ctx, config, 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; }; }@@ -41,6 +62,7 @@ } module.exports = { home, + highlightTheme, post, taxonGenerator, serveFiles