@@ -3,75 +3,75 @@ import { LoadPageChunkData, DecorationType } from "./types";
33import Asset from "./components/asset" ;
44import Code from "./components/code" ;
55
6- // import './styles.css';
7-
8- export const applyDecorator = ( properties : DecorationType [ ] ) => {
6+ export const renderChildText = ( properties : DecorationType [ ] ) => {
7+ if ( ! properties ) return null ;
98 return properties . map ( ( item , index ) => {
10- let newItem : JSX . Element = < > { item [ 0 ] } </ > ;
9+ let el : JSX . Element = < > { item [ 0 ] } </ > ;
1110 if ( item . length !== 1 ) {
1211 item [ 1 ] . forEach ( item => {
1312 switch ( item [ 0 ] ) {
1413 case "b" :
15- newItem = < b key = { index } > { newItem } </ b > ;
14+ el = < b key = { index } > { el } </ b > ;
1615 break ;
1716 case "i" :
18- newItem = < em key = { index } > { newItem } </ em > ;
17+ el = < em key = { index } > { el } </ em > ;
1918 break ;
2019 case "s" :
21- newItem = < s key = { index } > { newItem } </ s > ;
20+ el = < s key = { index } > { el } </ s > ;
2221 break ;
2322 case "a" :
24- newItem = (
23+ el = (
2524 < a className = "notion-link" href = { item [ 1 ] } key = { index } >
26- { newItem }
25+ { el }
2726 </ a >
2827 ) ;
2928 break ;
3029 case "c" :
31- newItem = (
30+ el = (
3231 < code className = "notion-inline-code" key = { index } >
33- { newItem }
32+ { el }
3433 </ code >
3534 ) ;
3635 break ;
3736 case "h" :
38- newItem = < span className = { `notion-${ item [ 1 ] } ` } > { newItem } </ span > ;
37+ el = < span className = { `notion-${ item [ 1 ] } ` } > { el } </ span > ;
3938 }
4039 } ) ;
4140 }
42- return newItem ;
41+ return el ;
4342 } ) ;
4443} ;
4544
46- interface BlockRenderer {
45+ interface Block {
4746 block : LoadPageChunkData [ "recordMap" ] [ "block" ] [ "" ] ;
4847 level : number ;
4948}
5049
51- export const BlockRenderer : React . FC < BlockRenderer > = props => {
50+ export const Block : React . FC < Block > = props => {
5251 const { block } = props ;
53- switch ( block . value . type ) {
52+
53+ switch ( block ?. value ?. type ) {
5454 case "page" :
5555 return null ;
5656 case "header" :
5757 if ( ! block . value . properties ) return null ;
5858 return (
5959 < h1 className = "notion-h1" >
60- < > { applyDecorator ( block . value . properties . title ) } </ >
60+ < > { renderChildText ( block . value . properties . title ) } </ >
6161 </ h1 >
6262 ) ;
6363 case "sub_header" :
6464 if ( ! block . value . properties ) return null ;
6565 return (
6666 < h2 className = "notion-h2" >
67- < > { applyDecorator ( block . value . properties . title ) } </ >
67+ < > { renderChildText ( block . value . properties . title ) } </ >
6868 </ h2 >
6969 ) ;
7070 case "sub_sub_header" :
7171 if ( ! block . value . properties ) return null ;
7272 return (
7373 < h3 className = "notion-h3" >
74- < > { applyDecorator ( block . value . properties . title ) } </ >
74+ < > { renderChildText ( block . value . properties . title ) } </ >
7575 </ h3 >
7676 ) ;
7777 case "column_list" :
@@ -80,7 +80,7 @@ export const BlockRenderer: React.FC<BlockRenderer> = props => {
8080 if ( ! block . value . properties ) return null ;
8181 return (
8282 < blockquote className = "notion-quote" >
83- < > { applyDecorator ( block . value . properties . title ) } </ >
83+ < > { renderChildText ( block . value . properties . title ) } </ >
8484 </ blockquote >
8585 ) ;
8686 case "column" :
@@ -93,15 +93,15 @@ export const BlockRenderer: React.FC<BlockRenderer> = props => {
9393 }
9494 return (
9595 < p className = { `notion-text` } >
96- < > { applyDecorator ( block . value . properties . title ) } </ >
96+ < > { renderChildText ( block . value . properties . title ) } </ >
9797 </ p >
9898 ) ;
9999 case "bulleted_list" :
100100 case "numbered_list" :
101101 if ( ! block . value . properties ) return null ;
102102 return (
103103 < li className = { `` } >
104- < > { applyDecorator ( block . value . properties . title ) } </ >
104+ < > { renderChildText ( block . value . properties . title ) } </ >
105105 </ li >
106106 ) ;
107107 case "image" :
@@ -123,19 +123,19 @@ export const BlockRenderer: React.FC<BlockRenderer> = props => {
123123 break ;
124124 }
125125 default :
126- console . log ( "Unsupported type " + block . value . type ) ;
126+ console . log ( "Unsupported type " + block ? .value ? .type ) ;
127127 return < div /> ;
128128 }
129129 return null ;
130130} ;
131131
132- interface ChildRendererProps {
132+ interface ChildProps {
133133 blockMap : LoadPageChunkData [ "recordMap" ] [ "block" ] ;
134134 level : number ;
135135 ids : string [ ] ;
136136}
137137
138- export const ChildRenderer : React . FC < ChildRendererProps > = props => {
138+ export const Child : React . FC < ChildProps > = props => {
139139 const { ids, blockMap } = props ;
140140
141141 let idArray = [ ] ;
@@ -146,23 +146,24 @@ export const ChildRenderer: React.FC<ChildRendererProps> = props => {
146146 const currentId = ids [ i ] ;
147147 if ( ! ( currentId in blockMap ) ) continue ;
148148 const currentBlock = blockMap [ currentId ] ;
149- if ( currentBlock . value . type === "bulleted_list" ) {
149+
150+ if ( currentBlock ?. value . type === "bulleted_list" ) {
150151 bulletArray . push (
151152 < NotionRenderer
152153 level = { props . level + 1 }
153154 currentID = { ids [ i ] }
154155 blockMap = { blockMap }
155156 />
156157 ) ;
157- } else if ( currentBlock . value . type === "numbered_list" ) {
158+ } else if ( currentBlock ? .value . type === "numbered_list" ) {
158159 orderedArray . push (
159160 < NotionRenderer
160161 level = { props . level + 1 }
161162 currentID = { ids [ i ] }
162163 blockMap = { blockMap }
163164 />
164165 ) ;
165- } else if ( currentBlock . value . type === "column_list" ) {
166+ } else if ( currentBlock ? .value . type === "column_list" ) {
166167 idArray . push (
167168 < div className = "notion-row" >
168169 < NotionRenderer
@@ -174,7 +175,7 @@ export const ChildRenderer: React.FC<ChildRendererProps> = props => {
174175 ) ;
175176 } else if ( currentBlock . value . type === "column" ) {
176177 const spacerWith = 46 ;
177- const spacerTotalWith = ( idArray . length - 1 ) * spacerWith ;
178+ const spacerTotalWith = idArray . length * spacerWith ;
178179 const width = `calc((100% - ${ spacerTotalWith } px) * ${ currentBlock . value . format . column_ratio } )` ;
179180 idArray . push (
180181 < >
@@ -243,16 +244,16 @@ export const NotionRenderer: React.FC<NotionProps> = props => {
243244 const currentBlock = props . blockMap [ props . currentID ] ;
244245
245246 const renderChildren = ! (
246- currentBlock . value . type === "page" && props . level > 0
247+ currentBlock ? .value ? .type === "page" && props . level > 0
247248 ) ;
248249
249250 return (
250251 < >
251- < BlockRenderer level = { props . level } block = { currentBlock } />
252- { currentBlock . value . content && renderChildren && (
253- < ChildRenderer
252+ < Block level = { props . level } block = { currentBlock } />
253+ { currentBlock ? .value ? .content && renderChildren && (
254+ < Child
254255 level = { props . level }
255- ids = { currentBlock . value . content }
256+ ids = { currentBlock ? .value ? .content }
256257 blockMap = { props . blockMap }
257258 />
258259 ) }
0 commit comments