Skip to content

Commit 3b3aa7e

Browse files
committed
Merge #111: fix search error for no results
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
2 parents d70fd78 + b6822db commit 3b3aa7e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/lib/utils/search.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ export function createPostsIndex(data: BlogPost[]) {
3030
}
3131

3232
export function searchPostsIndex(searchTerm: string) {
33+
if (!searchTerm) {
34+
return [];
35+
}
36+
3337
const match = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
3438
const results = postsIndex.search(match);
3539

3640
return results
3741
.map((index) => posts[index as number])
38-
.map(({ slug, title, excerpt, tags }) => {
42+
.map(({ slug, title = '', excerpt = '', tags = [] }) => {
3943
return {
4044
slug,
4145
title: replaceTextWithMarker(title, match),
@@ -46,11 +50,23 @@ export function searchPostsIndex(searchTerm: string) {
4650
}
4751

4852
function replaceTextWithMarker(text: string, match: string) {
53+
if (!text) {
54+
return '';
55+
}
56+
57+
if (!match) {
58+
return text;
59+
}
60+
4961
const regex = new RegExp(match, 'gi');
50-
return text.replaceAll(regex, (match) => `<mark>${match}</mark>`);
62+
return text.replaceAll(regex, (matchedText) => `<mark>${matchedText}</mark>`);
5163
}
5264

5365
function getMatches(text: string, searchTerm: string, limit = 1) {
66+
if (!searchTerm) {
67+
return [];
68+
}
69+
5470
const regex = new RegExp(searchTerm, 'gi');
5571
const indexes = [];
5672
let matches = 0;

src/routes/(pages)/about/+page.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
{ name: 'Performance & Efficiency', id: 'performanceEfficiency' },
2424
{ name: 'Security & Reliability', id: 'securityReliability' },
2525
{ name: 'User Experience & Accessibility', id: 'userExperience' },
26-
{ name: 'Future use cases', id: 'futureUses' },
2726
{ name: 'Integration & Interoperability', id: 'integration' }
2827
]
2928
},

0 commit comments

Comments
 (0)