MCPcopy Create free account
hub / github.com/adobe/react-spectrum / buildSectionsFromPages

Function buildSectionsFromPages

packages/dev/s2-docs/src/searchUtils.tsx:203–235  ·  view source on GitHub ↗
(pages: Page[], library: Library)

Source from the content-addressed store, hash-verified

201 * Sorts sections with 'Components' first.
202 */
203export function buildSectionsFromPages(pages: Page[], library: Library): Section[] {
204 const filteredPages = pages.filter(
205 page => getLibraryFromPage(page) === library && !page.exports?.hideFromSearch
206 );
207
208 const components = filteredPages.map(transformPageToComponentItem);
209
210 const sectionNames = Array.from(new Set(components.map(c => c.section || 'Components')));
211
212 return sectionNames
213 .map(sectionName => ({
214 id: sectionName.toLowerCase(),
215 name: sectionName,
216 children: components.filter(c => (c.section || 'Components') === sectionName)
217 }))
218 .sort((a, b) => {
219 if (a.id === 'components') {
220 return -1;
221 }
222 if (b.id === 'components') {
223 return 1;
224 }
225 let ai = END_SECTIONS.indexOf(a.id);
226 let bi = END_SECTIONS.indexOf(b.id);
227 if (ai >= 0) {
228 return bi < 0 ? 1 : ai - bi;
229 }
230 if (bi >= 0) {
231 return ai < 0 ? -1 : bi - ai;
232 }
233 return a.name.localeCompare(b.name);
234 });
235}
236
237/**
238 * Gets items for a given section selection (handles 'all' and specific sections).

Callers 1

useLibrarySectionsFunction · 0.85

Calls 3

getLibraryFromPageFunction · 0.90
filterMethod · 0.65
sortMethod · 0.65

Tested by

no test coverage detected