Added markdown support.

This commit is contained in:
BurnyLlama 2021-08-04 20:08:33 +02:00
parent 5e1c9fd266
commit 562acb70a9
4 changed files with 57 additions and 15 deletions

View File

@ -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 %}

View File

@ -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(

15
libs/requestHandler.js Normal file
View File

@ -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)
}

View File

@ -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"
}
}