summaryrefslogtreecommitdiff
path: root/static/js/search.js
blob: 703738683ec88809a54cd5a26de6dae437fef51e (plain)
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
53
54
55
56
57
58
/*
 * search.js -- Alternative search results processor for pagefind.
 * Copyright © 2022 - 2026 Jakob L. Kreuze <zerodaysfordays@sdf.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

window.addEventListener("load", async () => {
  const pagefind = await import("/_pagefind/pagefind.js");
  pagefind.init();
  
  const params = new URLSearchParams(window.location.search);
  const query = params.get('q');
  const outputContainer = document.getElementById('search-output');

  if (query) {
    const search = await pagefind.search(query);
    
    if (search.results.length === 0) {
      outputContainer.innerHTML = `<p>No results found for "<strong>${query}</strong>".</p>`;
      return;
    }

    outputContainer.innerHTML = `<p style="font-size: 9px; color: #999; margin-bottom: 20px;">
                    Found ${search.results.length} result(s) for "<strong>${query}</strong>":
                </p>`;

    for (let result of search.results) {
      const data = await result.data();
      
      const resultElement = document.createElement('article');
      resultElement.className = 'update-entry';
      console.log(data);
      resultElement.innerHTML = `
                        <header>
                            <h3 class="title"><a href="${data.url}">${data.meta.title}</a></h3>
                        </header>
                        <p>${data.excerpt} ...</p>
                        <p><a href="${data.url}">view page →</a></p>
                    `;
      outputContainer.appendChild(resultElement);
    }
  } else {
    outputContainer.innerHTML = `<p>Searching for '${query}'...</p>`;
  }
});