11'use client' ;
22
3- import type { DocumentBlockCode } from '@gitbook/api' ;
43import { useEffect , useMemo , useRef , useState } from 'react' ;
54
65import { useInViewportListener } from '@/components/hooks/useInViewportListener' ;
76import { useScrollListener } from '@/components/hooks/useScrollListener' ;
7+ import type { SlimDocumentBlockCode } from '@/lib/slim-document' ;
88import { useDebounceCallback } from 'usehooks-ts' ;
9- import type { BlockProps } from '../Block' ;
10- import { CodeBlockRenderer } from './CodeBlockRenderer' ;
9+ import { CodeBlockRenderer , type CodeBlockRendererProps } from './CodeBlockRenderer' ;
1110import type { HighlightLine , RenderedInline } from './highlight' ;
1211import { plainHighlight } from './plain-highlight' ;
1312
14- type ClientBlockProps = Pick < BlockProps < DocumentBlockCode > , 'block' | 'style' > & {
13+ interface ClientBlockProps extends Omit < CodeBlockRendererProps , 'lines' > {
14+ block : SlimDocumentBlockCode ;
1515 inlines : RenderedInline [ ] ;
16- } ;
16+ }
1717
1818/**
1919 * Render a code-block client-side by loading the highlighter asynchronously.
@@ -24,14 +24,14 @@ export function ClientCodeBlock(props: ClientBlockProps) {
2424 const blockRef = useRef < HTMLDivElement > ( null ) ;
2525 const isInViewportRef = useRef ( false ) ;
2626 const [ isInViewport , setIsInViewport ] = useState ( false ) ;
27- const plainLines = useMemo ( ( ) => plainHighlight ( block , [ ] ) , [ block ] ) ;
27+ const plainLines = useMemo ( ( ) => plainHighlight ( { block, inlines : [ ] } ) , [ block ] ) ;
2828 const [ lines , setLines ] = useState < null | HighlightLine [ ] > ( null ) ;
2929 const [ highlighting , setHighlighting ] = useState ( false ) ;
3030
3131 // Preload the highlighter when the block is mounted.
3232 useEffect ( ( ) => {
33- import ( './highlight' ) . then ( ( { preloadHighlight } ) => preloadHighlight ( block ) ) ;
34- } , [ block ] ) ;
33+ import ( './highlight' ) . then ( ( { preloadHighlight } ) => preloadHighlight ( block . data . syntax ) ) ;
34+ } , [ block . data . syntax ] ) ;
3535
3636 // When user scrolls, we need to wait for the scroll to finish before running the highlight
3737 const isScrollingRef = useRef ( false ) ;
@@ -80,7 +80,7 @@ export function ClientCodeBlock(props: ClientBlockProps) {
8080 if ( typeof window !== 'undefined' ) {
8181 setHighlighting ( true ) ;
8282 import ( './highlight' ) . then ( ( { highlight } ) => {
83- highlight ( block , inlines ) . then ( ( lines ) => {
83+ highlight ( { block, inlines } ) . then ( ( lines ) => {
8484 if ( cancelled ) {
8585 return ;
8686 }
@@ -104,7 +104,9 @@ export function ClientCodeBlock(props: ClientBlockProps) {
104104 < CodeBlockRenderer
105105 ref = { blockRef }
106106 aria-busy = { highlighting }
107- block = { block }
107+ title = { props . title }
108+ withLineNumbers = { props . withLineNumbers }
109+ withWrap = { props . withWrap }
108110 style = { style }
109111 lines = { lines ?? plainLines }
110112 />
0 commit comments