Skip to content

Commit c13f676

Browse files
authored
Fix content references to other spaces (GitbookIO#243)
1 parent d326822 commit c13f676

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/app/(space)/ClientContexts.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function ClientContexts(props: {
1919
/**
2020
* A bug in ThemeProvider is causing the nonce to be included incorrectly
2121
* on the client-side. Original issue: https://github.com/pacocoursey/next-themes/issues/218
22-
*
22+
*
2323
* This is a workaround for it, until next-themes fixes it in their library. There is already
2424
* a PR: https://github.com/pacocoursey/next-themes/pull/223
2525
*/

src/lib/pages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export function resolvePrevNextPages(
9898

9999
/**
100100
* Resolve a page to its canonical path.
101+
* The path will NOT start with "/".
101102
*/
102103
export function getPagePath(
103104
rootPages: Revision['pages'],

src/lib/references.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
ignoreAPIError,
1212
} from './api';
1313
import { gitbookAppHref, pageHref, PageHrefContext } from './links';
14-
import { resolvePageId } from './pages';
14+
import { getPagePath, resolvePageId } from './pages';
1515

1616
export interface ResolvedContentRef {
1717
/** Text to render in the content ref */
@@ -27,9 +27,29 @@ export interface ResolvedContentRef {
2727
}
2828

2929
export interface ContentRefContext extends PageHrefContext {
30+
/**
31+
* Base URL to use to prepend to relative URLs.
32+
*/
33+
baseUrl?: string;
34+
35+
/**
36+
* Space in which we are resolving the content reference.
37+
*/
3038
space: Space;
39+
40+
/**
41+
* Revision in which we are resolving the content reference.
42+
*/
3143
revisionId: string;
44+
45+
/**
46+
* Pages in the revision.
47+
*/
3248
pages: Revision['pages'];
49+
50+
/**
51+
* Page in which the content reference is being resolved.
52+
*/
3353
page?: RevisionPageDocument;
3454
}
3555

@@ -38,8 +58,10 @@ export interface ContentRefContext extends PageHrefContext {
3858
*/
3959
export async function resolveContentRef(
4060
contentRef: ContentRef,
41-
{ space, revisionId, pages, page: activePage, ...linksContext }: ContentRefContext,
61+
context: ContentRefContext,
4262
): Promise<ResolvedContentRef | null> {
63+
const { space, revisionId, pages, page: activePage, ...linksContext } = context;
64+
4365
switch (contentRef.kind) {
4466
case 'url': {
4567
return {
@@ -87,8 +109,21 @@ export async function resolveContentRef(
87109
}
88110

89111
const isCurrentPage = page.id === activePage?.id;
112+
let href = '';
113+
if (context.baseUrl) {
114+
// Page in another content
115+
href = new URL(getPagePath(pages, page), context.baseUrl).toString();
116+
117+
if (contentRef.anchor) {
118+
href += '#' + contentRef.anchor;
119+
}
120+
} else {
121+
// Page in the current content
122+
href = pageHref(pages, page, linksContext, contentRef.anchor);
123+
}
124+
90125
return {
91-
href: pageHref(pages, page, linksContext, contentRef.anchor),
126+
href,
92127
text: (isCurrentPage ? '' : page.title) + '#' + contentRef.anchor,
93128
emoji: isCurrentPage ? undefined : page.emoji,
94129
active: false,
@@ -173,9 +208,17 @@ async function resolveContentRefInSpace(spaceId: string, contentRef: ContentRef)
173208
}
174209

175210
const { space, pages } = result;
211+
212+
// Base URL to use to prepend to relative URLs.
213+
let baseUrl = space.urls.published ?? space.urls.app;
214+
if (!baseUrl.endsWith('/')) {
215+
baseUrl += '/';
216+
}
217+
176218
return resolveContentRef(contentRef, {
177219
space,
178220
revisionId: space.revision,
179221
pages,
222+
baseUrl,
180223
});
181224
}

0 commit comments

Comments
 (0)