Skip to content

Commit 4146ada

Browse files
committed
prioritize title in search
1 parent 4802207 commit 4146ada

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

layouts/_default/list.html

+11-12
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,27 @@ <h1 class="text-4xl font-bold tracking-tight text-gray-900">
5656

5757
searchInput.addEventListener("input", (e) => {
5858
const query = e.target.value.toLowerCase().trim();
59-
const results = [];
59+
let titleMatches = [];
60+
let descriptionMatches = [];
6061

6162
if (query) {
6263
allSoftwares.forEach(category => {
6364
category.services.forEach(service => {
64-
if (
65-
service.title.toLowerCase().includes(query) ||
66-
service.description.toLowerCase().includes(query)
67-
) {
68-
const alreadyExists = results.find(
69-
(result) => result.id === service.id
70-
);
65+
const titleMatch = service.title.toLowerCase().includes(query);
66+
const descriptionMatch = service.description.toLowerCase().includes(query);
7167

72-
if (!alreadyExists) {
73-
results.push(service);
74-
}
68+
if (titleMatch) {
69+
titleMatches.push(service);
70+
} else if (descriptionMatch) {
71+
descriptionMatches.push(service);
7572
}
7673
});
7774
});
7875
}
7976

80-
renderResults(results);
77+
const sortedResults = [...titleMatches, ...descriptionMatches].slice(0, 10);
78+
79+
renderResults(sortedResults);
8180
});
8281

8382
const renderResults = (results) => {

0 commit comments

Comments
 (0)