-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (89 loc) · 3.17 KB
/
script.js
File metadata and controls
102 lines (89 loc) · 3.17 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
document.addEventListener("DOMContentLoaded", function () {
var postContent = document.getElementById("post-content");
// Cari semua heading yang tersedia
var allHeadings = ["h2", "h3"];
var availableHeadings = allHeadings.filter((h) =>
postContent.querySelector(h)
);
var headingsSelector = availableHeadings.join(", ");
var headings = postContent.querySelectorAll(headingsSelector);
var toc = document.getElementById("table-of-contents");
var currentList = document.createElement("ul");
toc.appendChild(currentList);
var listStack = [currentList];
var lastLevel = 0; // Mulai dari 0 untuk menangani level pertama heading
headings.forEach(function (heading, index) {
var level = parseInt(heading.tagName.substring(1), 10);
var id =
heading.textContent
.replace(/\s+/g, "-")
.replace(/:/g, "")
.replace(/[^a-zA-Z0-9\-_]/g, "")
.toLowerCase() +
"-" +
index;
heading.id = id;
// Penanganan untuk nested list berdasarkan level heading
while (level > lastLevel) {
var newList = document.createElement("ul");
if (!listStack[lastLevel].lastElementChild) {
var tempLi = document.createElement("li");
listStack[lastLevel].appendChild(tempLi);
}
listStack[lastLevel].lastElementChild.appendChild(newList);
listStack.push(newList);
lastLevel++;
}
while (level < lastLevel) {
listStack.pop();
lastLevel--;
}
var li = document.createElement("li");
var a = document.createElement("a");
a.setAttribute("href", "#" + id);
a.textContent = heading.textContent;
li.appendChild(a);
listStack[listStack.length - 1].appendChild(li);
});
// Add click event listeners to ToC links
document.querySelectorAll("#table-of-contents a").forEach(function (link) {
link.addEventListener("click", function (e) {
e.preventDefault(); // Prevent default anchor behavior
var href = this.getAttribute("href");
var targetElement = document.querySelector(href);
if (targetElement) {
// Scroll to the target element with 50px offset from the top
window.scrollTo({
top: targetElement.offsetTop - 100,
// behavior: 'smooth'
});
}
});
});
// Highlight the active item in the ToC
function highlightActiveTocItem() {
var scrollPosition = window.scrollY || document.documentElement.scrollTop;
var offset = 100; // Offset from the top
var selectedHeadingId = null;
headings.forEach(function (heading, index) {
var headingTop = heading.offsetTop;
// Check if the heading is at or above the scroll position with the offset
if (scrollPosition >= headingTop - offset) {
selectedHeadingId = heading.id;
}
});
document
.querySelectorAll("#table-of-contents a")
.forEach(function (tocLink) {
if (
selectedHeadingId &&
tocLink.getAttribute("href") === "#" + selectedHeadingId
) {
tocLink.classList.add("active-toc-item");
} else {
tocLink.classList.remove("active-toc-item");
}
});
}
window.addEventListener("scroll", highlightActiveTocItem);
});