diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2026-02-15 08:36:30 -0500 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2026-02-15 08:36:30 -0500 |
| commit | 0e290794a649424c9853d1958be8674ac91353c0 (patch) | |
| tree | 5a85f03b5b3c3c11c0a98a3329a8983a7051078c /static | |
| parent | 0605db909c9edde1dcbdb6157f18f29ec1112e54 (diff) | |
Integrate pagefind
Diffstat (limited to 'static')
| -rw-r--r-- | static/js/search.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/static/js/search.js b/static/js/search.js new file mode 100644 index 0000000..7037386 --- /dev/null +++ b/static/js/search.js @@ -0,0 +1,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>`; + } +}); |