mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-20 09:33:21 +00:00
fix: section groups went missing (#2891)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'gitbook-v2': patch
|
||||
'gitbook': patch
|
||||
---
|
||||
|
||||
fix: lost section groups
|
||||
@@ -189,7 +189,9 @@ export async function fetchSiteContextByIds(
|
||||
...(customizations.site?.title ? { title: customizations.site.title } : {}),
|
||||
};
|
||||
|
||||
const sections = ids.siteSection ? parseSiteSectionsList(siteStructure, ids.siteSection) : null;
|
||||
const sections = ids.siteSection
|
||||
? parseSiteSectionsAndGroups(siteStructure, ids.siteSection)
|
||||
: null;
|
||||
|
||||
const siteSpace = (
|
||||
siteStructure.type === 'siteSpaces' && siteStructure.structure
|
||||
@@ -283,9 +285,14 @@ export async function fetchSpaceContextByIds(
|
||||
};
|
||||
}
|
||||
|
||||
function parseSiteSectionsList(structure: SiteStructure, siteSectionId: string) {
|
||||
const sections = getSiteStructureSections(structure);
|
||||
const section = sections.find((section) => section.id === siteSectionId);
|
||||
function parseSiteSectionsAndGroups(structure: SiteStructure, siteSectionId: string) {
|
||||
const sectionsAndGroups = getSiteStructureSections(structure, { ignoreGroups: false });
|
||||
const section = parseCurrentSection(structure, siteSectionId);
|
||||
assert(section, 'A section must be defined when there are multiple sections');
|
||||
return { list: sections, current: section } satisfies SiteSections;
|
||||
return { list: sectionsAndGroups, current: section } satisfies SiteSections;
|
||||
}
|
||||
|
||||
function parseCurrentSection(structure: SiteStructure, siteSectionId: string) {
|
||||
const sections = getSiteStructureSections(structure, { ignoreGroups: true });
|
||||
return sections.find((section) => section.id === siteSectionId);
|
||||
}
|
||||
|
||||
@@ -220,6 +220,37 @@ const testCases: TestsCase[] = [
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'GitBook Site (Sections and Section Groups)',
|
||||
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/sections/',
|
||||
tests: [
|
||||
{
|
||||
name: 'Site with sections and section groups',
|
||||
url: '',
|
||||
},
|
||||
{
|
||||
name: 'Section group dropdown',
|
||||
url: '',
|
||||
run: async (page) => {
|
||||
await page.getByRole('button', { name: 'Test Section Group 1' }).hover();
|
||||
await expect(page.getByRole('link', { name: /Section B/ })).toBeVisible();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Section group link',
|
||||
url: '',
|
||||
screenshot: false,
|
||||
run: async (page) => {
|
||||
const sectionGroupDropdown = await page.getByText('Test Section Group 1');
|
||||
await sectionGroupDropdown.hover();
|
||||
await page.getByText('Section B').click();
|
||||
await page.waitForURL(
|
||||
'https://gitbook-open-e2e-sites.gitbook.io/sections/sections-4'
|
||||
);
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'GitBook',
|
||||
baseUrl: 'https://docs.gitbook.com',
|
||||
|
||||
@@ -60,7 +60,9 @@ export async function GET(_req: NextRequest) {
|
||||
async function getNodesFromSiteStructure(siteStructure: SiteStructure): Promise<RootContent[]> {
|
||||
switch (siteStructure.type) {
|
||||
case 'sections':
|
||||
return getNodesFromSections(getSiteStructureSections(siteStructure));
|
||||
return getNodesFromSections(
|
||||
getSiteStructureSections(siteStructure, { ignoreGroups: true })
|
||||
);
|
||||
case 'siteSpaces':
|
||||
return getNodesFromSiteSpaces(siteStructure.structure, { heading: true });
|
||||
default:
|
||||
|
||||
@@ -62,7 +62,9 @@ export async function GET() {
|
||||
async function getUrlsFromSiteStructure(siteStructure: SiteStructure): Promise<string[]> {
|
||||
switch (siteStructure.type) {
|
||||
case 'sections':
|
||||
return getUrlsFromSiteSections(getSiteStructureSections(siteStructure));
|
||||
return getUrlsFromSiteSections(
|
||||
getSiteStructureSections(siteStructure, { ignoreGroups: true })
|
||||
);
|
||||
case 'siteSpaces':
|
||||
return getUrlsFromSiteSpaces(siteStructure.structure);
|
||||
default:
|
||||
|
||||
@@ -53,7 +53,7 @@ export function checkIsRootPointer(
|
||||
): boolean {
|
||||
switch (siteStructure.type) {
|
||||
case 'sections': {
|
||||
return getSiteStructureSections(siteStructure).some(
|
||||
return getSiteStructureSections(siteStructure, { ignoreGroups: true }).some(
|
||||
(structure) =>
|
||||
structure.default &&
|
||||
structure.id === pointer.siteSectionId &&
|
||||
|
||||
@@ -1,13 +1,28 @@
|
||||
import type { SiteSection, SiteSpace, SiteStructure } from '@gitbook/api';
|
||||
import type { SiteSection, SiteSectionGroup, SiteSpace, SiteStructure } from '@gitbook/api';
|
||||
|
||||
/**
|
||||
* Get all sections from a site structure.
|
||||
* Set the `ignoreGroups` option to true to flatten the list to only include SiteSection and to not include SiteSectionGroups.
|
||||
*/
|
||||
export function getSiteStructureSections(siteStructure: SiteStructure) {
|
||||
export function getSiteStructureSections(
|
||||
siteStructure: SiteStructure,
|
||||
options: { ignoreGroups: true }
|
||||
): SiteSection[];
|
||||
export function getSiteStructureSections(
|
||||
siteStructure: SiteStructure,
|
||||
options?: { ignoreGroups: false }
|
||||
): SiteSection[] | SiteSectionGroup[];
|
||||
export function getSiteStructureSections(
|
||||
siteStructure: SiteStructure,
|
||||
options?: { ignoreGroups: boolean }
|
||||
) {
|
||||
const { ignoreGroups } = options ?? { ignoreGroups: false };
|
||||
return siteStructure.type === 'sections'
|
||||
? siteStructure.structure.flatMap((item) =>
|
||||
item.object === 'site-section-group' ? item.sections : item
|
||||
)
|
||||
? ignoreGroups
|
||||
? siteStructure.structure.flatMap((item) =>
|
||||
item.object === 'site-section-group' ? item.sections : item
|
||||
)
|
||||
: siteStructure.structure
|
||||
: [];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user