summaryrefslogtreecommitdiff
path: root/static/js
diff options
context:
space:
mode:
Diffstat (limited to 'static/js')
-rw-r--r--static/js/search.js58
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>`;
+ }
+});