@@ -117,7 +117,7 @@ <h1 class="py-4">{{ .Title }}</h1>
117117 // Default the current page to 1
118118 let currentPage = 1 ;
119119
120- // Check if the page parameter exists
120+ // Check if the page parameter exists
121121 const page = params . get ( "page" ) ;
122122 // Calculate the range start based on the page parameter
123123 if ( page ) {
@@ -126,73 +126,63 @@ <h1 class="py-4">{{ .Title }}</h1>
126126 const rangeStart = ( currentPage - 1 ) * 10 ;
127127 const rangeEnd = rangeStart + 10 ;
128128
129- // Execute the search
130- const search = await pagefind . debouncedSearch ( query ) ;
131- // If no search results are found, exit
132- if ( search === null ) {
129+ // total number of results
130+ const resultsLength = fullResultsData . length ;
131+ // Slice the results based on the range
132+ const resultsData = fullResultsData . slice ( rangeStart , rangeEnd ) ;
133+
134+ // If the range does not have any results, display a message
135+ if ( resultsData . length === 0 ) {
136+ searchPageResults . innerHTML = `<div class="p-4">No results found</div>` ;
133137 return ;
138+ }
139+ // Add an index to the results, for heap tracking
140+ const results = resultsData . map ( ( item , index ) => ( {
141+ ...item ,
142+ index : rangeStart + index + 1 ,
143+ } ) ) ;
144+
145+ // If the query is not empty, display the search results container
146+ if ( query ) {
147+ searchPageResults . classList . remove ( "hidden" ) ;
134148 } else {
135- // total number of results
136- const resultsLength = search . results . length ;
137- // Get the data for the search results
138- // Slice the results based on the range start + 10
139- const fullResultsData = await Promise . all (
140- search . results . map ( ( r ) => r . data ( ) ) ,
141- ) ;
142- const resultsData = fullResultsData . slice ( rangeStart , rangeEnd ) ;
143- // If the range does not have any results, display a message
144- if ( resultsData . length === 0 ) {
145- searchPageResults . innerHTML = `<div class="p-4">No results found</div>` ;
146- return ;
147- }
148- // Add an index to the results, for heap tracking
149- const results = resultsData . map ( ( item , index ) => ( {
150- ...item ,
151- index : rangeStart + index + 1 ,
152- } ) ) ;
153-
154- // If the query is not empty, display the search results container
155- if ( query ) {
156- searchPageResults . classList . remove ( "hidden" ) ;
157- } else {
158- searchPageResults . classList . add ( "hidden" ) ;
159- }
149+ searchPageResults . classList . add ( "hidden" ) ;
150+ }
160151
161- // Generate the search results HTML
162- let resultsHTML = `<div class="text-gray-400 dark:text-gray-500 p-2">${ resultsLength } results</div>` ;
163-
164- // Map results to HTML
165- resultsHTML += results
166- . map ( ( item ) => {
167- return `<div class="p-4">
168- <div class="flex flex-col">
169- <span class="text-gray-400 dark:texty-gray-dark text-sm">${ item . meta . breadcrumbs } </span>
170- <a class="link" href="${ item . url } " data-query="${ query } " data-index="${ item . index } ">${ item . meta . title } </a>
171- <p class="text-black dark:text-white overflow-hidden">…${ item . excerpt } …</p>
172- </div>
173- </div>` ;
174- } )
175- . join ( "" ) ;
176- // If the results length is greater than 10, display links to show more results
177- if ( resultsLength > 10 ) {
178- resultsHTML += `<hr class="border-divider-light dark:border-divider-dark">` ;
179- resultsHTML += `<ul class="flex flex-wrap gap-1 pt-4 pb-8 justify-center text-sm">` ;
180- for ( let i = 1 ; i <= resultsLength / 10 ; i ++ ) {
181- if ( i == currentPage ) {
182- resultsHTML += `<li class="flex items-center justify-center">
183- <a href="/search/?q=${ query } &page=${ i } " class="pagination-link bg-gray-200 dark:bg-gray-800 dark:text-gray-200">${ i } </a>
184- </li>` ;
185- } else {
186- resultsHTML += `<li class="flex items-center justify-center">
187- <a href="/search/?q=${ query } &page=${ i } " class="pagination-link bg-gray-100 dark:bg-gray-900 dark:text-gray-400">${ i } </a>
188- </li>` ;
189- }
152+ // Generate the search results HTML
153+ let resultsHTML = `<div class="text-gray-400 dark:text-gray-500 p-2">${ resultsLength } results</div>` ;
154+
155+ // Map results to HTML
156+ resultsHTML += results
157+ . map ( ( item ) => {
158+ return `<div class="p-4">
159+ <div class="flex flex-col">
160+ <span class="text-gray-400 dark:texty-gray-dark text-sm">${ item . meta . breadcrumbs } </span>
161+ <a class="link" href="${ item . url } " data-query="${ query } " data-index="${ item . index } ">${ item . meta . title } </a>
162+ <p class="text-black dark:text-white overflow-hidden">…${ item . excerpt } …</p>
163+ </div>
164+ </div>` ;
165+ } )
166+ . join ( "" ) ;
167+ // If the results length is greater than 10, display links to show more results
168+ if ( resultsLength > 10 ) {
169+ resultsHTML += `<hr class="border-divider-light dark:border-divider-dark">` ;
170+ resultsHTML += `<ul class="flex flex-wrap gap-1 pt-4 pb-8 justify-center text-sm">` ;
171+ for ( let i = 1 ; i <= resultsLength / 10 ; i ++ ) {
172+ if ( i == currentPage ) {
173+ resultsHTML += `<li class="flex items-center justify-center">
174+ <a href="/search/?q=${ query } &page=${ i } " class="pagination-link bg-gray-200 dark:bg-gray-800 dark:text-gray-200">${ i } </a>
175+ </li>` ;
176+ } else {
177+ resultsHTML += `<li class="flex items-center justify-center">
178+ <a href="/search/?q=${ query } &page=${ i } " class="pagination-link bg-gray-100 dark:bg-gray-900 dark:text-gray-400">${ i } </a>
179+ </li>` ;
190180 }
191- resultsHTML += `</ul>` ;
192181 }
193-
194- searchPageResults . innerHTML = resultsHTML ;
182+ resultsHTML += `</ul>` ;
195183 }
184+
185+ searchPageResults . innerHTML = resultsHTML ;
196186 }
197187
198188 searchPageInput . addEventListener ( "input" , ( e ) => onPageSearch ( e ) ) ;
0 commit comments