Skip to content

Commit 4b6a4d9

Browse files
authored
3
1 parent ae5082b commit 4b6a4d9

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

docs/_layouts/on-this-page.html

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
{{ content }}
3737
</div>
3838
</div>
39-
<script>
39+
<!-- <script>
4040
document.addEventListener("DOMContentLoaded", function() {
4141
const toc = document.querySelector(".toc");
4242
const firstHeader = document.querySelector("h1");
@@ -69,6 +69,45 @@
6969
// Run on resize
7070
window.addEventListener("resize", moveTOC);
7171
});
72+
</script> -->
73+
<script>
74+
document.addEventListener("DOMContentLoaded", function() {
75+
const toc = document.querySelector(".toc");
76+
// Only grab the first h1 inside the main content div
77+
const firstHeader = document.querySelector(".content h1");
78+
const originalParent = toc?.parentNode;
79+
const originalNextSibling = toc?.nextSibling;
80+
81+
function moveTOC() {
82+
if (!toc || !firstHeader) return;
83+
84+
if (window.innerWidth <= 800) {
85+
// Only move if it's not already after the header
86+
if (firstHeader.nextElementSibling !== toc) {
87+
firstHeader.insertAdjacentElement("afterend", toc);
88+
}
89+
} else {
90+
// Put back in the original sidebar location
91+
if (originalParent && toc.parentNode !== originalParent) {
92+
if (originalNextSibling) {
93+
originalParent.insertBefore(toc, originalNextSibling);
94+
} else {
95+
originalParent.appendChild(toc);
96+
}
97+
}
98+
}
99+
}
100+
101+
// Run once on load
102+
moveTOC();
103+
104+
// Throttle resize so it doesn't jump around too much
105+
let resizeTimer;
106+
window.addEventListener("resize", function() {
107+
clearTimeout(resizeTimer);
108+
resizeTimer = setTimeout(moveTOC, 150);
109+
});
110+
});
72111
</script>
73112

74113
<style>

0 commit comments

Comments
 (0)