@@ -49,7 +49,7 @@ enum GapKind {
4949 Tight ,
5050 /// Blank line that is a genuine inter-item separator.
5151 Loose ,
52- /// Blank line required by another rule (MD031, MD058) after structural content.
52+ /// Blank line required by another rule (MD031, MD058) around structural content.
5353 /// Excluded from consistency analysis — neither loose nor tight.
5454 Structural ,
5555 /// Blank line after continuation content within a list item.
@@ -136,6 +136,29 @@ impl MD076ListItemSpacing {
136136 false
137137 }
138138
139+ /// Check whether a list item opens a fenced code block on its marker line.
140+ ///
141+ /// MD031 requires a blank line before that fence. MD076 must treat the same
142+ /// blank as structural rather than removing it as an inter-item separator.
143+ fn is_fenced_code_block_list_item ( ctx : & LintContext , line_num : usize ) -> bool {
144+ let Some ( info) = ctx. line_info ( line_num) else {
145+ return false ;
146+ } ;
147+ let Some ( item) = info. list_item . as_ref ( ) else {
148+ return false ;
149+ } ;
150+ if !info. in_code_block {
151+ return false ;
152+ }
153+
154+ info. content ( ctx. content )
155+ . get ( item. content_column ..)
156+ . is_some_and ( |content| {
157+ let content = content. trim_start ( ) ;
158+ content. starts_with ( "```" ) || content. starts_with ( "~~~" )
159+ } )
160+ }
161+
139162 /// Check whether a non-blank line is continuation content within a list item
140163 /// (indented prose that is not itself a list marker or structural content).
141164 ///
@@ -185,6 +208,12 @@ impl MD076ListItemSpacing {
185208 if !Self :: is_effectively_blank ( ctx, next - 1 ) {
186209 return GapKind :: Tight ;
187210 }
211+ // A fence opened directly on a list marker is still part of that list
212+ // item. The blank before it belongs to MD031, not to MD076's spacing
213+ // consistency policy, so removing it would create a fix loop.
214+ if Self :: is_fenced_code_block_list_item ( ctx, next) {
215+ return GapKind :: Structural ;
216+ }
188217 // Walk backwards past blank lines to find the last non-blank content line.
189218 // If that line is structural content, the blank is required (not a separator).
190219 let mut scan = next - 1 ;
0 commit comments