@@ -6,51 +6,79 @@ import {
66 BlockMapType ,
77 MapPageUrl ,
88 MapImageUrl ,
9- CustomComponents ,
10- BlockValueProp
9+ CustomBlockComponents ,
10+ BlockValueProp ,
11+ CustomDecoratorComponents ,
12+ CustomDecoratorComponentProps
1113} from "./types" ;
1214import Asset from "./components/asset" ;
1315import Code from "./components/code" ;
1416import PageIcon from "./components/page-icon" ;
1517import PageHeader from "./components/page-header" ;
1618import { classNames , getTextContent , getListNumber } from "./utils" ;
1719
18- export const renderChildText = ( properties : DecorationType [ ] ) => {
20+ export const createRenderChildText = (
21+ customDecoratorComponents ?: CustomDecoratorComponents
22+ ) => ( properties : DecorationType [ ] ) => {
1923 return properties ?. map ( ( [ text , decorations ] , i ) => {
2024 if ( ! decorations ) {
2125 return < React . Fragment key = { i } > { text } </ React . Fragment > ;
2226 }
2327
2428 return decorations . reduceRight ( ( element , decorator ) => {
25- switch ( decorator [ 0 ] ) {
26- case "h" :
27- return (
28- < span key = { i } className = { `notion-${ decorator [ 1 ] } ` } >
29- { element }
30- </ span >
31- ) ;
32- case "c" :
33- return (
34- < code key = { i } className = "notion-inline-code" >
35- { element }
36- </ code >
37- ) ;
38- case "b" :
39- return < b key = { i } > { element } </ b > ;
40- case "i" :
41- return < em key = { i } > { element } </ em > ;
42- case "s" :
43- return < s key = { i } > { element } </ s > ;
44- case "a" :
45- return (
46- < a className = "notion-link" href = { decorator [ 1 ] } key = { i } >
47- { element }
48- </ a >
49- ) ;
29+ const renderText = ( ) => {
30+ switch ( decorator [ 0 ] ) {
31+ case "h" :
32+ return (
33+ < span key = { i } className = { `notion-${ decorator [ 1 ] } ` } >
34+ { element }
35+ </ span >
36+ ) ;
37+ case "c" :
38+ return (
39+ < code key = { i } className = "notion-inline-code" >
40+ { element }
41+ </ code >
42+ ) ;
43+ case "b" :
44+ return < b key = { i } > { element } </ b > ;
45+ case "i" :
46+ return < em key = { i } > { element } </ em > ;
47+ case "s" :
48+ return < s key = { i } > { element } </ s > ;
49+ case "a" :
50+ return (
51+ < a className = "notion-link" href = { decorator [ 1 ] } key = { i } >
52+ { element }
53+ </ a >
54+ ) ;
55+
56+ default :
57+ return < React . Fragment key = { i } > { element } </ React . Fragment > ;
58+ }
59+ } ;
60+
61+ const CustomComponent = customDecoratorComponents ?. [ decorator [ 0 ] ] ;
62+
63+ if ( CustomComponent ) {
64+ const props = ( decorator [ 1 ]
65+ ? {
66+ decoratorValue : decorator [ 1 ]
67+ }
68+ : { } ) as CustomDecoratorComponentProps < typeof decorator [ 0 ] > ;
5069
51- default :
52- return < React . Fragment key = { i } > { element } </ React . Fragment > ;
70+ return (
71+ < CustomComponent
72+ key = { i }
73+ { ...( props as any ) }
74+ renderComponent = { renderText }
75+ >
76+ { text }
77+ </ CustomComponent >
78+ ) ;
5379 }
80+
81+ return renderText ( ) ;
5482 } , < > { text } </ > ) ;
5583 } ) ;
5684} ;
@@ -64,7 +92,8 @@ interface Block {
6492
6593 fullPage ?: boolean ;
6694 hideHeader ?: boolean ;
67- customComponents ?: CustomComponents ;
95+ customBlockComponents ?: CustomBlockComponents ;
96+ customDecoratorComponents ?: CustomDecoratorComponents ;
6897}
6998
7099export const Block : React . FC < Block > = props => {
@@ -77,11 +106,14 @@ export const Block: React.FC<Block> = props => {
77106 blockMap,
78107 mapPageUrl,
79108 mapImageUrl,
80- customComponents
109+ customBlockComponents,
110+ customDecoratorComponents
81111 } = props ;
82112 const blockValue = block ?. value ;
83113
84114 const renderComponent = ( ) => {
115+ const renderChildText = createRenderChildText ( customDecoratorComponents ) ;
116+
85117 switch ( blockValue ?. type ) {
86118 case "page" :
87119 if ( level === 0 ) {
@@ -468,8 +500,13 @@ export const Block: React.FC<Block> = props => {
468500 } ;
469501
470502 // render a custom component first if passed.
471- if ( customComponents && customComponents [ blockValue ?. type ] && level !== 0 ) {
472- const CustomComponent = customComponents [ blockValue ?. type ] ! ;
503+ if (
504+ customBlockComponents &&
505+ customBlockComponents [ blockValue ?. type ] &&
506+ // Do not use custom component for base page block
507+ level !== 0
508+ ) {
509+ const CustomComponent = customBlockComponents [ blockValue ?. type ] ! ;
473510 return (
474511 < CustomComponent
475512 renderComponent = { renderComponent }
0 commit comments