From 86a7b7c790c1807f2121cdb9ebf3fc7939989a4f Mon Sep 17 00:00:00 2001 From: HerrHase Date: Mon, 28 Nov 2022 23:14:42 +0100 Subject: [PATCH] adding --- example/views/helpers/meta.njk | 1 + example/views/home.njk | 2 +- example/views/post.njk | 5 ----- src/engines/nunjucks.js | 22 ++++++++++++++++++---- 4 files changed, 20 insertions(+), 10 deletions(-) delete mode 100644 example/views/post.njk diff --git a/example/views/helpers/meta.njk b/example/views/helpers/meta.njk index 71a0549..c4fb67d 100644 --- a/example/views/helpers/meta.njk +++ b/example/views/helpers/meta.njk @@ -25,5 +25,6 @@ {% endif %} + {% endif %} {% endmacro %} \ No newline at end of file diff --git a/example/views/home.njk b/example/views/home.njk index afb1500..31847cc 100644 --- a/example/views/home.njk +++ b/example/views/home.njk @@ -2,7 +2,7 @@ {% block main %} - + {{ page.content | safe }} {% endblock %} \ No newline at end of file diff --git a/example/views/post.njk b/example/views/post.njk deleted file mode 100644 index e3728e2..0000000 --- a/example/views/post.njk +++ /dev/null @@ -1,5 +0,0 @@ -{% extends('layout.njk') %} - -{% block main %} -{{ page.content | safe }} -{% endblock %} \ No newline at end of file diff --git a/src/engines/nunjucks.js b/src/engines/nunjucks.js index fae2d25..4056bcc 100644 --- a/src/engines/nunjucks.js +++ b/src/engines/nunjucks.js @@ -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,