Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeffleyfamous committed May 9, 2024
1 parent f454b33 commit 5f2ba70
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions best-cigars-guide/blocks/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ function highlightTextElements(terms, elements) {
if (!element || !element.textContent) return;

const matches = [];
const { textContent } = element;
const {textContent} = element;
terms.forEach((term) => {
let start = 0;
let offset = textContent.toLowerCase().indexOf(term.toLowerCase(), start);
while (offset >= 0) {
matches.push({ offset, term: textContent.substring(offset, offset + term.length) });
matches.push({offset, term: textContent.substring(offset, offset + term.length)});
start = offset + term.length;
offset = textContent.toLowerCase().indexOf(term.toLowerCase(), start);
}
Expand All @@ -44,7 +44,7 @@ function highlightTextElements(terms, elements) {

matches.sort((a, b) => a.offset - b.offset);
let currentIndex = 0;
const fragment = matches.reduce((acc, { offset, term }) => {
const fragment = matches.reduce((acc, {offset, term}) => {
if (offset < currentIndex) return acc;
const textBefore = textContent.substring(currentIndex, offset);
if (textBefore) {
Expand Down Expand Up @@ -90,7 +90,7 @@ function renderResult(result, searchTerms, titleTag) {
if (result.image) {
const wrapper = document.createElement('div');
wrapper.className = 'search-result-image';
const pic = createOptimizedPicture(result.image, '', false, [{ width: '375' }]);
const pic = createOptimizedPicture(result.image, '', false, [{width: '375'}]);
wrapper.append(pic);
a.append(wrapper);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ function filterData(searchTerms, data) {
});

if (minIdx >= 0) {
foundInHeader.push({ minIdx, result });
foundInHeader.push({minIdx, result});
return;
}

Expand All @@ -178,7 +178,7 @@ function filterData(searchTerms, data) {
});

if (minIdx >= 0) {
foundInMeta.push({ minIdx, result });
foundInMeta.push({minIdx, result});
}
});

Expand Down Expand Up @@ -228,7 +228,11 @@ function searchInput(block, config) {
handleSearch(e, block, config);
});

input.addEventListener('keyup', (e) => { if (e.code === 'Escape') { clearSearch(block); } });
input.addEventListener('keyup', (e) => {
if (e.code === 'Escape') {
clearSearch(block);
}
});

return input;
}
Expand All @@ -255,7 +259,7 @@ export default async function decorate(block) {
const source = block.querySelector('a[href]') ? block.querySelector('a[href]').href : '/query-index.json';
block.innerHTML = '';
block.append(
searchBox(block, { source, placeholders }),
searchBox(block, {source, placeholders}),
searchResultsContainer(block),
);

Expand Down

0 comments on commit 5f2ba70

Please sign in to comment.