Allow HTML embed, add extra scripts/styles support

master
kts of kettek (POWERQWACK) 2021-05-20 18:25:42 -07:00
parent d59ef1c589
commit 8a8e247a14
2 changed files with 54 additions and 24 deletions

View File

@ -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]]
@ -57,9 +58,26 @@ module.exports = (plugin, app) => {
text = data.toString()
}
// render out the md
let env = {}
let rendered = md.render(text, env)
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)
}
}
// build the location
let loc = '<a href="/">'+app.live.conf['shortname']+'</a>'
@ -68,10 +86,16 @@ module.exports = (plugin, app) => {
loc += ' &#8594 <a href="'+crumbs.slice(0,i).join('/')+'/'+crumbs[i]+'">'+(i == crumbs.length-1 ? env.title : crumbs[i].replace('-', ' '))+'</a>'
}
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 })
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,
})
})

View File

@ -3,7 +3,13 @@
<title>{{? it.title }} {{=it.title}} - {{?}} {{? it.www_name }} {{=it.www_name}} {{?}}</title>
<link rel="icon" type="image/png" href="/favicon.png">
<link rel='stylesheet' type='text/css' href='/style.css'>
{{~ it.styles :s}}
<link rel='stylesheet' type='text/css' href='{{=s}}'>
{{~}}
<script type='text/javascript' src='/candy.js'></script>
{{~ it.scripts :s}}
<script type='text/javascript' src='{{=s}}'></script>
{{~}}
</head>
<body>
<div id="location">{{=it.location || ''}}</div>