rumdl_lib/
lib.rs

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// Native-only modules (require tokio, tower-lsp, etc.)
22#[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// WASM module
32#[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/// Content characteristics for efficient rule filtering
44#[derive(Debug, Default)]
45struct ContentCharacteristics {
46    has_headings: bool,    // # or setext headings
47    has_lists: bool,       // *, -, +, 1. etc
48    has_links: bool,       // [text](url) or [text][ref]
49    has_code: bool,        // ``` or ~~~ or indented code
50    has_emphasis: bool,    // * or _ for emphasis
51    has_html: bool,        // < > tags
52    has_tables: bool,      // | pipes
53    has_blockquotes: bool, // > markers
54    has_images: bool,      // ![alt](url)
55}
56
57impl ContentCharacteristics {
58    fn analyze(content: &str) -> Self {
59        let mut chars = Self { ..Default::default() };
60
61        // Quick single-pass analysis
62        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            // Headings: ATX (#) or Setext (underlines)
69            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            // Quick character-based detection (more efficient than regex)
77            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    /// Check if a rule should be skipped based on content characteristics
117    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            // Always check these categories as they apply to all content
129            RuleCategory::Whitespace | RuleCategory::FrontMatter | RuleCategory::Other => false,
130        }
131    }
132}
133
134/// Compute content hash for incremental indexing change detection
135///
136/// Uses blake3 for native builds (fast, cryptographic-strength hash)
137/// Falls back to std::hash for WASM builds
138#[cfg(feature = "native")]
139fn compute_content_hash(content: &str) -> String {
140    blake3::hash(content.as_bytes()).to_hex().to_string()
141}
142
143/// Compute content hash for WASM builds using std::hash
144#[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
152/// Lint a file against the given rules with intelligent rule filtering
153/// Assumes the provided `rules` vector contains the final,
154/// configured, and filtered set of rules to be executed.
155pub 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    // Use lint_and_index but discard the FileIndex for backward compatibility
163    let (result, _file_index) = lint_and_index(content, rules, verbose, flavor, None, config);
164    result
165}
166
167/// Build FileIndex only (no linting) for cross-file analysis on cache hits
168///
169/// This is a lightweight function that only builds the FileIndex without running
170/// any rules. Used when we have a cache hit but still need the FileIndex for
171/// cross-file validation.
172///
173/// This avoids the overhead of re-running all rules when only the index data is needed.
174pub fn build_file_index_only(
175    content: &str,
176    rules: &[Box<dyn Rule>],
177    flavor: crate::config::MarkdownFlavor,
178) -> crate::workspace_index::FileIndex {
179    // Compute content hash for change detection
180    let content_hash = compute_content_hash(content);
181    let mut file_index = crate::workspace_index::FileIndex::with_hash(content_hash);
182
183    // Early return for empty content
184    if content.is_empty() {
185        return file_index;
186    }
187
188    // Parse LintContext once with the provided flavor
189    let lint_ctx = crate::lint_context::LintContext::new(content, flavor, None);
190
191    // Only call contribute_to_index for cross-file rules (no rule checking!)
192    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
201/// Lint a file and contribute to workspace index for cross-file analysis
202///
203/// This variant performs linting and optionally populates a `FileIndex` with data
204/// needed for cross-file validation. The FileIndex is populated during linting,
205/// avoiding duplicate parsing.
206///
207/// Returns: (warnings, FileIndex) - the FileIndex contains headings/links for cross-file rules
208pub 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    // Compute content hash for change detection
218    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    // Early return for empty content
225    if content.is_empty() {
226        return (Ok(warnings), file_index);
227    }
228
229    // Parse inline configuration comments once
230    let inline_config = crate::inline_config::InlineConfig::from_content(content);
231
232    // Export inline config data to FileIndex for cross-file rule filtering
233    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    // Analyze content characteristics for rule filtering
238    let characteristics = ContentCharacteristics::analyze(content);
239
240    // Filter rules based on content characteristics
241    let applicable_rules: Vec<_> = rules
242        .iter()
243        .filter(|rule| !characteristics.should_skip_rule(rule.as_ref()))
244        .collect();
245
246    // Calculate skipped rules count before consuming applicable_rules
247    let _total_rules = rules.len();
248    let _applicable_count = applicable_rules.len();
249
250    // Parse LintContext once with the provided flavor
251    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        // Run single-file check
263        let result = rule.check(&lint_ctx);
264
265        match result {
266            Ok(rule_warnings) => {
267                // Filter out warnings for rules disabled via inline comments
268                let filtered_warnings: Vec<_> = rule_warnings
269                    .into_iter()
270                    .filter(|warning| {
271                        // Use the warning's rule_name if available, otherwise use the rule's name
272                        let rule_name_to_check = warning.rule_name.as_deref().unwrap_or(rule.name());
273
274                        // Extract the base rule name for sub-rules like "MD029-style" -> "MD029"
275                        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, // Already 1-indexed
284                        )
285                    })
286                    .map(|mut warning| {
287                        // Apply severity override from config if present
288                        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    // Contribute to index for cross-file rules (done after all rules checked)
320    // NOTE: We iterate over ALL rules (not just applicable_rules) because cross-file
321    // rules need to extract data from every file in the workspace, regardless of whether
322    // that file has content that would trigger the rule. For example, MD051 needs to
323    // index headings from files that have no links (like target.md) so that links
324    // FROM other files TO those headings can be validated.
325    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
342/// Run cross-file checks for rules that need workspace-wide validation
343///
344/// This should be called after all files have been linted and the WorkspaceIndex
345/// has been built from the accumulated FileIndex data.
346///
347/// Note: This takes the FileIndex instead of content to avoid re-parsing each file.
348/// The FileIndex was already populated during contribute_to_index in the linting phase.
349///
350/// Rules can use workspace_index methods for cross-file validation:
351/// - `get_file(path)` - to look up headings in target files (for MD051)
352///
353/// Returns additional warnings from cross-file validation.
354pub 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    // Only check rules that need cross-file analysis
366    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                // Filter cross-file warnings based on inline config stored in file_index
374                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                        // Apply severity override from config if present
379                        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
399/// Get the profiling report
400pub fn get_profiling_report() -> String {
401    profiling::get_report()
402}
403
404/// Reset the profiling data
405pub fn reset_profiling() {
406    profiling::reset()
407}
408
409/// Get regex cache statistics for performance monitoring
410pub 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        // Test empty content
423        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        // Test content with headings
435        let chars = ContentCharacteristics::analyze("# Heading");
436        assert!(chars.has_headings);
437
438        // Test setext headings
439        let chars = ContentCharacteristics::analyze("Heading\n=======");
440        assert!(chars.has_headings);
441
442        // Test lists
443        let chars = ContentCharacteristics::analyze("* Item\n- Item 2\n+ Item 3");
444        assert!(chars.has_lists);
445
446        // Test ordered lists
447        let chars = ContentCharacteristics::analyze("1. First\n2. Second");
448        assert!(chars.has_lists);
449
450        // Test links
451        let chars = ContentCharacteristics::analyze("[link](url)");
452        assert!(chars.has_links);
453
454        // Test URLs
455        let chars = ContentCharacteristics::analyze("Visit https://example.com");
456        assert!(chars.has_links);
457
458        // Test images
459        let chars = ContentCharacteristics::analyze("![alt text](image.png)");
460        assert!(chars.has_images);
461
462        // Test code
463        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        // Test emphasis
470        let chars = ContentCharacteristics::analyze("*emphasis* and _more_");
471        assert!(chars.has_emphasis);
472
473        // Test HTML
474        let chars = ContentCharacteristics::analyze("<div>HTML content</div>");
475        assert!(chars.has_html);
476
477        // Test tables
478        let chars = ContentCharacteristics::analyze("| Header | Header |\n|--------|--------|");
479        assert!(chars.has_tables);
480
481        // Test blockquotes
482        let chars = ContentCharacteristics::analyze("> Quote");
483        assert!(chars.has_blockquotes);
484
485        // Test mixed content
486        let content = "# Heading\n* List item\n[link](url)\n`code`\n*emphasis*\n<p>html</p>\n| table |\n> quote\n![image](img.png)";
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        // Create test rules for different categories
514        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)); // Whitespace rules always run
519
520        // Test skipping based on content
521        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"; // Skips level 3
540        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        // Check the rule field of LintWarning struct
547        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()); // Should be disabled by inline comment
559    }
560
561    #[test]
562    fn test_lint_rule_filtering() {
563        // Content with no lists
564        let content = "# Heading\nJust text";
565        let rules: Vec<Box<dyn Rule>> = vec![
566            Box::new(MD001HeadingIncrement),
567            // A list-related rule would be skipped
568        ];
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        // Just test that it returns a string without panicking
577        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        // Test that reset_profiling doesn't panic
585        reset_profiling();
586
587        // After reset, report should indicate no measurements or profiling disabled
588        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        // Stats should be a valid HashMap (might be empty)
596        assert!(stats.is_empty() || !stats.is_empty());
597
598        // If not empty, all values should be positive
599        for count in stats.values() {
600            assert!(*count > 0);
601        }
602    }
603
604    #[test]
605    fn test_content_characteristics_edge_cases() {
606        // Test setext heading edge case
607        let chars = ContentCharacteristics::analyze("-"); // Single dash, not a heading
608        assert!(!chars.has_headings);
609
610        let chars = ContentCharacteristics::analyze("--"); // Two dashes, valid setext
611        assert!(chars.has_headings);
612
613        // Test list detection edge cases
614        let chars = ContentCharacteristics::analyze("*emphasis*"); // Not a list
615        assert!(!chars.has_lists);
616
617        let chars = ContentCharacteristics::analyze("1.Item"); // No space after period
618        assert!(!chars.has_lists);
619
620        // Test blockquote must be at start of line
621        let chars = ContentCharacteristics::analyze("text > not a quote");
622        assert!(!chars.has_blockquotes);
623    }
624}