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
use crate::lint::rule::Rule;
use crate::markdown::MarkdownParser;
use crate::types::Violation;
use serde_json::Value;
pub struct MD049;
impl Rule for MD049 {
fn name(&self) -> &str {
"MD049"
}
fn description(&self) -> &str {
"Emphasis style should be consistent"
}
fn tags(&self) -> &[&str] {
&["emphasis"]
}
fn check(&self, parser: &MarkdownParser, config: Option<&Value>) -> Vec<Violation> {
let style = config
.and_then(|c| c.get("style"))
.and_then(|v| v.as_str())
.unwrap_or("consistent");
let mut violations = Vec::new();
let mut first_style: Option<char> = None;
for (line_num, line) in parser.lines().iter().enumerate() {
let line_number = line_num + 1;
// Look for emphasis patterns: *text* or _text_ (not ** or __)
let chars: Vec<char> = line.chars().collect();
let mut i = 0;
while i < chars.len() {
let ch = chars[i];
// Check for single * or _ (emphasis, not strong)
if (ch == '*' || ch == '_') && i + 1 < chars.len() {
// Make sure it's not strong (** or __)
let is_strong = (i + 1 < chars.len() && chars[i + 1] == ch)
|| (i > 0 && chars[i - 1] == ch);
if !is_strong {
// Find closing marker
for j in (i + 1)..chars.len() {
if chars[j] == ch {
// Make sure closing is also not strong
let close_is_strong = (j + 1 < chars.len() && chars[j + 1] == ch)
|| (j > 0 && chars[j - 1] == ch);
if !close_is_strong {
// Track style and report violations for both opening and closing
if style == "consistent" {
if let Some(first) = first_style {
if ch != first {
// Report violation for opening marker
violations.push(Violation {
line: line_number,
column: Some(i + 1),
rule: self.name().to_string(),
message: format!(
"Emphasis style should be consistent: expected '{}', found '{}'",
first, ch
),
fix: None,
});
// Report violation for closing marker
violations.push(Violation {
line: line_number,
column: Some(j + 1),
rule: self.name().to_string(),
message: format!(
"Emphasis style should be consistent: expected '{}', found '{}'",
first, ch
),
fix: None,
});
}
} else {
first_style = Some(ch);
}
} else {
let expected = if style == "asterisk" { '*' } else { '_' };
if ch != expected {
// Report violation for opening marker
violations.push(Violation {
line: line_number,
column: Some(i + 1),
rule: self.name().to_string(),
message: format!(
"Emphasis style should be '{}', found '{}'",
expected, ch
),
fix: None,
});
// Report violation for closing marker
violations.push(Violation {
line: line_number,
column: Some(j + 1),
rule: self.name().to_string(),
message: format!(
"Emphasis style should be '{}', found '{}'",
expected, ch
),
fix: None,
});
}
}
i = j; // Skip to after closing
break;
}
}
}
}
}
i += 1;
}
}
violations
}
fn fixable(&self) -> bool {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_consistent_asterisk() {
let content = "This is *italic* and *more italic*.";
let parser = MarkdownParser::new(content);
let rule = MD049;
let violations = rule.check(&parser, None);
assert_eq!(violations.len(), 0);
}
#[test]
fn test_consistent_underscore() {
let content = "This is _italic_ and _more italic_.";
let parser = MarkdownParser::new(content);
let rule = MD049;
let violations = rule.check(&parser, None);
assert_eq!(violations.len(), 0);
}
#[test]
fn test_inconsistent() {
let content = "This is *italic* and _also italic_.";
let parser = MarkdownParser::new(content);
let rule = MD049;
let violations = rule.check(&parser, None);
// Reports violation for both opening and closing markers of the second emphasis
assert_eq!(violations.len(), 2);
}
#[test]
fn test_enforced_style() {
let content = "This is _italic_ text.";
let parser = MarkdownParser::new(content);
let rule = MD049;
let config = serde_json::json!({ "style": "asterisk" });
let violations = rule.check(&parser, Some(&config));
// Reports violation for both opening and closing markers
assert_eq!(violations.len(), 2);
}
}