@@ -11,6 +11,37 @@ interface BlockProps {
1111
1212const voidTypes = [ 'image' ] ;
1313
14+ /**
15+ * Add props that are specific to a block type, and not present in that node type
16+ */
17+ const augmentProps = ( content : Node ) => {
18+ const { children : childrenNodes , type, ...props } = content ;
19+
20+ if ( type === 'code' ) {
21+ // Builds a plain text string from an array of nodes, regardless of links or modifiers
22+ const getPlainText = ( children : typeof childrenNodes ) : string => {
23+ return children . reduce ( ( currentPlainText , node ) => {
24+ if ( node . type === 'text' ) {
25+ return currentPlainText . concat ( node . text ) ;
26+ }
27+
28+ if ( node . type === 'link' ) {
29+ return currentPlainText . concat ( getPlainText ( node . children ) ) ;
30+ }
31+
32+ return currentPlainText ;
33+ } , '' ) ;
34+ } ;
35+
36+ return {
37+ ...props ,
38+ plainText : getPlainText ( content . children ) ,
39+ } ;
40+ }
41+
42+ return props ;
43+ } ;
44+
1445const Block = ( { content } : BlockProps ) => {
1546 const { children : childrenNodes , type, ...props } = content ;
1647
@@ -34,8 +65,10 @@ const Block = ({ content }: BlockProps) => {
3465 return < BlockComponent { ...props } /> ;
3566 }
3667
68+ const augmentedProps = augmentProps ( content ) ;
69+
3770 return (
38- < BlockComponent { ...props } >
71+ < BlockComponent { ...augmentedProps } >
3972 { childrenNodes . map ( ( childNode , index ) => {
4073 if ( childNode . type === 'text' ) {
4174 const { type : _type , ...childNodeProps } = childNode ;
0 commit comments