Skip to content

Commit 3e11678

Browse files
authored
fix: section groups went missing (#2891)
1 parent 6e03638 commit 3e11678

File tree

7 files changed

+76
-13
lines changed

7 files changed

+76
-13
lines changed

.changeset/silent-games-know.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'gitbook-v2': patch
3+
'gitbook': patch
4+
---
5+
6+
fix: lost section groups

packages/gitbook-v2/src/lib/context.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ export async function fetchSiteContextByIds(
189189
...(customizations.site?.title ? { title: customizations.site.title } : {}),
190190
};
191191

192-
const sections = ids.siteSection ? parseSiteSectionsList(siteStructure, ids.siteSection) : null;
192+
const sections = ids.siteSection
193+
? parseSiteSectionsAndGroups(siteStructure, ids.siteSection)
194+
: null;
193195

194196
const siteSpace = (
195197
siteStructure.type === 'siteSpaces' && siteStructure.structure
@@ -283,9 +285,14 @@ export async function fetchSpaceContextByIds(
283285
};
284286
}
285287

286-
function parseSiteSectionsList(structure: SiteStructure, siteSectionId: string) {
287-
const sections = getSiteStructureSections(structure);
288-
const section = sections.find((section) => section.id === siteSectionId);
288+
function parseSiteSectionsAndGroups(structure: SiteStructure, siteSectionId: string) {
289+
const sectionsAndGroups = getSiteStructureSections(structure, { ignoreGroups: false });
290+
const section = parseCurrentSection(structure, siteSectionId);
289291
assert(section, 'A section must be defined when there are multiple sections');
290-
return { list: sections, current: section } satisfies SiteSections;
292+
return { list: sectionsAndGroups, current: section } satisfies SiteSections;
293+
}
294+
295+
function parseCurrentSection(structure: SiteStructure, siteSectionId: string) {
296+
const sections = getSiteStructureSections(structure, { ignoreGroups: true });
297+
return sections.find((section) => section.id === siteSectionId);
291298
}

packages/gitbook/e2e/internal.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,37 @@ const testCases: TestsCase[] = [
220220
},
221221
],
222222
},
223+
{
224+
name: 'GitBook Site (Sections and Section Groups)',
225+
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/sections/',
226+
tests: [
227+
{
228+
name: 'Site with sections and section groups',
229+
url: '',
230+
},
231+
{
232+
name: 'Section group dropdown',
233+
url: '',
234+
run: async (page) => {
235+
await page.getByRole('button', { name: 'Test Section Group 1' }).hover();
236+
await expect(page.getByRole('link', { name: /Section B/ })).toBeVisible();
237+
},
238+
},
239+
{
240+
name: 'Section group link',
241+
url: '',
242+
screenshot: false,
243+
run: async (page) => {
244+
const sectionGroupDropdown = await page.getByText('Test Section Group 1');
245+
await sectionGroupDropdown.hover();
246+
await page.getByText('Section B').click();
247+
await page.waitForURL(
248+
'https://gitbook-open-e2e-sites.gitbook.io/sections/sections-4'
249+
);
250+
},
251+
},
252+
],
253+
},
223254
{
224255
name: 'GitBook',
225256
baseUrl: 'https://docs.gitbook.com',

packages/gitbook/src/app/middleware/(site)/(core)/llms.txt/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export async function GET(_req: NextRequest) {
6060
async function getNodesFromSiteStructure(siteStructure: SiteStructure): Promise<RootContent[]> {
6161
switch (siteStructure.type) {
6262
case 'sections':
63-
return getNodesFromSections(getSiteStructureSections(siteStructure));
63+
return getNodesFromSections(
64+
getSiteStructureSections(siteStructure, { ignoreGroups: true })
65+
);
6466
case 'siteSpaces':
6567
return getNodesFromSiteSpaces(siteStructure.structure, { heading: true });
6668
default:

packages/gitbook/src/app/middleware/(site)/(core)/sitemap.xml/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ export async function GET() {
6262
async function getUrlsFromSiteStructure(siteStructure: SiteStructure): Promise<string[]> {
6363
switch (siteStructure.type) {
6464
case 'sections':
65-
return getUrlsFromSiteSections(getSiteStructureSections(siteStructure));
65+
return getUrlsFromSiteSections(
66+
getSiteStructureSections(siteStructure, { ignoreGroups: true })
67+
);
6668
case 'siteSpaces':
6769
return getUrlsFromSiteSpaces(siteStructure.structure);
6870
default:

packages/gitbook/src/lib/pointer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function checkIsRootPointer(
5353
): boolean {
5454
switch (siteStructure.type) {
5555
case 'sections': {
56-
return getSiteStructureSections(siteStructure).some(
56+
return getSiteStructureSections(siteStructure, { ignoreGroups: true }).some(
5757
(structure) =>
5858
structure.default &&
5959
structure.id === pointer.siteSectionId &&

packages/gitbook/src/lib/sites.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
import type { SiteSection, SiteSpace, SiteStructure } from '@gitbook/api';
1+
import type { SiteSection, SiteSectionGroup, SiteSpace, SiteStructure } from '@gitbook/api';
22

33
/**
44
* Get all sections from a site structure.
5+
* Set the `ignoreGroups` option to true to flatten the list to only include SiteSection and to not include SiteSectionGroups.
56
*/
6-
export function getSiteStructureSections(siteStructure: SiteStructure) {
7+
export function getSiteStructureSections(
8+
siteStructure: SiteStructure,
9+
options: { ignoreGroups: true }
10+
): SiteSection[];
11+
export function getSiteStructureSections(
12+
siteStructure: SiteStructure,
13+
options?: { ignoreGroups: false }
14+
): SiteSection[] | SiteSectionGroup[];
15+
export function getSiteStructureSections(
16+
siteStructure: SiteStructure,
17+
options?: { ignoreGroups: boolean }
18+
) {
19+
const { ignoreGroups } = options ?? { ignoreGroups: false };
720
return siteStructure.type === 'sections'
8-
? siteStructure.structure.flatMap((item) =>
9-
item.object === 'site-section-group' ? item.sections : item
10-
)
21+
? ignoreGroups
22+
? siteStructure.structure.flatMap((item) =>
23+
item.object === 'site-section-group' ? item.sections : item
24+
)
25+
: siteStructure.structure
1126
: [];
1227
}
1328

0 commit comments

Comments
 (0)