1
1
import { getSiteStructureSections } from '@/lib/sites' ;
2
2
import type {
3
3
ChangeRequest ,
4
- PublishedSiteContentLookup ,
4
+ PublishedSiteContent ,
5
5
RevisionPage ,
6
6
RevisionPageDocument ,
7
7
Site ,
@@ -14,11 +14,10 @@ import type {
14
14
Space ,
15
15
} from '@gitbook/api' ;
16
16
import { type GitBookDataFetcher , createDataFetcher , throwIfDataError } from '@v2/lib/data' ;
17
- import { redirect } from 'next/navigation' ;
18
17
import { assert } from 'ts-essentials' ;
19
18
import { GITBOOK_URL } from './env' ;
20
19
import { type ImageResizer , createImageResizer } from './images' ;
21
- import { type GitBookSpaceLinker , createLinker } from './links' ;
20
+ import { type GitBookLinker , createLinker } from './links' ;
22
21
23
22
/**
24
23
* Generic context when rendering content.
@@ -32,7 +31,7 @@ export type GitBookBaseContext = {
32
31
/**
33
32
* Linker to generate links in the current space.
34
33
*/
35
- linker : GitBookSpaceLinker ;
34
+ linker : GitBookLinker ;
36
35
37
36
/**
38
37
* Image resizer to resize images.
@@ -102,59 +101,33 @@ export type GitBookPageContext = (GitBookSpaceContext | GitBookSiteContext) & {
102
101
} ;
103
102
104
103
/**
105
- * Get the base context for a request.
104
+ * Get the base context for a request on a site .
106
105
*/
107
106
export function getBaseContext ( input : {
108
107
siteURL : URL | string ;
108
+ siteURLData : PublishedSiteContent ;
109
109
urlMode : 'url' | 'url-host' ;
110
- apiToken ?: string | null ;
111
110
} ) {
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 ;
114
113
115
114
const dataFetcher = createDataFetcher ( {
116
- apiToken : input . apiToken ?? null ,
115
+ apiToken : siteURLData . apiToken ?? null ,
117
116
} ) ;
118
117
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
-
147
118
const gitbookURL = GITBOOK_URL ? new URL ( GITBOOK_URL ) : undefined ;
148
119
const linker =
149
120
urlMode === 'url-host'
150
121
? createLinker ( {
151
122
host : siteURL . host ,
152
- pathname : siteURL . pathname ,
123
+ siteBasePath : siteURLData . siteBasePath ,
124
+ spaceBasePath : siteURLData . basePath ,
153
125
} )
154
126
: createLinker ( {
155
127
protocol : gitbookURL ?. protocol ,
156
128
host : gitbookURL ?. host ,
157
- pathname : `/url/${ siteURL . host } ${ siteURL . pathname } ` ,
129
+ siteBasePath : `/url/${ siteURL . host } ${ siteURLData . siteBasePath } ` ,
130
+ spaceBasePath : `/url/${ siteURL . host } ${ siteURLData . basePath } ` ,
158
131
} ) ;
159
132
160
133
if ( urlMode === 'url' ) {
@@ -165,39 +138,37 @@ export function getLinkerForSiteURL(input: {
165
138
} ;
166
139
}
167
140
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
+ } ;
169
153
}
170
154
171
155
/**
172
156
* Fetch the context of a site using the resolution of a URL
173
157
*/
174
158
export async function fetchSiteContextByURLLookup (
175
159
baseContext : GitBookBaseContext ,
176
- data : PublishedSiteContentLookup
160
+ data : PublishedSiteContent
177
161
) : 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
+ } ) ;
201
172
}
202
173
203
174
/**
0 commit comments