diff --git a/example/views/layout.njk b/example/views/layout.njk index fd77dc1..2222f58 100644 --- a/example/views/layout.njk +++ b/example/views/layout.njk @@ -8,7 +8,7 @@ {{ site.title }} | {{ page.title }} - {{ helpers.meta(page) }} + {{ helpers.meta(page, site) }} @@ -22,7 +22,7 @@
diff --git a/src/engines/nunjucks.js b/src/engines/nunjucks.js index 4c9f901..8c3ce84 100644 --- a/src/engines/nunjucks.js +++ b/src/engines/nunjucks.js @@ -1,6 +1,7 @@ const nunjucks = require('nunjucks') const fs = require('fs') const assign = require('assign-deep') +const { minify } = require('html-minifier') const configStore = require('./../config.js') const { asset, resize } = require('./helpers.js') @@ -68,6 +69,18 @@ class Engine { 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, + collapseWhitespace: true, + collapseInlineTagWhitespace: true + }) + } + done(error, response) }) } diff --git a/src/siteomat.js b/src/siteomat.js index 0973cae..b06ed2a 100644 --- a/src/siteomat.js +++ b/src/siteomat.js @@ -1,7 +1,7 @@ const fs = require('fs') const path = require('path') const mkdirp = require('mkdirp') -const { minify } = require('html-minifier') +const assign = require('assign-deep') const configStore = require('./config.js') @@ -31,6 +31,11 @@ class Siteomat { * */ constructor(source, views, options = {}) { + + if (options.destination === undefined) { + throw new Error('Destination is undefined') + } + this._source = source this._views = views this._destination = options.destination @@ -39,7 +44,7 @@ class Siteomat { configStore.set('source', source) configStore.set('destination', this._destination) configStore.set('views', views) - configStore.set('options', Object.assign({}, { + configStore.set('options', assign({ 'minifyHtml': true }, options)) @@ -81,17 +86,6 @@ class Siteomat { return; } - const options = configStore.get('options') - - // if options minifyHtml is set, minify html - if (options.minifyHtml === true) { - content = minify(content, { - removeComments: true, - collapseWhitespace: true, - collapseInlineTagWhitespace: true - }) - } - // create directories and write file = page mkdirp(this._destination + page.pathname).then(() => { fs.writeFileSync(this._destination + page.pathname + '/' + page.filename, content) diff --git a/test.js b/test.js index 521e49e..5b0c5ec 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,7 @@ const Siteomat = require('./src/siteomat.js') -const siteomat = new Siteomat('./example/site', './example/views', './public') +const siteomat = new Siteomat('./example/site', './example/views', { + 'destination': './public' +}) + siteomat.run() \ No newline at end of file