-
Notifications
You must be signed in to change notification settings - Fork 0
/
unify.js
49 lines (41 loc) · 1.05 KB
/
unify.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
const fs = require("fs");
const path = "./dist/calendoit/index.xhtml";
const format = "utf-8";
// Tags to close
const tags = ["link", "meta", "base"];
/**
* Read and edit file
*/
function readWriteSync() {
const data = fs.readFileSync(path, format);
var edit = data;
edit = unifyTags(edit);
edit = unifyScripts(edit);
fs.writeFileSync(path, edit, format);
}
/**
* Close tags
* @param {*} str File string
* @returns Edited string
*/
function unifyTags(str) {
var pattern = "<(?!/)";
pattern += "(" + tags[0];
for (let i = 1; i < tags.length; i++) pattern += "|" + tags[i];
pattern += ")";
pattern += "([^>]*)>";
const reg = new RegExp(pattern, "g");
return str.replace(reg, "<$1 $2/>");
}
/**
* Change script types
* @param {*} str File string
* @returns Edited string
*/
function unifyScripts(str) {
var pattern = "<(script([^>]*))(type=\"([^\"]*)\")([^>]*)>";
const reg = new RegExp(pattern, "g");
return str.replace(reg, "<$1 type=\"application/javascript\" $5>");
}
readWriteSync();
console.log("Unify complete!");