-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (53 loc) · 1.98 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
71
72
73
import * as quiz from "./quiz.js";
window.quiz = quiz;
export function parameter(parameter) {
return decodeURIComponent((new RegExp("[?|&]" + parameter + "=" + "([^&;]+?)(&|#|;|$)").exec(location.search) || [null, ""])[1].replace(/\+/g, "%20")) || null;
}
export function getResourceLink(page) {
return `index.html?page=${encodeURIComponent(page)}`;
}
export function setLink(element) {
var url = element.getAttribute("href");
if (url.startsWith("http://") || url.startsWith("https://")) {
return;
}
element.setAttribute("href", `index.html?page=${encodeURIComponent(url)}`);
}
export function parseHtml(html) {
var rootElement = document.createElement("div");
rootElement.innerHTML = html;
rootElement.querySelectorAll("a").forEach(setLink);
rootElement.querySelectorAll("quiz-set").forEach(function(setElement) {
var set = quiz.QuizSet.parse(setElement);
set.render();
setElement.replaceWith(set.element);
});
return rootElement;
}
export function renderResource(resource) {
return fetch(`resources/${resource}`).then(function(response) {
return response.text();
}).then(function(data) {
var converter = new showdown.Converter({
headingLevelStart: 2
});
document.querySelector("article").append(parseHtml(converter.makeHtml(data)));
});
}
export function toggleNav() {
document.querySelector("body").classList.toggle("showNav");
}
window.addEventListener("load", function() {
if (parameter("page") != null) {
renderResource(parameter("page"));
} else {
renderResource("introduction/welcome.md");
}
document.querySelectorAll("main nav a").forEach(function(element) {
if (element.getAttribute("href") == (parameter("page") || "introduction/welcome.md")) {
element.style.fontWeight = "bold";
}
setLink(element);
});
document.querySelector(".toggleNav").addEventListener("click", toggleNav);
});