-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (55 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const through = require('through2');
const Liquid = require('liquidjs');
const PluginError = require('plugin-error');
const pluginName = 'gulp-liquidjs';
const deepCopy = (obj) => {
return JSON.parse(JSON.stringify(obj));
}
/**
*
* @param {
* liquidjs options
* tags custom tags
* filters custom tags
* data render data
* } opts
*/
module.exports = function (opts) {
const defaults = {
extname: '.html'
};
opts = Object.assign({}, defaults, opts);
const engine = Liquid(opts);
if (opts.tags && typeof opts.tags === 'object') {
Object.keys(opts.tags).forEach(function (t) {
engine.registerTag(t, opts.tags[t]);
});
}
if (opts.filters && typeof opts.filters === 'object') {
Object.keys(opts.filters).forEach(function (f) {
engine.registerFilter(f, opts.filters[f]);
});
}
function liquid(file, enc, callback) {
if (file.isNull()) {
return callback();
}
if (file.isStream()) {
this.emit('error', new PluginError(pluginName, 'Stream content is not supported'));
return callback();
}
if (file.isBuffer()) {
// use deep copy data
engine.parseAndRender(file.contents.toString(), deepCopy(opts.data || {}))
.then((output) => {
file.contents = Buffer.from(output);
this.push(file);
callback();
}).catch((ex) => {
this.emit('error', new PluginError(pluginName, ex.message));
});
}
}
return through.obj(liquid);
};