11import type { DocumentBlockCode } from '@gitbook/api' ;
22
33import { getNodeFragmentByType } from '@/lib/document' ;
4+ import { isV2 } from '@/lib/v2' ;
45
56import type { BlockProps } from '../Block' ;
67import { Blocks } from '../Blocks' ;
78import { ClientCodeBlock } from './ClientCodeBlock' ;
8- import { type RenderedInline , getInlines } from './highlight' ;
9+ import { CodeBlockRenderer } from './CodeBlockRenderer' ;
10+ import { type RenderedInline , getInlines , highlight } from './highlight' ;
911
1012/**
1113 * Render a code block, can be client-side or server-side.
1214 */
13- export function CodeBlock ( props : BlockProps < DocumentBlockCode > ) {
14- const { block, document, style, context } = props ;
15+ export async function CodeBlock ( props : BlockProps < DocumentBlockCode > ) {
16+ const { block, document, style, isEstimatedOffscreen , context } = props ;
1517 const inlines = getInlines ( block ) ;
1618 const richInlines : RenderedInline [ ] = inlines . map ( ( inline , index ) => {
1719 const body = ( ( ) => {
@@ -34,5 +36,11 @@ export function CodeBlock(props: BlockProps<DocumentBlockCode>) {
3436 return { inline, body } ;
3537 } ) ;
3638
39+ if ( isV2 ( ) && ! isEstimatedOffscreen ) {
40+ // In v2, we render the code block server-side
41+ const lines = await highlight ( block , richInlines ) ;
42+ return < CodeBlockRenderer block = { block } style = { style } lines = { lines } /> ;
43+ }
44+
3745 return < ClientCodeBlock block = { block } style = { style } inlines = { richInlines } /> ;
3846}
0 commit comments