-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
33 lines (33 loc) · 1.07 KB
/
search.js
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
import * as hugoParams from "@params";
(async () => {
try {
const params = new URLSearchParams(window.location.search);
const query = params.get("q");
if (query) {
const db = await HugoLyra.fetchDb(
`/search/hugo-lyra-english.json?cache=${hugoParams.cur}`
);
const res = await HugoLyra.search(db, { term: query, properties: "*" });
document
.getElementById("search-input")
.setAttribute("value", res.options.term);
let resultList = "";
const searchResults = document.getElementById("results");
if (res?.search?.count) {
for (const hit of res.search.hits) {
const doc = hit.document;
resultList += "<li>";
resultList += '<span class="date">' + doc.meta.date + "</span>";
resultList +=
'<a class="title" href="' + doc.uri + '">' + doc.title + "</a>";
resultList += "</li>";
}
}
searchResults.innerHTML = resultList.length
? resultList
: "No results found";
}
} catch (e) {
console.error(e);
}
})();