2021-08-05 23:27:11 +02:00
|
|
|
import fs from 'fs'
|
2021-07-27 19:24:51 +02:00
|
|
|
import express from 'express'
|
2021-08-05 23:27:11 +02:00
|
|
|
|
2021-07-27 19:24:51 +02:00
|
|
|
import njk from 'nunjucks'
|
2021-08-04 20:08:33 +02:00
|
|
|
import njkMarkdown from 'nunjucks-markdown'
|
2021-08-05 23:27:11 +02:00
|
|
|
|
2021-08-04 20:08:33 +02:00
|
|
|
import marked from 'marked'
|
2021-08-04 22:52:49 +02:00
|
|
|
import hljs from 'highlight.js'
|
2021-07-27 19:24:51 +02:00
|
|
|
|
2021-08-05 23:27:11 +02:00
|
|
|
import { requestHandler } from './libs/requestHandler.js'
|
2021-08-06 14:46:16 +02:00
|
|
|
import { markedRenderer } from './libs/markedRenderer.js'
|
2021-07-27 19:24:51 +02:00
|
|
|
|
2021-07-27 21:04:57 +02:00
|
|
|
|
2021-08-05 23:27:11 +02:00
|
|
|
// Load in config
|
2021-08-04 20:08:33 +02:00
|
|
|
const ConfigFile = fs.readFileSync('./config.json')
|
2021-07-27 19:24:51 +02:00
|
|
|
const Config = JSON.parse(ConfigFile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Server = express()
|
|
|
|
|
2021-08-05 23:27:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Some config
|
2021-07-27 19:24:51 +02:00
|
|
|
Server.use('/assets', express.static(Config.assetsDir))
|
|
|
|
|
2021-08-04 17:48:11 +02:00
|
|
|
const njkEnv = njk.configure(
|
2021-07-27 19:24:51 +02:00
|
|
|
Config.contentDir,
|
|
|
|
{
|
2021-08-05 23:27:11 +02:00
|
|
|
autoescape: true,
|
2021-07-27 21:57:48 +02:00
|
|
|
watch: true,
|
|
|
|
trimBlocks: true,
|
|
|
|
lstripBlocks: true,
|
2021-07-27 19:24:51 +02:00
|
|
|
express: Server
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-08-04 22:52:49 +02:00
|
|
|
marked.setOptions({
|
|
|
|
highlight: (code, lang) => {
|
|
|
|
const language = hljs.getLanguage(lang) ? lang : 'plaintext'
|
|
|
|
return hljs.highlight(language, code)
|
|
|
|
.value.replace(/\n/g, "<br>").replace(/•/g, " ")
|
|
|
|
},
|
2021-08-05 23:27:11 +02:00
|
|
|
gfm: true
|
2021-08-04 22:52:49 +02:00
|
|
|
})
|
|
|
|
|
2021-08-06 14:46:16 +02:00
|
|
|
marked.use({ renderer: markedRenderer })
|
2021-08-05 23:27:11 +02:00
|
|
|
|
2021-08-04 20:08:33 +02:00
|
|
|
njkMarkdown.register(njkEnv, marked)
|
2021-07-27 19:24:51 +02:00
|
|
|
|
|
|
|
|
2021-07-27 21:04:57 +02:00
|
|
|
|
2021-08-05 23:27:11 +02:00
|
|
|
// Send all requests to the requestHandler.
|
|
|
|
Server.get('*', (req, res) => requestHandler(req, res, Config))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Start the server
|
2021-07-27 19:24:51 +02:00
|
|
|
Server.listen(
|
|
|
|
Config.serverPort,
|
|
|
|
() => {
|
|
|
|
console.log(`Started server on ${Config.serverPort}.`)
|
|
|
|
}
|
|
|
|
)
|