-
Notifications
You must be signed in to change notification settings - Fork 58
/
viewEngine.js
54 lines (48 loc) · 1.35 KB
/
viewEngine.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
function getProp(prop) {
if (typeof prop === 'object') {
return Object.keys(prop).map(name => `${name}:${prop[name]}`).join(';');
}
else return prop.toString();
}
function toProps(props) {
return Object.keys(props)
.map(name => ` ${name === 'className' ? 'class' : name}="${getProp(props[name])}"`)
.join('');
}
function toHTML(vdom) {
if (!vdom) return '';
if (Array.isArray(vdom)) return toHTMLArray(vdom)
if (vdom.tag) {
const props = vdom.props ? toProps(vdom.props) : '';
const children = vdom.children ? toHTMLArray(vdom.children) : '';
return `<${vdom.tag}${props}>${children}</${vdom.tag}>`;
}
if (typeof vdom === 'object') return JSON.stringify(vdom);
else {
const html = vdom.toString();
return html.startsWith('_html:') ? html.substring(6) : html;
}
}
function toHTMLArray(nodes) {
return nodes.map(node => toHTML(node)).join('');
}
function clean(obj) {
for (var i in obj) {
if (obj[i] == null) {
delete obj[i];
} else if (typeof obj[i] === 'object') {
clean(obj[i]);
}
}
}
function engine(name, options, callback) {
const fn = require(name).default;
const rendered = fn(options);
clean(rendered);
return options.ssr ?
callback(null, toHTML(rendered)) :
callback(null, rendered);
}
module.exports = function (mode) {
return mode === 'html' ? toHTML : engine
}