Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/cross-space-repository-links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook": patch
---

Automatically resolve GitHub and GitLab page links to matching pages in the same published site, including cross-space links imported before their target page was available.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export async function InlineLink(props: InlineProps<DocumentInlineLink>) {
const anchorElement = (
<InlineLinkAnchor
href={resolved.href}
contentRef={inline.data.ref}
isExternal={inline.data.ref.kind === 'url'}
contentRef={resolved.resolvedRef ?? inline.data.ref}
isExternal={(resolved.resolvedRef ?? inline.data.ref).kind === 'url'}
>
{inlinesElement}
</InlineLinkAnchor>
Expand Down Expand Up @@ -121,7 +121,7 @@ function InlineLinkTooltipWrapper(props: {

let breadcrumbs = resolved.ancestors ?? [];
const isMailto = resolved.href.startsWith('mailto:');
const isExternal = inline.data.ref.kind === 'url';
const isExternal = (resolved.resolvedRef ?? inline.data.ref).kind === 'url';
const isSamePage = inline.data.ref.kind === 'anchor' && inline.data.ref.page === undefined;

if (isMailto) {
Expand Down
2 changes: 2 additions & 0 deletions packages/gitbook/src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ export async function fetchSpaceContextByIds(
shareKey: string | undefined;
changeRequest: string | undefined;
revision: string | undefined;
revisionMetadata?: boolean;
}
): Promise<GitBookSpaceContext> {
const { dataFetcher } = baseContext;
Expand Down Expand Up @@ -552,6 +553,7 @@ export async function fetchSpaceContextByIds(
dataFetcher.getRevision({
spaceId: ids.space,
revisionId,
...(ids.revisionMetadata ? { metadata: true } : {}),
}),

// When trying to render a revision with an invalid / non-existing ID,
Expand Down
12 changes: 10 additions & 2 deletions packages/gitbook/src/lib/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function createDataFetcher(
return getRevision(input, {
spaceId: params.spaceId,
revisionId: params.revisionId,
metadata: params.metadata ?? false,
});
},
getRevisionPageByPath(params) {
Expand Down Expand Up @@ -319,16 +320,23 @@ const getChangeRequest = cache(

// We don't use remote cache on vercel because of the 2Mb limit on cache size that makes some route crash
const getRevision = cache(
async (input: DataFetcherInput, params: { spaceId: string; revisionId: string }) => {
async (
input: DataFetcherInput,
params: { spaceId: string; revisionId: string; metadata: boolean }
) => {
'use cache';
if (params.metadata) {
// Git paths can change without changing the content revision.
cacheTag(getCacheTag({ tag: 'space', space: params.spaceId }));
}
return wrapDataFetcherError(async () => {
return trace(`getRevision(${params.spaceId}, ${params.revisionId})`, async () => {
const api = apiClient(input);
const res = await api.spaces.getRevisionById(
params.spaceId,
params.revisionId,
{
metadata: false,
metadata: params.metadata,
},
{
...noCacheFetchOptions,
Expand Down
1 change: 1 addition & 0 deletions packages/gitbook/src/lib/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface GitBookDataFetcher {
getRevision(params: {
spaceId: string;
revisionId: string;
metadata?: boolean;
}): Promise<DataFetcherResponse<api.Revision>>;

/**
Expand Down
Loading
Loading