Skip to content

Commit 3bb6323

Browse files
committed
Add highlighting for search matches in snippet titles and descriptions
1 parent 0ccd530 commit 3bb6323

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

public/js/app.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ document.addEventListener('DOMContentLoaded', () => {
171171
setTimeout(() => {
172172
loadMoreIfNeeded();
173173
}, 100);
174-
renderTree(data.folders, activeSnippets);
174+
const snippetsForTree = activeSnippets.map(snippet => {
175+
const found = currentSnippets.find(s => s.id === snippet.id);
176+
return found ? { ...snippet, highlightedName: found.highlightedName } : snippet;
177+
});
178+
renderTree(data.folders, snippetsForTree);
175179

176180
// Update total snippets count
177181
document.getElementById('total-snippets').textContent = `${activeSnippets.length} snippets`;
@@ -225,7 +229,9 @@ document.addEventListener('DOMContentLoaded', () => {
225229
highlightedContent: snippet.content.map(content => ({
226230
...content,
227231
highlightedValue: content.value // No highlighting
228-
}))
232+
})),
233+
highlightedName: snippet.name,
234+
highlightedDescription: snippet.description
229235
}));
230236

231237
// Hide search info bar when no search is active
@@ -271,7 +277,9 @@ document.addEventListener('DOMContentLoaded', () => {
271277
highlightedContent: snippet.highlightedContent || snippet.content.map(content => ({
272278
...content,
273279
highlightedValue: content.value
274-
}))
280+
})),
281+
highlightedName: highlightSearchMatches(snippet.name, query),
282+
highlightedDescription: snippet.description ? highlightSearchMatches(snippet.description, query) : null
275283
}));
276284

277285
currentSnippets = filteredSnippets;
@@ -467,7 +475,7 @@ document.addEventListener('DOMContentLoaded', () => {
467475

468476
const title = document.createElement('h2');
469477
title.className = 'text-xl font-semibold text-gray-800 mb-2 dark:text-white';
470-
title.textContent = snippet.name;
478+
title.innerHTML = snippet.highlightedName || snippet.name;
471479
snippetDiv.appendChild(title);
472480

473481
if (snippet.tagsIds.length > 0) {
@@ -488,7 +496,7 @@ document.addEventListener('DOMContentLoaded', () => {
488496
if (snippet.description) {
489497
const desc = document.createElement('p');
490498
desc.className = 'text-gray-600 mb-2 dark:text-gray-300';
491-
desc.textContent = snippet.description;
499+
desc.innerHTML = snippet.highlightedDescription || snippet.description;
492500
snippetDiv.appendChild(desc);
493501
}
494502

@@ -678,7 +686,7 @@ document.addEventListener('DOMContentLoaded', () => {
678686
folder.snippets.forEach(snippet => {
679687
const snippetLi = document.createElement('li');
680688
snippetLi.className = 'snippet-item';
681-
snippetLi.textContent = snippet.name;
689+
snippetLi.innerHTML = snippet.highlightedName || snippet.name;
682690
snippetLi.dataset.snippetId = snippet.id;
683691
subUl.appendChild(snippetLi);
684692
});

public/styles.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,12 @@ video {
857857
margin-left: calc(1rem * calc(1 - var(--tw-space-x-reverse)));
858858
}
859859

860+
.space-y-0 > :not([hidden]) ~ :not([hidden]) {
861+
--tw-space-y-reverse: 0;
862+
margin-top: calc(0px * calc(1 - var(--tw-space-y-reverse)));
863+
margin-bottom: calc(0px * var(--tw-space-y-reverse));
864+
}
865+
860866
.space-y-1 > :not([hidden]) ~ :not([hidden]) {
861867
--tw-space-y-reverse: 0;
862868
margin-top: calc(0.25rem * calc(1 - var(--tw-space-y-reverse)));

0 commit comments

Comments
 (0)