1
- import { RevisionPage , SiteSection , SiteSpace , Space } from '@gitbook/api' ;
2
- import { assert } from 'ts-essentials' ;
1
+ import { RevisionPage } from '@gitbook/api' ;
3
2
4
3
import {
5
4
getRevisionPageByPath ,
6
5
getDocument ,
7
6
ContentTarget ,
7
+ getSpaceContentData ,
8
8
getSiteData ,
9
- getSite ,
10
- getCurrentSiteCustomization ,
11
- getSiteStructure ,
12
9
} from '@/lib/api' ;
13
10
import { resolvePagePath , resolvePageId } from '@/lib/pages' ;
14
11
import { getSiteContentPointer } from '@/lib/pointer' ;
@@ -21,35 +18,18 @@ export interface PageIdParams {
21
18
pageId : string ;
22
19
}
23
20
24
- export type SectionsList = { list : SiteSection [ ] ; section : SiteSection ; index : number } ;
25
-
26
21
/**
27
22
* Fetch all the data needed to render the content layout.
28
23
*/
29
24
export async function fetchContentData ( ) {
30
25
const content = getSiteContentPointer ( ) ;
31
26
32
- const [ { space, contentTarget, pages, customization, scripts } , siteStructure ] =
27
+ const [ { space, contentTarget, pages } , { customization, site , sections , spaces , scripts } ] =
33
28
await Promise . all ( [
29
+ getSpaceContentData ( content , content . siteShareKey ) ,
34
30
getSiteData ( content ) ,
35
- fetchSiteStructure ( {
36
- organizationId : content . organizationId ,
37
- siteId : content . siteId ,
38
- siteShareKey : content . siteShareKey ,
39
- } ) ,
40
31
] ) ;
41
32
42
- const site = siteStructure . site ;
43
-
44
- const siteSections =
45
- content . siteSectionId && siteStructure . sections
46
- ? parseSiteSectionsList ( content . siteSectionId , siteStructure . sections )
47
- : null ;
48
-
49
- const spaces =
50
- siteStructure . spaces ??
51
- ( siteSections ? parseSpacesFromSiteSpaces ( siteSections . section . siteSpaces ) : [ ] ) ;
52
-
53
33
// we grab the space attached to the parent as it contains overriden customizations
54
34
const spaceRelativeToParent = spaces ?. find ( ( space ) => space . id === content . spaceId ) ;
55
35
@@ -58,53 +38,30 @@ export async function fetchContentData() {
58
38
contentTarget,
59
39
space : spaceRelativeToParent ?? space ,
60
40
pages,
61
- sections : siteSections ,
62
41
site,
42
+ sections,
63
43
spaces,
64
44
customization,
65
45
scripts,
66
46
ancestors : [ ] ,
67
47
} ;
68
48
}
69
49
70
- function parseSiteSectionsList ( siteSectionId : string , sections : SiteSection [ ] ) {
71
- const section = sections . find ( ( section ) => section . id === siteSectionId ) ;
72
- assert ( sectionIsDefined ( section ) , 'A section must be defined when there are multiple sections' ) ;
73
- return { list : sections , section, index : sections . indexOf ( section ) } satisfies SectionsList ;
74
- }
75
-
76
- function sectionIsDefined ( section ?: SiteSection ) : section is NonNullable < SiteSection > {
77
- return typeof section !== 'undefined' && section !== null ;
78
- }
79
-
80
50
/**
81
51
* Fetch all the data needed to render the content.
82
52
* Optimized to fetch in parallel as much as possible.
83
53
*/
84
54
export async function fetchPageData ( params : PagePathParams | PageIdParams ) {
85
- const content = getSiteContentPointer ( ) ;
55
+ const contentData = await fetchContentData ( ) ;
86
56
87
- const { space, contentTarget, pages, customization, scripts } = await getSiteData ( content ) ;
88
- const page = await resolvePage ( contentTarget , pages , params ) ;
89
- const [ siteStructure , document ] = await Promise . all ( [
90
- fetchSiteStructure ( {
91
- organizationId : content . organizationId ,
92
- siteId : content . siteId ,
93
- siteShareKey : content . siteShareKey ,
94
- } ) ,
95
- page ?. page . documentId ? getDocument ( space . id , page . page . documentId ) : null ,
96
- ] ) ;
57
+ const page = await resolvePage ( contentData . contentTarget , contentData . pages , params ) ;
58
+ const document = page ?. page . documentId
59
+ ? await getDocument ( contentData . space . id , page . page . documentId )
60
+ : null ;
97
61
98
62
return {
99
- content,
100
- contentTarget,
101
- space,
102
- pages,
103
- customization,
104
- scripts,
105
- ancestors : [ ] ,
63
+ ...contentData ,
106
64
...page ,
107
- ...siteStructure ,
108
65
document,
109
66
} ;
110
67
}
@@ -150,59 +107,6 @@ async function resolvePage(
150
107
return undefined ;
151
108
}
152
109
153
- /**
154
- * Fetch the structure of an organization site.
155
- * This includes the site and its sections or spaces.
156
- */
157
- async function fetchSiteStructure ( args : {
158
- organizationId : string ;
159
- siteId : string ;
160
- siteShareKey : string | undefined ;
161
- } ) {
162
- const { organizationId, siteId, siteShareKey } = args ;
163
- const [ orgSite , siteStructure , siteParentCustomizations ] = await Promise . all ( [
164
- getSite ( organizationId , siteId ) ,
165
- getSiteStructure ( { organizationId, siteId, siteShareKey } ) ,
166
- getCurrentSiteCustomization ( { organizationId, siteId, siteSpaceId : undefined } ) ,
167
- ] ) ;
168
-
169
- const siteSections =
170
- siteStructure . type === 'sections' && siteStructure . structure
171
- ? siteStructure . structure
172
- : null ;
173
- const siteSpaces =
174
- siteStructure . type === 'siteSpaces' && siteStructure . structure
175
- ? parseSpacesFromSiteSpaces ( siteStructure . structure )
176
- : null ;
177
-
178
- // override the title with the customization title
179
- const site = {
180
- ...orgSite ,
181
- ...( siteParentCustomizations ?. title ? { title : siteParentCustomizations . title } : { } ) ,
182
- } ;
183
-
184
- return {
185
- site,
186
- spaces : siteSpaces ,
187
- sections : siteSections ,
188
- } ;
189
- }
190
-
191
- function parseSpacesFromSiteSpaces ( siteSpaces : SiteSpace [ ] ) {
192
- const spaces : Record < string , Space > = { } ;
193
- siteSpaces . forEach ( ( siteSpace ) => {
194
- spaces [ siteSpace . space . id ] = {
195
- ...siteSpace . space ,
196
- title : siteSpace . title ?? siteSpace . space . title ,
197
- urls : {
198
- ...siteSpace . space . urls ,
199
- published : siteSpace . urls . published ,
200
- } ,
201
- } ;
202
- } ) ;
203
- return Object . values ( spaces ) ;
204
- }
205
-
206
110
/**
207
111
* Get the page path from the params.
208
112
*/
0 commit comments