-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (47 loc) · 1.17 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
/*
* @Author: lushijie
* @Date: 2017-03-10 09:38:38
* @Last Modified by: lushijie
* @Last Modified time: 2017-07-19 13:10:49
*/
const helper = require('think-helper');
const pug = require('pug');
const renderFile = helper.promisify(pug.renderFile, pug);
/**
* pug default render options
* more options see at https://pugjs.org/api/reference.html
*/
const defaultOptions = {
cache: false,
debug: false
};
/**
* pug view adapter
*/
class Pug {
/**
* constructor
* @param {String} viewFile view file
* @param {Object} viewData view data for render file
* @param {Object} config for pug
*/
constructor(viewFile, viewData, config) {
this.viewFile = viewFile;
this.viewData = viewData;
this.config = config;
this.handleOptions = helper.extend({}, defaultOptions, config.options);
}
/**
* render view file
*/
render() {
const viewPath = this.config.viewPath;
this.config.basedir = viewPath;
if (this.config.beforeRender) {
this.config.beforeRender(pug, this.handleOptions);
}
const config = Object.assign(this.handleOptions, this.viewData);
return renderFile(this.viewFile, config);
}
}
module.exports = Pug;