Skip to content

Commit c04ce67

Browse files
committed
Support for hidden sections
1 parent 34b0c67 commit c04ce67

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

packages/gitbook/src/lib/context.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,44 @@ function filterHiddenSiteSpaces(siteSpaces: SiteSpace[]): SiteSpace[] {
434434
}
435435

436436
function parseSiteSectionsAndGroups(structure: SiteStructure, siteSectionId: string) {
437-
const sectionsAndGroups = getSiteStructureSections(structure, { ignoreGroups: false });
437+
const sectionsAndGroups = getSiteStructureSections(structure);
438+
const visibleSectionsAndGroups = filterSectionsAndGroupsWithHiddenSiteSpaces(sectionsAndGroups);
438439
const section = parseCurrentSection(structure, siteSectionId);
439440
assert(section, `couldn't find section "${siteSectionId}" in site structure`);
440-
return { list: sectionsAndGroups, current: section } satisfies SiteSections;
441+
return { list: visibleSectionsAndGroups, current: section } satisfies SiteSections;
441442
}
442443

443444
function parseCurrentSection(structure: SiteStructure, siteSectionId: string) {
444445
const sections = getSiteStructureSections(structure, { ignoreGroups: true });
445446
return sections.find((section) => section.id === siteSectionId);
446447
}
448+
449+
type SectionOrGroup = SiteSection | SiteSectionGroup;
450+
451+
/**
452+
* Filter out sections and groups with hidden site spaces.
453+
*/
454+
function filterSectionsAndGroupsWithHiddenSiteSpaces(
455+
sectionsOrGroups: SectionOrGroup[]
456+
): SectionOrGroup[] {
457+
return sectionsOrGroups
458+
.map((entry) => {
459+
if (entry.object === 'site-section') {
460+
return sectionHasHiddenSiteSpace(entry) ? null : entry;
461+
}
462+
463+
const visibleChildren: SectionOrGroup[] = filterSectionsAndGroupsWithHiddenSiteSpaces(
464+
entry.children
465+
);
466+
467+
return {
468+
...entry,
469+
children: visibleChildren,
470+
};
471+
})
472+
.filter((entry): entry is SiteSection | SiteSectionGroup => Boolean(entry));
473+
}
474+
475+
function sectionHasHiddenSiteSpace(section: SiteSection) {
476+
return section.siteSpaces.every((siteSpace) => siteSpace.hidden);
477+
}

0 commit comments

Comments
 (0)