diff --git a/src/assets/js/search.js b/src/assets/js/search.js index d6bdcc111..c0f6364fd 100644 --- a/src/assets/js/search.js +++ b/src/assets/js/search.js @@ -65,44 +65,49 @@ const displaySearchResults = (results, searchTerm) => { const searchResultsElement = document.getElementById("search-results"); - if (results.length) { - let appendString = ""; - - results.forEach((result) => { - const item = window.store[result.ref]; - - const breadcrumbs = item.url - .replace(".html", "") - .replace(/-/g, " ") - .split("/") - .filter(i => i) - .map(breadcrumb => breadcrumb[0].toUpperCase() + breadcrumb.substring(1)) - - breadcrumbs.pop(); - - const matchesRegex = new RegExp(searchTerm, "gi"); - const matchCount = item.content.match(matchesRegex)?.length || 0; - - appendString += - '
  • ' + - item.title + - "

    " + - '
    '; - if (breadcrumbs.length) { - appendString += "" + breadcrumbs.join(" > ") + ""; - }; - appendString += - "" + matchCount + (matchCount == 1 ? " match
    " : " matches"); - appendString += - '

    ...' + getExcerpt(item.content, searchTerm) + "...

  • "; - }); - - searchResultsElement.innerHTML = appendString; - } else { + if (!results.length) { searchResultsElement.innerHTML = "
  • No results found
  • "; + return; } + + let innerHtml = ""; + + results.forEach((result) => { + const item = window.store[result.ref]; + + const breadcrumbs = item.url + .replace(".html", "") + .replace(/-/g, " ") + .split("/") + .filter(i => i) + .map(breadcrumb => breadcrumb[0].toUpperCase() + breadcrumb.substring(1)) + + breadcrumbs.pop(); + + const matchesRegex = new RegExp(searchTerm, "gi"); + const matchCount = item.content.match(matchesRegex)?.length || 0; + + innerHtml += + '
  • ' + + item.title + + '

    '; + + if (breadcrumbs.length) { + innerHtml += "" + breadcrumbs.join(" > ") + ""; + }; + + innerHtml += + "" + + matchCount + + (matchCount == 1 ? " match
    " : " matches") + + '

    ...' + + getExcerpt(item.content, searchTerm) + + "...

  • "; + }); + + searchResultsElement.innerHTML = innerHtml; }; const getExcerpt = (content, query) => {