-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
70 lines (55 loc) · 1.75 KB
/
script.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
class Section {
constructor(title, body) {
this.title = title;
this.body = body;
}
}
class Builder {
constructor(sections) {
this.sections = sections;
}
async build() {
const beforeHtmlRequest = await fetch("parts/before.html");
const beforeHtml = await beforeHtmlRequest.text();
const afterHtmlRequest = await fetch("parts/after.html");
const afterHtml = await afterHtmlRequest.text();
const sectionHtmlRequest = await fetch("parts/section.html");
const sectionHtml = await sectionHtmlRequest.text();
var result = beforeHtml;
this.sections.forEach(section => {
result += sectionHtml
.replace("%%TITLE%%", section.title)
.replace("%%BODY%%", section.body);
});
result += afterHtml;
return result;
}
}
class Parser {
constructor(text) {
this.text = text;
}
parse() {
const sections = [];
const lines = this.text.split("\n");
var currentTitle = null;
var currentBody = "";
lines.forEach(line => {
if (line === "---") {
if (currentTitle === null) throw new Error("Empty section");
sections.push(new Section(currentTitle, currentBody));
currentTitle = null;
currentBody = "";
} else if (currentTitle === null) {
currentTitle = line;
} else {
currentBody += line + "\n";
}
});
// Deal with any unfinished section
if (currentTitle !== null) {
sections.push(new Section(currentTitle, currentBody));
}
return sections;
}
}