1
1
import type { DocumentBlockCode } from '@gitbook/api' ;
2
2
3
3
import { getNodeFragmentByType } from '@/lib/document' ;
4
+ import { isV2 } from '@/lib/v2' ;
4
5
5
6
import type { BlockProps } from '../Block' ;
6
7
import { Blocks } from '../Blocks' ;
7
8
import { ClientCodeBlock } from './ClientCodeBlock' ;
8
- import { type RenderedInline , getInlines } from './highlight' ;
9
+ import { CodeBlockRenderer } from './CodeBlockRenderer' ;
10
+ import { type RenderedInline , getInlines , highlight } from './highlight' ;
9
11
10
12
/**
11
13
* Render a code block, can be client-side or server-side.
12
14
*/
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 ;
15
17
const inlines = getInlines ( block ) ;
16
18
const richInlines : RenderedInline [ ] = inlines . map ( ( inline , index ) => {
17
19
const body = ( ( ) => {
@@ -34,5 +36,11 @@ export function CodeBlock(props: BlockProps<DocumentBlockCode>) {
34
36
return { inline, body } ;
35
37
} ) ;
36
38
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
+
37
45
return < ClientCodeBlock block = { block } style = { style } inlines = { richInlines } /> ;
38
46
}
0 commit comments