-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
264 lines (249 loc) · 9.61 KB
/
functions.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const { promises: fs, existsSync: eS, createReadStream, createWriteStream, mkdirSync } = require("fs");
const config = require("./config.json");
const path = require("path");
const numberOfSpace = 4;
const pathToProject = __dirname;
const pathToSrc = path.join(pathToProject, config.srcDir);
const pathToBuild = path.join(pathToProject, config.buildDir);
const pathToTemplate = path.join(pathToProject, config.templateDir);
const keyword = require("./datas.json");
const CleanCSS = require("clean-css");
const showdown = require("showdown");
const showdownKatex = require("showdown-katex");
const showdownHighlight = require("showdown-highlight");
const { default: axios } = require("axios");
const converter = new showdown.Converter({
openLinksInNewWindow: true,
extensions: [
showdownKatex({
throwOnError: true,
displayMode: false,
}),
showdownHighlight(),
],
});
converter.setFlavor("github");
const renderMarkdown = (string) => {
const finalStr = converter.makeHtml(string);
return finalStr.endsWith("\n") ? finalStr.slice(0, -1) : finalStr;
};
const makeMenu = async (pathToCheck = pathToSrc) => {
const navigation = [];
if (eS(pathToCheck)) {
const list = await fs.readdir(pathToCheck);
for (const oneElement of list) {
const pathToElement = path.join(pathToCheck, oneElement);
const statOfElement = await fs.lstat(pathToElement);
if (statOfElement.isDirectory()) {
if (["data", ".git"].includes(oneElement)) {
continue;
}
const res = await makeMenu(pathToElement);
navigation.push({
name: correctName(oneElement),
htmlName: correctHTMLName(oneElement),
isDir: true,
files: res,
});
} else {
if (
pathToElement.endsWith(".git") ||
pathToElement.endsWith("LICENSE") ||
pathToElement.endsWith("README.md")
) {
continue;
}
navigation.push({
name: correctName(oneElement),
htmlName: correctHTMLName(oneElement),
isDir: false,
files: pathToElement,
});
}
}
}
return navigation.sort((a, b) => {
if (a.htmlName === "index.html") {
return -1;
} else if (b.htmlName === "index.html") {
return 1;
}
if (a.isDir && b.isDir) {
return 0;
} else if (a.isDir && !b.isDir) {
return 1;
} else if (!a.isDir && b.isDir) {
return -1;
} else if (!a.isDir && !b.isDir) {
return 0;
}
});
};
const correctName = (badName) => {
if (keyword[badName]) {
badName = keyword[badName];
} else {
badName = badName.charAt(0).toUpperCase() + badName.slice(1);
const lastIndex = badName.lastIndexOf(".");
badName = lastIndex === -1 ? badName : badName.substring(0, lastIndex);
}
return badName;
};
const correctHTMLName = (badName) => {
badName = badName.toLowerCase();
const lastIndex = badName.lastIndexOf(".");
badName = badName.substring(0, lastIndex);
badName = `${badName}.html`;
return badName;
};
let number = 0;
const makeHTMLMenu = (menu, actualPath, offset = 1) => {
if (offset == 1) {
console.log(`making ${actualPath}`);
}
const addToHtml = (str, time = offset, lineReturn = true) => {
return `${" ".repeat(numberOfSpace * time)}${str}${lineReturn ? "\n" : ""}`;
};
let html = addToHtml(`<ul>`);
for (const oneEntry of menu) {
if (oneEntry.isDir === false) {
html += addToHtml(
`<li${
oneEntry.files === actualPath ? ` class="coloredMenu"` : ""
}><a href="./${oneEntry.htmlName.replace(".html", "")}">${oneEntry.name}</a><span></span></li>`,
offset + 1
);
} else {
const isCorrect = oneEntry.files.findIndex((el) => {
return el.files === actualPath;
});
const menuList = makeHTMLMenu(oneEntry.files, actualPath, offset + 1, isCorrect + 1);
html += addToHtml(
`<div><input type="checkbox" class="hidden toggle" ${
isCorrect === -1 ? "" : ` checked`
} id="menu-control-${number}"><div><label for="menu-control-${number++}"><span>${
oneEntry.name
}</span><span></span></label></div>${menuList}</div>`,
offset + 1
);
}
}
html += addToHtml("</ul>", offset, false);
return html;
};
const build = async (menu, completeMenu = menu, variable = {}) => {
const { dev, files } = variable;
const prod = !dev;
const cssLinks = (await fs.readFile(path.join(pathToTemplate, "head.html"))).toString();
const template = (await fs.readFile(path.join(pathToTemplate, "template.html"))).toString();
// let indexJS = (await fs.readFile(path.join(pathToTemplate, "index.js"))).toString();
// if (prod) {
// indexJS = UglifyJS.minify(indexJS).code;
// }
for (const oneEntry of menu) {
if (oneEntry.isDir === false) {
number = 0;
const htmlMenu = `<nav>\n${makeHTMLMenu(completeMenu, oneEntry.files).slice(4)}\n</nav>`.replace(
"<ul>",
`<ul class="open-nav">`
);
let headData = `${cssLinks}<link rel="canonical" href="https://golb.n4n5.dev/${oneEntry.htmlName.replace(
".html",
""
)}" />\n<title>golb | ${oneEntry.name}</title>`
.split("\n")
.join("\n ");
let data = (await fs.readFile(oneEntry.files)).toString();
let finalFile = "";
if (oneEntry.files.endsWith(".md")) {
const markdown = renderMarkdown(data);
finalFile = template.replace("<!--FILE-->", markdown);
} else if (oneEntry.files.endsWith(".html")) {
finalFile = template.replace("<!--FILE-->", data);
}
finalFile = finalFile.replace("<!--HEAD-->", headData);
finalFile = finalFile.replace("<!--MENU-->", htmlMenu);
//finalFile = finalFile.replace("<!--STYLE-->", `<style> ${styles4} ${styles} ${styles3}</style>`)
//finalFile = finalFile.replace("<!--SPECIAL_SCRIPT-->", `<script>${indexJS}</script>`)
await fs.writeFile(path.join(pathToBuild, oneEntry.htmlName), finalFile);
} else {
await build(oneEntry.files, completeMenu, { dev });
}
}
};
const compileCSS = async () => {
let css = "";
const CSScompiler = new CleanCSS();
if (config.cssFile) {
for (const oneFile of config.cssFile) {
const filename = oneFile.split("/").pop();
if (!eS("raws")) {
mkdirSync("raws");
}
const listOfRaws = await fs.readdir("raws");
let dataCSS = "";
if (listOfRaws.includes(filename)) {
dataCSS = await fs.readFile(path.join("raws", filename));
} else {
const req = await axios.get(oneFile);
await fs.writeFile(path.join("raws", filename), req.data);
dataCSS = req.data;
}
css += CSScompiler.minify(dataCSS).styles;
}
return;
}
const pathToCompiled = path.join(config.buildDir, "style.css");
for (const oneFile of config.styles) {
const cssFile = (await fs.readFile(path.join(pathToTemplate, oneFile))).toString();
css += CSScompiler.minify(cssFile).styles;
}
await fs.writeFile(pathToCompiled, css);
};
const moveFile = async () => {
const array = ["style.css"];
for (const oneElement of array) {
const oldPath = path.join(__dirname, config.templateDir, oneElement);
const goodPath = path.join(__dirname, config.buildDir, oneElement);
const writeStream = createWriteStream(goodPath);
createReadStream(oldPath).pipe(writeStream);
}
};
const copyDataFolder = async () => {
const list = await fs.readdir(`./${config.srcDir}/`);
for (const oneFolder of list) {
const pathFile = path.join(__dirname, config.srcDir, oneFolder);
const statOfElement = await fs.lstat(pathFile);
if (statOfElement.isDirectory()) {
const list2 = await fs.readdir(pathFile);
for (const oneElement of list2) {
const pathFile2 = path.join(pathFile, oneElement);
const statOfElement2 = await fs.lstat(pathFile2);
if (statOfElement2.isDirectory() && oneElement == "data") {
// we move
await copyDir(`./${config.srcDir}/${oneFolder}/data`, path.join(config.buildDir, "data"));
}
}
}
}
};
async function copyDir(src, dest) {
await fs.mkdir(dest, { recursive: true });
let entries = await fs.readdir(src, { withFileTypes: true });
for (let entry of entries) {
let srcPath = path.join(src, entry.name);
let destPath = path.join(dest, entry.name);
entry.isDirectory() ? await copyDir(srcPath, destPath) : await fs.copyFile(srcPath, destPath);
}
}
async function copyPublicFolder() {
await copyDir("./public", path.join(config.buildDir));
}
module.exports = {
makeMenu,
makeHTMLMenu,
build,
copyDataFolder,
compileCSS,
copyPublicFolder,
};