-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
101 lines (76 loc) · 4.92 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var fs = require('fs');
let featuredActionFiles = ["index.html", "action.html"]
let skipDirectories = ["update-action", "img", ".github", ".git"]
let featuredAction = fs.readFileSync('./featured-action.html', 'utf8').trim();
let headerContent = fs.readFileSync("./header.html", 'utf8').trim();
let contentFooter = fs.readFileSync("./footer.html", 'utf8').trim();
let credit = JSON.parse(fs.readFileSync("./footer-credits.json", 'utf8'));
let metadataContent = fs.readFileSync("./metadata.html", 'utf8').trim();
featuredAction = featuredAction.replaceAll("\r", "");
headerContent = headerContent.replaceAll("\r", "");
contentFooter = contentFooter.replaceAll("\r", "");
metadataContent = metadataContent.replaceAll("\r", "");
function addIndent(query, content, replaceContent) {
let indentation = 0;
let startLine = content.match(new RegExp("([\t ]+)" + query));
indentation = startLine[1].length;
var separateLines = replaceContent.split(/\r?\n|\r|\n/g);
for (line in separateLines) {
if (line == 0) continue;
separateLines[line] = "\t".repeat(indentation) + separateLines[line];
}
return separateLines.join("\n")
}
function replaceContent(files, leadingPath, prefixLength) {
files.forEach((file, _) => {
if (file.isDirectory() && !skipDirectories.includes(file.name)) {
let files = fs.readdirSync(leadingPath + file.name, { withFileTypes: true });
replaceContent(files, leadingPath + file.name + "/", prefixLength + 1);
}
if (file.name.endsWith(".html")) {
let content = fs.readFileSync(leadingPath + file.name, { encoding: 'utf8' });
// header
let startHeader = content.indexOf('<!--BELOW THIS GETS COPIED TO ALL PAGES WHEN MENU CHANGES (CHANGE BODY CLASS TO PAGE NAME)-->');
let endHeader = content.indexOf('<!--ABOVE THIS GETS COPIED TO ALL PAGES MENU CHANGES-->');
let prefix = "../".repeat(prefixLength);
let headerPrefixed = headerContent.replaceAll("{prefix}", prefix);
headerPrefixed = addIndent("<!--BELOW THIS GETS COPIED TO ALL PAGES WHEN MENU CHANGES \\(CHANGE BODY CLASS TO PAGE NAME\\)-->", content, headerPrefixed)
if (startHeader != -1 && endHeader != -1) {
content = content.substring(0, startHeader) + headerPrefixed + content.substring(endHeader + "<!--ABOVE THIS GETS COPIED TO ALL PAGES MENU CHANGES-->".length);
}
// footer
let startFooter = content.indexOf('<footer class="site-foot">');
let endFooter = content.indexOf('</footer>');
let footerContentReplaced = contentFooter.replaceAll("{prefix}", prefix);
footerContentReplaced = footerContentReplaced.replaceAll("{credits}", (credit[file.name] == undefined) ? "" : credit[file.name]);
footerContentReplaced = addIndent("<footer class=\"site-foot\">", content, footerContentReplaced)
if (startFooter != -1 && endFooter != -1) {
content = content.substring(0, startFooter) + footerContentReplaced + content.substring(endFooter + "</footer>".length);
}
// metadata
let metadataContentReplaced = metadataContent.replaceAll("{prefix}", prefix);
let startMetadata = content.indexOf("<!--Meta tags-->");
let endMetadata = content.indexOf("<!-- Variable Tags -->");
metadataContentReplaced = addIndent("<!--Meta tags-->", content, metadataContentReplaced)
if (startMetadata != -1 && endMetadata != -1) {
content = content.substring(0, startMetadata) + metadataContentReplaced + content.substring(endMetadata + "<!-- Variable Tags -->".length)
}
// featured action
if (featuredActionFiles.includes(file.name)) {
const startText = "<!-- featured Action Start -->";
const endText = "<!-- featured Action End -->";
let startFeaturedAction = content.indexOf(startText, 0);
let endFeaturedAction = content.indexOf(endText, 0);
while (startFeaturedAction != -1 && endFeaturedAction != -1) {
featuredActionContentReplace = addIndent(startText, content, featuredAction)
content = content.substring(0, startFeaturedAction) + featuredActionContentReplace + content.substring(endFeaturedAction + endText.length)
startFeaturedAction = content.indexOf(startText, startFeaturedAction + featuredActionContentReplace.length);
endFeaturedAction = content.indexOf(endText, endFeaturedAction + featuredActionContentReplace.length);
}
}
fs.writeFile(leadingPath + file.name, content, () => { })
}
})
}
let files = fs.readdirSync("../", { withFileTypes: true });
replaceContent(files, "../", 0)