|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
const nunjucks = require('nunjucks')
|
|
|
|
|
const { minify } = require('html-minifier')
|
|
|
|
|
const fs = require('fs')
|
|
|
|
|
const assign = require('assign-deep')
|
|
|
|
|
|
|
|
|
|
const configStore = require('./../config.js')
|
|
|
|
|
const { asset, resize } = require('./helpers.js')
|
|
|
|
@ -17,10 +18,18 @@ const PageQuery = require('./../queries/pages.js')
|
|
|
|
|
*/
|
|
|
|
|
class Engine {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param {string} views
|
|
|
|
|
* @param {object} site
|
|
|
|
|
* @param {object} options
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
constructor(views, site, options) {
|
|
|
|
|
|
|
|
|
|
// merge options
|
|
|
|
|
this._options = Object.assign({}, {
|
|
|
|
|
// merge data
|
|
|
|
|
this._options = assign({}, {
|
|
|
|
|
autoescapes: true,
|
|
|
|
|
throwOnUndefined: true,
|
|
|
|
|
web: {
|
|
|
|
@ -29,6 +38,8 @@ class Engine {
|
|
|
|
|
}, options)
|
|
|
|
|
|
|
|
|
|
this.nunjucks = nunjucks.configure(views, this._options)
|
|
|
|
|
|
|
|
|
|
// add filter: resize
|
|
|
|
|
this.nunjucks.addFilter('resize', (...args) => {
|
|
|
|
|
const done = args.pop()
|
|
|
|
|
const options = args?.[2] ? {} : args[2]
|
|
|
|
@ -36,7 +47,7 @@ class Engine {
|
|
|
|
|
resize(args[0], args[1], options, done)
|
|
|
|
|
}, true)
|
|
|
|
|
|
|
|
|
|
// adding defaults for view, function and data = config.yml
|
|
|
|
|
// adding defaults for view, data from site.yml, functions and pageQuery
|
|
|
|
|
this._defaults = {
|
|
|
|
|
site: site,
|
|
|
|
|
asset: asset,
|
|
|
|
@ -53,12 +64,15 @@ class Engine {
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
render(view, data, done) {
|
|
|
|
|
data = Object.assign({}, data, this._defaults)
|
|
|
|
|
|
|
|
|
|
// merge data
|
|
|
|
|
data = assign({}, data, this._defaults)
|
|
|
|
|
|
|
|
|
|
this.nunjucks.render(view, data, (error, response) => {
|
|
|
|
|
|
|
|
|
|
const options = configStore.get('options')
|
|
|
|
|
|
|
|
|
|
// if options minifyHtml is set, minify html
|
|
|
|
|
if (options.minifyHtml === true) {
|
|
|
|
|
response = minify(response, {
|
|
|
|
|
removeComments: true,
|
|
|
|
|