1- import { RevisionPage , SiteSection , SiteSpace , Space } from '@gitbook/api' ;
2- import { assert } from 'ts-essentials' ;
1+ import { RevisionPage } from '@gitbook/api' ;
32
43import {
54 getRevisionPageByPath ,
65 getDocument ,
76 ContentTarget ,
7+ getSpaceContentData ,
88 getSiteData ,
9- getSite ,
10- getCurrentSiteCustomization ,
11- getSiteStructure ,
129} from '@/lib/api' ;
1310import { resolvePagePath , resolvePageId } from '@/lib/pages' ;
1411import { getSiteContentPointer } from '@/lib/pointer' ;
@@ -21,35 +18,18 @@ export interface PageIdParams {
2118 pageId : string ;
2219}
2320
24- export type SectionsList = { list : SiteSection [ ] ; section : SiteSection ; index : number } ;
25-
2621/**
2722 * Fetch all the data needed to render the content layout.
2823 */
2924export async function fetchContentData ( ) {
3025 const content = getSiteContentPointer ( ) ;
3126
32- const [ { space, contentTarget, pages, customization, scripts } , siteStructure ] =
27+ const [ { space, contentTarget, pages } , { customization, site , sections , spaces , scripts } ] =
3328 await Promise . all ( [
29+ getSpaceContentData ( content , content . siteShareKey ) ,
3430 getSiteData ( content ) ,
35- fetchSiteStructure ( {
36- organizationId : content . organizationId ,
37- siteId : content . siteId ,
38- siteShareKey : content . siteShareKey ,
39- } ) ,
4031 ] ) ;
4132
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-
5333 // we grab the space attached to the parent as it contains overriden customizations
5434 const spaceRelativeToParent = spaces ?. find ( ( space ) => space . id === content . spaceId ) ;
5535
@@ -58,53 +38,30 @@ export async function fetchContentData() {
5838 contentTarget,
5939 space : spaceRelativeToParent ?? space ,
6040 pages,
61- sections : siteSections ,
6241 site,
42+ sections,
6343 spaces,
6444 customization,
6545 scripts,
6646 ancestors : [ ] ,
6747 } ;
6848}
6949
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-
8050/**
8151 * Fetch all the data needed to render the content.
8252 * Optimized to fetch in parallel as much as possible.
8353 */
8454export async function fetchPageData ( params : PagePathParams | PageIdParams ) {
85- const content = getSiteContentPointer ( ) ;
55+ const contentData = await fetchContentData ( ) ;
8656
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 ;
9761
9862 return {
99- content,
100- contentTarget,
101- space,
102- pages,
103- customization,
104- scripts,
105- ancestors : [ ] ,
63+ ...contentData ,
10664 ...page ,
107- ...siteStructure ,
10865 document,
10966 } ;
11067}
@@ -150,59 +107,6 @@ async function resolvePage(
150107 return undefined ;
151108}
152109
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-
206110/**
207111 * Get the page path from the params.
208112 */
0 commit comments