1
- import { RevisionPage , SiteSpace , Space } from '@gitbook/api' ;
1
+ import { RevisionPage , SiteSection , SiteSpace , Space } from '@gitbook/api' ;
2
+ import { assert } from 'ts-essentials' ;
2
3
3
4
import {
4
5
getRevisionPageByPath ,
@@ -20,6 +21,8 @@ export interface PageIdParams {
20
21
pageId : string ;
21
22
}
22
23
24
+ type SectionsList = { list : SiteSection [ ] ; section : SiteSection ; } ;
25
+
23
26
/**
24
27
* Fetch all the data needed to render the content layout.
25
28
*/
@@ -33,17 +36,15 @@ export async function fetchContentData() {
33
36
organizationId : content . organizationId ,
34
37
siteId : content . siteId ,
35
38
siteShareKey : content . siteShareKey ,
36
- siteSpaceId : content . siteSpaceId ,
37
39
} ) ,
38
40
] ) ;
39
41
40
42
const site = siteStructure . site ;
41
43
42
- const sections = siteStructure . sections ;
43
- const section = siteStructure . sections ?. find ( ( section ) => section . id === content . siteSectionId ) ;
44
-
44
+ const siteSections = getSiteSectionsList ( content . siteSectionId , siteStructure . sections ) ;
45
+
45
46
const spaces =
46
- siteStructure . spaces ?? ( section ? parseSpacesFromSiteSpaces ( section . siteSpaces ) : [ ] ) ;
47
+ siteStructure . spaces ?? ( siteSections ? parseSpacesFromSiteSpaces ( siteSections . section . siteSpaces ) : [ ] ) ;
47
48
48
49
// we grab the space attached to the parent as it contains overriden customizations
49
50
const spaceRelativeToParent = spaces ?. find ( ( space ) => space . id === content . spaceId ) ;
@@ -53,8 +54,7 @@ export async function fetchContentData() {
53
54
contentTarget,
54
55
space : spaceRelativeToParent ?? space ,
55
56
pages,
56
- sections,
57
- section,
57
+ sections : siteSections ,
58
58
site,
59
59
spaces,
60
60
customization,
@@ -63,6 +63,19 @@ export async function fetchContentData() {
63
63
} ;
64
64
}
65
65
66
+ function getSiteSectionsList ( siteSectionId ?: string , sections ?: SiteSection [ ] | null ) {
67
+ if ( ! sections ) {
68
+ return null ;
69
+ }
70
+ const section = sections ?. find ( ( section ) => section . id === siteSectionId ) ;
71
+ assert ( sectionIsDefined ( section ) , "A section must be defined when there are multiple sections" ) ;
72
+ return { list : sections , section } satisfies SectionsList ;
73
+ }
74
+
75
+ function sectionIsDefined ( section ?: SiteSection ) : section is NonNullable < SiteSection > {
76
+ return typeof section !== 'undefined' && section !== null ;
77
+ }
78
+
66
79
/**
67
80
* Fetch all the data needed to render the content.
68
81
* Optimized to fetch in parallel as much as possible.
@@ -77,7 +90,6 @@ export async function fetchPageData(params: PagePathParams | PageIdParams) {
77
90
organizationId : content . organizationId ,
78
91
siteId : content . siteId ,
79
92
siteShareKey : content . siteShareKey ,
80
- siteSpaceId : content . siteSpaceId ,
81
93
} ) ,
82
94
page ?. page . documentId ? getDocument ( space . id , page . page . documentId ) : null ,
83
95
] ) ;
@@ -139,13 +151,12 @@ async function resolvePage(
139
151
140
152
/**
141
153
* Fetch the structure of an organization site.
142
- * This includes the site and its sections and spaces.
154
+ * This includes the site and its sections or spaces.
143
155
*/
144
156
async function fetchSiteStructure ( args : {
145
157
organizationId : string ;
146
158
siteId : string ;
147
159
siteShareKey : string | undefined ;
148
- siteSpaceId : string | undefined ;
149
160
} ) {
150
161
const { organizationId, siteId, siteShareKey } = args ;
151
162
const [ orgSite , siteStructure , siteParentCustomizations ] = await Promise . all ( [
0 commit comments