Skip to content

Commit c8810ea

Browse files
SamyPessescazan
andauthored
Hide pages from table of contents when set as hidden (#2383)
Co-authored-by: Scott Cazan <[email protected]>
1 parent aa8bbad commit c8810ea

File tree

6 files changed

+51
-12
lines changed

6 files changed

+51
-12
lines changed

bun.lockb

32 Bytes
Binary file not shown.

e2e/pages.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ const testCases: TestsCase[] = [
363363
name: 'Page options',
364364
baseUrl: 'https://gitbook.gitbook.io/test-1-1/',
365365
tests: [
366+
{
367+
name: 'Hidden',
368+
url: 'page-options/page-hidden',
369+
run: waitForCookiesDialog,
370+
},
366371
{
367372
name: 'With cover',
368373
url: 'page-options/page-with-cover',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
],
2121
"dependencies": {
2222
"@geist-ui/icons": "^1.0.2",
23-
"@gitbook/api": "^0.53.0",
23+
"@gitbook/api": "^0.56.0",
2424
"@radix-ui/react-checkbox": "^1.0.4",
2525
"@radix-ui/react-popover": "^1.0.7",
2626
"@sentry/nextjs": "^7.94.1",

src/app/(space)/(core)/~gitbook/pdf/page.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Revision,
55
RevisionPageDocument,
66
RevisionPageGroup,
7+
RevisionPageLink,
78
SiteCustomizationSettings,
89
Space,
910
} from '@gitbook/api';
@@ -313,9 +314,17 @@ function selectPages(
313314
): FlatPageEntry[] => {
314315
return [
315316
{ page, depth },
316-
...page.pages.flatMap((child) =>
317-
child.type === 'link' ? [] : flattenPage(child, depth + 1),
318-
),
317+
...page.pages.flatMap((child) => {
318+
if (child.type === 'link') {
319+
return [];
320+
}
321+
322+
if (child.hidden) {
323+
return [];
324+
}
325+
326+
return flattenPage(child, depth + 1);
327+
}),
319328
];
320329
};
321330

@@ -340,8 +349,16 @@ function selectPages(
340349
return limitTo(flattenPage(found.page, 0));
341350
}
342351

343-
const allPages = rootPages.flatMap((page) =>
344-
page.type === 'link' ? [] : flattenPage(page, 0),
345-
);
352+
const allPages = rootPages.flatMap((page) => {
353+
if (page.type === 'link') {
354+
return [];
355+
}
356+
357+
if (page.hidden) {
358+
return [];
359+
}
360+
361+
return flattenPage(page, 0);
362+
});
346363
return limitTo(allPages);
347364
}

src/components/TableOfContents/PagesList.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ export function PagesList(props: {
1919
return (
2020
<ul className={tcls('flex', 'flex-1', 'flex-col', 'gap-y-0.5', style)}>
2121
{pages.map((page) => {
22+
if (page.type === 'link') {
23+
return <PageLinkItem key={page.id} page={page} context={context} />;
24+
}
25+
26+
if (page.hidden) {
27+
return null;
28+
}
29+
2230
if (page.type === 'group') {
2331
return (
2432
<PageGroupItem
@@ -29,8 +37,6 @@ export function PagesList(props: {
2937
context={context}
3038
/>
3139
);
32-
} else if (page.type === 'link') {
33-
return <PageLinkItem key={page.id} page={page} context={context} />;
3440
}
3541

3642
return (

src/lib/pages.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ export function resolvePageId(
7575

7676
/**
7777
* Resolve the next/previous page before another one.
78+
* It ignores hidden pages as this is used for navigation purpose.
7879
*/
7980
export function resolvePrevNextPages(
8081
rootPages: Revision['pages'],
8182
page: RevisionPageDocument,
8283
): { previous?: RevisionPageDocument; next?: RevisionPageDocument } {
83-
const flat = flattenPages(rootPages);
84+
const flat = flattenPages(rootPages, (page) => !page.hidden);
8485

8586
const currentIndex = flat.findIndex((p) => p.id === page.id);
8687
if (currentIndex === -1) {
@@ -152,17 +153,27 @@ function resolvePageDocument(
152153
return { page, ancestors };
153154
}
154155

155-
function flattenPages(pages: RevisionPage[]): RevisionPageDocument[] {
156+
/**
157+
* Flatten a list of pages into a list of page documents.
158+
*/
159+
function flattenPages(
160+
pages: RevisionPage[],
161+
filter?: (page: RevisionPageDocument | RevisionPageGroup) => boolean,
162+
): RevisionPageDocument[] {
156163
const result: RevisionPageDocument[] = [];
157164
for (const page of pages) {
158165
if (page.type === 'link') {
159166
continue;
160167
}
161168

169+
if (filter && !filter(page)) {
170+
continue;
171+
}
172+
162173
if (page.type === 'document') {
163174
result.push(page);
164175
}
165-
result.push(...flattenPages(page.pages));
176+
result.push(...flattenPages(page.pages, filter));
166177
}
167178

168179
return result;

0 commit comments

Comments
 (0)