blob: 3e618ff73d8645687571f810c749f8bbb649a1a2 (
plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
|
<style type="text/css" media="screen">
.post-menu ul {
list-style: none;
padding: 0;
margin: 0;
}
</style>
<div class="post-menu">
<div class="post-menu-title">TOC</div>
<div class="post-menu-content"></div>
</div>
<script>
function generateContent() {
var menu = document.querySelector(".post-menu");
var menuContent = menu.querySelector(".post-menu-content");
var headings = document.querySelector(".post-content").querySelectorAll("h2, h3, h4, h5, h6");
// Hide menu when no headings
if (headings.length === 0) {
return menu.style.display = "none";
}
// Generate post menu
var menuHTML = '';
for (var i = 0; i < headings.length; i++) {
var h = headings[i];
menuHTML += (
'<li class="h-' + h.tagName.toLowerCase() + '">'
+ '<a href="#h-' + h.getAttribute('id') + '">' + h.textContent + '</a></li>');
}
menuContent.innerHTML = '<ul>' + menuHTML + '</ul>';
// The header element
var header = document.querySelector('header.site-header');
function doMenuCollapse(index, over_items) {
var items = menuContent.firstChild.children;
if (over_items == undefined) {
over_items = 20;
}
if (items.length < over_items) {
return;
}
var activeItem = items[index];
var beginItem = activeItem
var endItem = activeItem
var beginIndex = index;
var endIndex = index + 1;
while (beginIndex >= 0
&& !items[beginIndex].classList.contains('h-h2')) {
beginIndex -= 1;
}
while (endIndex < items.length
&& !items[endIndex].classList.contains('h-h2')) {
endIndex += 1;
}
for (var i = 0; i < beginIndex; i++) {
item = items[i]
if (!item.classList.contains('h-h2')) {
item.style.display = 'none';
}
}
for (var i = beginIndex + 1; i < endIndex; i++) {
item = items[i]
// if (!item.classList.contains('h-h2')) {
item.style.display = '';
// }
}
for (var i = endIndex; i < items.length; i++) {
item = items[i]
if (!item.classList.contains('h-h2')) {
item.style.display = 'none';
}
}
}
// Init menu collapsed
doMenuCollapse(-1);
// Active the menu item
window.addEventListener('scroll', function (event) {
var lastActive = menuContent.querySelector('.active');
var changed = true;
var activeIndex = -1;
for (var i = headings.length - 1; i >= 0; i--) {
var h = headings[i];
var headingRect = h.getBoundingClientRect();
var headerRect = header.getBoundingClientRect();
var headerTop = Math.floor(headerRect.top);
var headerHeight = Math.floor(headerRect.height);
var headerHeight = headerTop + headerHeight + 20;
if (headingRect.top <= headerHeight) {
var id = 'h-' + h.getAttribute('id');
var a = menuContent.querySelector('a[href="#' + id + '"]');
var curActive = a.parentNode;
if (curActive) {
curActive.classList.add('active');
activeIndex = i;
}
if (lastActive == curActive) {
changed = false;
}
break;
}
}
if (changed) {
if (lastActive) {
lastActive.classList.remove('active');
}
doMenuCollapse(activeIndex);
}
event.preventDefault();
});
}
generateContent();
</script>
|