forked from jeffThompson/CollisionDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_html.js
84 lines (81 loc) · 2.73 KB
/
generate_html.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
const marky = require("marky-markdown"),
fs = require("fs");
const DEST_FOLDER = "Website/"
const order = [
"index",
"table_of_contents",
"license",
"what_you_should_already_know",
"point-point",
"point-circle",
"circle-circle",
"section_1_challenges",
"point-rect",
"rect-rect",
"circle-rect",
"section_2_challenges",
"line-point",
"line-circle",
"line-line",
"line-rect",
"section_3_challenges",
"poly-point",
"poly-circle",
"poly-rect",
"poly-line",
"poly-poly",
"section_4_challenges",
"tri-point",
"where_are_the_other_triangle_examples",
"section_5_challenges",
"object_oriented_collision",
"thanks"
];
const template = fs.readFileSync("./page-template.html", "utf8");
fs.readdir("MarkdownFiles", function(err, files) {
for (let i = 0; i < files.length; i++) {
// underscore for drafts || only markdown files please
if (files[i].charAt(0) === "_" || !files[i].match(".md$")) {
console.log(" x " + files[i]);
continue;
}
var name = files[i].substr(0, files[i].length - 3);
// Code Examples source
var code = "- code missing -";
var codeExists = fs.existsSync("CodeExamples/" + name + ".js");
if (codeExists) {
code = fs.readFileSync("CodeExamples/" + name + ".js", "utf8").trim();
}
console.log(" + " + name, codeExists ? "(with code)" : "");
const text = fs.readFileSync("MarkdownFiles/" + files[i], "utf8");
const markdown = marky(
// remove canvas parenting from displayed code
text.replace("{{ code }}", code.replace(" canvas.parent('sketch');\n", "")),
{
enableHeadingLinkIcons: false,
prefixHeadingIds: false,
sanitize: false
}
);
// Prev - Next
const orderIndex = order.indexOf(name);
var prev = '<span id="prev"> </span>';
var next = '<span id="next"> </span>';
if (orderIndex > 0) {
prev = '<a href="' + order[orderIndex - 1] + '.html" id="prev">←</a>';
}
if (orderIndex < order.length - 1) {
next = '<a href="' + order[orderIndex + 1] + '.html" id="next">→</a>';
}
// Write through template
if (codeExists) {
code += "document.getElementById('sketch').removeAttribute('hidden');";
}
const html = template
.replace("{{ prev }}", prev)
.replace("{{ next }}", next)
.replace("{{ markdown }}", markdown)
.replace("{{ code }}", code);
fs.writeFileSync(DEST_FOLDER + name + ".html", html, "utf8");
}
});