@@ -67,9 +67,10 @@ const isLargeScreenNav = (elementWithinNav) => {
6767/**
6868 * Create the markup for a single search result.
6969 * @param {object } result
70+ * @param {boolean } showDescription Show smaller text under heading with type of result.
7071 * @returns {HTMLLIElement }
7172 */
72- function renderResult ( result ) {
73+ function renderResult ( result , showDescription ) {
7374 const li = document . createElement ( 'li' ) ;
7475 li . className = 'search__results-item' ;
7576
@@ -80,12 +81,16 @@ function renderResult(result) {
8081 title . className = 'search__results-title util-title-s' ;
8182 title . textContent = result . title ;
8283
83- const description = document . createElement ( 'div' ) ;
84- description . className = 'search__results-description util-body-s' ;
85- const descriptionText = sectionNameFromPath ( result . path , result ?. author ) ;
86- if ( descriptionText ) description . textContent = descriptionText ;
84+ if ( showDescription ) {
85+ const description = document . createElement ( 'div' ) ;
86+ description . className = 'search__results-description util-body-s' ;
87+ const descriptionText = sectionNameFromPath ( result . path , result ?. author ) ;
88+ if ( descriptionText ) description . textContent = descriptionText ;
89+ a . append ( title , description ) ;
90+ } else {
91+ a . append ( title ) ;
92+ }
8793
88- a . append ( title , description ) ;
8994 li . appendChild ( a ) ;
9095 return li ;
9196}
@@ -108,7 +113,7 @@ async function renderResults(block, config, filteredData) {
108113 if ( filteredData ?. length ) {
109114 // Has results; append results to container.
110115 searchResults . classList . remove ( 'search__results--no-results' ) ;
111- filteredData . forEach ( ( result ) => searchResults . append ( renderResult ( result ) ) ) ;
116+ filteredData . forEach ( ( result ) => searchResults . append ( renderResult ( result , false ) ) ) ;
112117 } else {
113118 // No results; display message.
114119 const noResultsMessage = document . createElement ( 'li' ) ;
@@ -118,18 +123,6 @@ async function renderResults(block, config, filteredData) {
118123 }
119124}
120125
121- /**
122- * Compare function for used by Array.sort on search results from `filterData`.
123- * Sorts by the `minIdx` property, smallest to largest, i.e. when searched text
124- * is found closer to the start of the searched text, it appears in results first.
125- * @param {object } hit1
126- * @param {object } hit2
127- * @returns
128- */
129- function compareFound ( hit1 , hit2 ) {
130- return hit1 . minIdx - hit2 . minIdx ;
131- }
132-
133126/**
134127 * Array of unique search terms from the search string (that were separated by a space). Used by `filterData`.
135128 * @param {string } searchValue
@@ -141,7 +134,7 @@ export const getSearchTermsArray = (searchValue) => searchValue.toLowerCase().sp
141134 * Searches data for search terms and returns data with matches.
142135 * @param {string[] } searchTerms
143136 * @param {object } data
144- * @returns {object[] }
137+ * @returns {object[] } Objects containing `result` object and `minIdx`.
145138 */
146139export function filterData ( searchTerms , data ) {
147140 const foundInMeta = [ ] ;
@@ -169,7 +162,10 @@ export function filterData(searchTerms, data) {
169162 }
170163 } ) ;
171164
172- return foundInMeta . sort ( compareFound ) . map ( ( item ) => item . result ) ;
165+ // Sort by publication date.
166+ return foundInMeta
167+ . sort ( ( a , b ) => parseInt ( b . result . publicationDate ) - parseInt ( a . result . publicationDate ) )
168+ . map ( ( item ) => item . result ) ;
173169}
174170
175171/**
@@ -385,7 +381,7 @@ export default async function decorate(block) {
385381 const placeholders = { } ;
386382
387383 // Endpoint for search data.
388- const source = dataStore . commonEndpoints . queryIndex ;
384+ const source = dataStore . commonEndpoints . ideas ;
389385
390386 // Build block markup.
391387 block . innerHTML = '' ;
0 commit comments