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) -> LintResult {
161    // Use lint_and_index but discard the FileIndex for backward compatibility
162    let (result, _file_index) = lint_and_index(content, rules, verbose, flavor, None);
163    result
164}
165
166/// Build FileIndex only (no linting) for cross-file analysis on cache hits
167///
168/// This is a lightweight function that only builds the FileIndex without running
169/// any rules. Used when we have a cache hit but still need the FileIndex for
170/// cross-file validation.
171///
172/// This avoids the overhead of re-running all rules when only the index data is needed.
173pub fn build_file_index_only(
174    content: &str,
175    rules: &[Box<dyn Rule>],
176    flavor: crate::config::MarkdownFlavor,
177) -> crate::workspace_index::FileIndex {
178    // Compute content hash for change detection
179    let content_hash = compute_content_hash(content);
180    let mut file_index = crate::workspace_index::FileIndex::with_hash(content_hash);
181
182    // Early return for empty content
183    if content.is_empty() {
184        return file_index;
185    }
186
187    // Parse LintContext once with the provided flavor
188    let lint_ctx = crate::lint_context::LintContext::new(content, flavor, None);
189
190    // Only call contribute_to_index for cross-file rules (no rule checking!)
191    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
200/// Lint a file and contribute to workspace index for cross-file analysis
201///
202/// This variant performs linting and optionally populates a `FileIndex` with data
203/// needed for cross-file validation. The FileIndex is populated during linting,
204/// avoiding duplicate parsing.
205///
206/// Returns: (warnings, FileIndex) - the FileIndex contains headings/links for cross-file rules
207pub 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    // Compute content hash for change detection
216    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    // Early return for empty content
223    if content.is_empty() {
224        return (Ok(warnings), file_index);
225    }
226
227    // Parse inline configuration comments once
228    let inline_config = crate::inline_config::InlineConfig::from_content(content);
229
230    // Export inline config data to FileIndex for cross-file rule filtering
231    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    // Analyze content characteristics for rule filtering
236    let characteristics = ContentCharacteristics::analyze(content);
237
238    // Filter rules based on content characteristics
239    let applicable_rules: Vec<_> = rules
240        .iter()
241        .filter(|rule| !characteristics.should_skip_rule(rule.as_ref()))
242        .collect();
243
244    // Calculate skipped rules count before consuming applicable_rules
245    let _total_rules = rules.len();
246    let _applicable_count = applicable_rules.len();
247
248    // Parse LintContext once with the provided flavor
249    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        // Run single-file check
261        let result = rule.check(&lint_ctx);
262
263        match result {
264            Ok(rule_warnings) => {
265                // Filter out warnings for rules disabled via inline comments
266                let filtered_warnings: Vec<_> = rule_warnings
267                    .into_iter()
268                    .filter(|warning| {
269                        // Use the warning's rule_name if available, otherwise use the rule's name
270                        let rule_name_to_check = warning.rule_name.as_deref().unwrap_or(rule.name());
271
272                        // Extract the base rule name for sub-rules like "MD029-style" -> "MD029"
273                        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, // Already 1-indexed
282                        )
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    // Contribute to index for cross-file rules (done after all rules checked)
308    // NOTE: We iterate over ALL rules (not just applicable_rules) because cross-file
309    // rules need to extract data from every file in the workspace, regardless of whether
310    // that file has content that would trigger the rule. For example, MD051 needs to
311    // index headings from files that have no links (like target.md) so that links
312    // FROM other files TO those headings can be validated.
313    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
330/// Run cross-file checks for rules that need workspace-wide validation
331///
332/// This should be called after all files have been linted and the WorkspaceIndex
333/// has been built from the accumulated FileIndex data.
334///
335/// Note: This takes the FileIndex instead of content to avoid re-parsing each file.
336/// The FileIndex was already populated during contribute_to_index in the linting phase.
337///
338/// Rules can use workspace_index methods for cross-file validation:
339/// - `get_file(path)` - to look up headings in target files (for MD051)
340///
341/// Returns additional warnings from cross-file validation.
342pub 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    // Only check rules that need cross-file analysis
353    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                // Filter cross-file warnings based on inline config stored in file_index
361                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
377/// Get the profiling report
378pub fn get_profiling_report() -> String {
379    profiling::get_report()
380}
381
382/// Reset the profiling data
383pub fn reset_profiling() {
384    profiling::reset()
385}
386
387/// Get regex cache statistics for performance monitoring
388pub 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        // Test empty content
401        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        // Test content with headings
413        let chars = ContentCharacteristics::analyze("# Heading");
414        assert!(chars.has_headings);
415
416        // Test setext headings
417        let chars = ContentCharacteristics::analyze("Heading\n=======");
418        assert!(chars.has_headings);
419
420        // Test lists
421        let chars = ContentCharacteristics::analyze("* Item\n- Item 2\n+ Item 3");
422        assert!(chars.has_lists);
423
424        // Test ordered lists
425        let chars = ContentCharacteristics::analyze("1. First\n2. Second");
426        assert!(chars.has_lists);
427
428        // Test links
429        let chars = ContentCharacteristics::analyze("[link](url)");
430        assert!(chars.has_links);
431
432        // Test URLs
433        let chars = ContentCharacteristics::analyze("Visit https://example.com");
434        assert!(chars.has_links);
435
436        // Test images
437        let chars = ContentCharacteristics::analyze("![alt text](image.png)");
438        assert!(chars.has_images);
439
440        // Test code
441        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        // Test emphasis
448        let chars = ContentCharacteristics::analyze("*emphasis* and _more_");
449        assert!(chars.has_emphasis);
450
451        // Test HTML
452        let chars = ContentCharacteristics::analyze("<div>HTML content</div>");
453        assert!(chars.has_html);
454
455        // Test tables
456        let chars = ContentCharacteristics::analyze("| Header | Header |\n|--------|--------|");
457        assert!(chars.has_tables);
458
459        // Test blockquotes
460        let chars = ContentCharacteristics::analyze("> Quote");
461        assert!(chars.has_blockquotes);
462
463        // Test mixed content
464        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)";
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        // Create test rules for different categories
492        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)); // Whitespace rules always run
497
498        // Test skipping based on content
499        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"; // Skips level 3
518        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        // Check the rule field of LintWarning struct
525        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()); // Should be disabled by inline comment
537    }
538
539    #[test]
540    fn test_lint_rule_filtering() {
541        // Content with no lists
542        let content = "# Heading\nJust text";
543        let rules: Vec<Box<dyn Rule>> = vec![
544            Box::new(MD001HeadingIncrement),
545            // A list-related rule would be skipped
546        ];
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        // Just test that it returns a string without panicking
555        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        // Test that reset_profiling doesn't panic
563        reset_profiling();
564
565        // After reset, report should indicate no measurements or profiling disabled
566        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        // Stats should be a valid HashMap (might be empty)
574        assert!(stats.is_empty() || !stats.is_empty());
575
576        // If not empty, all values should be positive
577        for count in stats.values() {
578            assert!(*count > 0);
579        }
580    }
581
582    #[test]
583    fn test_content_characteristics_edge_cases() {
584        // Test setext heading edge case
585        let chars = ContentCharacteristics::analyze("-"); // Single dash, not a heading
586        assert!(!chars.has_headings);
587
588        let chars = ContentCharacteristics::analyze("--"); // Two dashes, valid setext
589        assert!(chars.has_headings);
590
591        // Test list detection edge cases
592        let chars = ContentCharacteristics::analyze("*emphasis*"); // Not a list
593        assert!(!chars.has_lists);
594
595        let chars = ContentCharacteristics::analyze("1.Item"); // No space after period
596        assert!(!chars.has_lists);
597
598        // Test blockquote must be at start of line
599        let chars = ContentCharacteristics::analyze("text > not a quote");
600        assert!(!chars.has_blockquotes);
601    }
602}