Skip to content

Commit 8339e91

Browse files
authored
Fix images and other content refs in reusable content across spaces. (#3190)
1 parent 3119066 commit 8339e91

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

.changeset/big-clocks-rush.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": minor
3+
---
4+
5+
Fix images in reusable content across spaces.

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { DocumentBlockReusableContent } from '@gitbook/api';
22

33
import { resolveContentRef } from '@/lib/references';
44

5+
import type { GitBookSpaceContext } from '@v2/lib/context';
56
import { getDataOrNull } from '@v2/lib/data';
67
import type { BlockProps } from './Block';
78
import { UnwrappedBlocks } from './Blocks';
@@ -33,7 +34,7 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
3334

3435
const document = await getDataOrNull(
3536
dataFetcher.getDocument({
36-
spaceId: resolved.reusableContent.space,
37+
spaceId: resolved.reusableContent.space.id,
3738
documentId: reusableContent.document,
3839
})
3940
);
@@ -42,12 +43,27 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
4243
return null;
4344
}
4445

46+
// Create a new context for reusable content block, including
47+
// the data fetcher with the token from the block meta and the correct
48+
// space and revision pointers.
49+
const reusableContentContext: GitBookSpaceContext = {
50+
...context.contentContext,
51+
dataFetcher,
52+
space: resolved.reusableContent.space,
53+
revisionId: resolved.reusableContent.revision,
54+
pages: [],
55+
shareKey: undefined,
56+
};
57+
4558
return (
4659
<UnwrappedBlocks
4760
nodes={document.nodes}
4861
document={document}
4962
ancestorBlocks={[...ancestorBlocks, block]}
50-
context={context}
63+
context={{
64+
...context,
65+
contentContext: reusableContentContext,
66+
}}
5167
/>
5268
);
5369
}

packages/gitbook/src/lib/references.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface ResolvedContentRef {
4444
/** Resolved reusable content, if the ref points to reusable content on a revision. Also contains the space and revision used for resolution. */
4545
reusableContent?: {
4646
revisionReusableContent: RevisionReusableContent;
47-
space: string;
47+
space: Space;
4848
revision: string;
4949
};
5050
/** Resolve OpenAPI spec filesystem. */
@@ -236,10 +236,10 @@ export async function resolveContentRef(
236236

237237
case 'reusable-content': {
238238
// Figure out which space and revision the reusable content is in.
239-
const container: { space: string; revision: string } | null = await (async () => {
239+
const container: { space: Space; revision: string } | null = await (async () => {
240240
// without a space on the content ref, or if the space is the same as the current one, we can use the current revision.
241241
if (!contentRef.space || contentRef.space === context.space.id) {
242-
return { space: context.space.id, revision: revisionId };
242+
return { space: context.space, revision: revisionId };
243243
}
244244

245245
const space = await getDataOrNull(
@@ -253,7 +253,7 @@ export async function resolveContentRef(
253253
return null;
254254
}
255255

256-
return { space: space.id, revision: space.revision };
256+
return { space, revision: space.revision };
257257
})();
258258

259259
if (!container) {
@@ -262,7 +262,7 @@ export async function resolveContentRef(
262262

263263
const reusableContent = await getDataOrNull(
264264
dataFetcher.getReusableContent({
265-
spaceId: container.space,
265+
spaceId: container.space.id,
266266
revisionId: container.revision,
267267
reusableContentId: contentRef.reusableContent,
268268
})

0 commit comments

Comments
 (0)