@@ -190,10 +190,11 @@ export const improveHtmlAccessibility = (
190190 listItem : HTMLElement ;
191191 contentType : string ;
192192 level : number ;
193+ blockOuterIndex : number ;
193194 }
194195
195196 const listItemsInfo : ListItemInfo [ ] = [ ] ;
196- allBlockOuters . forEach ( ( blockOuter ) => {
197+ allBlockOuters . forEach ( ( blockOuter , index ) => {
197198 const listItem = blockOuter . querySelector < HTMLElement > ( listItemSelector ) ;
198199 if ( listItem ) {
199200 const contentType = listItem . getAttribute ( 'data-content-type' ) ;
@@ -204,6 +205,7 @@ export const improveHtmlAccessibility = (
204205 listItem,
205206 contentType,
206207 level,
208+ blockOuterIndex : index ,
207209 } ) ;
208210 }
209211 }
@@ -218,13 +220,19 @@ export const improveHtmlAccessibility = (
218220 const isBullet = contentType === 'bulletListItem' ;
219221 const listTag = isBullet ? 'ul' : 'ol' ;
220222
221- // Check if previous item continues the same list (same type and level)
223+ // Check if previous item continues the same list (same type, level, and
224+ // no non-list block between them in the DOM : e.g. a heading separates lists).
222225 const previousInfo = idx > 0 ? listItemsInfo [ idx - 1 ] : null ;
226+ const isAdjacentBlock = previousInfo && info . blockOuterIndex === previousInfo . blockOuterIndex + 1 ;
223227 const continuesPreviousList =
224- previousInfo &&
228+ isAdjacentBlock &&
225229 previousInfo . contentType === contentType &&
226230 previousInfo . level === level ;
227231
232+ if ( previousInfo && ! isAdjacentBlock ) {
233+ listStack . length = 0 ;
234+ }
235+
228236 // Find or create the appropriate list
229237 let targetList : HTMLElement | null = null ;
230238
0 commit comments