Skip to content

Commit 2d60067

Browse files
fix(MD076): preserve fenced list item spacing (#788)
Co-authored-by: Ruben J. Jongejan <[email protected]>
1 parent ef926b7 commit 2d60067

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎src/rules/md076_list_item_spacing.rs‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Regression test for issue #787: MD031 and MD076 must converge when a list
2+
//! item starts with a fenced code block.
3+
4+
use rumdl_lib::config::{Config, MarkdownFlavor};
5+
use rumdl_lib::fix_coordinator::FixCoordinator;
6+
use rumdl_lib::rules::all_rules;
7+
8+
const REPRO_INPUT: &str = "# Heading\n\n- foo\n- bar\n\n- ```text\n baz\n ```\n";
9+
10+
#[test]
11+
fn md031_md076_converge_for_fenced_list_item() {
12+
let config = Config::default();
13+
let rules = all_rules(&config);
14+
let warnings =
15+
rumdl_lib::lint(REPRO_INPUT, &rules, false, MarkdownFlavor::Standard, None, None).expect("lint must succeed");
16+
let mut fixed = REPRO_INPUT.to_string();
17+
18+
let result = FixCoordinator::new()
19+
.apply_fixes_iterative(&rules, &warnings, &mut fixed, &config, 100, None)
20+
.expect("fix pipeline must succeed");
21+
22+
assert!(result.converged, "fix pipeline reported a cycle: {result:?}");
23+
assert!(
24+
result.conflicting_rules.is_empty(),
25+
"MD031 and MD076 must not conflict: {result:?}"
26+
);
27+
assert!(
28+
fixed.contains("- bar\n\n- ```text"),
29+
"MD031's required blank line before the fenced list item must be preserved:\n{fixed}"
30+
);
31+
}

‎tests/regressions/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod md013_reflow_math_blocks_issue_762_test;
2222
mod md013_reflow_ordered_lookalike_issue_760_test;
2323
mod md013_reflow_span_spacing_issue_767_test;
2424
mod md030_grid_cards_issue_583_test;
25+
mod md031_md076_issue_787_test;
2526
mod md032_edge_cases_test;
2627
mod md032_ordered_list_bug_test;
2728
mod md033_edge_cases_test;

0 commit comments

Comments
 (0)