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