diff --git a/example/views/helpers/meta.njk b/example/views/helpers/meta.njk index c4fb67d..362b7dc 100644 --- a/example/views/helpers/meta.njk +++ b/example/views/helpers/meta.njk @@ -25,6 +25,6 @@ {% endif %} - + {% endif %} {% endmacro %} \ No newline at end of file diff --git a/src/engines/nunjucks.js b/src/engines/nunjucks.js index 8c3ce84..5a8f346 100644 --- a/src/engines/nunjucks.js +++ b/src/engines/nunjucks.js @@ -31,10 +31,7 @@ class Engine { // merge data this._options = assign({}, { autoescapes: true, - throwOnUndefined: true, - web: { - async: true - } + throwOnUndefined: true }, options) this.nunjucks = nunjucks.configure(views, this._options) @@ -63,12 +60,18 @@ class Engine { * @return {string} * */ - render(view, data, done) { + render(page, done) { // merge data - data = assign(data, this._defaults) + const data = assign({ + page: page + }, this._defaults) + + this.nunjucks.render(data.page.view, data, (error, response) => { - this.nunjucks.render(view, data, (error, response) => { + if (error) { + console.error(error) + } const options = configStore.get('options') diff --git a/src/factories/block.js b/src/factories/block.js new file mode 100644 index 0000000..4be1ad9 --- /dev/null +++ b/src/factories/block.js @@ -0,0 +1,47 @@ +const path = require('path') +const parseMarkdownFile = require('./../parsers/markdown.js') + +/** + * Block + * + * parsed markdown-file that can + * contains fields as yaml + * + * @author Björn Hase + * @license http://opensource.org/licenses/MIT The MIT License + * @link https://gitea.node001.net/HerrHase/siteomat-webpack-plugin.git + * + */ + +class Block { + + /** + * + * + * @param {string} fileString + * + */ + constructor(fileString) { + + // parse string of file + const parsedFile = parseMarkdownFile(fileString) + + // getting parsed data + this._content = parsedFile.content + this._fields = parsedFile.fields + } + + /** + * + * + * @return {object} + * + */ + get() { + return assign({ + 'content': this._content + }, this._fields) + } +} + +module.exports = Block \ No newline at end of file diff --git a/src/factories/page.js b/src/factories/page.js new file mode 100644 index 0000000..b5f3209 --- /dev/null +++ b/src/factories/page.js @@ -0,0 +1,111 @@ +const path = require('path') +const slugify = require('slugify') +const merge = require('lodash.merge') +const nunjucks = require('nunjucks') +const assign = require('assign-deep') + +const parseMarkdownFile = require('./../parsers/markdown.js') + +/** + * Page + * + * + * @author Björn Hase + * @license http://opensource.org/licenses/MIT The MIT License + * @link https://gitea.node001.net/HerrHase/siteomat-webpack-plugin.git + * + */ + +class Page { + + /** + * + * + * @param {object} file + * @param {string} parent + * @param {string} fileString + * @param {object} [blocks=null] + * + */ + constructor(file, parent, fileString, blocks = {}) { + + // parse file + const result = parseMarkdownFile(fileString) + + // adding filename for html as pathname and relative path in structure + this._filename = this._resolveFilename(file) + this._pathname = this._resolvePathname(parent) + + // fields merge by default values + this._fields = merge({ + view: 'page', + meta: { + robots: 'index' + } + }, result.fields) + + this._content = result.content + this._blocks = blocks + } + + /** + * create Page Object + * + * + * @return {object} + * + */ + get() { + return assign({ + 'content' : this._content, + 'blocks' : this._blocks, + 'path' : this._pathname + '/' + this._filename, + 'filename' : this._filename, + 'pathname' : this._pathname + + }, this._fields) + } + + /** + * create html-filename = filename + * + * @param {string} file + * @return {string} + * + */ + _resolveFilename(file) { + + let filename = file.name + + if (filename === 'index.md') { + filename = 'index' + } else { + if (path.extname(filename) === '.md') { + filename = filename.replace('.md', '') + } + + filename = slugify(filename) + } + + return filename + '.html' + } + + /** + * pathname = parent + * + * @param {string} parent + * @return {string} + * + */ + _resolvePathname(parent) { + let pathname = parent + + if (parent === '/') { + pathname = '' + } + + return pathname + } +} + +module.exports = Page \ No newline at end of file diff --git a/src/factories/sitemap.js b/src/factories/sitemap.js index 1869e09..ef57117 100644 --- a/src/factories/sitemap.js +++ b/src/factories/sitemap.js @@ -60,6 +60,7 @@ class Sitemap { let result = true if (page.meta) { + page.meta = Object.entries(page.meta) page.meta.forEach((meta) => { if (meta['name'] === 'robots' && meta['content'].includes('noindex')) { result = false @@ -103,4 +104,4 @@ class Sitemap { } } -module.exports =Sitemap \ No newline at end of file +module.exports = Sitemap \ No newline at end of file diff --git a/src/queries/pages.js b/src/queries/pages.js index 4fb41e6..37c1a22 100644 --- a/src/queries/pages.js +++ b/src/queries/pages.js @@ -3,7 +3,7 @@ const path = require('path') const orderBy = require('lodash.orderby') -const Page = require('./../models/page.js') +const PageFactory = require('./../factories/page.js') const Blocks = require('./../queries/blocks.js') /** @@ -140,8 +140,8 @@ class Pages { const blocks = this._getBlocks(dirPath + options.parent + '/' + file.name) // create page object and add to page - const page = new Page(file, options.parent, content, blocks) - this._results.push(page) + const page = new PageFactory(file, options.parent, content, blocks) + this._results.push(page.get()) }) } diff --git a/src/siteomat.js b/src/siteomat.js index b06ed2a..6059a3a 100644 --- a/src/siteomat.js +++ b/src/siteomat.js @@ -73,7 +73,7 @@ class Siteomat { // run through pages and generate html files results.forEach((page, index) => { - page.render(this._engine, (error, content) => { + this._engine.render(page, (error, content) => { // show errors if (error) { @@ -97,6 +97,7 @@ class Siteomat { if ((index + 1) === results.length) { fs.writeFileSync(this._destination + '/sitemap.xml', sitemap.getXmlAsString()) } + }) }) }