11import { getSiteStructureSections } from '@/lib/sites' ;
22import type {
33 ChangeRequest ,
4- PublishedSiteContentLookup ,
4+ PublishedSiteContent ,
55 RevisionPage ,
66 RevisionPageDocument ,
77 Site ,
@@ -14,11 +14,10 @@ import type {
1414 Space ,
1515} from '@gitbook/api' ;
1616import { type GitBookDataFetcher , createDataFetcher , throwIfDataError } from '@v2/lib/data' ;
17- import { redirect } from 'next/navigation' ;
1817import { assert } from 'ts-essentials' ;
1918import { GITBOOK_URL } from './env' ;
2019import { type ImageResizer , createImageResizer } from './images' ;
21- import { type GitBookSpaceLinker , createLinker } from './links' ;
20+ import { type GitBookLinker , createLinker } from './links' ;
2221
2322/**
2423 * Generic context when rendering content.
@@ -32,7 +31,7 @@ export type GitBookBaseContext = {
3231 /**
3332 * Linker to generate links in the current space.
3433 */
35- linker : GitBookSpaceLinker ;
34+ linker : GitBookLinker ;
3635
3736 /**
3837 * Image resizer to resize images.
@@ -102,59 +101,33 @@ export type GitBookPageContext = (GitBookSpaceContext | GitBookSiteContext) & {
102101} ;
103102
104103/**
105- * Get the base context for a request.
104+ * Get the base context for a request on a site .
106105 */
107106export function getBaseContext ( input : {
108107 siteURL : URL | string ;
108+ siteURLData : PublishedSiteContent ;
109109 urlMode : 'url' | 'url-host' ;
110- apiToken ?: string | null ;
111110} ) {
112- const url = typeof input . siteURL === 'string' ? new URL ( input . siteURL ) : input . siteURL ;
113- const urlMode = input . urlMode ;
111+ const { urlMode , siteURLData } = input ;
112+ const siteURL = typeof input . siteURL === 'string' ? new URL ( input . siteURL ) : input . siteURL ;
114113
115114 const dataFetcher = createDataFetcher ( {
116- apiToken : input . apiToken ?? null ,
115+ apiToken : siteURLData . apiToken ?? null ,
117116 } ) ;
118117
119- const linker = getLinkerForSiteURL ( {
120- siteURL : url ,
121- urlMode,
122- } ) ;
123-
124- const imageResizer = createImageResizer ( {
125- host : url . host ,
126- // To ensure image resizing work for proxied sites,
127- // we serve images from the root of the site.
128- linker : linker ,
129- } ) ;
130-
131- return {
132- dataFetcher,
133- linker,
134- imageResizer,
135- } ;
136- }
137-
138- /**
139- * Get the linker for a given site URL.
140- */
141- export function getLinkerForSiteURL ( input : {
142- siteURL : URL ;
143- urlMode : 'url' | 'url-host' ;
144- } ) {
145- const { siteURL, urlMode } = input ;
146-
147118 const gitbookURL = GITBOOK_URL ? new URL ( GITBOOK_URL ) : undefined ;
148119 const linker =
149120 urlMode === 'url-host'
150121 ? createLinker ( {
151122 host : siteURL . host ,
152- pathname : siteURL . pathname ,
123+ siteBasePath : siteURLData . siteBasePath ,
124+ spaceBasePath : siteURLData . basePath ,
153125 } )
154126 : createLinker ( {
155127 protocol : gitbookURL ?. protocol ,
156128 host : gitbookURL ?. host ,
157- pathname : `/url/${ siteURL . host } ${ siteURL . pathname } ` ,
129+ siteBasePath : `/url/${ siteURL . host } ${ siteURLData . siteBasePath } ` ,
130+ spaceBasePath : `/url/${ siteURL . host } ${ siteURLData . basePath } ` ,
158131 } ) ;
159132
160133 if ( urlMode === 'url' ) {
@@ -165,39 +138,37 @@ export function getLinkerForSiteURL(input: {
165138 } ;
166139 }
167140
168- return linker ;
141+ const imageResizer = createImageResizer ( {
142+ host : siteURL . host ,
143+ // To ensure image resizing work for proxied sites,
144+ // we serve images from the root of the site.
145+ linker : linker ,
146+ } ) ;
147+
148+ return {
149+ dataFetcher,
150+ linker,
151+ imageResizer,
152+ } ;
169153}
170154
171155/**
172156 * Fetch the context of a site using the resolution of a URL
173157 */
174158export async function fetchSiteContextByURLLookup (
175159 baseContext : GitBookBaseContext ,
176- data : PublishedSiteContentLookup
160+ data : PublishedSiteContent
177161) : Promise < GitBookSiteContext > {
178- const { dataFetcher } = baseContext ;
179- if ( 'redirect' in data ) {
180- redirect ( data . redirect ) ;
181- }
182-
183- return await fetchSiteContextByIds (
184- {
185- ...baseContext ,
186- dataFetcher : dataFetcher . withToken ( {
187- apiToken : data . apiToken ,
188- } ) ,
189- } ,
190- {
191- organization : data . organization ,
192- site : data . site ,
193- siteSection : data . siteSection ,
194- siteSpace : data . siteSpace ,
195- space : data . space ,
196- shareKey : data . shareKey ,
197- changeRequest : data . changeRequest ,
198- revision : data . revision ,
199- }
200- ) ;
162+ return await fetchSiteContextByIds ( baseContext , {
163+ organization : data . organization ,
164+ site : data . site ,
165+ siteSection : data . siteSection ,
166+ siteSpace : data . siteSpace ,
167+ space : data . space ,
168+ shareKey : data . shareKey ,
169+ changeRequest : data . changeRequest ,
170+ revision : data . revision ,
171+ } ) ;
201172}
202173
203174/**
0 commit comments