1use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, Severity};
5use crate::utils::range_utils::calculate_match_range;
6use lazy_static::lazy_static;
7use regex::Regex;
8use std::collections::HashMap;
9use std::ops::Range;
10use std::sync::RwLock;
11
12mod md026_config;
13use md026_config::{DEFAULT_PUNCTUATION, MD026Config};
14
15lazy_static! {
16 static ref ATX_HEADING_UNIFIED: Regex = Regex::new(r"^( {0,3})(#{1,6})(\s+)(.+?)(\s+#{1,6})?$").unwrap();
18
19 static ref QUICK_PUNCTUATION_CHECK: Regex = Regex::new(&format!(r"[{}]", regex::escape(DEFAULT_PUNCTUATION))).unwrap();
21
22 static ref PUNCTUATION_REGEX_CACHE: RwLock<HashMap<String, Regex>> = RwLock::new(HashMap::new());
24}
25
26#[derive(Clone, Default)]
28pub struct MD026NoTrailingPunctuation {
29 config: MD026Config,
30}
31
32impl MD026NoTrailingPunctuation {
33 pub fn new(punctuation: Option<String>) -> Self {
34 Self {
35 config: MD026Config {
36 punctuation: punctuation.unwrap_or_else(|| DEFAULT_PUNCTUATION.to_string()),
37 },
38 }
39 }
40
41 pub fn from_config_struct(config: MD026Config) -> Self {
42 Self { config }
43 }
44
45 #[inline]
46 fn get_punctuation_regex(&self) -> Result<Regex, regex::Error> {
47 {
49 let cache = PUNCTUATION_REGEX_CACHE.read().unwrap();
50 if let Some(cached_regex) = cache.get(&self.config.punctuation) {
51 return Ok(cached_regex.clone());
52 }
53 }
54
55 let pattern = format!(r"([{}]+)$", regex::escape(&self.config.punctuation));
57 let regex = Regex::new(&pattern)?;
58
59 {
60 let mut cache = PUNCTUATION_REGEX_CACHE.write().unwrap();
61 cache.insert(self.config.punctuation.clone(), regex.clone());
62 }
63
64 Ok(regex)
65 }
66
67 #[inline]
68 fn has_trailing_punctuation(&self, text: &str, re: &Regex) -> bool {
69 let trimmed = text.trim();
70 re.is_match(trimmed)
71 }
72
73 #[inline]
74 fn get_line_byte_range(&self, content: &str, line_num: usize) -> Range<usize> {
75 let mut start_pos = 0;
76
77 for (idx, line) in content.lines().enumerate() {
78 if idx + 1 == line_num {
79 return Range {
80 start: start_pos,
81 end: start_pos + line.len(),
82 };
83 }
84 start_pos += line.len() + 1;
86 }
87
88 Range {
89 start: content.len(),
90 end: content.len(),
91 }
92 }
93
94 #[inline]
96 fn remove_trailing_punctuation(&self, text: &str, re: &Regex) -> String {
97 re.replace_all(text.trim(), "").to_string()
98 }
99
100 #[inline]
102 fn fix_atx_heading(&self, line: &str, re: &Regex) -> String {
103 if let Some(captures) = ATX_HEADING_UNIFIED.captures(line) {
104 let indentation = captures.get(1).unwrap().as_str();
105 let hashes = captures.get(2).unwrap().as_str();
106 let space = captures.get(3).unwrap().as_str();
107 let content = captures.get(4).unwrap().as_str();
108
109 let fixed_content = if let Some(id_pos) = content.rfind(" {#") {
112 let before_id = &content[..id_pos];
114 let id_part = &content[id_pos..];
115 let fixed_before = self.remove_trailing_punctuation(before_id, re);
116 format!("{fixed_before}{id_part}")
117 } else {
118 self.remove_trailing_punctuation(content, re)
120 };
121
122 if let Some(trailing) = captures.get(5) {
124 return format!(
125 "{}{}{}{}{}",
126 indentation,
127 hashes,
128 space,
129 fixed_content,
130 trailing.as_str()
131 );
132 }
133
134 return format!("{indentation}{hashes}{space}{fixed_content}");
135 }
136
137 line.to_string()
139 }
140
141 #[inline]
143 fn fix_setext_heading(&self, content_line: &str, re: &Regex) -> String {
144 let trimmed = content_line.trim_end();
145 let mut whitespace = "";
146
147 if content_line.len() > trimmed.len() {
149 whitespace = &content_line[trimmed.len()..];
150 }
151
152 format!("{}{}", self.remove_trailing_punctuation(trimmed, re), whitespace)
154 }
155}
156
157impl Rule for MD026NoTrailingPunctuation {
158 fn name(&self) -> &'static str {
159 "MD026"
160 }
161
162 fn description(&self) -> &'static str {
163 "Trailing punctuation in heading"
164 }
165
166 fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
167 if !ctx.content.contains('#') {
169 return true;
170 }
171 let punctuation = &self.config.punctuation;
173 !punctuation.chars().any(|p| ctx.content.contains(p))
174 }
175
176 fn as_maybe_document_structure(&self) -> Option<&dyn crate::rule::MaybeDocumentStructure> {
177 None
178 }
179
180 fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
181 let content = ctx.content;
182
183 if content.is_empty() {
185 return Ok(Vec::new());
186 }
187
188 if self.config.punctuation == DEFAULT_PUNCTUATION {
191 if !QUICK_PUNCTUATION_CHECK.is_match(content) {
192 return Ok(Vec::new());
193 }
194 } else {
195 let has_custom_punctuation = self.config.punctuation.chars().any(|c| content.contains(c));
197 if !has_custom_punctuation {
198 return Ok(Vec::new());
199 }
200 }
201
202 let has_headings = ctx.lines.iter().any(|line| line.heading.is_some());
204 if !has_headings {
205 return Ok(Vec::new());
206 }
207
208 let mut warnings = Vec::new();
209 let re = match self.get_punctuation_regex() {
210 Ok(regex) => regex,
211 Err(_) => return Ok(warnings),
212 };
213
214 for (line_num, line_info) in ctx.lines.iter().enumerate() {
216 if let Some(heading) = &line_info.heading {
217 if line_info.indent >= 4 && matches!(heading.style, crate::lint_context::HeadingStyle::ATX) {
219 continue;
220 }
221
222 let text_to_check = heading.text.clone();
226
227 if self.has_trailing_punctuation(&text_to_check, &re) {
228 if let Some(punctuation_match) = re.find(&text_to_check) {
230 let line = &line_info.content;
231
232 let punctuation_pos_in_text = punctuation_match.start();
234 let text_pos_in_line = line.find(&heading.text).unwrap_or(heading.content_column);
235 let punctuation_start_in_line = text_pos_in_line + punctuation_pos_in_text;
236 let punctuation_len = punctuation_match.len();
237
238 let (start_line, start_col, end_line, end_col) = calculate_match_range(
239 line_num + 1, line,
241 punctuation_start_in_line,
242 punctuation_len,
243 );
244
245 let last_char = text_to_check.chars().last().unwrap_or(' ');
246 warnings.push(LintWarning {
247 rule_name: Some(self.name()),
248 line: start_line,
249 column: start_col,
250 end_line,
251 end_column: end_col,
252 message: format!("Heading '{text_to_check}' ends with punctuation '{last_char}'"),
253 severity: Severity::Warning,
254 fix: Some(Fix {
255 range: self.get_line_byte_range(content, line_num + 1),
256 replacement: if matches!(heading.style, crate::lint_context::HeadingStyle::ATX) {
257 self.fix_atx_heading(line, &re)
258 } else {
259 self.fix_setext_heading(line, &re)
260 },
261 }),
262 });
263 }
264 }
265 }
266 }
267
268 Ok(warnings)
269 }
270
271 fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
272 let content = ctx.content;
273
274 if content.is_empty() {
276 return Ok(content.to_string());
277 }
278
279 if self.config.punctuation == DEFAULT_PUNCTUATION {
282 if !QUICK_PUNCTUATION_CHECK.is_match(content) {
283 return Ok(content.to_string());
284 }
285 } else {
286 let has_custom_punctuation = self.config.punctuation.chars().any(|c| content.contains(c));
288 if !has_custom_punctuation {
289 return Ok(content.to_string());
290 }
291 }
292
293 let has_headings = ctx.lines.iter().any(|line| line.heading.is_some());
295 if !has_headings {
296 return Ok(content.to_string());
297 }
298
299 let re = match self.get_punctuation_regex() {
300 Ok(regex) => regex,
301 Err(_) => return Ok(content.to_string()),
302 };
303
304 let lines: Vec<&str> = content.lines().collect();
305 let mut fixed_lines: Vec<String> = lines.iter().map(|&s| s.to_string()).collect();
306
307 for (line_num, line_info) in ctx.lines.iter().enumerate() {
309 if let Some(heading) = &line_info.heading {
310 if line_info.indent >= 4 && matches!(heading.style, crate::lint_context::HeadingStyle::ATX) {
312 continue;
313 }
314
315 let text_to_check = heading.text.clone();
318
319 if self.has_trailing_punctuation(&text_to_check, &re) {
321 fixed_lines[line_num] = if matches!(heading.style, crate::lint_context::HeadingStyle::ATX) {
322 self.fix_atx_heading(&line_info.content, &re)
323 } else {
324 self.fix_setext_heading(&line_info.content, &re)
325 };
326 }
327 }
328 }
329
330 let mut result = String::with_capacity(content.len());
332 for (i, line) in fixed_lines.iter().enumerate() {
333 result.push_str(line);
334 if i < fixed_lines.len() - 1 || content.ends_with('\n') {
335 result.push('\n');
336 }
337 }
338
339 Ok(result)
340 }
341
342 fn as_any(&self) -> &dyn std::any::Any {
343 self
344 }
345
346 fn default_config_section(&self) -> Option<(String, toml::Value)> {
347 let json_value = serde_json::to_value(&self.config).ok()?;
348 Some((
349 self.name().to_string(),
350 crate::rule_config_serde::json_to_toml_value(&json_value)?,
351 ))
352 }
353
354 fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
355 where
356 Self: Sized,
357 {
358 let rule_config = crate::rule_config_serde::load_rule_config::<MD026Config>(config);
359 Box::new(Self::from_config_struct(rule_config))
360 }
361}
362
363#[cfg(test)]
364mod tests {
365 use super::*;
366 use crate::lint_context::LintContext;
367
368 #[test]
369 fn test_no_trailing_punctuation() {
370 let rule = MD026NoTrailingPunctuation::new(None);
371 let content = "# This is a heading\n\n## Another heading";
372 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
373 let result = rule.check(&ctx).unwrap();
374 assert!(result.is_empty(), "Headings without punctuation should not be flagged");
375 }
376
377 #[test]
378 fn test_trailing_period() {
379 let rule = MD026NoTrailingPunctuation::new(None);
380 let content = "# This is a heading.\n\n## Another one.";
381 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
382 let result = rule.check(&ctx).unwrap();
383 assert_eq!(result.len(), 2);
384 assert_eq!(result[0].line, 1);
385 assert_eq!(result[0].column, 20);
386 assert!(result[0].message.contains("ends with punctuation '.'"));
387 assert_eq!(result[1].line, 3);
388 assert_eq!(result[1].column, 15);
389 }
390
391 #[test]
392 fn test_trailing_comma() {
393 let rule = MD026NoTrailingPunctuation::new(None);
394 let content = "# Heading,\n## Sub-heading,";
395 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
396 let result = rule.check(&ctx).unwrap();
397 assert_eq!(result.len(), 2);
398 assert!(result[0].message.contains("ends with punctuation ','"));
399 }
400
401 #[test]
402 fn test_trailing_semicolon() {
403 let rule = MD026NoTrailingPunctuation::new(None);
404 let content = "# Title;\n## Subtitle;";
405 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
406 let result = rule.check(&ctx).unwrap();
407 assert_eq!(result.len(), 2);
408 assert!(result[0].message.contains("ends with punctuation ';'"));
409 }
410
411 #[test]
412 fn test_custom_punctuation() {
413 let rule = MD026NoTrailingPunctuation::new(Some("!".to_string()));
414 let content = "# Important!\n## Regular heading.";
415 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
416 let result = rule.check(&ctx).unwrap();
417 assert_eq!(result.len(), 1, "Only exclamation should be flagged with custom config");
418 assert_eq!(result[0].line, 1);
419 assert!(result[0].message.contains("ends with punctuation '!'"));
420 }
421
422 #[test]
423 fn test_legitimate_question_mark() {
424 let rule = MD026NoTrailingPunctuation::new(Some(".,;?".to_string()));
425 let content = "# What is this?\n# This is bad.";
426 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
427 let result = rule.check(&ctx).unwrap();
428 assert_eq!(result.len(), 2, "Both should be flagged with custom punctuation");
430 }
431
432 #[test]
433 fn test_question_marks_not_in_default() {
434 let rule = MD026NoTrailingPunctuation::new(None);
435 let content = "# What is Rust?\n# How does it work?\n# Is it fast?";
436 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
437 let result = rule.check(&ctx).unwrap();
438 assert!(result.is_empty(), "Question marks are not in default punctuation list");
439 }
440
441 #[test]
442 fn test_colons_in_default() {
443 let rule = MD026NoTrailingPunctuation::new(None);
444 let content = "# FAQ:\n# API Reference:\n# Step 1:\n# Version 2.0:";
445 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
446 let result = rule.check(&ctx).unwrap();
447 assert_eq!(
448 result.len(),
449 4,
450 "Colons are in default punctuation list and should be flagged"
451 );
452 }
453
454 #[test]
455 fn test_fix_atx_headings() {
456 let rule = MD026NoTrailingPunctuation::new(None);
457 let content = "# Title.\n## Subtitle,\n### Sub-subtitle;";
458 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
459 let fixed = rule.fix(&ctx).unwrap();
460 assert_eq!(fixed, "# Title\n## Subtitle\n### Sub-subtitle");
461 }
462
463 #[test]
464 fn test_fix_setext_headings() {
465 let rule = MD026NoTrailingPunctuation::new(None);
466 let content = "Title.\n======\n\nSubtitle,\n---------";
467 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
468 let fixed = rule.fix(&ctx).unwrap();
469 assert_eq!(fixed, "Title\n======\n\nSubtitle\n---------");
470 }
471
472 #[test]
473 fn test_fix_preserves_trailing_hashes() {
474 let rule = MD026NoTrailingPunctuation::new(None);
475 let content = "# Title. #\n## Subtitle, ##";
476 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
477 let fixed = rule.fix(&ctx).unwrap();
478 assert_eq!(fixed, "# Title #\n## Subtitle ##");
479 }
480
481 #[test]
482 fn test_indented_headings() {
483 let rule = MD026NoTrailingPunctuation::new(None);
484 let content = " # Title.\n ## Subtitle.";
485 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
486 let result = rule.check(&ctx).unwrap();
487 assert_eq!(result.len(), 2, "Indented headings (< 4 spaces) should be checked");
488 }
489
490 #[test]
491 fn test_deeply_indented_ignored() {
492 let rule = MD026NoTrailingPunctuation::new(None);
493 let content = " # This is code.";
494 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
495 let result = rule.check(&ctx).unwrap();
496 assert!(result.is_empty(), "Deeply indented lines (4+ spaces) should be ignored");
497 }
498
499 #[test]
500 fn test_multiple_punctuation() {
501 let rule = MD026NoTrailingPunctuation::new(None);
502 let content = "# Title...";
503 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
504 let result = rule.check(&ctx).unwrap();
505 assert_eq!(result.len(), 1);
506 assert_eq!(result[0].column, 8); }
508
509 #[test]
510 fn test_empty_content() {
511 let rule = MD026NoTrailingPunctuation::new(None);
512 let content = "";
513 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
514 let result = rule.check(&ctx).unwrap();
515 assert!(result.is_empty());
516 }
517
518 #[test]
519 fn test_no_headings() {
520 let rule = MD026NoTrailingPunctuation::new(None);
521 let content = "This is just text.\nMore text with punctuation.";
522 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
523 let result = rule.check(&ctx).unwrap();
524 assert!(result.is_empty(), "Non-heading lines should not be checked");
525 }
526
527 #[test]
528 fn test_get_punctuation_regex() {
529 let rule = MD026NoTrailingPunctuation::new(Some("!?".to_string()));
530 let regex = rule.get_punctuation_regex().unwrap();
531 assert!(regex.is_match("text!"));
532 assert!(regex.is_match("text?"));
533 assert!(!regex.is_match("text."));
534 }
535
536 #[test]
537 fn test_regex_caching() {
538 let rule1 = MD026NoTrailingPunctuation::new(Some("!".to_string()));
539 let rule2 = MD026NoTrailingPunctuation::new(Some("!".to_string()));
540
541 let _regex1 = rule1.get_punctuation_regex().unwrap();
543 let _regex2 = rule2.get_punctuation_regex().unwrap();
544
545 let cache = PUNCTUATION_REGEX_CACHE.read().unwrap();
547 assert!(cache.contains_key("!"));
548 }
549
550 #[test]
551 fn test_config_from_toml() {
552 let mut config = crate::config::Config::default();
553 let mut rule_config = crate::config::RuleConfig::default();
554 rule_config
555 .values
556 .insert("punctuation".to_string(), toml::Value::String("!?".to_string()));
557 config.rules.insert("MD026".to_string(), rule_config);
558
559 let rule = MD026NoTrailingPunctuation::from_config(&config);
560 let content = "# Title!\n# Another?";
561 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
562 let result = rule.check(&ctx).unwrap();
563 assert_eq!(result.len(), 2, "Custom punctuation from config should be used");
564 }
565
566 #[test]
567 fn test_fix_removes_punctuation() {
568 let rule = MD026NoTrailingPunctuation::new(None);
569 let content = "# Title. \n## Subtitle, ";
570 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
571 let fixed = rule.fix(&ctx).unwrap();
572 assert_eq!(fixed, "# Title\n## Subtitle");
574 }
575
576 #[test]
577 fn test_final_newline_preservation() {
578 let rule = MD026NoTrailingPunctuation::new(None);
579 let content = "# Title.\n";
580 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
581 let fixed = rule.fix(&ctx).unwrap();
582 assert_eq!(fixed, "# Title\n");
583
584 let content_no_newline = "# Title.";
585 let ctx2 = LintContext::new(content_no_newline, crate::config::MarkdownFlavor::Standard);
586 let fixed2 = rule.fix(&ctx2).unwrap();
587 assert_eq!(fixed2, "# Title");
588 }
589}