From 0e290794a649424c9853d1958be8674ac91353c0 Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Sun, 15 Feb 2026 08:36:30 -0500 Subject: Integrate pagefind --- static/js/search.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 static/js/search.js (limited to 'static') 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 + * + * 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 + * . + */ + +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 = `

No results found for "${query}".

`; + return; + } + + outputContainer.innerHTML = `

+ Found ${search.results.length} result(s) for "${query}": +

`; + + 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 = ` +
+

${data.meta.title}

+
+

${data.excerpt} ...

+

view page →

+ `; + outputContainer.appendChild(resultElement); + } + } else { + outputContainer.innerHTML = `

Searching for '${query}'...

`; + } +}); -- cgit v1.3