-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.js
More file actions
88 lines (70 loc) · 2.39 KB
/
frontend.js
File metadata and controls
88 lines (70 loc) · 2.39 KB
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
import frontmatter from "./lib/frontmatter.js";
const purify = DOMPurify(window);
const heading_name_renderer = {
heading(text, level) {
return `
<h${level}>${text}</h${level}>
`;
},
// blockquote(quote) {
// const quoteRegex = /^(>\s*?)$/;
// const matches = quoteRegex.exec(quote);
// console.log("Quote", quote, matches);
// return `
// <blockquote>${quote}</blockquote>
// `;
// },
};
marked.use({ renderer: heading_name_renderer });
function highlight(target, names) {
//https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker
const treeWalker = document.createTreeWalker(output, NodeFilter.SHOW_ELEMENT);
var node = treeWalker.nextNode();
let matching = false;
while (node) {
const isName = names.includes(node.innerText);
if (isName && node.tagName === "H3") {
if (node.innerText === target) {
matching = true;
} else {
matching = false;
}
}
// We don't want to apply the toggle to blockquotes
if (["P", "H3"].includes(node.tagName)) {
node.classList.toggle("active", matching);
node.classList.toggle("inactive", !matching);
}
node = treeWalker.nextSibling();
}
}
fetch("./example.md")
.then((res) => res.text())
.then((text) => {
const [front, body] = frontmatter(text);
meta.innerText = JSON.stringify(front, null, 2);
document.querySelector("#title").innerText = front.title || "A thing";
document.querySelector("#by").innerText = front.by || "-";
// console.log(front);
const names = Object.keys(front.characters).map((n) => n.toUpperCase());
const characterList = document.querySelector("ul#characters");
for (const [name, about] of Object.entries(front.characters)) {
const a = document.createElement("a");
a.href = "#" + name;
a.innerText = name;
const li = document.createElement("li");
li.appendChild(a);
characterList.append(li);
}
const html = purify.sanitize(marked(body));
output.innerHTML = html;
console.error(
"Uncaught TypeError: Cannot read property 'hi ben' of undefined"
);
const target = document.location.hash.slice(1).toUpperCase();
highlight(target, names);
window.addEventListener("hashchange", () => {
const target = document.location.hash.slice(1).toUpperCase();
highlight(target, names);
});
});