1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule};
use crate::rules::blockquote_utils::BlockquoteUtils;
#[derive(Debug, Default)]
pub struct MD028NoBlanksBlockquote;
impl MD028NoBlanksBlockquote {
/// Checks if a line is completely empty (just whitespace)
fn is_completely_empty_line(line: &str) -> bool {
line.trim().is_empty()
}
/// Generates the replacement for a blank blockquote line
fn get_replacement(indent: &str, level: usize) -> String {
let mut result = indent.to_string();
// For nested blockquotes: ">>" or ">" based on level
for _ in 0..level {
result.push('>');
}
// Add a single space after the last '>'
result.push(' ');
result
}
}
impl Rule for MD028NoBlanksBlockquote {
fn name(&self) -> &'static str {
"MD028"
}
fn description(&self) -> &'static str {
"Blank line inside blockquote"
}
fn check(&self, content: &str) -> LintResult {
let mut warnings = Vec::new();
let lines: Vec<&str> = content.lines().collect();
let mut in_blockquote = false;
for (i, &line) in lines.iter().enumerate() {
if Self::is_completely_empty_line(line) {
// A completely empty line separates blockquotes
in_blockquote = false;
continue;
}
if BlockquoteUtils::is_blockquote(line) {
let level = BlockquoteUtils::get_nesting_level(line);
if !in_blockquote {
// Start of a new blockquote
in_blockquote = true;
}
// Check if this is an empty blockquote line
if BlockquoteUtils::is_empty_blockquote(line) {
let indent = BlockquoteUtils::extract_indentation(line);
warnings.push(LintWarning {
message: "Blank line inside blockquote".to_string(),
line: i + 1,
column: 1,
fix: Some(Fix {
line: i + 1,
column: 1,
replacement: Self::get_replacement(&indent, level),
}),
});
}
} else {
// Non-blockquote line
in_blockquote = false;
}
}
Ok(warnings)
}
fn fix(&self, content: &str) -> Result<String, LintError> {
let lines: Vec<&str> = content.lines().collect();
let mut result = Vec::with_capacity(lines.len());
let mut in_blockquote = false;
for line in lines {
if Self::is_completely_empty_line(line) {
// Add empty lines as-is
in_blockquote = false;
result.push(line.to_string());
continue;
}
if BlockquoteUtils::is_blockquote(line) {
let level = BlockquoteUtils::get_nesting_level(line);
if !in_blockquote {
// Start of a new blockquote
in_blockquote = true;
}
// Handle empty blockquote lines
if BlockquoteUtils::is_empty_blockquote(line) {
let indent = BlockquoteUtils::extract_indentation(line);
result.push(Self::get_replacement(&indent, level));
} else {
// Add the line as is
result.push(line.to_string());
}
} else {
// Non-blockquote line
in_blockquote = false;
result.push(line.to_string());
}
}
// Preserve trailing newline if original content had one
Ok(result.join("\n") + if content.ends_with('\n') { "\n" } else { "" })
}
}