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 markdownlint_config;
8pub mod profiling;
9pub mod rule;
10#[cfg(feature = "native")]
11pub mod vscode;
12pub mod workspace_index;
13#[macro_use]
14pub mod rule_config;
15#[macro_use]
16pub mod rule_config_serde;
17pub mod rules;
18pub mod types;
19pub mod utils;
20
21#[cfg(feature = "native")]
23pub mod lsp;
24#[cfg(feature = "native")]
25pub mod output;
26#[cfg(feature = "native")]
27pub mod parallel;
28#[cfg(feature = "native")]
29pub mod performance;
30
31#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
33pub mod wasm;
34
35pub use rules::heading_utils::{Heading, HeadingStyle};
36pub use rules::*;
37
38pub use crate::lint_context::{LineInfo, LintContext, ListItemInfo};
39use crate::rule::{LintResult, Rule, RuleCategory};
40#[cfg(not(target_arch = "wasm32"))]
41use std::time::Instant;
42
43#[derive(Debug, Default)]
45struct ContentCharacteristics {
46 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, }
56
57impl ContentCharacteristics {
58 fn analyze(content: &str) -> Self {
59 let mut chars = Self { ..Default::default() };
60
61 let mut has_atx_heading = false;
63 let mut has_setext_heading = false;
64
65 for line in content.lines() {
66 let trimmed = line.trim();
67
68 if !has_atx_heading && trimmed.starts_with('#') {
70 has_atx_heading = true;
71 }
72 if !has_setext_heading && (trimmed.chars().all(|c| c == '=' || c == '-') && trimmed.len() > 1) {
73 has_setext_heading = true;
74 }
75
76 if !chars.has_lists && (line.contains("* ") || line.contains("- ") || line.contains("+ ")) {
78 chars.has_lists = true;
79 }
80 if !chars.has_lists && line.chars().next().is_some_and(|c| c.is_ascii_digit()) && line.contains(". ") {
81 chars.has_lists = true;
82 }
83 if !chars.has_links
84 && (line.contains('[')
85 || line.contains("http://")
86 || line.contains("https://")
87 || line.contains("ftp://")
88 || line.contains("www."))
89 {
90 chars.has_links = true;
91 }
92 if !chars.has_images && line.contains("![") {
93 chars.has_images = true;
94 }
95 if !chars.has_code && (line.contains('`') || line.contains("~~~")) {
96 chars.has_code = true;
97 }
98 if !chars.has_emphasis && (line.contains('*') || line.contains('_')) {
99 chars.has_emphasis = true;
100 }
101 if !chars.has_html && line.contains('<') {
102 chars.has_html = true;
103 }
104 if !chars.has_tables && line.contains('|') {
105 chars.has_tables = true;
106 }
107 if !chars.has_blockquotes && line.starts_with('>') {
108 chars.has_blockquotes = true;
109 }
110 }
111
112 chars.has_headings = has_atx_heading || has_setext_heading;
113 chars
114 }
115
116 fn should_skip_rule(&self, rule: &dyn Rule) -> bool {
118 match rule.category() {
119 RuleCategory::Heading => !self.has_headings,
120 RuleCategory::List => !self.has_lists,
121 RuleCategory::Link => !self.has_links && !self.has_images,
122 RuleCategory::Image => !self.has_images,
123 RuleCategory::CodeBlock => !self.has_code,
124 RuleCategory::Html => !self.has_html,
125 RuleCategory::Emphasis => !self.has_emphasis,
126 RuleCategory::Blockquote => !self.has_blockquotes,
127 RuleCategory::Table => !self.has_tables,
128 RuleCategory::Whitespace | RuleCategory::FrontMatter | RuleCategory::Other => false,
130 }
131 }
132}
133
134#[cfg(feature = "native")]
139fn compute_content_hash(content: &str) -> String {
140 blake3::hash(content.as_bytes()).to_hex().to_string()
141}
142
143#[cfg(not(feature = "native"))]
145fn compute_content_hash(content: &str) -> String {
146 use std::hash::{DefaultHasher, Hash, Hasher};
147 let mut hasher = DefaultHasher::new();
148 content.hash(&mut hasher);
149 format!("{:016x}", hasher.finish())
150}
151
152pub fn lint(
156 content: &str,
157 rules: &[Box<dyn Rule>],
158 verbose: bool,
159 flavor: crate::config::MarkdownFlavor,
160 config: Option<&crate::config::Config>,
161) -> LintResult {
162 let (result, _file_index) = lint_and_index(content, rules, verbose, flavor, None, config);
164 result
165}
166
167pub fn build_file_index_only(
175 content: &str,
176 rules: &[Box<dyn Rule>],
177 flavor: crate::config::MarkdownFlavor,
178) -> crate::workspace_index::FileIndex {
179 let content_hash = compute_content_hash(content);
181 let mut file_index = crate::workspace_index::FileIndex::with_hash(content_hash);
182
183 if content.is_empty() {
185 return file_index;
186 }
187
188 let lint_ctx = crate::lint_context::LintContext::new(content, flavor, None);
190
191 for rule in rules {
193 if rule.cross_file_scope() == crate::rule::CrossFileScope::Workspace {
194 rule.contribute_to_index(&lint_ctx, &mut file_index);
195 }
196 }
197
198 file_index
199}
200
201pub fn lint_and_index(
209 content: &str,
210 rules: &[Box<dyn Rule>],
211 _verbose: bool,
212 flavor: crate::config::MarkdownFlavor,
213 source_file: Option<std::path::PathBuf>,
214 config: Option<&crate::config::Config>,
215) -> (LintResult, crate::workspace_index::FileIndex) {
216 let mut warnings = Vec::new();
217 let content_hash = compute_content_hash(content);
219 let mut file_index = crate::workspace_index::FileIndex::with_hash(content_hash);
220
221 #[cfg(not(target_arch = "wasm32"))]
222 let _overall_start = Instant::now();
223
224 if content.is_empty() {
226 return (Ok(warnings), file_index);
227 }
228
229 let inline_config = crate::inline_config::InlineConfig::from_content(content);
231
232 let (file_disabled, line_disabled) = inline_config.export_for_file_index();
234 file_index.file_disabled_rules = file_disabled;
235 file_index.line_disabled_rules = line_disabled;
236
237 let characteristics = ContentCharacteristics::analyze(content);
239
240 let applicable_rules: Vec<_> = rules
242 .iter()
243 .filter(|rule| !characteristics.should_skip_rule(rule.as_ref()))
244 .collect();
245
246 let _total_rules = rules.len();
248 let _applicable_count = applicable_rules.len();
249
250 let lint_ctx = crate::lint_context::LintContext::new(content, flavor, source_file);
252
253 #[cfg(not(target_arch = "wasm32"))]
254 let profile_rules = std::env::var("RUMDL_PROFILE_RULES").is_ok();
255 #[cfg(target_arch = "wasm32")]
256 let profile_rules = false;
257
258 for rule in &applicable_rules {
259 #[cfg(not(target_arch = "wasm32"))]
260 let _rule_start = Instant::now();
261
262 let result = rule.check(&lint_ctx);
264
265 match result {
266 Ok(rule_warnings) => {
267 let filtered_warnings: Vec<_> = rule_warnings
269 .into_iter()
270 .filter(|warning| {
271 let rule_name_to_check = warning.rule_name.as_deref().unwrap_or(rule.name());
273
274 let base_rule_name = if let Some(dash_pos) = rule_name_to_check.find('-') {
276 &rule_name_to_check[..dash_pos]
277 } else {
278 rule_name_to_check
279 };
280
281 !inline_config.is_rule_disabled(
282 base_rule_name,
283 warning.line, )
285 })
286 .map(|mut warning| {
287 if let Some(cfg) = config {
289 let rule_name_to_check = warning.rule_name.as_deref().unwrap_or(rule.name());
290 if let Some(&override_severity) = cfg.rule_severities.get(rule_name_to_check) {
291 warning.severity = override_severity;
292 }
293 }
294 warning
295 })
296 .collect();
297 warnings.extend(filtered_warnings);
298 }
299 Err(e) => {
300 log::error!("Error checking rule {}: {}", rule.name(), e);
301 return (Err(e), file_index);
302 }
303 }
304
305 #[cfg(not(target_arch = "wasm32"))]
306 {
307 let rule_duration = _rule_start.elapsed();
308 if profile_rules {
309 eprintln!("[RULE] {:6} {:?}", rule.name(), rule_duration);
310 }
311
312 #[cfg(not(test))]
313 if _verbose && rule_duration.as_millis() > 500 {
314 log::debug!("Rule {} took {:?}", rule.name(), rule_duration);
315 }
316 }
317 }
318
319 for rule in rules {
326 if rule.cross_file_scope() == crate::rule::CrossFileScope::Workspace {
327 rule.contribute_to_index(&lint_ctx, &mut file_index);
328 }
329 }
330
331 #[cfg(not(test))]
332 if _verbose {
333 let skipped_rules = _total_rules - _applicable_count;
334 if skipped_rules > 0 {
335 log::debug!("Skipped {skipped_rules} of {_total_rules} rules based on content analysis");
336 }
337 }
338
339 (Ok(warnings), file_index)
340}
341
342pub fn run_cross_file_checks(
355 file_path: &std::path::Path,
356 file_index: &crate::workspace_index::FileIndex,
357 rules: &[Box<dyn Rule>],
358 workspace_index: &crate::workspace_index::WorkspaceIndex,
359 config: Option<&crate::config::Config>,
360) -> LintResult {
361 use crate::rule::CrossFileScope;
362
363 let mut warnings = Vec::new();
364
365 for rule in rules {
367 if rule.cross_file_scope() != CrossFileScope::Workspace {
368 continue;
369 }
370
371 match rule.cross_file_check(file_path, file_index, workspace_index) {
372 Ok(rule_warnings) => {
373 let filtered: Vec<_> = rule_warnings
375 .into_iter()
376 .filter(|w| !file_index.is_rule_disabled_at_line(rule.name(), w.line))
377 .map(|mut warning| {
378 if let Some(cfg) = config
380 && let Some(&override_severity) = cfg.rule_severities.get(rule.name())
381 {
382 warning.severity = override_severity;
383 }
384 warning
385 })
386 .collect();
387 warnings.extend(filtered);
388 }
389 Err(e) => {
390 log::error!("Error in cross-file check for rule {}: {}", rule.name(), e);
391 return Err(e);
392 }
393 }
394 }
395
396 Ok(warnings)
397}
398
399pub fn get_profiling_report() -> String {
401 profiling::get_report()
402}
403
404pub fn reset_profiling() {
406 profiling::reset()
407}
408
409pub fn get_regex_cache_stats() -> std::collections::HashMap<String, u64> {
411 crate::utils::regex_cache::get_cache_stats()
412}
413
414#[cfg(test)]
415mod tests {
416 use super::*;
417 use crate::rule::Rule;
418 use crate::rules::{MD001HeadingIncrement, MD009TrailingSpaces};
419
420 #[test]
421 fn test_content_characteristics_analyze() {
422 let chars = ContentCharacteristics::analyze("");
424 assert!(!chars.has_headings);
425 assert!(!chars.has_lists);
426 assert!(!chars.has_links);
427 assert!(!chars.has_code);
428 assert!(!chars.has_emphasis);
429 assert!(!chars.has_html);
430 assert!(!chars.has_tables);
431 assert!(!chars.has_blockquotes);
432 assert!(!chars.has_images);
433
434 let chars = ContentCharacteristics::analyze("# Heading");
436 assert!(chars.has_headings);
437
438 let chars = ContentCharacteristics::analyze("Heading\n=======");
440 assert!(chars.has_headings);
441
442 let chars = ContentCharacteristics::analyze("* Item\n- Item 2\n+ Item 3");
444 assert!(chars.has_lists);
445
446 let chars = ContentCharacteristics::analyze("1. First\n2. Second");
448 assert!(chars.has_lists);
449
450 let chars = ContentCharacteristics::analyze("[link](url)");
452 assert!(chars.has_links);
453
454 let chars = ContentCharacteristics::analyze("Visit https://example.com");
456 assert!(chars.has_links);
457
458 let chars = ContentCharacteristics::analyze("");
460 assert!(chars.has_images);
461
462 let chars = ContentCharacteristics::analyze("`inline code`");
464 assert!(chars.has_code);
465
466 let chars = ContentCharacteristics::analyze("~~~\ncode block\n~~~");
467 assert!(chars.has_code);
468
469 let chars = ContentCharacteristics::analyze("*emphasis* and _more_");
471 assert!(chars.has_emphasis);
472
473 let chars = ContentCharacteristics::analyze("<div>HTML content</div>");
475 assert!(chars.has_html);
476
477 let chars = ContentCharacteristics::analyze("| Header | Header |\n|--------|--------|");
479 assert!(chars.has_tables);
480
481 let chars = ContentCharacteristics::analyze("> Quote");
483 assert!(chars.has_blockquotes);
484
485 let content = "# Heading\n* List item\n[link](url)\n`code`\n*emphasis*\n<p>html</p>\n| table |\n> quote\n";
487 let chars = ContentCharacteristics::analyze(content);
488 assert!(chars.has_headings);
489 assert!(chars.has_lists);
490 assert!(chars.has_links);
491 assert!(chars.has_code);
492 assert!(chars.has_emphasis);
493 assert!(chars.has_html);
494 assert!(chars.has_tables);
495 assert!(chars.has_blockquotes);
496 assert!(chars.has_images);
497 }
498
499 #[test]
500 fn test_content_characteristics_should_skip_rule() {
501 let chars = ContentCharacteristics {
502 has_headings: true,
503 has_lists: false,
504 has_links: true,
505 has_code: false,
506 has_emphasis: true,
507 has_html: false,
508 has_tables: true,
509 has_blockquotes: false,
510 has_images: false,
511 };
512
513 let heading_rule = MD001HeadingIncrement;
515 assert!(!chars.should_skip_rule(&heading_rule));
516
517 let trailing_spaces_rule = MD009TrailingSpaces::new(2, false);
518 assert!(!chars.should_skip_rule(&trailing_spaces_rule)); let chars_no_headings = ContentCharacteristics {
522 has_headings: false,
523 ..Default::default()
524 };
525 assert!(chars_no_headings.should_skip_rule(&heading_rule));
526 }
527
528 #[test]
529 fn test_lint_empty_content() {
530 let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
531
532 let result = lint("", &rules, false, crate::config::MarkdownFlavor::Standard, None);
533 assert!(result.is_ok());
534 assert!(result.unwrap().is_empty());
535 }
536
537 #[test]
538 fn test_lint_with_violations() {
539 let content = "## Level 2\n#### Level 4"; let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
541
542 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard, None);
543 assert!(result.is_ok());
544 let warnings = result.unwrap();
545 assert!(!warnings.is_empty());
546 assert_eq!(warnings[0].rule_name.as_deref(), Some("MD001"));
548 }
549
550 #[test]
551 fn test_lint_with_inline_disable() {
552 let content = "<!-- rumdl-disable MD001 -->\n## Level 2\n#### Level 4";
553 let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
554
555 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard, None);
556 assert!(result.is_ok());
557 let warnings = result.unwrap();
558 assert!(warnings.is_empty()); }
560
561 #[test]
562 fn test_lint_rule_filtering() {
563 let content = "# Heading\nJust text";
565 let rules: Vec<Box<dyn Rule>> = vec![
566 Box::new(MD001HeadingIncrement),
567 ];
569
570 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard, None);
571 assert!(result.is_ok());
572 }
573
574 #[test]
575 fn test_get_profiling_report() {
576 let report = get_profiling_report();
578 assert!(!report.is_empty());
579 assert!(report.contains("Profiling"));
580 }
581
582 #[test]
583 fn test_reset_profiling() {
584 reset_profiling();
586
587 let report = get_profiling_report();
589 assert!(report.contains("disabled") || report.contains("no measurements"));
590 }
591
592 #[test]
593 fn test_get_regex_cache_stats() {
594 let stats = get_regex_cache_stats();
595 assert!(stats.is_empty() || !stats.is_empty());
597
598 for count in stats.values() {
600 assert!(*count > 0);
601 }
602 }
603
604 #[test]
605 fn test_content_characteristics_edge_cases() {
606 let chars = ContentCharacteristics::analyze("-"); assert!(!chars.has_headings);
609
610 let chars = ContentCharacteristics::analyze("--"); assert!(chars.has_headings);
612
613 let chars = ContentCharacteristics::analyze("*emphasis*"); assert!(!chars.has_lists);
616
617 let chars = ContentCharacteristics::analyze("1.Item"); assert!(!chars.has_lists);
619
620 let chars = ContentCharacteristics::analyze("text > not a quote");
622 assert!(!chars.has_blockquotes);
623 }
624}