.
This commit is contained in:
parent
c4dcb6e38b
commit
531029a18f
129 changed files with 7765 additions and 6938 deletions
46
public/js/search.js
Normal file
46
public/js/search.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
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);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue