Skip to content

Commit 4493aab

Browse files
committed
Improving search result
1 parent 2780318 commit 4493aab

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

src/components/overrides/Sidebar.astro

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
147147
return false;
148148
}
149149

150+
/**
151+
* Show all contents within a matching folder (including nested folders)
152+
*/
153+
function showAllContentsInFolder(folder: Element, visibleGroups: Set<Element>): void {
154+
// Show all list items within this folder
155+
const allItemsInFolder = folder.querySelectorAll(SELECTORS.listItems);
156+
allItemsInFolder.forEach(item => {
157+
toggleItemVisibility(item as HTMLElement, true);
158+
});
159+
160+
// Show and expand all nested folders within this folder
161+
const nestedGroups = folder.querySelectorAll(SELECTORS.detailsGroups);
162+
nestedGroups.forEach(nestedGroup => {
163+
visibleGroups.add(nestedGroup);
164+
toggleItemVisibility(nestedGroup as HTMLElement, true);
165+
(nestedGroup as HTMLDetailsElement).open = true;
166+
});
167+
}
168+
150169
/**
151170
* Filter sidebar items based on search query
152171
*/
@@ -167,6 +186,7 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
167186
const allGroups = sidebarContent.querySelectorAll(SELECTORS.detailsGroups);
168187
const visibleGroups = new Set<Element>();
169188
const matchingFolders = new Set<Element>();
189+
const itemsInMatchingFolders = new Set<Element>();
170190

171191
// First pass: identify folders that match the query
172192
allGroups.forEach(group => {
@@ -175,6 +195,12 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
175195
matchingFolders.add(group);
176196
visibleGroups.add(group);
177197
(group as HTMLDetailsElement).open = true;
198+
// Show all contents within this matching folder
199+
showAllContentsInFolder(group, visibleGroups);
200+
201+
// Track all items within this matching folder
202+
const itemsInFolder = group.querySelectorAll(SELECTORS.listItems);
203+
itemsInFolder.forEach(item => itemsInMatchingFolders.add(item));
178204
}
179205
});
180206

@@ -183,8 +209,7 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
183209
let shouldShowItem = false;
184210

185211
// Check if this item is inside a matching folder
186-
const parentGroup = item.closest(SELECTORS.detailsGroups);
187-
if (parentGroup && matchingFolders.has(parentGroup)) {
212+
if (itemsInMatchingFolders.has(item)) {
188213
shouldShowItem = true;
189214
} else {
190215
// Otherwise, check if the individual item matches
@@ -198,12 +223,25 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
198223
}
199224
});
200225

201-
// Hide empty groups that don't contain visible items
226+
// Handle nested groups visibility
202227
allGroups.forEach(group => {
203228
if (!visibleGroups.has(group)) {
204-
const visibleChildren = group.querySelectorAll('li:not([style*="display: none"])');
205-
const hasVisibleChildren = visibleChildren.length > 0;
206-
toggleItemVisibility(group as HTMLElement, hasVisibleChildren);
229+
// Check if this group is inside a matching folder
230+
const parentMatchingFolder = Array.from(matchingFolders).find(folder =>
231+
folder.contains(group)
232+
);
233+
234+
if (parentMatchingFolder) {
235+
// This group is inside a matching folder, so it should be visible
236+
visibleGroups.add(group);
237+
toggleItemVisibility(group as HTMLElement, true);
238+
(group as HTMLDetailsElement).open = true;
239+
} else {
240+
// Check if it has visible children
241+
const visibleChildren = group.querySelectorAll('li:not([style*="display: none"])');
242+
const hasVisibleChildren = visibleChildren.length > 0;
243+
toggleItemVisibility(group as HTMLElement, hasVisibleChildren);
244+
}
207245
} else {
208246
toggleItemVisibility(group as HTMLElement, true);
209247
}

0 commit comments

Comments
 (0)