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) -> LintResult {
161 let (result, _file_index) = lint_and_index(content, rules, verbose, flavor, None);
163 result
164}
165
166pub fn build_file_index_only(
174 content: &str,
175 rules: &[Box<dyn Rule>],
176 flavor: crate::config::MarkdownFlavor,
177) -> crate::workspace_index::FileIndex {
178 let content_hash = compute_content_hash(content);
180 let mut file_index = crate::workspace_index::FileIndex::with_hash(content_hash);
181
182 if content.is_empty() {
184 return file_index;
185 }
186
187 let lint_ctx = crate::lint_context::LintContext::new(content, flavor, None);
189
190 for rule in rules {
192 if rule.cross_file_scope() == crate::rule::CrossFileScope::Workspace {
193 rule.contribute_to_index(&lint_ctx, &mut file_index);
194 }
195 }
196
197 file_index
198}
199
200pub fn lint_and_index(
208 content: &str,
209 rules: &[Box<dyn Rule>],
210 _verbose: bool,
211 flavor: crate::config::MarkdownFlavor,
212 source_file: Option<std::path::PathBuf>,
213) -> (LintResult, crate::workspace_index::FileIndex) {
214 let mut warnings = Vec::new();
215 let content_hash = compute_content_hash(content);
217 let mut file_index = crate::workspace_index::FileIndex::with_hash(content_hash);
218
219 #[cfg(not(target_arch = "wasm32"))]
220 let _overall_start = Instant::now();
221
222 if content.is_empty() {
224 return (Ok(warnings), file_index);
225 }
226
227 let inline_config = crate::inline_config::InlineConfig::from_content(content);
229
230 let (file_disabled, line_disabled) = inline_config.export_for_file_index();
232 file_index.file_disabled_rules = file_disabled;
233 file_index.line_disabled_rules = line_disabled;
234
235 let characteristics = ContentCharacteristics::analyze(content);
237
238 let applicable_rules: Vec<_> = rules
240 .iter()
241 .filter(|rule| !characteristics.should_skip_rule(rule.as_ref()))
242 .collect();
243
244 let _total_rules = rules.len();
246 let _applicable_count = applicable_rules.len();
247
248 let lint_ctx = crate::lint_context::LintContext::new(content, flavor, source_file);
250
251 #[cfg(not(target_arch = "wasm32"))]
252 let profile_rules = std::env::var("RUMDL_PROFILE_RULES").is_ok();
253 #[cfg(target_arch = "wasm32")]
254 let profile_rules = false;
255
256 for rule in &applicable_rules {
257 #[cfg(not(target_arch = "wasm32"))]
258 let _rule_start = Instant::now();
259
260 let result = rule.check(&lint_ctx);
262
263 match result {
264 Ok(rule_warnings) => {
265 let filtered_warnings: Vec<_> = rule_warnings
267 .into_iter()
268 .filter(|warning| {
269 let rule_name_to_check = warning.rule_name.as_deref().unwrap_or(rule.name());
271
272 let base_rule_name = if let Some(dash_pos) = rule_name_to_check.find('-') {
274 &rule_name_to_check[..dash_pos]
275 } else {
276 rule_name_to_check
277 };
278
279 !inline_config.is_rule_disabled(
280 base_rule_name,
281 warning.line, )
283 })
284 .collect();
285 warnings.extend(filtered_warnings);
286 }
287 Err(e) => {
288 log::error!("Error checking rule {}: {}", rule.name(), e);
289 return (Err(e), file_index);
290 }
291 }
292
293 #[cfg(not(target_arch = "wasm32"))]
294 {
295 let rule_duration = _rule_start.elapsed();
296 if profile_rules {
297 eprintln!("[RULE] {:6} {:?}", rule.name(), rule_duration);
298 }
299
300 #[cfg(not(test))]
301 if _verbose && rule_duration.as_millis() > 500 {
302 log::debug!("Rule {} took {:?}", rule.name(), rule_duration);
303 }
304 }
305 }
306
307 for rule in rules {
314 if rule.cross_file_scope() == crate::rule::CrossFileScope::Workspace {
315 rule.contribute_to_index(&lint_ctx, &mut file_index);
316 }
317 }
318
319 #[cfg(not(test))]
320 if _verbose {
321 let skipped_rules = _total_rules - _applicable_count;
322 if skipped_rules > 0 {
323 log::debug!("Skipped {skipped_rules} of {_total_rules} rules based on content analysis");
324 }
325 }
326
327 (Ok(warnings), file_index)
328}
329
330pub fn run_cross_file_checks(
343 file_path: &std::path::Path,
344 file_index: &crate::workspace_index::FileIndex,
345 rules: &[Box<dyn Rule>],
346 workspace_index: &crate::workspace_index::WorkspaceIndex,
347) -> LintResult {
348 use crate::rule::CrossFileScope;
349
350 let mut warnings = Vec::new();
351
352 for rule in rules {
354 if rule.cross_file_scope() != CrossFileScope::Workspace {
355 continue;
356 }
357
358 match rule.cross_file_check(file_path, file_index, workspace_index) {
359 Ok(rule_warnings) => {
360 let filtered: Vec<_> = rule_warnings
362 .into_iter()
363 .filter(|w| !file_index.is_rule_disabled_at_line(rule.name(), w.line))
364 .collect();
365 warnings.extend(filtered);
366 }
367 Err(e) => {
368 log::error!("Error in cross-file check for rule {}: {}", rule.name(), e);
369 return Err(e);
370 }
371 }
372 }
373
374 Ok(warnings)
375}
376
377pub fn get_profiling_report() -> String {
379 profiling::get_report()
380}
381
382pub fn reset_profiling() {
384 profiling::reset()
385}
386
387pub fn get_regex_cache_stats() -> std::collections::HashMap<String, u64> {
389 crate::utils::regex_cache::get_cache_stats()
390}
391
392#[cfg(test)]
393mod tests {
394 use super::*;
395 use crate::rule::Rule;
396 use crate::rules::{MD001HeadingIncrement, MD009TrailingSpaces};
397
398 #[test]
399 fn test_content_characteristics_analyze() {
400 let chars = ContentCharacteristics::analyze("");
402 assert!(!chars.has_headings);
403 assert!(!chars.has_lists);
404 assert!(!chars.has_links);
405 assert!(!chars.has_code);
406 assert!(!chars.has_emphasis);
407 assert!(!chars.has_html);
408 assert!(!chars.has_tables);
409 assert!(!chars.has_blockquotes);
410 assert!(!chars.has_images);
411
412 let chars = ContentCharacteristics::analyze("# Heading");
414 assert!(chars.has_headings);
415
416 let chars = ContentCharacteristics::analyze("Heading\n=======");
418 assert!(chars.has_headings);
419
420 let chars = ContentCharacteristics::analyze("* Item\n- Item 2\n+ Item 3");
422 assert!(chars.has_lists);
423
424 let chars = ContentCharacteristics::analyze("1. First\n2. Second");
426 assert!(chars.has_lists);
427
428 let chars = ContentCharacteristics::analyze("[link](url)");
430 assert!(chars.has_links);
431
432 let chars = ContentCharacteristics::analyze("Visit https://example.com");
434 assert!(chars.has_links);
435
436 let chars = ContentCharacteristics::analyze("");
438 assert!(chars.has_images);
439
440 let chars = ContentCharacteristics::analyze("`inline code`");
442 assert!(chars.has_code);
443
444 let chars = ContentCharacteristics::analyze("~~~\ncode block\n~~~");
445 assert!(chars.has_code);
446
447 let chars = ContentCharacteristics::analyze("*emphasis* and _more_");
449 assert!(chars.has_emphasis);
450
451 let chars = ContentCharacteristics::analyze("<div>HTML content</div>");
453 assert!(chars.has_html);
454
455 let chars = ContentCharacteristics::analyze("| Header | Header |\n|--------|--------|");
457 assert!(chars.has_tables);
458
459 let chars = ContentCharacteristics::analyze("> Quote");
461 assert!(chars.has_blockquotes);
462
463 let content = "# Heading\n* List item\n[link](url)\n`code`\n*emphasis*\n<p>html</p>\n| table |\n> quote\n";
465 let chars = ContentCharacteristics::analyze(content);
466 assert!(chars.has_headings);
467 assert!(chars.has_lists);
468 assert!(chars.has_links);
469 assert!(chars.has_code);
470 assert!(chars.has_emphasis);
471 assert!(chars.has_html);
472 assert!(chars.has_tables);
473 assert!(chars.has_blockquotes);
474 assert!(chars.has_images);
475 }
476
477 #[test]
478 fn test_content_characteristics_should_skip_rule() {
479 let chars = ContentCharacteristics {
480 has_headings: true,
481 has_lists: false,
482 has_links: true,
483 has_code: false,
484 has_emphasis: true,
485 has_html: false,
486 has_tables: true,
487 has_blockquotes: false,
488 has_images: false,
489 };
490
491 let heading_rule = MD001HeadingIncrement;
493 assert!(!chars.should_skip_rule(&heading_rule));
494
495 let trailing_spaces_rule = MD009TrailingSpaces::new(2, false);
496 assert!(!chars.should_skip_rule(&trailing_spaces_rule)); let chars_no_headings = ContentCharacteristics {
500 has_headings: false,
501 ..Default::default()
502 };
503 assert!(chars_no_headings.should_skip_rule(&heading_rule));
504 }
505
506 #[test]
507 fn test_lint_empty_content() {
508 let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
509
510 let result = lint("", &rules, false, crate::config::MarkdownFlavor::Standard);
511 assert!(result.is_ok());
512 assert!(result.unwrap().is_empty());
513 }
514
515 #[test]
516 fn test_lint_with_violations() {
517 let content = "## Level 2\n#### Level 4"; let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
519
520 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
521 assert!(result.is_ok());
522 let warnings = result.unwrap();
523 assert!(!warnings.is_empty());
524 assert_eq!(warnings[0].rule_name.as_deref(), Some("MD001"));
526 }
527
528 #[test]
529 fn test_lint_with_inline_disable() {
530 let content = "<!-- rumdl-disable MD001 -->\n## Level 2\n#### Level 4";
531 let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
532
533 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
534 assert!(result.is_ok());
535 let warnings = result.unwrap();
536 assert!(warnings.is_empty()); }
538
539 #[test]
540 fn test_lint_rule_filtering() {
541 let content = "# Heading\nJust text";
543 let rules: Vec<Box<dyn Rule>> = vec![
544 Box::new(MD001HeadingIncrement),
545 ];
547
548 let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
549 assert!(result.is_ok());
550 }
551
552 #[test]
553 fn test_get_profiling_report() {
554 let report = get_profiling_report();
556 assert!(!report.is_empty());
557 assert!(report.contains("Profiling"));
558 }
559
560 #[test]
561 fn test_reset_profiling() {
562 reset_profiling();
564
565 let report = get_profiling_report();
567 assert!(report.contains("disabled") || report.contains("no measurements"));
568 }
569
570 #[test]
571 fn test_get_regex_cache_stats() {
572 let stats = get_regex_cache_stats();
573 assert!(stats.is_empty() || !stats.is_empty());
575
576 for count in stats.values() {
578 assert!(*count > 0);
579 }
580 }
581
582 #[test]
583 fn test_content_characteristics_edge_cases() {
584 let chars = ContentCharacteristics::analyze("-"); assert!(!chars.has_headings);
587
588 let chars = ContentCharacteristics::analyze("--"); assert!(chars.has_headings);
590
591 let chars = ContentCharacteristics::analyze("*emphasis*"); assert!(!chars.has_lists);
594
595 let chars = ContentCharacteristics::analyze("1.Item"); assert!(!chars.has_lists);
597
598 let chars = ContentCharacteristics::analyze("text > not a quote");
600 assert!(!chars.has_blockquotes);
601 }
602}