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

PDF not supoted yet

") // 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: `

${vak.title}

` }) }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: "

Hoi

" }, 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: "

404 - Page not found

" }, (err) => { if(err){ res.send("

404 - Page not found

") } }) } app.listen(8081, ()=>{console.log("server started")})