diff --git a/plugins/srd-router/route.js b/plugins/srd-router/route.js index 1ed5e64..4c9f52c 100644 --- a/plugins/srd-router/route.js +++ b/plugins/srd-router/route.js @@ -1,6 +1,6 @@ const router = require('express').Router() const mcache = require('memory-cache') -const md = require('markdown-it')({ typographer: true }) +const md = require('markdown-it')({ html: true, typographer: true }) md.use(require('markdown-it-anchor'), {permalink: true, level: 2, permalinkBefore: true, permalinkSymbol: "ยง"}) md.use(require('markdown-it-title'), 0) md.use(require('markdown-it-attrs')) @@ -26,7 +26,7 @@ module.exports = (plugin, app) => { } } - router.get('*', cache(1), function(req, res) { + router.get('*', cache(1), async function(req, res) { let target = req.path if (target === "/") { // Root target = '/index' @@ -34,17 +34,18 @@ module.exports = (plugin, app) => { target = target.substring(0, target.length-1) } - let file = path.join(app.config.srd, target+'.md') + let md_file = path.join(app.config.srd, target+'.md') + let json_file = path.join(app.config.srd, target+'.json') + let output = '' + let env = {} + let title = '' let crumbs = target.split('/') - fs.readFile(file, (err, data) => { - if (err) { - res.render('index', {content: '404', menu: app.live.menu, location: '404', title: '404', www_name: app.live.conf.www_name, www_copyright: app.live.conf.www_copyright }) - return - //throw err - } - + // Attempt to read md + try { + // Process source data. + let data = await fs.promises.readFile(md_file) // make a local copy of our dictionary and remove the current page, if existing let local_dictionary = Object.assign({}, app.live.dictionary) delete local_dictionary[crumbs[crumbs.length-1]] @@ -56,22 +57,45 @@ module.exports = (plugin, app) => { } else { text = data.toString() } - - // render out the md - let env = {} - let rendered = md.render(text, env) - // build the location - let loc = ''+app.live.conf['shortname']+'' - for (let i = 1; i < crumbs.length; i++) { - if (crumbs[i] == '') continue - loc += ' → '+(i == crumbs.length-1 ? env.title : crumbs[i].replace('-', ' '))+'' + output = md.render(text, env) + title = env.title + } catch(err) { + output = '404' + title = '404' + } + + // Check for json extra + let extra = { + scripts: [], + styles: [], + } + try { + let data = await fs.promises.readFile(json_file) + extra = {...extra, ...JSON.parse(data)} + } catch(err) { + if (err.code !== 'ENOENT') { + plugin.error(err) } - - let title = env.title - - // render with the engine - res.render('index', {content: rendered, menu: app.live.menu, location: loc, title: (title != 'index' ? title : ''), www_name: app.live.conf.www_name, www_copyright: app.live.conf.www_copyright }) + } + + // build the location + let loc = ''+app.live.conf['shortname']+'' + for (let i = 1; i < crumbs.length; i++) { + if (crumbs[i] == '') continue + loc += ' → '+(i == crumbs.length-1 ? env.title : crumbs[i].replace('-', ' '))+'' + } + + // render with the engine + res.render('index', { + content: output, + menu: app.live.menu, + location: loc, + title: (title != 'index' ? title : ''), + www_name: app.live.conf.www_name, + www_copyright: app.live.conf.www_copyright, + scripts: extra.scripts, + styles: extra.styles, }) }) diff --git a/views/index.dot b/views/index.dot index cc86eb5..ba629f8 100644 --- a/views/index.dot +++ b/views/index.dot @@ -3,7 +3,13 @@ {{? it.title }} {{=it.title}} - {{?}} {{? it.www_name }} {{=it.www_name}} {{?}} + {{~ it.styles :s}} + + {{~}} + {{~ it.scripts :s}} + + {{~}}
{{=it.location || ''}}