Skip to content

Commit

Permalink
Merge #111: fix search error for no results
Browse files Browse the repository at this point in the history
b6822db fix search error for no results (Graeme Byrne)

Pull request description:

  Fix for [The search box in blog section does not work #108](#108). Search worked when there was a result but when the user entered something in search bar which had no result then searched again nothing would appear when the user searched for something which should return a result.

  To prevent this issue, the functions in `lib/utils/search.ts` ensure that if there is no search term or no matching results, they return default values (like empty arrays or the original text) and properly handle new searches, ensuring the UI remains responsive and updates with results as expected.

  ```
  export function searchPostsIndex(searchTerm: string) {
      if (!searchTerm) {
  return [];
      }
  ```

  ```
  function replaceTextWithMarker(text: string, match: string) {
      if (!text) {
  return '';
      }

      if (!match) {
  return text;
      }
  ```

  ```
  function getMatches(text: string, searchTerm: string, limit = 1) {
      if (!searchTerm) {
  return [];
      }
  ```

ACKs for top commit:
  josecelano:
    ACK b6822db

Tree-SHA512: aaa332b8b32219cca16bf5dbfa64e6ff60f61b8305d86dc424e40478837db2fc0496e1003ecfe0f697fb433db71082154f387492ff2034237c411df6ae678ba0
  • Loading branch information
josecelano committed Nov 11, 2024
2 parents d70fd78 + b6822db commit 3b3aa7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/lib/utils/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ export function createPostsIndex(data: BlogPost[]) {
}

export function searchPostsIndex(searchTerm: string) {
if (!searchTerm) {
return [];
}

const match = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const results = postsIndex.search(match);

return results
.map((index) => posts[index as number])
.map(({ slug, title, excerpt, tags }) => {
.map(({ slug, title = '', excerpt = '', tags = [] }) => {
return {
slug,
title: replaceTextWithMarker(title, match),
Expand All @@ -46,11 +50,23 @@ export function searchPostsIndex(searchTerm: string) {
}

function replaceTextWithMarker(text: string, match: string) {
if (!text) {
return '';
}

if (!match) {
return text;
}

const regex = new RegExp(match, 'gi');
return text.replaceAll(regex, (match) => `<mark>${match}</mark>`);
return text.replaceAll(regex, (matchedText) => `<mark>${matchedText}</mark>`);
}

function getMatches(text: string, searchTerm: string, limit = 1) {
if (!searchTerm) {
return [];
}

const regex = new RegExp(searchTerm, 'gi');
const indexes = [];
let matches = 0;
Expand Down
1 change: 0 additions & 1 deletion src/routes/(pages)/about/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
{ name: 'Performance & Efficiency', id: 'performanceEfficiency' },
{ name: 'Security & Reliability', id: 'securityReliability' },
{ name: 'User Experience & Accessibility', id: 'userExperience' },
{ name: 'Future use cases', id: 'futureUses' },
{ name: 'Integration & Interoperability', id: 'integration' }
]
},
Expand Down

0 comments on commit 3b3aa7e

Please sign in to comment.