-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsidebar.js
More file actions
174 lines (158 loc) · 5.46 KB
/
Copy pathsidebar.js
File metadata and controls
174 lines (158 loc) · 5.46 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* global window, document, IntersectionObserver, history */
(function () {
// ─── Nav Hierarchy ──────────────────────────────────────────────
var sections = [
{
title: "Getting Started",
links: [
{ label: "Usage", href: "/usage" },
{ label: "Client Setup", href: "/clients" },
],
},
{
title: "Setup",
links: [
{ label: "Config Reference", href: "/config" },
{ label: "Deployment", href: "/deploy" },
{ label: "Search Guide", href: "/search" },
],
},
{
title: "Switching",
links: [
{ label: "From Mintlify", href: "/migrate-from-mintlify" },
{ label: "From GitBook", href: "/migrate-from-gitbook" },
{ label: "From mcp-ragdocs", href: "/migrate-from-ragdocs" },
{ label: "From mcp-local-rag", href: "/migrate-from-local-rag" },
],
},
];
// ─── Detect current page ────────────────────────────────────────
var p = window.location.pathname
.replace(/\/index\.html$/, "")
.replace(/\/$/, "");
var currentPage = p || "/";
// ─── Build Sidebar HTML ─────────────────────────────────────────
function buildSidebar() {
var html = "";
for (var i = 0; i < sections.length; i++) {
var section = sections[i];
html += '<div class="sidebar-section">';
html += "<h3>" + section.title + "</h3>";
for (var j = 0; j < section.links.length; j++) {
var link = section.links[j];
var activeClass = link.href === currentPage ? ' class="active"' : "";
html +=
'<a href="' +
link.href +
'"' +
activeClass +
">" +
link.label +
"</a>";
}
html += "</div>";
}
return html;
}
// ─── Inject into DOM ────────────────────────────────────────────
var sidebarEl = document.getElementById("sidebar");
if (sidebarEl) {
sidebarEl.innerHTML = buildSidebar();
var active = sidebarEl.querySelector(".active");
if (active) active.scrollIntoView({ block: "center" });
}
// ─── Page TOC (right sidebar) ──────────────────────────────────
function buildPageToc() {
var tocEl = document.getElementById("page-toc");
if (!tocEl) return;
var content = document.querySelector(".docs-content");
if (!content) return;
var headings = content.querySelectorAll("h2, h3");
if (headings.length < 4) return;
// Ensure each heading has an id for anchor links
for (var i = 0; i < headings.length; i++) {
var h = headings[i];
if (!h.id) {
h.id = h.textContent
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, "")
.replace(/\s+/g, "-")
.replace(/-+/g, "-")
.replace(/^-|-$/g, "");
}
}
// Build TOC HTML
var html = '<div class="page-toc-label">On this page</div>';
for (var j = 0; j < headings.length; j++) {
var heading = headings[j];
var cls = heading.tagName === "H3" ? ' class="toc-h3"' : "";
html +=
'<a href="#' +
heading.id +
'"' +
cls +
">" +
heading.textContent +
"</a>";
}
tocEl.innerHTML = html;
// Active state tracking with IntersectionObserver
var tocLinks = tocEl.querySelectorAll('a[href^="#"]');
var headingEls = [];
for (var k = 0; k < tocLinks.length; k++) {
var target = document.getElementById(
tocLinks[k].getAttribute("href").slice(1),
);
if (target) headingEls.push(target);
}
if (!headingEls.length || typeof IntersectionObserver === "undefined")
return;
function setActive(index) {
for (var m = 0; m < tocLinks.length; m++) {
tocLinks[m].classList.remove("active");
}
if (tocLinks[index]) {
tocLinks[index].classList.add("active");
}
}
var observer = new IntersectionObserver(
function (entries) {
// Find the topmost visible heading
var topmostIndex = -1;
for (var n = 0; n < entries.length; n++) {
if (entries[n].isIntersecting) {
var idx = headingEls.indexOf(entries[n].target);
if (idx !== -1 && (topmostIndex === -1 || idx < topmostIndex)) {
topmostIndex = idx;
}
}
}
if (topmostIndex !== -1) {
setActive(topmostIndex);
}
},
{ rootMargin: "-80px 0px -60% 0px", threshold: 0 },
);
for (var q = 0; q < headingEls.length; q++) {
observer.observe(headingEls[q]);
}
// Set initial active state
setActive(0);
// Smooth scroll on click
for (var r = 0; r < tocLinks.length; r++) {
tocLinks[r].addEventListener("click", function (e) {
e.preventDefault();
var targetEl = document.getElementById(
this.getAttribute("href").slice(1),
);
if (targetEl) {
targetEl.scrollIntoView({ behavior: "smooth", block: "start" });
// Update URL hash without jumping
history.pushState(null, "", this.getAttribute("href"));
}
});
}
}
buildPageToc();
})();