1
1
import type {
2
2
ContentRef ,
3
3
RevisionFile ,
4
+ RevisionPage ,
4
5
RevisionPageDocument ,
5
6
RevisionReusableContent ,
6
7
SiteSpace ,
7
8
Space ,
8
9
} from '@gitbook/api' ;
9
10
import type { Filesystem } from '@gitbook/openapi-parser' ;
10
- import { type GitBookAnyContext , fetchSpaceContextByIds } from '@v2/lib/context' ;
11
+ import {
12
+ type GitBookAnyContext ,
13
+ type GitBookSpaceContext ,
14
+ fetchSpaceContextByIds ,
15
+ } from '@v2/lib/context' ;
11
16
import { getDataOrNull , getPageDocument , ignoreDataThrownError } from '@v2/lib/data' ;
12
- import { createLinker } from '@v2/lib/links' ;
17
+ import { type GitBookLinker , createLinker } from '@v2/lib/links' ;
13
18
import assertNever from 'assert-never' ;
14
19
import type React from 'react' ;
15
20
@@ -78,8 +83,8 @@ export async function resolveContentRef(
78
83
context : GitBookAnyContext ,
79
84
options : ResolveContentRefOptions = { }
80
85
) : Promise < ResolvedContentRef | null > {
81
- const { resolveAnchorText = false , resolveAsAbsoluteURL = false , iconStyle } = options ;
82
- const { linker, dataFetcher, space, revisionId, pages } = context ;
86
+ const { resolveAnchorText = false , iconStyle } = options ;
87
+ const { linker, dataFetcher, space, revisionId } = context ;
83
88
84
89
const activePage = 'page' in context ? context . page : undefined ;
85
90
@@ -117,6 +122,33 @@ export async function resolveContentRef(
117
122
return resolveContentRefInSpace ( contentRef . space , context , contentRef ) ;
118
123
}
119
124
125
+ let resolveAsAbsoluteURL = options . resolveAsAbsoluteURL ?? false ;
126
+ let linker = context . linker ;
127
+
128
+ const pages : RevisionPage [ ] = await ( async ( ) => {
129
+ if ( context . pages . length ) {
130
+ return context . pages ;
131
+ }
132
+
133
+ const pages = await getDataOrNull (
134
+ dataFetcher . getRevision ( {
135
+ spaceId : space . id ,
136
+ revisionId,
137
+ metadata : false ,
138
+ } )
139
+ ) ;
140
+
141
+ const ctx = await createLinkerForSpace ( space . id , context ) ;
142
+
143
+ if ( ! ctx ) {
144
+ return [ ] ;
145
+ }
146
+
147
+ resolveAsAbsoluteURL = true ;
148
+ linker = ctx . linker ;
149
+ return pages ?. pages ?? [ ] ;
150
+ } ) ( ) ;
151
+
120
152
const resolvePageResult =
121
153
! contentRef . page || contentRef . page === activePage ?. id
122
154
? activePage
@@ -338,6 +370,8 @@ async function getBestTargetSpace(
338
370
: null ,
339
371
] ) ;
340
372
373
+ console . log ( 'publishedContentSite' , context . site , context . shareKey , publishedContentSite ) ;
374
+
341
375
// In the context of sites, we try to find our target space in the site structure.
342
376
// because the url of this space will be in the same site.
343
377
if ( publishedContentSite ) {
@@ -356,6 +390,50 @@ async function resolveContentRefInSpace(
356
390
context : GitBookAnyContext ,
357
391
contentRef : ContentRef
358
392
) {
393
+ const ctx = await createLinkerForSpace ( spaceId , context ) ;
394
+
395
+ if ( ! ctx ) {
396
+ return null ;
397
+ }
398
+
399
+ const resolved = await resolveContentRef (
400
+ contentRef ,
401
+ {
402
+ ...ctx . spaceContext ,
403
+ space : ctx . space ,
404
+ linker : ctx . linker ,
405
+ } ,
406
+ {
407
+ // Resolve pages as absolute URLs as we are in a different site.
408
+ resolveAsAbsoluteURL : true ,
409
+ }
410
+ ) ;
411
+
412
+ if ( ! resolved ) {
413
+ return null ;
414
+ }
415
+
416
+ return {
417
+ ...resolved ,
418
+ ancestors : [
419
+ {
420
+ label : ctx . space . title ,
421
+ href : ctx . baseURL . toString ( ) ,
422
+ } ,
423
+ ...( resolved . ancestors ?? [ ] ) ,
424
+ ] . filter ( filterOutNullable ) ,
425
+ } ;
426
+ }
427
+
428
+ async function createLinkerForSpace (
429
+ spaceId : string ,
430
+ context : GitBookAnyContext
431
+ ) : Promise < {
432
+ spaceContext : GitBookSpaceContext ;
433
+ linker : GitBookLinker ;
434
+ space : Space ;
435
+ baseURL : URL ;
436
+ } | null > {
359
437
const [ spaceContext , bestTargetSpace ] = await Promise . all ( [
360
438
ignoreDataThrownError (
361
439
fetchSpaceContextByIds ( context , {
@@ -373,6 +451,8 @@ async function resolveContentRefInSpace(
373
451
374
452
const space = bestTargetSpace ?. space ?? spaceContext . space ;
375
453
454
+ console . log ( 'bestTargetSpace' , bestTargetSpace , space ) ;
455
+
376
456
// Resolve URLs relative to the space.
377
457
const baseURL = new URL (
378
458
bestTargetSpace ?. siteSpace ?. urls . published ?? space . urls . published ?? space . urls . app
@@ -383,31 +463,10 @@ async function resolveContentRefInSpace(
383
463
siteBasePath : baseURL . pathname ,
384
464
} ) ;
385
465
386
- const resolved = await resolveContentRef (
387
- contentRef ,
388
- {
389
- ...spaceContext ,
390
- space,
391
- linker,
392
- } ,
393
- {
394
- // Resolve pages as absolute URLs as we are in a different site.
395
- resolveAsAbsoluteURL : true ,
396
- }
397
- ) ;
398
-
399
- if ( ! resolved ) {
400
- return null ;
401
- }
402
-
403
466
return {
404
- ...resolved ,
405
- ancestors : [
406
- {
407
- label : space . title ,
408
- href : baseURL . toString ( ) ,
409
- } ,
410
- ...( resolved . ancestors ?? [ ] ) ,
411
- ] . filter ( filterOutNullable ) ,
467
+ spaceContext,
468
+ linker,
469
+ space,
470
+ baseURL,
412
471
} ;
413
472
}
0 commit comments