1use crate::filtered_lines::FilteredLinesExt;
2use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
3use crate::rules::emphasis_style::EmphasisStyle;
4use crate::utils::emphasis_utils::{find_emphasis_markers, find_single_emphasis_spans, replace_inline_code};
5use crate::utils::skip_context::is_in_mkdocs_markup;
6
7mod md049_config;
8use md049_config::MD049Config;
9
10#[derive(Debug, Default, Clone)]
20pub struct MD049EmphasisStyle {
21 config: MD049Config,
22}
23
24impl MD049EmphasisStyle {
25 pub fn new(style: EmphasisStyle) -> Self {
27 MD049EmphasisStyle {
28 config: MD049Config { style },
29 }
30 }
31
32 pub fn from_config_struct(config: MD049Config) -> Self {
33 Self { config }
34 }
35
36 fn is_in_link(ctx: &crate::lint_context::LintContext, byte_pos: usize) -> bool {
39 ctx.is_in_link(byte_pos)
40 }
41
42 fn collect_emphasis_from_line(
44 &self,
45 line: &str,
46 line_num: usize,
47 line_start_pos: usize,
48 emphasis_info: &mut Vec<(usize, usize, usize, char, String)>, ) {
50 let line_no_code = replace_inline_code(line);
52
53 let markers = find_emphasis_markers(&line_no_code);
55 if markers.is_empty() {
56 return;
57 }
58
59 let spans = find_single_emphasis_spans(&line_no_code, &markers);
61
62 for span in spans {
63 let marker_char = span.opening.as_char();
64 let col = span.opening.start_pos + 1; let abs_pos = line_start_pos + span.opening.start_pos;
66
67 emphasis_info.push((line_num, col, abs_pos, marker_char, span.content.clone()));
68 }
69 }
70}
71
72impl Rule for MD049EmphasisStyle {
73 fn name(&self) -> &'static str {
74 "MD049"
75 }
76
77 fn description(&self) -> &'static str {
78 "Emphasis style should be consistent"
79 }
80
81 fn category(&self) -> RuleCategory {
82 RuleCategory::Emphasis
83 }
84
85 fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
86 let mut warnings = vec![];
87
88 if !ctx.likely_has_emphasis() {
90 return Ok(warnings);
91 }
92
93 let line_index = &ctx.line_index;
96
97 let mut emphasis_info = vec![];
99
100 for line in ctx
104 .filtered_lines()
105 .skip_front_matter()
106 .skip_code_blocks()
107 .skip_html_comments()
108 .skip_jsx_expressions()
109 .skip_mdx_comments()
110 .skip_math_blocks()
111 .skip_obsidian_comments()
112 .skip_mkdocstrings()
113 {
114 if !line.content.contains('*') && !line.content.contains('_') {
116 continue;
117 }
118
119 let line_start = line_index.get_line_start_byte(line.line_num).unwrap_or(0);
121 self.collect_emphasis_from_line(line.content, line.line_num, line_start, &mut emphasis_info);
122 }
123
124 let lines = ctx.raw_lines();
126 let math_ranges: Vec<(usize, usize)> = {
144 let code_spans = ctx.code_spans();
150 let math_source: std::borrow::Cow<'_, str> = if ctx.code_blocks.is_empty() && code_spans.is_empty() {
151 std::borrow::Cow::Borrowed(ctx.content)
152 } else {
153 let mut bytes = ctx.content.as_bytes().to_vec();
154 let len = bytes.len();
155 let mut mask = |start: usize, end: usize| {
156 for b in &mut bytes[start.min(len)..end.min(len)] {
157 if *b == b'$' {
158 *b = b' ';
159 }
160 }
161 };
162 for &(start, end) in &ctx.code_blocks {
163 mask(start, end);
164 }
165 for span in code_spans.iter() {
166 mask(span.byte_offset, span.byte_end);
167 }
168 std::borrow::Cow::Owned(String::from_utf8(bytes).expect("ASCII-only substitution"))
171 };
172 let mut r = crate::utils::skip_context::math_byte_ranges(&math_source);
173 r.sort_unstable_by_key(|&(start, _)| start);
174 let mut merged: Vec<(usize, usize)> = Vec::with_capacity(r.len());
175 for (start, end) in r {
176 match merged.last_mut() {
177 Some(last) if start <= last.1 => last.1 = last.1.max(end),
178 _ => merged.push((start, end)),
179 }
180 }
181 merged
182 };
183 emphasis_info.retain(|(line_num, col, abs_pos, _, _)| {
184 let idx = math_ranges.partition_point(|&(start, _)| start <= *abs_pos);
188 if idx > 0 && *abs_pos < math_ranges[idx - 1].1 {
189 return false;
190 }
191 if ctx.is_in_obsidian_comment(*abs_pos) {
193 return false;
194 }
195 if Self::is_in_link(ctx, *abs_pos) {
197 return false;
198 }
199 if let Some(line) = lines.get(*line_num - 1) {
201 let line_pos = col.saturating_sub(1); if is_in_mkdocs_markup(line, line_pos, ctx.flavor) {
203 return false;
204 }
205 }
206 true
207 });
208
209 match self.config.style {
210 EmphasisStyle::Consistent => {
211 if emphasis_info.len() < 2 {
213 return Ok(warnings);
214 }
215
216 let asterisk_count = emphasis_info.iter().filter(|(_, _, _, m, _)| *m == '*').count();
218 let underscore_count = emphasis_info.iter().filter(|(_, _, _, m, _)| *m == '_').count();
219
220 let target_marker = if asterisk_count >= underscore_count { '*' } else { '_' };
223
224 for (line_num, col, abs_pos, marker, content) in &emphasis_info {
226 if *marker != target_marker {
227 let emphasis_len = 1 + content.len() + 1;
229
230 warnings.push(LintWarning {
231 rule_name: Some(self.name().to_string()),
232 line: *line_num,
233 column: *col,
234 end_line: *line_num,
235 end_column: col + emphasis_len,
236 message: format!("Emphasis should use {target_marker} instead of {marker}"),
237 fix: Some(Fix::new(
238 *abs_pos..*abs_pos + emphasis_len,
239 format!("{target_marker}{content}{target_marker}"),
240 )),
241 severity: Severity::Warning,
242 });
243 }
244 }
245 }
246 EmphasisStyle::Asterisk | EmphasisStyle::Underscore => {
247 let (wrong_marker, correct_marker) = match self.config.style {
248 EmphasisStyle::Asterisk => ('_', '*'),
249 EmphasisStyle::Underscore => ('*', '_'),
250 EmphasisStyle::Consistent => {
251 ('_', '*')
254 }
255 };
256
257 for (line_num, col, abs_pos, marker, content) in &emphasis_info {
258 if *marker == wrong_marker {
259 let emphasis_len = 1 + content.len() + 1;
261
262 warnings.push(LintWarning {
263 rule_name: Some(self.name().to_string()),
264 line: *line_num,
265 column: *col,
266 end_line: *line_num,
267 end_column: col + emphasis_len,
268 message: format!("Emphasis should use {correct_marker} instead of {wrong_marker}"),
269 fix: Some(Fix::new(
270 *abs_pos..*abs_pos + emphasis_len,
271 format!("{correct_marker}{content}{correct_marker}"),
272 )),
273 severity: Severity::Warning,
274 });
275 }
276 }
277 }
278 }
279 Ok(warnings)
280 }
281
282 fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
283 let warnings = self.check(ctx)?;
285 let warnings =
286 crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
287
288 if warnings.is_empty() {
290 return Ok(ctx.content.to_string());
291 }
292
293 let mut fixes: Vec<_> = warnings
295 .iter()
296 .filter_map(|w| w.fix.as_ref().map(|f| (f.range.start, f.range.end, &f.replacement)))
297 .collect();
298 fixes.sort_by(|a, b| b.0.cmp(&a.0));
299
300 let mut result = ctx.content.to_string();
302 for (start, end, replacement) in fixes {
303 if start < result.len() && end <= result.len() && start <= end {
304 result.replace_range(start..end, replacement);
305 }
306 }
307
308 Ok(result)
309 }
310
311 fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
313 ctx.content.is_empty() || !ctx.likely_has_emphasis()
314 }
315
316 fn as_any(&self) -> &dyn std::any::Any {
317 self
318 }
319
320 fn default_config_section(&self) -> Option<(String, toml::Value)> {
321 let json_value = serde_json::to_value(&self.config).ok()?;
322 Some((
323 self.name().to_string(),
324 crate::rule_config_serde::json_to_toml_value(&json_value)?,
325 ))
326 }
327
328 fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
329 where
330 Self: Sized,
331 {
332 let rule_config = crate::rule_config_serde::load_rule_config::<MD049Config>(config);
333 Box::new(Self::from_config_struct(rule_config))
334 }
335}
336
337#[cfg(test)]
338mod tests {
339 use super::*;
340
341 #[test]
342 fn test_name() {
343 let rule = MD049EmphasisStyle::default();
344 assert_eq!(rule.name(), "MD049");
345 }
346
347 #[test]
348 fn test_style_from_str() {
349 assert_eq!(EmphasisStyle::from("asterisk"), EmphasisStyle::Asterisk);
350 assert_eq!(EmphasisStyle::from("underscore"), EmphasisStyle::Underscore);
351 assert_eq!(EmphasisStyle::from("other"), EmphasisStyle::Consistent);
352 }
353
354 #[test]
355 fn test_emphasis_in_links_not_flagged() {
356 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
357 let content = r#"Check this [*asterisk*](https://example.com/*pattern*) link and [_underscore_](https://example.com/_private_).
358
359Also see the [`__init__`][__init__] reference.
360
361This should be _flagged_ since we're using asterisk style.
362
363[__init__]: https://example.com/__init__.py"#;
364 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
365 let result = rule.check(&ctx).unwrap();
366
367 assert_eq!(result.len(), 1);
369 assert!(result[0].message.contains("Emphasis should use * instead of _"));
370 assert!(result[0].line == 5); }
373
374 #[test]
375 fn test_emphasis_in_links_vs_outside_links() {
376 let rule = MD049EmphasisStyle::new(EmphasisStyle::Underscore);
377 let content = r#"Check [*emphasis*](https://example.com/*test*) and inline *real emphasis* text.
378
379[*link*]: https://example.com/*path*"#;
380 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
381 let result = rule.check(&ctx).unwrap();
382
383 assert_eq!(result.len(), 1);
385 assert!(result[0].message.contains("Emphasis should use _ instead of *"));
386 assert!(result[0].line == 1);
388 }
389
390 #[test]
391 fn test_mkdocs_keys_notation_not_flagged() {
392 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
394 let content = "Press ++ctrl+alt+del++ to restart.";
395 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
396 let result = rule.check(&ctx).unwrap();
397
398 assert!(
400 result.is_empty(),
401 "Keys notation should not be flagged as emphasis. Got: {result:?}"
402 );
403 }
404
405 #[test]
406 fn test_mkdocs_caret_notation_not_flagged() {
407 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
409 let content = "This is ^superscript^ and ^^inserted^^ text.";
410 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
411 let result = rule.check(&ctx).unwrap();
412
413 assert!(
414 result.is_empty(),
415 "Caret notation should not be flagged as emphasis. Got: {result:?}"
416 );
417 }
418
419 #[test]
420 fn test_mkdocs_mark_notation_not_flagged() {
421 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
423 let content = "This is ==highlighted== text.";
424 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
425 let result = rule.check(&ctx).unwrap();
426
427 assert!(
428 result.is_empty(),
429 "Mark notation should not be flagged as emphasis. Got: {result:?}"
430 );
431 }
432
433 #[test]
434 fn test_mkdocs_mixed_content_with_real_emphasis() {
435 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
437 let content = "Press ++ctrl++ and _underscore emphasis_ here.";
438 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
439 let result = rule.check(&ctx).unwrap();
440
441 assert_eq!(result.len(), 1, "Expected 1 warning, got: {result:?}");
443 assert!(result[0].message.contains("Emphasis should use * instead of _"));
444 }
445
446 #[test]
447 fn test_mkdocs_icon_shortcode_not_flagged() {
448 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
450 let content = "Click :material-check: and _this should be flagged_.";
451 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
452 let result = rule.check(&ctx).unwrap();
453
454 assert_eq!(result.len(), 1);
456 assert!(result[0].message.contains("Emphasis should use * instead of _"));
457 }
458
459 #[test]
460 fn test_mkdocstrings_block_not_flagged() {
461 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
462 let content = "# Example\n\n::: my_module.MyClass\n options:\n members:\n - _private_method\n";
463 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
464 let result = rule.check(&ctx).unwrap();
465
466 assert!(
467 result.is_empty(),
468 "_private_method_ inside mkdocstrings block should not be flagged. Got: {result:?}"
469 );
470 }
471
472 #[test]
473 fn test_mkdocstrings_block_with_emphasis_outside() {
474 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
475 let content = "::: my_module.MyClass\n options:\n members:\n - _init\n\nThis _should be flagged_ outside.\n";
476 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
477 let result = rule.check(&ctx).unwrap();
478
479 assert_eq!(
480 result.len(),
481 1,
482 "Only emphasis outside mkdocstrings should be flagged. Got: {result:?}"
483 );
484 assert_eq!(result[0].line, 6);
485 }
486
487 #[test]
488 fn test_obsidian_inline_comment_emphasis_ignored() {
489 let rule = MD049EmphasisStyle::new(EmphasisStyle::Asterisk);
491 let content = "Visible %%_hidden_%% text.";
492 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
493 let result = rule.check(&ctx).unwrap();
494
495 assert!(
496 result.is_empty(),
497 "Should ignore emphasis inside Obsidian comments. Got: {result:?}"
498 );
499 }
500}