-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
52 lines (43 loc) · 1.72 KB
/
search.js
File metadata and controls
52 lines (43 loc) · 1.72 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
let docsData = [];
// Fetch docs.json on page load
fetch("/docs/docs.json")
.then(res => res.json())
.then(data => docsData = data)
.catch(err => console.error("Failed to load docs.json:", err));
const input = document.getElementById("docsSearchInput");
const resultsBox = document.getElementById("docsSearchResults");
input.addEventListener("input", () => {
const q = input.value.trim().toLowerCase();
if (q.length === 0) {
resultsBox.classList.add("hidden");
resultsBox.innerHTML = "";
return;
}
const matches = docsData.filter(item =>
item.title.toLowerCase().includes(q) ||
(item.description && item.description.toLowerCase().includes(q))
);
if (matches.length === 0) {
resultsBox.innerHTML = `
<div class="p-4 text-sm text-gray-500">No results found.</div>
`;
resultsBox.classList.remove("hidden");
return;
}
resultsBox.innerHTML = matches
.map(item => `
<a href="${item.link}"
class="block p-4 border-b last:border-b-0 border-gray-100 hover:bg-gray-50 transition rounded-lg">
<div class="font-semibold text-gray-900">${item.title}</div>
<div class="text-sm text-gray-600">${item.description || ""}</div>
</a>
`)
.join("");
resultsBox.classList.remove("hidden");
});
// Click-away close
document.addEventListener("click", (e) => {
if (!resultsBox.contains(e.target) && e.target !== input) {
resultsBox.classList.add("hidden");
}
});