@@ -174,7 +174,7 @@ const joinEmoji = (current: Inlines, previous: Inlines | undefined, next: Inline
174174 }
175175
176176 return {
177- ... current . value ,
177+ type : 'PLAIN_TEXT' ,
178178 value : `:${ current . value . value } :` ,
179179 } ;
180180 }
@@ -183,19 +183,55 @@ const joinEmoji = (current: Inlines, previous: Inlines | undefined, next: Inline
183183} ;
184184
185185export const reducePlainTexts = ( values : Paragraph [ 'value' ] ) : Paragraph [ 'value' ] => {
186- const flattenedValues = values . flat ( ) ;
187186 const result : Paragraph [ 'value' ] = [ ] ;
187+ const flattenableValues = values as Array < Inlines | Inlines [ ] > ;
188188
189- for ( let index = 0 ; index < flattenedValues . length ; index ++ ) {
190- const current = joinEmoji ( flattenedValues [ index ] , flattenedValues [ index - 1 ] , flattenedValues [ index + 1 ] ) ;
189+ let previousInline = undefined as Inlines | undefined ;
190+ let pendingInline = undefined as Inlines | undefined ;
191+
192+ const appendJoinedInline = ( inline : Inlines , nextInline : Inlines | undefined ) : void => {
193+ const current = joinEmoji ( inline , previousInline , nextInline ) ;
191194 const previous = result [ result . length - 1 ] ;
192195
193196 if ( previous && current . type === 'PLAIN_TEXT' && previous . type === 'PLAIN_TEXT' ) {
194197 previous . value += current . value ;
198+ } else {
199+ result . push ( current ) ;
200+ }
201+
202+ previousInline = inline ;
203+ } ;
204+
205+ for ( let index = 0 ; index < flattenableValues . length ; index ++ ) {
206+ const entry = flattenableValues [ index ] ;
207+
208+ if ( Array . isArray ( entry ) ) {
209+ for ( let nestedIndex = 0 ; nestedIndex < entry . length ; nestedIndex ++ ) {
210+ const currentInline = entry [ nestedIndex ] ;
211+
212+ if ( pendingInline === undefined ) {
213+ pendingInline = currentInline ;
214+ continue ;
215+ }
216+
217+ appendJoinedInline ( pendingInline , currentInline ) ;
218+ pendingInline = currentInline ;
219+ }
220+
195221 continue ;
196222 }
197223
198- result . push ( current ) ;
224+ if ( pendingInline === undefined ) {
225+ pendingInline = entry ;
226+ continue ;
227+ }
228+
229+ appendJoinedInline ( pendingInline , entry ) ;
230+ pendingInline = entry ;
231+ }
232+
233+ if ( pendingInline !== undefined ) {
234+ appendJoinedInline ( pendingInline , undefined ) ;
199235 }
200236
201237 return result ;
0 commit comments