|
| 1 | +import { type DocumentInlineLink, SiteInsightsLinkPosition } from '@gitbook/api'; |
| 2 | + |
| 3 | +import { getSpaceLanguage, tString } from '@/intl/server'; |
| 4 | +import { languages } from '@/intl/translations'; |
| 5 | +import { type ResolvedContentRef, resolveContentRef } from '@/lib/references'; |
| 6 | +import { Icon } from '@gitbook/icons'; |
| 7 | +import type { GitBookAnyContext } from '@v2/lib/context'; |
| 8 | +import { StyledLink } from '../../primitives'; |
| 9 | +import type { InlineProps } from '../Inline'; |
| 10 | +import { Inlines } from '../Inlines'; |
| 11 | +import { InlineLinkTooltip } from './InlineLinkTooltip'; |
| 12 | + |
| 13 | +export async function InlineLink(props: InlineProps<DocumentInlineLink>) { |
| 14 | + const { inline, document, context, ancestorInlines } = props; |
| 15 | + |
| 16 | + const resolved = context.contentContext |
| 17 | + ? await resolveContentRef(inline.data.ref, context.contentContext, { |
| 18 | + // We don't want to resolve the anchor text here, as it can be very expensive and will block rendering if there is a lot of anchors link. |
| 19 | + resolveAnchorText: false, |
| 20 | + }) |
| 21 | + : null; |
| 22 | + |
| 23 | + if (!context.contentContext || !resolved) { |
| 24 | + return ( |
| 25 | + <span title="Broken link" className="underline"> |
| 26 | + <Inlines |
| 27 | + context={context} |
| 28 | + document={document} |
| 29 | + nodes={inline.nodes} |
| 30 | + ancestorInlines={[...ancestorInlines, inline]} |
| 31 | + /> |
| 32 | + </span> |
| 33 | + ); |
| 34 | + } |
| 35 | + const isExternal = inline.data.ref.kind === 'url'; |
| 36 | + const content = ( |
| 37 | + <StyledLink |
| 38 | + href={resolved.href} |
| 39 | + insights={{ |
| 40 | + type: 'link_click', |
| 41 | + link: { |
| 42 | + target: inline.data.ref, |
| 43 | + position: SiteInsightsLinkPosition.Content, |
| 44 | + }, |
| 45 | + }} |
| 46 | + > |
| 47 | + <Inlines |
| 48 | + context={context} |
| 49 | + document={document} |
| 50 | + nodes={inline.nodes} |
| 51 | + ancestorInlines={[...ancestorInlines, inline]} |
| 52 | + /> |
| 53 | + {isExternal ? ( |
| 54 | + <Icon |
| 55 | + icon="arrow-up-right" |
| 56 | + className="ml-0.5 inline size-3 links-accent:text-tint-subtle" |
| 57 | + /> |
| 58 | + ) : null} |
| 59 | + </StyledLink> |
| 60 | + ); |
| 61 | + |
| 62 | + if (context.shouldRenderLinkPreviews) { |
| 63 | + return ( |
| 64 | + <InlineLinkTooltipWrapper |
| 65 | + inline={inline} |
| 66 | + context={context.contentContext} |
| 67 | + resolved={resolved} |
| 68 | + > |
| 69 | + {content} |
| 70 | + </InlineLinkTooltipWrapper> |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + return content; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * An SSR component that renders a link with a tooltip. |
| 79 | + * Essentially it pulls the minimum amount of props from the context to render the tooltip. |
| 80 | + */ |
| 81 | +function InlineLinkTooltipWrapper(props: { |
| 82 | + inline: DocumentInlineLink; |
| 83 | + context: GitBookAnyContext; |
| 84 | + children: React.ReactNode; |
| 85 | + resolved: ResolvedContentRef; |
| 86 | +}) { |
| 87 | + const { inline, context, resolved, children } = props; |
| 88 | + |
| 89 | + let breadcrumbs = resolved.ancestors ?? []; |
| 90 | + const language = |
| 91 | + 'customization' in context ? getSpaceLanguage(context.customization) : languages.en; |
| 92 | + const isExternal = inline.data.ref.kind === 'url'; |
| 93 | + const isSamePage = inline.data.ref.kind === 'anchor' && inline.data.ref.page === undefined; |
| 94 | + if (isExternal) { |
| 95 | + breadcrumbs = [ |
| 96 | + { |
| 97 | + label: tString(language, 'link_tooltip_external_link'), |
| 98 | + }, |
| 99 | + ]; |
| 100 | + } |
| 101 | + if (isSamePage) { |
| 102 | + breadcrumbs = [ |
| 103 | + { |
| 104 | + label: tString(language, 'link_tooltip_page_anchor'), |
| 105 | + icon: <Icon icon="arrow-down-short-wide" className="size-3" />, |
| 106 | + }, |
| 107 | + ]; |
| 108 | + resolved.subText = undefined; |
| 109 | + } |
| 110 | + |
| 111 | + const aiSummary: { pageId: string; spaceId: string } | undefined = (() => { |
| 112 | + if (isExternal) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + if (isSamePage) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (!('customization' in context) || !context.customization.ai?.pageLinkSummaries.enabled) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if (!('page' in context) || !('page' in inline.data.ref)) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + if (inline.data.ref.kind === 'page' || inline.data.ref.kind === 'anchor') { |
| 129 | + return { |
| 130 | + pageId: resolved.page?.id ?? inline.data.ref.page ?? context.page.id, |
| 131 | + spaceId: inline.data.ref.space ?? context.space.id, |
| 132 | + }; |
| 133 | + } |
| 134 | + })(); |
| 135 | + |
| 136 | + return ( |
| 137 | + <InlineLinkTooltip |
| 138 | + breadcrumbs={breadcrumbs} |
| 139 | + isExternal={isExternal} |
| 140 | + isSamePage={isSamePage} |
| 141 | + aiSummary={aiSummary} |
| 142 | + openInNewTabLabel={tString(language, 'open_in_new_tab')} |
| 143 | + target={{ |
| 144 | + href: resolved.href, |
| 145 | + text: resolved.text, |
| 146 | + subText: resolved.subText, |
| 147 | + icon: resolved.icon, |
| 148 | + }} |
| 149 | + > |
| 150 | + {children} |
| 151 | + </InlineLinkTooltip> |
| 152 | + ); |
| 153 | +} |
0 commit comments