@@ -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