Skip to content

Commit 7aad9f7

Browse files
authored
v2: render code block with syntax highlighting on the server (#2932)
1 parent 3fe8f7c commit 7aad9f7

File tree

1 file changed

+11
-3
lines changed
  • packages/gitbook/src/components/DocumentView/CodeBlock

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import type { DocumentBlockCode } from '@gitbook/api';
22

33
import { getNodeFragmentByType } from '@/lib/document';
4+
import { isV2 } from '@/lib/v2';
45

56
import type { BlockProps } from '../Block';
67
import { Blocks } from '../Blocks';
78
import { 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

Comments
 (0)