46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
document.addEventListener("DOMContentLoaded", function () {
|
|
let index = [];
|
|
|
|
// Load JSON index
|
|
fetch("/index.json")
|
|
.then((res) => res.json())
|
|
.then((data) => {
|
|
index = data;
|
|
console.log("Index loaded:", index.length, "posts");
|
|
})
|
|
.catch((err) => console.error("Error loading index.json", err));
|
|
|
|
const searchInput = document.getElementById("search");
|
|
const resultsList = document.getElementById("results");
|
|
|
|
function runSearch() {
|
|
const query = searchInput.value.trim().toLowerCase();
|
|
resultsList.innerHTML = "";
|
|
|
|
if (!query) return;
|
|
|
|
// filter out posts that don't match
|
|
const matches = index.filter(
|
|
(post) =>
|
|
post.title.toLowerCase().includes(query) ||
|
|
post.content.toLowerCase().includes(query),
|
|
);
|
|
|
|
if (matches.length === 0) {
|
|
resultsList.innerHTML = "<li>No results found.</li>";
|
|
return;
|
|
}
|
|
|
|
// Render matching posts
|
|
matches.forEach((post) => {
|
|
const li = document.createElement("li");
|
|
const link = document.createElement("a");
|
|
link.href = post.url;
|
|
link.textContent = post.title;
|
|
li.appendChild(link);
|
|
resultsList.appendChild(li);
|
|
});
|
|
}
|
|
|
|
searchInput.addEventListener("input", runSearch);
|
|
});
|