-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.ts
110 lines (101 loc) · 3.22 KB
/
func.ts
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
const toggleTableOfContentsHidden = function () {
const textTOC = document?.getElementById("text-table-of-contents");
if (textTOC !== null) {
const displayStyle = window?.getComputedStyle(textTOC)?.display;
if (displayStyle) {
textTOC.style.display = displayStyle === "none" ? "inline" : "none";
}
}
};
const linkifyTableOfContents = function () {
const heading = document.querySelector("#table-of-contents h2");
if (heading != null) {
heading.innerHTML =
'<a href="javascript:toggleTableOfContentsHidden()">Table of Contents</a>';
}
};
const orgDateToHumanDate = function () {
let timestamps = document.querySelectorAll(".timestamp");
let options: any = {
weekday: "short",
year: "numeric",
month: "short",
day: "numeric",
};
for (let timestamp of Array.from(timestamps)) {
// timestamp.innerHTML is like [2024-01-01]
timestamp.innerHTML = new Date(
parseInt(timestamp.innerHTML.substring(1, 5)), // year
parseInt(timestamp.innerHTML.substring(6, 8)) - 1, // monthIndex (0-based)
parseInt(timestamp.innerHTML.substring(9, 11)), // day
).toLocaleDateString(undefined, options);
}
};
const linkifyTags = function () {
const tags = Array.from(document.querySelectorAll(".tag"));
for (const tagHeading of tags) {
for (let tag of Array.from(tagHeading.children)) {
let tagClassName = tag.className;
tag.addEventListener("click", () => filterByTag(tagClassName));
}
}
};
// Contains all the active tags on the page.
let activeTags: string[] = [];
// Filter by the provided tag, pass empty string to show all.
const filterByTag = function (filterTag: string) {
let articles = document.querySelectorAll(".outline-2");
if (filterTag == "") {
activeTags = [];
} else if (activeTags.includes(filterTag)) {
console.log("Ignoring repeated tag application");
return;
} else {
activeTags.push(filterTag);
}
for (let article of Array.from(articles)) {
if (filterTag == "") {
(<HTMLElement>article).hidden = false;
continue;
}
let tags = article.querySelector(".tag")?.children;
if (tags == undefined) {
(<HTMLElement>article).hidden = true;
continue;
}
var shouldKeep = false;
for (let tag of Array.from(tags)) {
if (tag.className == filterTag) {
shouldKeep = true;
break;
}
}
(<HTMLElement>article).hidden = !shouldKeep;
}
let textTOC = document.getElementById("table-of-contents");
if (!textTOC) {
console.log("Could not find table of contents element!");
return;
}
if (filterTag == "") {
textTOC.hidden = false;
return;
}
textTOC.hidden = true;
var tagFilter = document.createElement("span");
tagFilter.className = "tag";
tagFilter.innerHTML = "<span>" + filterTag + " X" + "</span>";
tagFilter.onclick = function (_) {
// TODO: Bug when clearing multiple tags, should instead filter on
// the remaining tags. Would need marking tags specially.
// Probably should switch filterTag to array too.
filterByTag("");
tagFilter.remove();
};
textTOC.parentNode?.insertBefore(tagFilter, textTOC);
};
const globalOnLoad = function () {
linkifyTableOfContents();
orgDateToHumanDate();
linkifyTags();
};