Skip to content

Commit 7e56ead

Browse files
committed
Use updated API
1 parent d662e27 commit 7e56ead

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function Block<T extends DocumentBlock>(props: BlockProps<T>) {
9292
case 'math':
9393
return <BlockMath {...props} {...contextProps} block={block} />;
9494
case 'file':
95-
return <File {...props} {...contextProps} block={block} />
95+
return <File {...props} {...contextProps} block={block} />;
9696
case 'divider':
9797
return <Divider {...props} {...contextProps} block={block} />;
9898
case 'drawing':

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

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

3-
import { getReusableContentDocument } from '@/lib/api';
3+
import { getDocument, getReusableContent } from '@/lib/api';
44

55
import { BlockProps } from './Block';
66
import { UnwrappedBlocks } from './Blocks';
@@ -9,14 +9,15 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
99
const { block, context, ancestorBlocks } = props;
1010

1111
if (!context.content) {
12+
throw new Error(`Expected a content context to render a reusable content block`);
13+
}
14+
15+
const resolved = await context.resolveContentRef(block.data.ref);
16+
if (!resolved?.documentId) {
1217
return null;
1318
}
1419

15-
const document = await getReusableContentDocument(
16-
context.content.spaceId,
17-
context.content.revisionId,
18-
block.data.ref.reusableContent,
19-
);
20+
const document = await getDocument(context.content.spaceId, resolved.documentId);
2021

2122
if (!document) {
2223
return null;

packages/gitbook/src/lib/api.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -614,31 +614,26 @@ export const getRevisionFile = batch<[string, string, string], RevisionFile | nu
614614
);
615615

616616
/**
617-
* Get reusable content in a revision. Also returns the document.
617+
* Get reusable content in a revision.
618618
*/
619-
export const getReusableContentDocument = async (
619+
export const getReusableContent = async (
620620
spaceId: string,
621621
revisionId: string,
622622
reusableContentId: string,
623-
): Promise<JSONDocument | null> => {
623+
): Promise<RevisionReusableContent | null> => {
624624
const hasRevisionInMemory = await getRevision.hasInMemory(spaceId, revisionId, {
625625
metadata: false,
626626
});
627627

628628
if (hasRevisionInMemory) {
629629
const revision = await getRevision(spaceId, revisionId, { metadata: false });
630-
const reusableContent = revision.reusableContents[reusableContentId];
631-
if (!reusableContent) {
632-
return null;
633-
}
634-
return getDocument(reusableContent.documentId);
630+
return revision.reusableContents[reusableContentId] ?? null;
635631
} else {
636-
const response = await getRevisionReusableContentById(
632+
return getRevisionReusableContentById(
637633
spaceId,
638634
revisionId,
639635
reusableContentId,
640636
);
641-
return response?.document ?? null;
642637
}
643638
};
644639

packages/gitbook/src/lib/references.tsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
SpaceContentPointer,
1010
getCollection,
1111
getDocument,
12+
getReusableContent,
1213
getRevisionFile,
1314
getSpace,
1415
getSpaceContentData,
@@ -35,6 +36,8 @@ export interface ResolvedContentRef {
3536
active: boolean;
3637
/** File, if the reference is a file */
3738
file?: RevisionFile;
39+
/** ID of an attached document. */
40+
documentId?: string;
3841
}
3942

4043
export interface ContentRefContext extends PageHrefContext {
@@ -255,7 +258,18 @@ export async function resolveContentRef(
255258
};
256259
}
257260

258-
case 'reusable-content':
261+
case 'reusable-content': {
262+
const reusableContent = await getReusableContent(space.id, revisionId, contentRef.reusableContent);
263+
if (!reusableContent) {
264+
return null;
265+
}
266+
return {
267+
href: gitbookAppHref(`/s/${space.id}`),
268+
text: reusableContent.title,
269+
active: false,
270+
documentId: reusableContent.document,
271+
}
272+
}
259273
case 'synced-block':
260274
return null;
261275

0 commit comments

Comments
 (0)