-
Notifications
You must be signed in to change notification settings - Fork 1
/
html-process.js
42 lines (36 loc) · 1.14 KB
/
html-process.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
var html = require('basisjs-tools-ast').html;
module.exports = function(options){
var registratorName = options.registratorName;
var injectScriptFilename = options.injectScriptFilename;
return function processHtml(content, filename, cb) {
var ast = html.parse(content);
var scripts = html.getElementsByName(ast, 'script');
var firstScript = scripts[0];
var injectScript = {
type: 'tag',
name: 'script',
attribs: {
src: injectScriptFilename
},
children: []
};
scripts.forEach(function(script) {
if (script.attribs) {
var attrName =
'data-basis-config' in script.attribs ? 'data-basis-config' :
'basis-config' in script.attribs ? 'basis-config' :
false;
if (attrName) {
var oldValue = script.attribs[attrName].trim();
script.attribs[attrName] = (oldValue ? oldValue + '\n,' : '') + 'devInfoResolver:' + registratorName;
}
}
});
if (firstScript) {
html.insertBefore(firstScript, injectScript);
} else {
html.injectToHead(ast, injectScript);
}
cb(null, html.translate(ast));
};
};