From 562acb70a9dd560a0414b7a24fbd01fc6f1c64a3 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Wed, 4 Aug 2021 20:08:33 +0200 Subject: [PATCH] Added markdown support. --- content/pages/article/markdown.njk | 32 ++++++++++++++++++++++++++++++ index.js | 21 +++++++------------- libs/requestHandler.js | 15 ++++++++++++++ package.json | 4 +++- 4 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 content/pages/article/markdown.njk create mode 100644 libs/requestHandler.js diff --git a/content/pages/article/markdown.njk b/content/pages/article/markdown.njk new file mode 100644 index 0000000..e39e92c --- /dev/null +++ b/content/pages/article/markdown.njk @@ -0,0 +1,32 @@ +{% extends "templates/article.njk" %} +{% import "components/toc.njk" as toc with context %} + +{% block header %} + Markdown +{% endblock %} + +{% block toc %} + {% output "toc" %}{% endoutput %} +{% endblock %} + +{% block body %} + {% markdown %} +# Test of markdown + +Lmao i mitt liv. + +```js + const testing = "a code block"; +``` + +* A list item +* A list item +* A list item +* A list item + +*italic* **bold** + +{{ toc.header("h1", "test header") }} + + {% endmarkdown %} +{% endblock %} \ No newline at end of file diff --git a/index.js b/index.js index 28d1301..0be83eb 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,15 @@ import express from 'express' import njk from 'nunjucks' import njkAppend from 'nunjucks-append' +import njkMarkdown from 'nunjucks-markdown' +import marked from 'marked' import fs from 'fs' +import { requestHandler } from './libs/requestHandler.js' +import { markedRenderer } from './libs/markedRenderer.js' -const ConfigFile = await fs.readFileSync('./config.json') +const ConfigFile = fs.readFileSync('./config.json') const Config = JSON.parse(ConfigFile) @@ -26,23 +30,12 @@ const njkEnv = njk.configure( ) njkAppend.initialise(njkEnv) +njkMarkdown.register(njkEnv, marked) Server.get( '*', - (req, res) => { - const context = { - serverName: Config.serverName - } - - if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}.njk`, context)) - return res.render(`pages/${req.path}.njk`) - - if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}/index.njk`, context)) - return res.render(`pages/${req.path}/index.njk`) - - return res.status(404).render('errors/404.njk', context) - } + (req, res) => requestHandler(req, res, Config) ) Server.listen( diff --git a/libs/requestHandler.js b/libs/requestHandler.js new file mode 100644 index 0000000..62a8695 --- /dev/null +++ b/libs/requestHandler.js @@ -0,0 +1,15 @@ +import fs from 'fs' + +export function requestHandler(req, res, Config) { + const context = { + serverName: Config.serverName + } + + if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}.njk`, context)) + return res.render(`pages/${req.path}.njk`) + + if (fs.existsSync(`./${Config.contentDir}/pages/${req.path}/index.njk`, context)) + return res.render(`pages/${req.path}/index.njk`) + + return res.status(404).render('errors/404.njk', context) +} \ No newline at end of file diff --git a/package.json b/package.json index f4cbd00..e1c4e96 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,9 @@ "dependencies": { "chokidar": "^3.5.2", "express": "^4.17.1", + "marked": "^2.1.3", "nunjucks": "^3.2.3", - "nunjucks-append": "^0.0.3" + "nunjucks-append": "^0.0.3", + "nunjucks-markdown": "^2.0.1" } }