aboutsummaryrefslogtreecommitdiff
path: root/static/js/search.js
blob: 62bcbbb6000cb487470b3daca2a76f58e5c31897 (plain) (blame)
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
fetch('/index.json')
  .then(response => response.json())
  .then(data => {
    const fuse = new Fuse(data, {
      keys: ['title', 'contents', 'tags'],
      includeScore: true
    });
    document.getElementById('search-input').addEventListener('input', function (e) {
      const results = fuse.search(e.target.value);
      displayResults(results);
    });
  });

function displayResults(results) {
  const searchResults = document.getElementById('search-results');
  searchResults.innerHTML = '';
  if (results.length > 0) {
    searchResults.classList.remove("hidden");
  } else {
    searchResults.classList.add("hidden");
  }
  results.forEach(result => {
    const elem = document.createElement('div');
    elem.innerHTML = `<a class="list-group-item list-group-item-action" href="${result.item.permalink}">${result.item.title}</a></a`;
    searchResults.appendChild(elem);
  });
}