mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-14 16:36:21 +00:00
45 lines
1001 B
JavaScript
45 lines
1001 B
JavaScript
|
const nunjucks = require('nunjucks')
|
||
|
|
||
|
class NunjucksRenderer {
|
||
|
directory
|
||
|
environment
|
||
|
|
||
|
constructor (directory = '', options = {}) {
|
||
|
if (typeof directory !== 'string') {
|
||
|
throw new TypeError('Root directory must be a string value')
|
||
|
}
|
||
|
|
||
|
this.directory = directory
|
||
|
|
||
|
this.environment = nunjucks.configure(
|
||
|
this.directory,
|
||
|
Object.assign(options, {
|
||
|
autoescape: true
|
||
|
})
|
||
|
)
|
||
|
}
|
||
|
|
||
|
#middleware (req, res, next) {
|
||
|
// Inject render() method into Response on each requests
|
||
|
res.render = (path, params) => this.#render(res, path, params)
|
||
|
return next()
|
||
|
}
|
||
|
|
||
|
#render (res, path, params) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
this.environment.render(`${path}.njk`, params, (err, html) => {
|
||
|
if (err) return reject(err)
|
||
|
resolve(html)
|
||
|
})
|
||
|
}).then(html => {
|
||
|
return res.type('html').send(html)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
get middleware () {
|
||
|
return this.#middleware.bind(this)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = NunjucksRenderer
|