let index = [];

fetch("/search-index.json")
  .then(r => r.json())
  .then(data => index = data);

const box = document.getElementById("search-box");
const results = document.getElementById("search-results");


document.addEventListener("click", (e) => {
  const searchBox = document.getElementById("search-box");
  const results = document.getElementById("search-results");

});


box.addEventListener("input", () => {
  const q = box.value.toLowerCase();
  results.innerHTML = "";

  if (q.length < 2) return;

  index
    .filter(e =>
      e.title.toLowerCase().includes(q) 
    )
    //.slice(0, 10)
    .forEach(e => {
      const div = document.createElement("div");
      div.innerHTML = `<a href="${e.url}">${e.title}</a>`;
      results.appendChild(div);
    });
});
