Skip to content

Commit 453a459

Browse files
authored
Fix content ref resolution for pages of other spaces (#2900)
1 parent 722f02e commit 453a459

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

packages/gitbook/src/lib/references.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ export interface ResolveContentRefOptions {
5353
* Styles to apply to the icon.
5454
*/
5555
iconStyle?: ClassValue;
56+
57+
/**
58+
* Resolve the content URL as absolute.
59+
* @default false
60+
*/
61+
resolveAsAbsoluteURL?: boolean;
5662
}
5763

5864
/**
@@ -63,7 +69,7 @@ export async function resolveContentRef(
6369
context: GitBookAnyContext,
6470
options: ResolveContentRefOptions = {}
6571
): Promise<ResolvedContentRef | null> {
66-
const { resolveAnchorText = false, iconStyle } = options;
72+
const { resolveAnchorText = false, resolveAsAbsoluteURL = false, iconStyle } = options;
6773
const { linker, dataFetcher, space, revisionId, pages } = context;
6874

6975
const activePage = 'page' in context ? context.page : undefined;
@@ -144,8 +150,10 @@ export async function resolveContentRef(
144150
icon = <PageIcon page={page} style={iconStyle} />;
145151
}
146152

153+
const href = linker.toPathForPage({ page, pages, anchor });
154+
147155
return {
148-
href: linker.toPathForPage({ page, pages, anchor }),
156+
href: resolveAsAbsoluteURL ? linker.toAbsoluteURL(href) : href,
149157
text,
150158
emoji,
151159
icon,
@@ -311,11 +319,18 @@ async function resolveContentRefInSpace(
311319
pathname: baseURL.pathname,
312320
});
313321

314-
const resolved = await resolveContentRef(contentRef, {
315-
...spaceContext,
316-
space,
317-
linker,
318-
});
322+
const resolved = await resolveContentRef(
323+
contentRef,
324+
{
325+
...spaceContext,
326+
space,
327+
linker,
328+
},
329+
{
330+
// Resolve pages as absolute URLs as we are in a different site.
331+
resolveAsAbsoluteURL: true,
332+
}
333+
);
319334

320335
if (!resolved) {
321336
return null;

0 commit comments

Comments
 (0)