1pub mod config;
2pub mod exit_codes;
3pub mod filtered_lines;
4pub mod fix_coordinator;
5pub mod inline_config;
6pub mod lint_context;
7pub mod lsp;
8pub mod markdownlint_config;
9pub mod output;
10pub mod parallel;
11pub mod performance;
12pub mod profiling;
13pub mod rule;
14pub mod vscode;
15#[macro_use]
16pub mod rule_config;
17#[macro_use]
18pub mod rule_config_serde;
19pub mod rules;
20pub mod utils;
21
22pub use rules::heading_utils::{Heading, HeadingStyle};
23pub use rules::*;
24
25pub use crate::lint_context::{LineInfo, LintContext, ListItemInfo};
26use crate::rule::{LintResult, Rule, RuleCategory};
27use std::time::Instant;
28
29#[derive(Debug, Default)]
31struct ContentCharacteristics {
32 has_headings: bool, has_lists: bool, has_links: bool, has_code: bool, has_emphasis: bool, has_html: bool, has_tables: bool, has_blockquotes: bool, has_images: bool, }
42
43impl ContentCharacteristics {
44 fn analyze(content: &str) -> Self {
45 let mut chars = Self { ..Default::default() };
46
47 let mut has_atx_heading = false;
49 let mut has_setext_heading = false;
50
51 for line in content.lines() {
52 let trimmed = line.trim();
53
54 if !has_atx_heading && trimmed.starts_with('#') {
56 has_atx_heading = true;
57 }
58 if !has_setext_heading && (trimmed.chars().all(|c| c == '=' || c == '-') && trimmed.len() > 1) {
59 has_setext_heading = true;
60 }
61
62 if !chars.has_lists && (line.contains("* ") || line.contains("- ") || line.contains("+ ")) {
64 chars.has_lists = true;
65 }
66 if !chars.has_lists && line.chars().next().is_some_and(|c| c.is_ascii_digit()) && line.contains(". ") {
67 chars.has_lists = true;
68 }
69 if !chars.has_links
70 && (line.contains('[')
71 || line.contains("http://")
72 || line.contains("https://")
73 || line.contains("ftp://"))
74 {
75 chars.has_links = true;
76 }
77 if !chars.has_images && line.contains("![") {
78 chars.has_images = true;
79 }
80 if !chars.has_code && (line.contains('`') || line.contains("~~~")) {
81 chars.has_code = true;
82 }
83 if !chars.has_emphasis && (line.contains('*') || line.contains('_')) {
84 chars.has_emphasis = true;
85 }
86 if !chars.has_html && line.contains('<') {
87 chars.has_html = true;
88 }
89 if !chars.has_tables && line.contains('|') {
90 chars.has_tables = true;
91 }
92 if !chars.has_blockquotes && line.starts_with('>') {
93 chars.has_blockquotes = true;
94 }
95 }
96
97 chars.has_headings = has_atx_heading || has_setext_heading;
98 chars
99 }
100
101 fn should_skip_rule(&self, rule: &dyn Rule) -> bool {
103 match rule.category() {
104 RuleCategory::Heading => !self.has_headings,
105 RuleCategory::List => !self.has_lists,
106 RuleCategory::Link => !self.has_links && !self.has_images,
107 RuleCategory::Image => !self.has_images,
108 RuleCategory::CodeBlock => !self.has_code,
109 RuleCategory::Html => !self.has_html,
110 RuleCategory::Emphasis => !self.has_emphasis,
111 RuleCategory::Blockquote => !self.has_blockquotes,
112 RuleCategory::Table => !self.has_tables,
113 RuleCategory::Whitespace | RuleCategory::FrontMatter | RuleCategory::Other => false,
115 }
116 }
117}
118
119pub fn lint(
123 content: &str,
124 rules: &[Box<dyn Rule>],
125 _verbose: bool,
126 flavor: crate::config::MarkdownFlavor,
127) -> LintResult {
128 let mut warnings = Vec::new();
129 let _overall_start = Instant::now();
130
131 if content.is_empty() {
133 return Ok(warnings);
134 }
135
136 let inline_config = crate::inline_config::InlineConfig::from_content(content);
138
139 let characteristics = ContentCharacteristics::analyze(content);
141
142 let applicable_rules: Vec<_> = rules
144 .iter()
145 .filter(|rule| !characteristics.should_skip_rule(rule.as_ref()))
146 .collect();
147
148 let _total_rules = rules.len();
150 let _applicable_count = applicable_rules.len();
151
152 let lint_ctx = crate::lint_context::LintContext::new(content, flavor);
154
155 let profile_rules = std::env::var("RUMDL_PROFILE_RULES").is_ok();
156
157 for rule in applicable_rules {
158 let _rule_start = Instant::now();
159
160 let result = rule.check(&lint_ctx);
161
162 match result {
163 Ok(rule_warnings) => {
164 let filtered_warnings: Vec<_> = rule_warnings
166 .into_iter()
167 .filter(|warning| {
168 let rule_name_to_check = warning.rule_name.as_deref().unwrap_or(rule.name());
170
171 let base_rule_name = if let Some(dash_pos) = rule_name_to_check.find('-') {
173 &rule_name_to_check[..dash_pos]
174 } else {
175 rule_name_to_check
176 };
177
178 !inline_config.is_rule_disabled(
179 base_rule_name,
180 warning.line, )
182 })
183 .collect();
184 warnings.extend(filtered_warnings);
185 }
186 Err(e) => {
187 log::error!("Error checking rule {}: {}", rule.name(), e);
188 return Err(e);
189 }
190 }
191
192 let rule_duration = _rule_start.elapsed();
193 if profile_rules {
194 eprintln!("[RULE] {:6} {:?}", rule.name(), rule_duration);
195 }
196
197 #[cfg(not(test))]
198 if _verbose && rule_duration.as_millis() > 500 {
199 log::debug!("Rule {} took {:?}", rule.name(), rule_duration);
200 }
201 }
202
203 #[cfg(not(test))]
204 if _verbose {
205 let skipped_rules = _total_rules - _applicable_count;
206 if skipped_rules > 0 {
207 log::debug!("Skipped {skipped_rules} of {_total_rules} rules based on content analysis");
208 }
209 }
210
211 Ok(warnings)
212}
213
214pub fn get_profiling_report() -> String {
216 profiling::get_report()
217}
218
219pub fn reset_profiling() {
221 profiling::reset()
222}
223
224pub fn get_regex_cache_stats() -> std::collections::HashMap<String, u64> {
226 crate::utils::regex_cache::get_cache_stats()
227}
228
229pub fn get_ast_cache_stats() -> std::collections::HashMap<u64, u64> {
231 crate::utils::ast_utils::get_ast_cache_stats()
232}
233
234pub fn clear_all_caches() {
236 crate::utils::ast_utils::clear_ast_cache();
237 }
239
240pub fn get_cache_performance_report() -> String {
242 let regex_stats = get_regex_cache_stats();
243 let ast_stats = get_ast_cache_stats();
244
245 let mut report = String::new();
246
247 report.push_str("=== Cache Performance Report ===\n\n");
248
249 report.push_str("Regex Cache:\n");
251 if regex_stats.is_empty() {
252 report.push_str(" No regex patterns cached\n");
253 } else {
254 let total_usage: u64 = regex_stats.values().sum();
255 report.push_str(&format!(" Total patterns: {}\n", regex_stats.len()));
256 report.push_str(&format!(" Total usage: {total_usage}\n"));
257
258 let mut sorted_patterns: Vec<_> = regex_stats.iter().collect();
260 sorted_patterns.sort_by(|a, b| b.1.cmp(a.1));
261
262 report.push_str(" Top patterns by usage:\n");
263 for (pattern, count) in sorted_patterns.iter().take(5) {
264 let truncated_pattern = if pattern.len() > 50 {
265 format!("{}...", &pattern[..47])
266 } else {
267 pattern.to_string()
268 };
269 report.push_str(&format!(
270 " {} ({}x): {}\n",
271 count,
272 pattern.len().min(50),
273 truncated_pattern
274 ));
275 }
276 }
277
278 report.push('\n');
279
280 report.push_str("AST Cache:\n");
282 if ast_stats.is_empty() {
283 report.push_str(" No ASTs cached\n");
284 } else {
285 let total_usage: u64 = ast_stats.values().sum();
286 report.push_str(&format!(" Total ASTs: {}\n", ast_stats.len()));
287 report.push_str(&format!(" Total usage: {total_usage}\n"));
288
289 if total_usage > ast_stats.len() as u64 {
290 let cache_hit_rate = ((total_usage - ast_stats.len() as u64) as f64 / total_usage as f64) * 100.0;
291 report.push_str(&format!(" Cache hit rate: {cache_hit_rate:.1}%\n"));
292 }
293 }
294
295 report
296}
297
298#[cfg(test)]
299mod tests {
300 use super::*;
301 use crate::rule::Rule;
302 use crate::rules::{MD001HeadingIncrement, MD009TrailingSpaces, MD012NoMultipleBlanks};
303
304 #[test]
305 fn test_content_characteristics_analyze() {
306 let chars = ContentCharacteristics::analyze("");
308 assert!(!chars.has_headings);
309 assert!(!chars.has_lists);
310 assert!(!chars.has_links);
311 assert!(!chars.has_code);
312 assert!(!chars.has_emphasis);
313 assert!(!chars.has_html);
314 assert!(!chars.has_tables);
315 assert!(!chars.has_blockquotes);
316 assert!(!chars.has_images);
317
318 let chars = ContentCharacteristics::analyze("# Heading");
320 assert!(chars.has_headings);
321
322 let chars = ContentCharacteristics::analyze("Heading\n=======");
324 assert!(chars.has_headings);
325
326 let chars = ContentCharacteristics::analyze("* Item\n- Item 2\n+ Item 3");
328 assert!(chars.has_lists);
329
330 let chars = ContentCharacteristics::analyze("1. First\n2. Second");
332 assert!(chars.has_lists);
333
334 let chars = ContentCharacteristics::analyze("[link](url)");
336 assert!(chars.has_links);
337
338 let chars = ContentCharacteristics::analyze("Visit https://example.com");
340 assert!(chars.has_links);
341
342 let chars = ContentCharacteristics::analyze("");
344 assert!(chars.has_images);
345
346 let chars = ContentCharacteristics::analyze("`inline code`");
348 assert!(chars.has_code);
349
350 let chars = ContentCharacteristics::analyze("~~~\ncode block\n~~~");
351 assert!(chars.has_code);
352
353 let chars = ContentCharacteristics::analyze("*emphasis* and _more_");
355 assert!(chars.has_emphasis);
356
357 let chars = ContentCharacteristics::analyze("<div>HTML content</div>");
359 assert!(chars.has_html);
360
361 let chars = ContentCharacteristics::analyze("| Header | Header |\n|--------|--------|");
363 assert!(chars.has_tables);
364
365 let chars = ContentCharacteristics::analyze("> Quote");
367 assert!(chars.has_blockquotes);
368
369 let content = "# Heading\n* List item\n[link](url)\n`code`\n*emphasis*\n<p>html</p>\n| table |\n> quote\n";
371 let chars = ContentCharacteristics::analyze(content);
372 assert!(chars.has_headings);
373 assert!(chars.has_lists);
374 assert!(chars.has_links);
375 assert!(chars.has_code);
376 assert!(chars.has_emphasis);
377 assert!(chars.has_html);
378 assert!(chars.has_tables);
379 assert!(chars.has_blockquotes);
380 assert!(chars.has_images);
381 }
382
383 #[test]
384 fn test_content_characteristics_should_skip_rule() {
385 let chars = ContentCharacteristics {
386 has_headings: true,
387 has_lists: false,
388 has_links: true,
389 has_code: false,
390 has_emphasis: true,
391 has_html: false,
392 has_tables: true,
393 has_blockquotes: false,
394 has_images: false,
395 };
396
397 let heading_rule = MD001HeadingIncrement;
399 assert!(!chars.should_skip_rule(&heading_rule));
400
401 let trailing_spaces_rule = MD009TrailingSpaces::new(2, false);
402 assert!(!chars.should_skip_rule(&trailing_spaces_rule)); let chars_no_headings = ContentCharacteristics {
406 has_headings: false,
407 ..Default::default()
408 };
409 assert!(chars_no_headings.should_skip_rule(&heading_rule));
410 }
411
412 #[test]
413 fn test_lint_empty_content() {
414 let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
415
416 let result = lint("", &rules, false, crate::config::MarkdownFlavor::Standard);
417 assert!(result.is_ok());
418 assert!(result.unwrap().is_empty());
419 }
420
421 #[test]
422 fn test_lint_with_violations() {
423 let content = "## Level 2\n#### Level 4"; let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
425
426 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
427 assert!(result.is_ok());
428 let warnings = result.unwrap();
429 assert!(!warnings.is_empty());
430 assert_eq!(warnings[0].rule_name.as_deref(), Some("MD001"));
432 }
433
434 #[test]
435 fn test_lint_with_inline_disable() {
436 let content = "<!-- rumdl-disable MD001 -->\n## Level 2\n#### Level 4";
437 let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
438
439 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
440 assert!(result.is_ok());
441 let warnings = result.unwrap();
442 assert!(warnings.is_empty()); }
444
445 #[test]
446 fn test_lint_rule_filtering() {
447 let content = "# Heading\nJust text";
449 let rules: Vec<Box<dyn Rule>> = vec![
450 Box::new(MD001HeadingIncrement),
451 ];
453
454 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
455 assert!(result.is_ok());
456 }
457
458 #[test]
459 fn test_get_profiling_report() {
460 let report = get_profiling_report();
462 assert!(!report.is_empty());
463 assert!(report.contains("Profiling"));
464 }
465
466 #[test]
467 fn test_reset_profiling() {
468 reset_profiling();
470
471 let report = get_profiling_report();
473 assert!(report.contains("disabled") || report.contains("no measurements"));
474 }
475
476 #[test]
477 fn test_get_regex_cache_stats() {
478 let stats = get_regex_cache_stats();
479 assert!(stats.is_empty() || !stats.is_empty());
481
482 for count in stats.values() {
484 assert!(*count > 0);
485 }
486 }
487
488 #[test]
489 fn test_get_ast_cache_stats() {
490 let stats = get_ast_cache_stats();
491 assert!(stats.is_empty() || !stats.is_empty());
493
494 for count in stats.values() {
496 assert!(*count > 0);
497 }
498 }
499
500 #[test]
501 fn test_clear_all_caches() {
502 clear_all_caches();
504
505 }
508
509 #[test]
510 fn test_get_cache_performance_report() {
511 let report = get_cache_performance_report();
513
514 assert!(report.contains("Cache Performance Report"));
516 assert!(report.contains("Regex Cache:"));
517 assert!(report.contains("AST Cache:"));
518
519 assert!(report.contains("Total patterns:") || report.contains("No regex patterns cached"));
522 assert!(report.contains("Total ASTs:") || report.contains("No ASTs cached"));
523 }
524
525 #[test]
526 fn test_lint_with_ast_rules() {
527 let content = "# Heading\n\nParagraph with **bold** text.";
529 let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD012NoMultipleBlanks::new(1))];
530
531 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
532 assert!(result.is_ok());
533 }
534
535 #[test]
536 fn test_content_characteristics_edge_cases() {
537 let chars = ContentCharacteristics::analyze("-"); assert!(!chars.has_headings);
540
541 let chars = ContentCharacteristics::analyze("--"); assert!(chars.has_headings);
543
544 let chars = ContentCharacteristics::analyze("*emphasis*"); assert!(!chars.has_lists);
547
548 let chars = ContentCharacteristics::analyze("1.Item"); assert!(!chars.has_lists);
550
551 let chars = ContentCharacteristics::analyze("text > not a quote");
553 assert!(!chars.has_blockquotes);
554 }
555
556 #[test]
557 fn test_cache_performance_report_formatting() {
558 let report = get_cache_performance_report();
562
563 assert!(!report.is_empty());
567 assert!(report.lines().count() > 3); }
569}