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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule};
/// Ensures all rows in a table have the same number of cells
#[derive(Debug)]
pub struct MD056TableColumnCount;
impl MD056TableColumnCount {
/// Check if a line is in a code block
fn is_in_code_block(&self, lines: &[&str], line_index: usize) -> bool {
let mut in_code_block = false;
let mut code_fence = None;
for (_i, line) in lines.iter().enumerate().take(line_index + 1) {
let trimmed = line.trim();
// Check for code fence markers
if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
if !in_code_block {
// Start of a code block
in_code_block = true;
code_fence = Some(if trimmed.starts_with("```") { "```" } else { "~~~" });
} else if let Some(fence) = code_fence {
// End of a code block if the fence type matches
if trimmed.starts_with(fence) {
in_code_block = false;
code_fence = None;
}
}
}
}
in_code_block
}
/// Count cells in a table row
fn count_cells(&self, row: &str) -> usize {
let trimmed = row.trim();
// Skip non-table rows
if !trimmed.contains('|') {
return 0;
}
// Handle case with leading/trailing pipes
let mut cell_count = 0;
let parts: Vec<&str> = trimmed.split('|').collect();
for (i, part) in parts.iter().enumerate() {
// Skip first part if it's empty and there's a leading pipe
if i == 0 && part.trim().is_empty() && parts.len() > 1 {
continue;
}
// Skip last part if it's empty and there's a trailing pipe
if i == parts.len() - 1 && part.trim().is_empty() && parts.len() > 1 {
continue;
}
cell_count += 1;
}
cell_count
}
/// Identify table sections (groups of lines that form a table)
fn identify_tables(&self, lines: &[&str]) -> Vec<(usize, usize)> {
let mut tables = Vec::new();
let mut current_table_start: Option<usize> = None;
for (i, line) in lines.iter().enumerate() {
if self.is_in_code_block(lines, i) {
// If we were tracking a table, end it
if let Some(start) = current_table_start {
if i - start >= 2 { // At least header + delimiter rows
tables.push((start, i - 1));
}
current_table_start = None;
}
continue;
}
let trimmed = line.trim();
let is_table_row = trimmed.contains('|');
// Possible table row
if is_table_row {
if current_table_start.is_none() {
current_table_start = Some(i);
}
} else if current_table_start.is_some() && !is_table_row && !trimmed.is_empty() {
// End of table
if let Some(start) = current_table_start {
if i - start >= 2 { // At least header + delimiter rows
tables.push((start, i - 1));
}
}
current_table_start = None;
}
}
// Handle case where table ends at EOF
if let Some(start) = current_table_start {
if lines.len() - start >= 2 {
tables.push((start, lines.len() - 1));
}
}
tables
}
/// Try to fix a table row to match the expected column count
fn fix_table_row(&self, row: &str, expected_count: usize) -> Option<String> {
let trimmed = row.trim();
let current_count = self.count_cells(trimmed);
if current_count == expected_count || current_count == 0 {
return None;
}
let has_leading_pipe = trimmed.starts_with('|');
let has_trailing_pipe = trimmed.ends_with('|');
let parts: Vec<&str> = trimmed.split('|').collect();
let mut cells = Vec::new();
// Extract actual cell content
for (i, part) in parts.iter().enumerate() {
// Skip empty leading/trailing parts
if (i == 0 && part.trim().is_empty() && has_leading_pipe) ||
(i == parts.len() - 1 && part.trim().is_empty() && has_trailing_pipe) {
continue;
}
cells.push(part.trim());
}
// Too many cells, remove excess
if current_count > expected_count {
cells.truncate(expected_count);
}
// Too few cells, add empty ones
else if current_count < expected_count {
while cells.len() < expected_count {
cells.push("");
}
}
// Reconstruct row
let mut result = String::new();
if has_leading_pipe {
result.push('|');
}
for (i, cell) in cells.iter().enumerate() {
result.push_str(&format!(" {} ", cell));
if i < cells.len() - 1 || has_trailing_pipe {
result.push('|');
}
}
Some(result)
}
}
impl Rule for MD056TableColumnCount {
fn name(&self) -> &'static str {
"MD056"
}
fn description(&self) -> &'static str {
"Table column count should be consistent"
}
fn check(&self, content: &str) -> LintResult {
let mut warnings = Vec::new();
let lines: Vec<&str> = content.lines().collect();
let tables = self.identify_tables(&lines);
for (table_start, table_end) in tables {
// Find first non-empty row to determine expected column count
let mut expected_count = 0;
let mut found_header = false;
for i in table_start..=table_end {
if self.is_in_code_block(&lines, i) {
continue;
}
let count = self.count_cells(lines[i]);
if count > 0 {
if !found_header {
expected_count = count;
found_header = true;
} else if count != expected_count {
let fix_result = self.fix_table_row(lines[i], expected_count);
warnings.push(LintWarning {
line: i + 1,
column: 1,
message: format!(
"Table row has {} cells, but expected {}",
count, expected_count
),
fix: fix_result.map(|fixed_row| Fix {
line: i + 1,
column: 1,
replacement: fixed_row,
}),
});
}
}
}
}
Ok(warnings)
}
fn fix(&self, content: &str) -> Result<String, LintError> {
let mut warnings = self.check(content)?;
if warnings.is_empty() {
return Ok(content.to_string());
}
let lines: Vec<&str> = content.lines().collect();
let mut result = Vec::new();
for (i, line) in lines.iter().enumerate() {
let warning_idx = warnings.iter().position(|w| w.line == i + 1);
if let Some(idx) = warning_idx {
if let Some(fix) = &warnings[idx].fix {
result.push(fix.replacement.clone());
} else {
result.push(line.to_string());
}
warnings.remove(idx);
} else {
result.push(line.to_string());
}
}
// Preserve the original line endings
if content.ends_with('\n') {
Ok(result.join("\n") + "\n")
} else {
Ok(result.join("\n"))
}
}
}