Skip to content

Commit 2ce6758

Browse files
authored
feat: search articles only and sort by publication date (#217)
1 parent 275ca0f commit 2ce6758

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

blocks/search-results/search-results.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ const buildresultsGrid = (results, titleHeadingLevel = 'h2') => {
3131
contentWrap.classList.add('search-results__content');
3232
anchor.append(contentWrap);
3333

34-
// Badge with type of result (page / article / etc).
35-
const badge = document.createElement('p');
36-
badge.classList.add('search-results__badge');
37-
badge.textContent = sectionName;
38-
contentWrap.append(badge);
34+
// Badge with publication date.
35+
if (result?.publicationDate) {
36+
// Convert Excel serial date (e.g. "45981") to Date object that can be displayed.
37+
const publicationDate = new Date(Date.UTC(0, 0, parseInt(result.publicationDate) - 1));
38+
const badge = document.createElement('p');
39+
badge.classList.add('search-results__badge');
40+
badge.textContent = publicationDate.toLocaleDateString('en-US', { timeZone: 'UTC', month: 'short', day: 'numeric', year: 'numeric' });
41+
contentWrap.append(badge);
42+
}
3943

4044
// Result title.
4145
const title = document.createElement(titleHeadingLevel);
@@ -117,7 +121,7 @@ export default function decorate(block) {
117121

118122
(async () => {
119123
// Query all data and search it.
120-
const allFetchedData = await dataStore.getData(dataStore.commonEndpoints.queryIndex);
124+
const allFetchedData = await dataStore.getData(dataStore.commonEndpoints.ideas);
121125
const results = filterData(searchTerms, allFetchedData?.data);
122126
const hasResults = results && results.length > 0;
123127

blocks/search/search.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
146139
export 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

Comments
 (0)