1
1
'use client' ;
2
2
3
- import type { DocumentBlockCode } from '@gitbook/api' ;
4
3
import { useEffect , useMemo , useRef , useState } from 'react' ;
5
4
6
5
import { useInViewportListener } from '@/components/hooks/useInViewportListener' ;
7
6
import { useScrollListener } from '@/components/hooks/useScrollListener' ;
7
+ import type { SlimDocumentBlockCode } from '@/lib/slim-document' ;
8
8
import { useDebounceCallback } from 'usehooks-ts' ;
9
- import type { BlockProps } from '../Block' ;
10
- import { CodeBlockRenderer } from './CodeBlockRenderer' ;
9
+ import { CodeBlockRenderer , type CodeBlockRendererProps } from './CodeBlockRenderer' ;
11
10
import type { HighlightLine , RenderedInline } from './highlight' ;
12
11
import { plainHighlight } from './plain-highlight' ;
13
12
14
- type ClientBlockProps = Pick < BlockProps < DocumentBlockCode > , 'block' | 'style' > & {
13
+ interface ClientBlockProps extends Omit < CodeBlockRendererProps , 'lines' > {
14
+ block : SlimDocumentBlockCode ;
15
15
inlines : RenderedInline [ ] ;
16
- } ;
16
+ }
17
17
18
18
/**
19
19
* Render a code-block client-side by loading the highlighter asynchronously.
@@ -24,13 +24,13 @@ export function ClientCodeBlock(props: ClientBlockProps) {
24
24
const blockRef = useRef < HTMLDivElement > ( null ) ;
25
25
const isInViewportRef = useRef ( false ) ;
26
26
const [ isInViewport , setIsInViewport ] = useState ( false ) ;
27
- const plainLines = useMemo ( ( ) => plainHighlight ( block , [ ] ) , [ block ] ) ;
27
+ const plainLines = useMemo ( ( ) => plainHighlight ( { block, inlines : [ ] } ) , [ block ] ) ;
28
28
const [ lines , setLines ] = useState < null | HighlightLine [ ] > ( null ) ;
29
29
30
30
// Preload the highlighter when the block is mounted.
31
31
useEffect ( ( ) => {
32
- import ( './highlight' ) . then ( ( { preloadHighlight } ) => preloadHighlight ( block ) ) ;
33
- } , [ block ] ) ;
32
+ import ( './highlight' ) . then ( ( { preloadHighlight } ) => preloadHighlight ( block . data . syntax ) ) ;
33
+ } , [ block . data . syntax ] ) ;
34
34
35
35
// When user scrolls, we need to wait for the scroll to finish before running the highlight
36
36
const isScrollingRef = useRef ( false ) ;
@@ -78,7 +78,7 @@ export function ClientCodeBlock(props: ClientBlockProps) {
78
78
79
79
if ( typeof window !== 'undefined' ) {
80
80
import ( './highlight' ) . then ( ( { highlight } ) => {
81
- highlight ( block , inlines ) . then ( ( lines ) => {
81
+ highlight ( { block, inlines } ) . then ( ( lines ) => {
82
82
if ( cancelled ) {
83
83
return ;
84
84
}
@@ -98,6 +98,13 @@ export function ClientCodeBlock(props: ClientBlockProps) {
98
98
} , [ isInViewport , block , inlines ] ) ;
99
99
100
100
return (
101
- < CodeBlockRenderer ref = { blockRef } block = { block } style = { style } lines = { lines ?? plainLines } />
101
+ < CodeBlockRenderer
102
+ ref = { blockRef }
103
+ title = { props . title }
104
+ withLineNumbers = { props . withLineNumbers }
105
+ withWrap = { props . withWrap }
106
+ style = { style }
107
+ lines = { lines ?? plainLines }
108
+ />
102
109
) ;
103
110
}
0 commit comments