@@ -152,8 +152,7 @@ const mdcRemarkNodeHandlers = {
152152 return result
153153 }
154154
155- const isInlineElement = ( parent ?. children || [ ] )
156- . some ( child => child . type === 'text' ) || [ 'p' , 'li' , 'strong' , 'em' , 'span' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] . includes ( parent ?. tagName )
155+ const isInlineElement = isForcedToBeInlineByItsParent ( node , parent )
157156 if ( isInlineElement ) {
158157 return {
159158 type : mdastTextComponentType ,
@@ -287,20 +286,17 @@ const mdcRemarkHandlers: Record<string, (state: State, node: Parents, parent: Pa
287286 meta,
288287 }
289288 } ,
290- 'button' : ( state : State , node : Parents ) => {
291- if (
292- // @ts -expect-error: custom type
293- node . children ?. find ( child => child . type === mdcRemarkElementType )
294- || node . children ?. find ( child => child . type === 'text' && child . value . includes ( '\n' ) )
295- ) {
296- return {
297- type : 'containerComponent' ,
298- name : 'button' ,
299- children : state . all ( node ) ,
300- attributes : node . properties ,
301- }
289+ 'button' : ( state : State , node : Parents , parent : Parents | undefined ) => {
290+ if ( isInlineNode ( node , parent ) ) {
291+ return createTextComponent ( 'button' ) ( state , node )
292+ }
293+
294+ return {
295+ type : 'containerComponent' ,
296+ name : 'button' ,
297+ children : state . all ( node ) ,
298+ attributes : node . properties ,
302299 }
303- return createTextComponent ( 'button' ) ( state , node )
304300 } ,
305301 'span' : createTextComponent ( 'span' ) ,
306302 'kbd' : createTextComponent ( 'kbd' ) ,
@@ -390,3 +386,31 @@ function createTextComponent(name: string) {
390386 return result
391387 }
392388}
389+
390+ function isForcedToBeInlineByItsParent ( node : Parents , parent : Parents | undefined ) {
391+ if ( [ 'p' , 'li' , 'strong' , 'em' , 'span' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] . includes ( parent ?. tagName || '' ) ) {
392+ return true
393+ }
394+
395+ if ( parent ?. children ?. some ( child => child . type === 'text' ) ) {
396+ return true
397+ }
398+
399+ return false
400+ }
401+
402+ function isInlineNode ( node : Parents , parent : Parents | undefined ) {
403+ if (
404+ // @ts -expect-error: custom type
405+ node . children ?. find ( child => child . type === mdcRemarkElementType )
406+ || node . children ?. find ( child => child . type === 'text' && child . value . includes ( '\n' ) )
407+ ) {
408+ return false
409+ }
410+
411+ if ( ! isForcedToBeInlineByItsParent ( node , parent ) ) {
412+ return false
413+ }
414+
415+ return true
416+ }
0 commit comments