|
7 | 7 | const fs = require('fs')
|
8 | 8 | const posthtml = require('posthtml')
|
9 | 9 |
|
10 |
| -exports = module.exports = function (path, options, cb) { |
| 10 | +/** |
| 11 | + * @author Michael Ciniawsky (@michael-ciniawsky) <michael.ciniawsky@gmail.com> |
| 12 | + * @description PostHTML View Engine for Express |
| 13 | + * @license MIT |
| 14 | + * |
| 15 | + * @module express-posthtml |
| 16 | + * @version 1.1.0 |
| 17 | + * |
| 18 | + * @requires posthtml |
| 19 | + * |
| 20 | + * @method posthtml |
| 21 | + * |
| 22 | + * @param {String} path View Path |
| 23 | + * @param {Object} options View Options |
| 24 | + * @param {Function} cb Callback |
| 25 | + * |
| 26 | + * @return {Function} cb HTML |
| 27 | + */ |
| 28 | +module.exports = function (path, options, cb) { |
11 | 29 | options.extend = options.extend || false
|
12 | 30 |
|
13 | 31 | let plugins
|
14 | 32 |
|
15 |
| - if (!options.plugins && options.extend === false) { |
16 |
| - plugins = options.settings['view options'] || [] |
| 33 | + if (!options.plugins && !options.extend) { |
| 34 | + plugins = options.settings['view options'].plugins || [] |
17 | 35 | } else if (options.extend === true) {
|
18 |
| - plugins = options.settings['view options'].concat(options.plugins) |
| 36 | + plugins = options.settings['view options'].plugins |
| 37 | + plugins = plugins.concat(options.plugins) |
19 | 38 | } else {
|
20 | 39 | plugins = options.plugins || []
|
21 | 40 | }
|
22 | 41 |
|
23 |
| - fs.readFile(path, function (err, content) { |
24 |
| - if (err) return cb(new Error(err)) |
| 42 | + options = options.settings['view options'].options || {} |
25 | 43 |
|
26 |
| - posthtml(plugins) |
27 |
| - .process(content.toString()) |
| 44 | + fs.readFile(path, 'utf8', (err, html) => { |
| 45 | + if (err) return cb(err) |
| 46 | + |
| 47 | + return posthtml(plugins) |
| 48 | + .process(html, options) |
28 | 49 | .then(result => cb(null, result.html))
|
| 50 | + .catch((err) => cb(err)) |
29 | 51 | })
|
30 | 52 | }
|
0 commit comments