158 lines
3.7 KiB
JavaScript
Executable File
158 lines
3.7 KiB
JavaScript
Executable File
const showdown = require('showdown')
|
|
const express = require('express')
|
|
const fs = require('fs')
|
|
|
|
const siteMap = require('./siteMap')
|
|
|
|
// ========================================
|
|
// == compiles ============================
|
|
// ========================================
|
|
|
|
const MDCompile = new showdown.Converter({tables: true})
|
|
|
|
function compileMD(file, fn){
|
|
fs.readFile(file, 'utf-8', function(err, markdown){
|
|
if(err){
|
|
console.error("ERROR: could not read file:", file)
|
|
if(typeof fn == 'function')
|
|
fn(-1)
|
|
}
|
|
html = MDCompile.makeHtml(markdown);
|
|
if(typeof fn == 'function')
|
|
fn(html)
|
|
else
|
|
fs.writeFile(fn, html, function(err){
|
|
if(err)
|
|
console.error(err)
|
|
})
|
|
})
|
|
}
|
|
|
|
function compilePDF(file, fn){
|
|
fs.readFile(file, 'utf-8', function(err, pdf){
|
|
if(err){
|
|
console.error("ERROR: could not read file:", file)
|
|
if(typeof fn == 'function')
|
|
fn(-1)
|
|
}
|
|
return fn("<h1>PDF not supoted yet</h1>")
|
|
// pdf = MDCompile.makeHtml(markdown);
|
|
// if(typeof fn == 'function')
|
|
// fn(html)
|
|
// else
|
|
// fs.writeFile(fn, html, function(err){
|
|
// if(err)
|
|
// console.error(err)
|
|
// })
|
|
})
|
|
}
|
|
|
|
// ========================================
|
|
// == webserver ===========================
|
|
// ========================================
|
|
|
|
const app = express()
|
|
app.set('view engine', 'ejs');
|
|
app.set('views', __dirname + '/views')
|
|
|
|
app.use('/img', function(req, res, next){
|
|
res.sendFile(__dirname + '/img' + req.path)
|
|
})
|
|
|
|
app.use('/:vak/static', renderStatic)
|
|
app.use('/:vak/:page', renderPage)
|
|
app.use('/:vak', renderVak)
|
|
app.use('/', renderIndex)
|
|
app.use(render404)
|
|
|
|
function onError404(req, res, next){
|
|
return function(err){
|
|
render404(req, res, next)
|
|
}
|
|
}
|
|
|
|
function renderStatic(req, res, next){
|
|
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
|
vak = siteMap[req.params.vak]
|
|
|
|
dir = __dirname.split('/'); dir.pop() // remove dir of this code
|
|
dir = `${dir.join('/')}/${vak.title}/notes/static${req.path}`
|
|
|
|
res.sendFile(dir, onError404(req, res, next))
|
|
}
|
|
|
|
function renderPage(req, res, next){
|
|
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
|
vak = siteMap[req.params.vak]
|
|
|
|
if(typeof vak.pages[req.params.page] == 'undefined') return next();
|
|
page = vak.pages[req.params.page]
|
|
|
|
|
|
compileMD(page.file, function(html){
|
|
if(typeof html != 'string'){ return next() }
|
|
|
|
res.render('mainV2', {
|
|
siteMap,
|
|
title: "notes",
|
|
topNav: vak.pages,
|
|
vak: req.params.vak,
|
|
page: req.params.page,
|
|
main: html
|
|
})
|
|
})
|
|
}
|
|
|
|
function renderVak(req, res, next){
|
|
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
|
vak = siteMap[req.params.vak]
|
|
|
|
if(vak.index == null){
|
|
res.render('mainV2', {
|
|
siteMap,
|
|
title: "notes",
|
|
topNav: vak.pages,
|
|
vak: req.params.vak,
|
|
main: `<h1>${vak.title}</h1>`
|
|
})
|
|
}else{
|
|
compileMD(vak.index, function(html){
|
|
if(typeof html != 'string'){ return next() }
|
|
|
|
res.render('mainV2', {
|
|
siteMap,
|
|
title: "notes",
|
|
topNav: vak.pages,
|
|
vak: req.params.vak,
|
|
main: html
|
|
}, onError404(req, res, next))
|
|
})
|
|
}
|
|
}
|
|
|
|
function renderIndex(req, res, next){
|
|
res.render('mainV2', {
|
|
siteMap,
|
|
title: "notes",
|
|
topNav: siteMap,
|
|
main: "<h1>Hoi</h1>"
|
|
}, onError404(req, res, next))
|
|
}
|
|
|
|
function render404(req, res, next){
|
|
res.status(404)
|
|
res.render('mainV2', {
|
|
siteMap,
|
|
title: "notes",
|
|
topNav: vak.pages,
|
|
vak: req.params.vak,
|
|
main: "<h1>404 - Page not found</h1>"
|
|
}, (err) => {
|
|
if(err){
|
|
res.send("<h1>404 - Page not found</h1>")
|
|
}
|
|
})
|
|
}
|
|
|
|
app.listen(8081, ()=>{console.log("server started")})
|