@@ -6,6 +6,7 @@ type CalloutTone = 'note' | 'tip' | 'important' | 'warning' | 'caution'
66type BlockquoteProps = ComponentPropsWithoutRef < 'blockquote' >
77
88const CALLOUT_PATTERN = / ^ \s * \[ ! ( n o t e | t i p | i m p o r t a n t | w a r n i n g | c a u t i o n ) \] \s * / i
9+ const CALLOUT_TONES = new Set < CalloutTone > ( [ 'note' , 'tip' , 'important' , 'warning' , 'caution' ] )
910
1011const CALLOUT_LABELS : Record < CalloutTone , string > = {
1112 note : 'Note' ,
@@ -43,42 +44,51 @@ function getMeaningfulChildren(children: ReactNode): ReactNode[] {
4344
4445function stripMarkerFromChildren ( children : ReactNode ) : ReactNode {
4546 const items = getMeaningfulChildren ( children )
47+ const strippedItems : ReactNode [ ] = [ ]
4648
47- return items . map ( async ( item , index ) => {
49+ for ( const [ index , item ] of items . entries ( ) ) {
4850 if ( index !== 0 ) {
49- return item
51+ strippedItems . push ( item )
52+ continue
5053 }
5154
5255 if ( typeof item === 'string' ) {
53- return item . replace ( CALLOUT_PATTERN , '' )
56+ strippedItems . push ( item . replace ( CALLOUT_PATTERN , '' ) )
57+ continue
5458 }
5559
5660 if ( ! isValidElement ( item ) ) {
57- return item
61+ strippedItems . push ( item )
62+ continue
5863 }
5964
6065 const element = item as ReactElement < { children ?: ReactNode } >
6166 const text = extractText ( element . props . children )
6267
6368 if ( ! CALLOUT_PATTERN . test ( text ) ) {
64- return item
69+ strippedItems . push ( item )
70+ continue
6571 }
6672
67- return cloneElement ( element , {
73+ strippedItems . push ( cloneElement ( element , {
6874 ...element . props ,
6975 children : text . replace ( CALLOUT_PATTERN , '' )
70- } )
71- } )
76+ } ) )
77+ }
78+
79+ return strippedItems
80+ }
81+
82+ function isCalloutTone ( value : string | undefined ) : value is CalloutTone {
83+ return value != null && CALLOUT_TONES . has ( value as CalloutTone )
7284}
7385
7486function resolveCalloutTone ( children : ReactNode ) : CalloutTone | null {
7587 const firstChild = getMeaningfulChildren ( children ) [ 0 ]
7688 const firstText = extractText ( firstChild ) . trimStart ( )
7789 const matched = CALLOUT_PATTERN . exec ( firstText ) ?. [ 1 ] ?. toLowerCase ( )
7890
79- if (
80- new Set ( [ 'note' , 'tip' , 'important' , 'warning' , 'caution' ] ) . has ( matched )
81- ) {
91+ if ( isCalloutTone ( matched ) ) {
8292 return matched
8393 }
8494
0 commit comments