From 82ed911cd80c733db9171ef88e3cea3df8965fcb Mon Sep 17 00:00:00 2001 From: Nicolas Dorseuil Date: Mon, 16 Jun 2025 10:12:40 +0200 Subject: [PATCH] Refactor content reference resolution to use context.getContentRef method across components --- .../DocumentView/BlockContentRef.tsx | 12 +++---- .../src/components/DocumentView/Drawing.tsx | 11 +++---- .../src/components/DocumentView/File.tsx | 6 ++-- .../src/components/DocumentView/Images.tsx | 7 ++--- .../components/DocumentView/InlineButton.tsx | 3 +- .../components/DocumentView/InlineImage.tsx | 8 ++--- .../src/components/DocumentView/Mention.tsx | 9 ++---- .../DocumentView/ReusableContent.tsx | 1 + .../DocumentView/Table/RecordColumnValue.tsx | 31 +++++++------------ .../components/Footer/FooterLinksGroup.tsx | 1 + 10 files changed, 34 insertions(+), 55 deletions(-) diff --git a/packages/gitbook/src/components/DocumentView/BlockContentRef.tsx b/packages/gitbook/src/components/DocumentView/BlockContentRef.tsx index 7640a66e2..a8941d52a 100644 --- a/packages/gitbook/src/components/DocumentView/BlockContentRef.tsx +++ b/packages/gitbook/src/components/DocumentView/BlockContentRef.tsx @@ -1,19 +1,17 @@ import { type DocumentBlockContentRef, SiteInsightsLinkPosition } from '@gitbook/api'; import { Card } from '@/components/primitives'; -import { type ResolvedContentRef, resolveContentRef } from '@/lib/references'; +import type { ResolvedContentRef } from '@/lib/references'; import type { BlockProps } from './Block'; export async function BlockContentRef(props: BlockProps) { const { block, context, style } = props; - const resolved = context.contentContext - ? await resolveContentRef(block.data.ref, context.contentContext, { - resolveAnchorText: true, - iconStyle: ['text-xl', 'text-tint'], - }) - : null; + const resolved = await context.getContentRef(block.data.ref, { + resolveAnchorText: true, + iconStyle: ['text-xl', 'text-tint'], + }); if (!resolved) { return null; diff --git a/packages/gitbook/src/components/DocumentView/Drawing.tsx b/packages/gitbook/src/components/DocumentView/Drawing.tsx index ca565a690..e9d7cff79 100644 --- a/packages/gitbook/src/components/DocumentView/Drawing.tsx +++ b/packages/gitbook/src/components/DocumentView/Drawing.tsx @@ -1,7 +1,5 @@ import type { DocumentBlockDrawing } from '@gitbook/api'; -import { resolveContentRef } from '@/lib/references'; - import { Image } from '../utils'; import type { BlockProps } from './Block'; import { Caption } from './Caption'; @@ -9,11 +7,12 @@ import { imageBlockSizes } from './Images'; export async function Drawing(props: BlockProps) { const { block, context } = props; + if (!block.data.ref) { + return null; + } + + const resolved = await context.getContentRef(block.data.ref); - const resolved = - block.data.ref && context.contentContext - ? await resolveContentRef(block.data.ref, context.contentContext) - : null; if (!resolved) { return null; } diff --git a/packages/gitbook/src/components/DocumentView/File.tsx b/packages/gitbook/src/components/DocumentView/File.tsx index fe72164fc..a604c7165 100644 --- a/packages/gitbook/src/components/DocumentView/File.tsx +++ b/packages/gitbook/src/components/DocumentView/File.tsx @@ -1,7 +1,6 @@ import { type DocumentBlockFile, SiteInsightsLinkPosition } from '@gitbook/api'; import { getSimplifiedContentType } from '@/lib/files'; -import { resolveContentRef } from '@/lib/references'; import { tcls } from '@/lib/tailwind'; import { Link } from '../primitives'; @@ -12,9 +11,8 @@ import { FileIcon } from './FileIcon'; export async function File(props: BlockProps) { const { block, context } = props; - const contentRef = context.contentContext - ? await resolveContentRef(block.data.ref, context.contentContext) - : null; + const contentRef = await context.getContentRef(block.data.ref); + const file = contentRef?.file; if (!file) { diff --git a/packages/gitbook/src/components/DocumentView/Images.tsx b/packages/gitbook/src/components/DocumentView/Images.tsx index 91d869002..f2d8a5ad1 100644 --- a/packages/gitbook/src/components/DocumentView/Images.tsx +++ b/packages/gitbook/src/components/DocumentView/Images.tsx @@ -1,7 +1,6 @@ import type { DocumentBlockImage, DocumentBlockImages, JSONDocument, Length } from '@gitbook/api'; import { Image, type ImageResponsiveSize } from '@/components/utils'; -import { resolveContentRef } from '@/lib/references'; import { type ClassValue, tcls } from '@/lib/tailwind'; import type { BlockProps } from './Block'; @@ -66,10 +65,8 @@ async function ImageBlock(props: { const { block, context, isEstimatedOffscreen } = props; const [src, darkSrc] = await Promise.all([ - context.contentContext ? resolveContentRef(block.data.ref, context.contentContext) : null, - block.data.refDark && context.contentContext - ? resolveContentRef(block.data.refDark, context.contentContext) - : null, + context.getContentRef(block.data.ref), + block.data.refDark ? context.getContentRef(block.data.refDark) : null, ]); if (!src) { diff --git a/packages/gitbook/src/components/DocumentView/InlineButton.tsx b/packages/gitbook/src/components/DocumentView/InlineButton.tsx index a36cd7452..fe42dfae4 100644 --- a/packages/gitbook/src/components/DocumentView/InlineButton.tsx +++ b/packages/gitbook/src/components/DocumentView/InlineButton.tsx @@ -1,4 +1,3 @@ -import { resolveContentRef } from '@/lib/references'; import * as api from '@gitbook/api'; import { Button } from '../primitives'; import type { InlineProps } from './Inline'; @@ -10,7 +9,7 @@ export async function InlineButton(props: InlineProps) throw new Error('InlineButton requires a contentContext'); } - const resolved = await resolveContentRef(inline.data.ref, context.contentContext); + const resolved = await context.getContentRef(inline.data.ref); if (!resolved) { return null; diff --git a/packages/gitbook/src/components/DocumentView/InlineImage.tsx b/packages/gitbook/src/components/DocumentView/InlineImage.tsx index 4a3663b82..d857af0e0 100644 --- a/packages/gitbook/src/components/DocumentView/InlineImage.tsx +++ b/packages/gitbook/src/components/DocumentView/InlineImage.tsx @@ -2,7 +2,7 @@ import type { DocumentInlineImage } from '@gitbook/api'; import type { GitBookBaseContext } from '@v2/lib/context'; import assertNever from 'assert-never'; -import { type ResolvedContentRef, resolveContentRef } from '@/lib/references'; +import type { ResolvedContentRef } from '@/lib/references'; import { tcls } from '@/lib/tailwind'; import { Image } from '../utils'; @@ -13,10 +13,8 @@ export async function InlineImage(props: InlineProps) { const { size = 'original' } = inline.data; const [src, darkSrc] = await Promise.all([ - context.contentContext ? resolveContentRef(inline.data.ref, context.contentContext) : null, - inline.data.refDark && context.contentContext - ? resolveContentRef(inline.data.refDark, context.contentContext) - : null, + context.getContentRef(inline.data.ref), + inline.data.refDark ? context.getContentRef(inline.data.refDark) : null, ]); if (!src) { diff --git a/packages/gitbook/src/components/DocumentView/Mention.tsx b/packages/gitbook/src/components/DocumentView/Mention.tsx index 7d4db5178..b013ab921 100644 --- a/packages/gitbook/src/components/DocumentView/Mention.tsx +++ b/packages/gitbook/src/components/DocumentView/Mention.tsx @@ -1,18 +1,15 @@ import { type DocumentInlineMention, SiteInsightsLinkPosition } from '@gitbook/api'; import { StyledLink } from '@/components/primitives'; -import { resolveContentRef } from '@/lib/references'; import type { InlineProps } from './Inline'; export async function Mention(props: InlineProps) { const { inline, context } = props; - const resolved = context.contentContext - ? await resolveContentRef(inline.data.ref, context.contentContext, { - resolveAnchorText: true, - }) - : null; + const resolved = await context.getContentRef(inline.data.ref, { + resolveAnchorText: true, + }); if (!resolved) { return null; diff --git a/packages/gitbook/src/components/DocumentView/ReusableContent.tsx b/packages/gitbook/src/components/DocumentView/ReusableContent.tsx index ccb66babd..8af7f7832 100644 --- a/packages/gitbook/src/components/DocumentView/ReusableContent.tsx +++ b/packages/gitbook/src/components/DocumentView/ReusableContent.tsx @@ -18,6 +18,7 @@ export async function ReusableContent(props: BlockProps( case 'files': { const files = await Promise.all( (value as string[]).map((fileId) => - context.contentContext - ? resolveContentRef( - { - kind: 'file', - file: fileId, - }, - context.contentContext - ) - : null + context.getContentRef({ + kind: 'file', + file: fileId, + }) ) ); @@ -217,13 +211,12 @@ export async function RecordColumnValue( } case 'content-ref': { const contentRef = value ? (value as ContentRef) : null; - const resolved = - contentRef && context.contentContext - ? await resolveContentRef(contentRef, context.contentContext, { - resolveAnchorText: true, - iconStyle: ['mr-2', 'text-tint-subtle'], - }) - : null; + const resolved = contentRef + ? await context.getContentRef(contentRef, { + resolveAnchorText: true, + iconStyle: ['mr-2', 'text-tint-subtle'], + }) + : null; return ( ( kind: 'user', user: userId, }; - const resolved = context.contentContext - ? await resolveContentRef(contentRef, context.contentContext) - : null; + const resolved = await context.getContentRef(contentRef); if (!resolved) { return null; } diff --git a/packages/gitbook/src/components/Footer/FooterLinksGroup.tsx b/packages/gitbook/src/components/Footer/FooterLinksGroup.tsx index dd2de59eb..e7bc3b753 100644 --- a/packages/gitbook/src/components/Footer/FooterLinksGroup.tsx +++ b/packages/gitbook/src/components/Footer/FooterLinksGroup.tsx @@ -34,6 +34,7 @@ export function FooterLinksGroup(props: { async function FooterLink(props: { link: CustomizationContentLink; context: GitBookAnyContext }) { const { link, context } = props; + // TODO: prefetch content ref outside of the main document const resolved = await resolveContentRef(link.to, context); if (!resolved) {