@@ -11,7 +11,7 @@ import {
1111 ignoreAPIError ,
1212} from './api' ;
1313import { gitbookAppHref , pageHref , PageHrefContext } from './links' ;
14- import { resolvePageId } from './pages' ;
14+ import { getPagePath , resolvePageId } from './pages' ;
1515
1616export interface ResolvedContentRef {
1717 /** Text to render in the content ref */
@@ -27,9 +27,29 @@ export interface ResolvedContentRef {
2727}
2828
2929export 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 */
3959export 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