11import { getBlockTitle } from "notion-utils" ;
22import React , { useEffect , useState } from "react" ;
3- import { cs , useNotionContext } from "react-notion-x" ;
3+ import { cs , useNotionContext , Text } from "react-notion-x" ;
44import { codeToHtml } from "shiki" ;
55import { BundledTheme } from "shiki/themes" ;
66
@@ -12,6 +12,7 @@ export const Code: React.FC<{
1212 block : CodeBlock ;
1313 defaultLanguage ?: string ;
1414 className ?: string ;
15+ captionClassName ?: string ;
1516 themes ?: {
1617 light : BundledTheme ;
1718 dark : BundledTheme ;
@@ -23,16 +24,17 @@ export const Code: React.FC<{
2324 light : "catppuccin-latte" ,
2425 dark : "dracula"
2526 } ,
26- className
27+ className,
28+ captionClassName
2729} ) => {
2830 const { recordMap } = useNotionContext ( ) ;
2931 const content = getBlockTitle ( block , recordMap ) ;
30-
3132 const [ code , setCode ] = useState < string | undefined > ( undefined ) ;
3233
3334 const language = (
3435 block . properties ?. language ?. [ 0 ] ?. [ 0 ] || defaultLanguage
3536 ) . toLowerCase ( ) ;
37+ const caption = block . properties . caption ;
3638
3739 useEffect ( ( ) => {
3840 async function renderCodeToHtml ( ) {
@@ -45,14 +47,18 @@ export const Code: React.FC<{
4547 content && renderCodeToHtml ( ) ;
4648 } , [ content , language , themes ] ) ;
4749
48- return code ? (
49- < div
50- className = { cs ( styles . codeBlock , className ) }
51- dangerouslySetInnerHTML = { { __html : code } }
52- />
53- ) : (
54- < div className = { cs ( styles . codeBlock , className ) } >
55- < pre > { content } </ pre >
56- </ div >
50+ return (
51+ < figure className = { cs ( styles . codeBlock , className ) } >
52+ { code == undefined ? (
53+ < pre > { content } </ pre >
54+ ) : (
55+ < div dangerouslySetInnerHTML = { { __html : code } } />
56+ ) }
57+ { caption && (
58+ < figcaption className = { cs ( styles . codeCaption , captionClassName ) } >
59+ < Text value = { caption } block = { block } />
60+ </ figcaption >
61+ ) }
62+ </ figure >
5763 ) ;
5864} ;
0 commit comments