33import type { DocumentBlockCode } from '@gitbook/api' ;
44import { useEffect , useMemo , useRef , useState } from 'react' ;
55
6+ import { useAdaptiveVisitor } from '@/components/Adaptive' ;
67import { useInViewportListener } from '@/components/hooks/useInViewportListener' ;
78import { useScrollListener } from '@/components/hooks/useScrollListener' ;
89import { useDebounceCallback } from 'usehooks-ts' ;
910import type { BlockProps } from '../Block' ;
11+ import { type InlineExpressionVariables , useEvaluateInlineExpression } from '../InlineExpression' ;
1012import { CodeBlockRenderer } from './CodeBlockRenderer' ;
1113import type { HighlightLine , RenderedInline } from './highlight' ;
1214import { plainHighlight } from './plain-highlight' ;
1315
1416type ClientBlockProps = Pick < BlockProps < DocumentBlockCode > , 'block' | 'style' > & {
1517 inlines : RenderedInline [ ] ;
18+ inlineExprVariables : InlineExpressionVariables ;
1619} ;
1720
1821/**
1922 * Render a code-block client-side by loading the highlighter asynchronously.
2023 * It allows us to defer some load to avoid blocking the rendering of the whole page with block highlighting.
2124 */
2225export function ClientCodeBlock ( props : ClientBlockProps ) {
23- const { block, style, inlines } = props ;
26+ const { block, style, inlines, inlineExprVariables } = props ;
2427 const blockRef = useRef < HTMLDivElement > ( null ) ;
2528 const isInViewportRef = useRef ( false ) ;
2629 const [ isInViewport , setIsInViewport ] = useState ( false ) ;
27- const plainLines = useMemo ( ( ) => plainHighlight ( block , [ ] ) , [ block ] ) ;
30+
31+ const getAdaptiveVisitorClaims = useAdaptiveVisitor ( ) ;
32+ const visitorClaims = getAdaptiveVisitorClaims ( ) ;
33+ const evaluateInlineExpression = useEvaluateInlineExpression ( {
34+ visitorClaims,
35+ variables : inlineExprVariables ,
36+ } ) ;
37+ const plainLines = useMemo (
38+ ( ) => plainHighlight ( block , inlines , { evaluateInlineExpression } ) ,
39+ [ block , inlines , evaluateInlineExpression ]
40+ ) ;
2841 const [ lines , setLines ] = useState < null | HighlightLine [ ] > ( null ) ;
2942 const [ highlighting , setHighlighting ] = useState ( false ) ;
3043
@@ -80,7 +93,7 @@ export function ClientCodeBlock(props: ClientBlockProps) {
8093 if ( typeof window !== 'undefined' ) {
8194 setHighlighting ( true ) ;
8295 import ( './highlight' ) . then ( ( { highlight } ) => {
83- highlight ( block , inlines ) . then ( ( lines ) => {
96+ highlight ( block , inlines , { evaluateInlineExpression } ) . then ( ( lines ) => {
8497 if ( cancelled ) {
8598 return ;
8699 }
@@ -98,7 +111,7 @@ export function ClientCodeBlock(props: ClientBlockProps) {
98111
99112 // Otherwise if the block is not in viewport, we reset to the plain lines
100113 setLines ( null ) ;
101- } , [ isInViewport , block , inlines ] ) ;
114+ } , [ isInViewport , block , inlines , evaluateInlineExpression ] ) ;
102115
103116 return (
104117 < CodeBlockRenderer
0 commit comments