Skip to content

Commit 89be91a

Browse files
committed
WIP
1 parent f6717e8 commit 89be91a

File tree

2 files changed

+90
-30
lines changed

2 files changed

+90
-30
lines changed

packages/gitbook/src/components/DocumentView/ReusableContent.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
5252
: {
5353
...context.contentContext,
5454
dataFetcher,
55+
5556
space: resolved.reusableContent.space,
5657
revisionId: resolved.reusableContent.revision,
5758
// When the reusable content is in a different space, we don't resolve relative links to pages
5859
// as this space might not be part of the current site.
5960
// In the future, we might expand the logic to look up the space from the list of all spaces in the site
6061
// and adapt the relative links to point to the correct variant.
6162
pages: [],
62-
shareKey: undefined,
63+
// shareKey: undefined,
6364
};
6465

6566
return (

packages/gitbook/src/lib/references.tsx

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import type {
22
ContentRef,
33
RevisionFile,
4+
RevisionPage,
45
RevisionPageDocument,
56
RevisionReusableContent,
67
SiteSpace,
78
Space,
89
} from '@gitbook/api';
910
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';
1116
import { getDataOrNull, getPageDocument, ignoreDataThrownError } from '@v2/lib/data';
12-
import { createLinker } from '@v2/lib/links';
17+
import { type GitBookLinker, createLinker } from '@v2/lib/links';
1318
import assertNever from 'assert-never';
1419
import type React from 'react';
1520

@@ -78,8 +83,8 @@ export async function resolveContentRef(
7883
context: GitBookAnyContext,
7984
options: ResolveContentRefOptions = {}
8085
): 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;
8388

8489
const activePage = 'page' in context ? context.page : undefined;
8590

@@ -117,6 +122,33 @@ export async function resolveContentRef(
117122
return resolveContentRefInSpace(contentRef.space, context, contentRef);
118123
}
119124

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+
120152
const resolvePageResult =
121153
!contentRef.page || contentRef.page === activePage?.id
122154
? activePage
@@ -338,6 +370,8 @@ async function getBestTargetSpace(
338370
: null,
339371
]);
340372

373+
console.log('publishedContentSite', context.site, context.shareKey, publishedContentSite);
374+
341375
// In the context of sites, we try to find our target space in the site structure.
342376
// because the url of this space will be in the same site.
343377
if (publishedContentSite) {
@@ -356,6 +390,50 @@ async function resolveContentRefInSpace(
356390
context: GitBookAnyContext,
357391
contentRef: ContentRef
358392
) {
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> {
359437
const [spaceContext, bestTargetSpace] = await Promise.all([
360438
ignoreDataThrownError(
361439
fetchSpaceContextByIds(context, {
@@ -373,6 +451,8 @@ async function resolveContentRefInSpace(
373451

374452
const space = bestTargetSpace?.space ?? spaceContext.space;
375453

454+
console.log('bestTargetSpace', bestTargetSpace, space);
455+
376456
// Resolve URLs relative to the space.
377457
const baseURL = new URL(
378458
bestTargetSpace?.siteSpace?.urls.published ?? space.urls.published ?? space.urls.app
@@ -383,31 +463,10 @@ async function resolveContentRefInSpace(
383463
siteBasePath: baseURL.pathname,
384464
});
385465

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-
403466
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,
412471
};
413472
}

0 commit comments

Comments
 (0)