Skip to content

Commit eaab65d

Browse files
committed
improve search
1 parent 5b282b0 commit eaab65d

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

_layouts/api.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{%- assign ref_api_language = ref.info.api_language | default: ref.info.language -%}
1717

1818
{%- assign api_index_exclude = false -%}
19-
{%- if page.url contains "/ref/alpha/" or page.url contains "/ref/beta/" -%}
19+
{%- if page.url contains "/ref/stable/" or page.url contains "/ref/alpha/" or page.url contains "/ref/beta/" -%}
2020
{%- assign api_index_exclude = true -%}
2121
{%- endif -%}
2222
{%- if ref_api_language == "" or ref_api_language == nil -%}

search.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,45 @@ <h3>Results</h3>
2121

2222
<script>
2323
window.addEventListener('DOMContentLoaded', (event) => {
24+
const normalizeSearchTerm = (term) => {
25+
return (term || "")
26+
.toLowerCase()
27+
.trim()
28+
.replace(/[\.`~!@#\$%\^&\*\(\)\{\}\[\]\\\|:;'",<>\/\?\-]/g, "")
29+
.replace(/\s{2,}/g, " ")
30+
.trim();
31+
};
32+
33+
let currentQuery = "";
34+
35+
const boostExactSubResultMatch = (result) => {
36+
const normalizedQuery = normalizeSearchTerm(currentQuery);
37+
if (!normalizedQuery || !Array.isArray(result.sub_results)) {
38+
return result;
39+
}
40+
41+
for (const subResult of result.sub_results) {
42+
const normalizedTitle = normalizeSearchTerm(subResult.title || "");
43+
if (normalizedTitle !== normalizedQuery) {
44+
continue;
45+
}
46+
47+
const locations = Array.isArray(subResult.locations) ? subResult.locations : [];
48+
const lastLocation = locations.length ? locations[locations.length - 1] : 0;
49+
subResult.locations = locations.concat(Array(8).fill(lastLocation));
50+
break;
51+
}
52+
53+
return result;
54+
};
55+
2456
new PagefindUI({
2557
element: "#pagefind-search",
2658
pageSize: 10,
2759
showSubResults: true,
2860
showImages: false,
2961
excerptLength: 25,
62+
processResult: boostExactSubResultMatch,
3063
resetStyles: false,
3164
showEmptyFilters: true,
3265
openFilters: ['section']
@@ -36,6 +69,7 @@ <h3>Results</h3>
3669
const urlParams = new URLSearchParams(window.location.search);
3770
const query = urlParams.get('q');
3871
if (query) {
72+
currentQuery = query;
3973
// Wait for Pagefind UI to initialize then set query
4074
setTimeout(() => {
4175
const searchInput = document.querySelector('#pagefind-search input[type="text"]');
@@ -47,5 +81,11 @@ <h3>Results</h3>
4781
}
4882
}, 100);
4983
}
84+
85+
document.addEventListener('input', (event) => {
86+
if (event.target && event.target.matches('#pagefind-search input[type="text"]')) {
87+
currentQuery = event.target.value;
88+
}
89+
});
5090
});
5191
</script>

0 commit comments

Comments
 (0)