Skip to content

Commit 36bd45c

Browse files
more fixes
1 parent 6ee9c17 commit 36bd45c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

layouts/_default/search.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ <h1 class="py-4">{{ .Title }}</h1>
8585
async function onPageSearch(e) {
8686
pagefind.init();
8787
const query = e.target.value;
88+
const terms = query.trim().split(/\s+/);
89+
90+
// Search each term individually
91+
const allResults = [];
92+
93+
for (let term of terms) {
94+
const result = await pagefind.search(term);
95+
if (result) {
96+
allResults.push(...result.results);
97+
}
98+
}
99+
100+
// Deduplicate results by URL (some results match multiple terms)
101+
const resultMap = new Map();
102+
for (const r of allResults) {
103+
if (!resultMap.has(r.url)) {
104+
resultMap.set(r.url, r);
105+
}
106+
}
107+
108+
const dedupedResults = [...resultMap.values()];
109+
const fullResultsData = await Promise.all(
110+
dedupedResults.map((r) => r.data()),
111+
);
88112

89113
// Set the query parameter in the URL
90114
const params = new URLSearchParams(document.location.search);
@@ -113,7 +137,7 @@ <h1 class="py-4">{{ .Title }}</h1>
113137
// Get the data for the search results
114138
// Slice the results based on the range start + 10
115139
const fullResultsData = await Promise.all(
116-
search.results.map((r) => r.data())
140+
search.results.map((r) => r.data()),
117141
);
118142
const resultsData = fullResultsData.slice(rangeStart, rangeEnd);
119143
// If the range does not have any results, display a message

0 commit comments

Comments
 (0)