quickmark-core 1.1.0

Lightning-fast Markdown/CommonMark linter core library with tree-sitter based parsing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use tree_sitter::Node;

use crate::{
    linter::{range_from_tree_sitter, RuleViolation},
    rules::{Context, Rule, RuleLinter, RuleType},
};

// MD053-specific configuration types
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct MD053LinkImageReferenceDefinitionsTable {
    #[serde(default)]
    pub ignored_definitions: Vec<String>,
}

impl Default for MD053LinkImageReferenceDefinitionsTable {
    fn default() -> Self {
        Self {
            ignored_definitions: vec!["//".to_string()],
        }
    }
}

// Pre-compiled regex patterns for performance
static FULL_REFERENCE_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"\[([^\]]*)\]\[([^\]]*)\]").unwrap());

static COLLAPSED_REFERENCE_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"\[([^\]]+)\]\[\]").unwrap());

static SHORTCUT_REFERENCE_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"\[([^\]]+)\]").unwrap());

static REFERENCE_DEFINITION_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?m)^\s*\[([^\]]+)\]:\s*").unwrap());

#[derive(Debug, Clone)]
struct ReferenceDefinition {
    label: String,
    range: tree_sitter::Range,
}

pub(crate) struct MD053Linter {
    context: Rc<Context>,
    definitions: HashMap<String, Vec<ReferenceDefinition>>, // Track multiple definitions per label
    references: HashSet<String>,                            // All referenced labels
}

impl MD053Linter {
    pub fn new(context: Rc<Context>) -> Self {
        Self {
            context,
            definitions: HashMap::new(),
            references: HashSet::new(),
        }
    }

    fn normalize_reference(&self, label: &str) -> String {
        // Normalize reference labels according to CommonMark spec:
        // - Convert to lowercase
        // - Trim whitespace
        // - Collapse consecutive whitespace to single spaces
        let mut result = String::with_capacity(label.len());
        let mut prev_was_space = false;

        for ch in label.chars() {
            if ch.is_whitespace() {
                if !prev_was_space && !result.is_empty() {
                    result.push(' ');
                    prev_was_space = true;
                }
            } else {
                result.push(ch.to_lowercase().next().unwrap_or(ch));
                prev_was_space = false;
            }
        }

        // Remove trailing space if present
        if result.ends_with(' ') {
            result.pop();
        }

        result
    }

    fn extract_reference_definition(&self, node: &Node) -> Vec<ReferenceDefinition> {
        // Extract the label from reference definition nodes
        // [label]: url "title"
        let start_byte = node.start_byte();
        let end_byte = node.end_byte();
        let document_content = self.context.document_content.borrow();
        let content = &document_content[start_byte..end_byte];

        REFERENCE_DEFINITION_PATTERN
            .captures_iter(content)
            .filter_map(|cap| {
                cap.get(1).map(|label| {
                    let normalized_label = self.normalize_reference(label.as_str());
                    ReferenceDefinition {
                        label: normalized_label,
                        range: node.range(),
                    }
                })
            })
            .collect()
    }

    fn extract_reference_links(&self, node: &Node) -> Vec<String> {
        // Extract reference links in different formats:
        // Full: [text][label]
        // Collapsed: [label][]
        // Shortcut: [label]
        let start_byte = node.start_byte();
        let end_byte = node.end_byte();
        let document_content = self.context.document_content.borrow();
        let content = &document_content[start_byte..end_byte];

        let mut links = Vec::new();

        // Check for inline links first (contain parentheses - not reference links)
        if content.contains('(') && content.contains(')') {
            return links; // This is an inline link, not a reference link
        }

        // Full reference: [text][label]
        for cap in FULL_REFERENCE_PATTERN.captures_iter(content) {
            if let Some(label) = cap.get(2) {
                let label_str = label.as_str();
                if !label_str.is_empty() {
                    links.push(self.normalize_reference(label_str));
                }
            }
        }

        // Collapsed reference: [label][]
        for cap in COLLAPSED_REFERENCE_PATTERN.captures_iter(content) {
            if let Some(label) = cap.get(1) {
                links.push(self.normalize_reference(label.as_str()));
            }
        }

        // Shortcut reference: [label] - check all potential shortcuts
        // We need to be careful not to double-count references that were already caught by full/collapsed patterns
        let mut shortcut_candidates = Vec::new();
        for cap in SHORTCUT_REFERENCE_PATTERN.captures_iter(content) {
            if let Some(label) = cap.get(1) {
                let full_match = cap.get(0).expect("regex match should have group 0");
                let start = full_match.start();
                let end = full_match.end();
                let remaining = &content[end..];

                // Check if this looks like a shortcut (not immediately followed by [] or [label])
                // We only reject if immediately followed by brackets, not if there's whitespace/newline first
                let immediately_followed_by_bracket = remaining.starts_with('[');
                if !immediately_followed_by_bracket {
                    shortcut_candidates.push((
                        start,
                        end,
                        self.normalize_reference(label.as_str()),
                    ));
                }
            }
        }

        // Filter out shortcut candidates that overlap with already found full/collapsed references
        // Use a HashSet for O(1) lookup performance
        let mut existing_labels: HashSet<String> = links.iter().cloned().collect();
        for (_start, _end, normalized_label) in shortcut_candidates {
            // Check if this shortcut overlaps with any full/collapsed reference we already found
            // For now, we'll use a simple heuristic: if we didn't find this as a full/collapsed reference,
            // and it's not followed by brackets, treat it as a shortcut
            if !existing_labels.contains(&normalized_label) {
                existing_labels.insert(normalized_label.clone());
                links.push(normalized_label);
            }
        }

        links
    }
}

impl RuleLinter for MD053Linter {
    fn feed(&mut self, node: &Node) {
        match node.kind() {
            // Handle reference definitions like [label]: url
            "link_reference_definition" => {
                let definitions = self.extract_reference_definition(node);
                for definition in definitions {
                    self.definitions
                        .entry(definition.label.clone())
                        .or_default()
                        .push(definition);
                }
            }
            // Handle paragraphs for reference links
            "paragraph" => {
                let links = self.extract_reference_links(node);
                for link in links {
                    self.references.insert(link);
                }
            }
            // Handle reference links [text][label], [label][], [label]
            "link" | "image" => {
                let links = self.extract_reference_links(node);
                for link in links {
                    self.references.insert(link);
                }
            }
            _ => {
                // Ignore other node types
            }
        }
    }

    fn finalize(&mut self) -> Vec<RuleViolation> {
        let mut violations = Vec::new();
        let config = &self
            .context
            .config
            .linters
            .settings
            .link_image_reference_definitions;
        let ignored_definitions: HashSet<String> = config
            .ignored_definitions
            .iter()
            .map(|label| self.normalize_reference(label))
            .collect();

        // Check for unused definitions and duplicates
        for (label, definitions) in &self.definitions {
            // Skip if label is in ignored list
            if ignored_definitions.contains(label) {
                continue;
            }

            // Check if definition is unused (no references to it)
            let is_unused = !self.references.contains(label);

            if definitions.len() > 1 {
                // Handle duplicate definitions
                if is_unused {
                    // If unused, report the first definition as unused
                    let first_def = &definitions[0];
                    violations.push(RuleViolation::new(
                        &MD053,
                        format!(
                            "Unused link or image reference definition: \"{}\"",
                            first_def.label
                        ),
                        self.context.file_path.clone(),
                        range_from_tree_sitter(&first_def.range),
                    ));
                }
                // Report all subsequent definitions as duplicates (first definition wins per CommonMark)
                for definition in &definitions[1..] {
                    violations.push(RuleViolation::new(
                        &MD053,
                        format!(
                            "Duplicate link or image reference definition: \"{}\"",
                            definition.label
                        ),
                        self.context.file_path.clone(),
                        range_from_tree_sitter(&definition.range),
                    ));
                }
            } else if is_unused {
                // Single definition that is unused
                let def = &definitions[0];
                violations.push(RuleViolation::new(
                    &MD053,
                    format!(
                        "Unused link or image reference definition: \"{}\"",
                        def.label
                    ),
                    self.context.file_path.clone(),
                    range_from_tree_sitter(&def.range),
                ));
            }
        }

        violations
    }
}

pub const MD053: Rule = Rule {
    id: "MD053",
    alias: "link-image-reference-definitions",
    tags: &["links", "images"],
    description: "Link and image reference definitions should be needed",
    rule_type: RuleType::Document,
    required_nodes: &["link", "image", "paragraph", "link_reference_definition"],
    new_linter: |context| Box::new(MD053Linter::new(context)),
};

#[cfg(test)]
mod test {
    use std::path::PathBuf;

    use crate::config::{
        LintersSettingsTable, MD053LinkImageReferenceDefinitionsTable, RuleSeverity,
    };
    use crate::linter::MultiRuleLinter;
    use crate::test_utils::test_helpers::test_config_with_rules;

    fn test_config() -> crate::config::QuickmarkConfig {
        test_config_with_rules(vec![(
            "link-image-reference-definitions",
            RuleSeverity::Error,
        )])
    }

    fn test_config_with_ignored_definitions(
        ignored_definitions: Vec<String>,
    ) -> crate::config::QuickmarkConfig {
        crate::test_utils::test_helpers::test_config_with_settings(
            vec![("link-image-reference-definitions", RuleSeverity::Error)],
            LintersSettingsTable {
                link_image_reference_definitions: MD053LinkImageReferenceDefinitionsTable {
                    ignored_definitions,
                },
                ..Default::default()
            },
        )
    }

    #[test]
    fn test_unused_definition_basic() {
        let input = "[unused]: https://example.com

Some text.
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have 1 violation - unused reference definition
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Unused link or image reference definition: \"unused\""));
    }

    #[test]
    fn test_used_definition_basic() {
        let input = "[label]: https://example.com

[Good link][label]
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have no violations - definition is used
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_duplicate_definitions() {
        let input = "[label]: https://example.com/1
[label]: https://example.com/2

[Good link][label]
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have 1 violation - duplicate definition (second one)
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Duplicate link or image reference definition: \"label\""));
    }

    #[test]
    fn test_unused_and_duplicate() {
        let input = "[unused1]: https://example.com/1
[unused2]: https://example.com/2
[duplicate]: https://example.com/3
[duplicate]: https://example.com/4

Some text.
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have 4 violations: 2 unused + 1 duplicate + 1 unused (both duplicates are unused)
        assert_eq!(4, violations.len());

        // Check violation types
        let messages: Vec<&str> = violations.iter().map(|v| v.message()).collect();
        let unused_count = messages.iter().filter(|m| m.contains("Unused")).count();
        let duplicate_count = messages.iter().filter(|m| m.contains("Duplicate")).count();

        assert_eq!(3, unused_count); // unused1, unused2, and both duplicate entries are unused
        assert_eq!(1, duplicate_count); // second duplicate entry
    }

    #[test]
    fn test_collapsed_reference_format() {
        let input = "[label]: https://example.com

[label][]
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have no violations - collapsed reference is used
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_shortcut_reference_format() {
        let input = "[label]: https://example.com

[label]
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have no violations - shortcut reference is used
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_image_references() {
        let input = "[image]: https://example.com/image.png
[unused-image]: https://example.com/unused.png

![Alt text][image]
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have 1 violation - unused image reference
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Unused link or image reference definition: \"unused-image\""));
    }

    #[test]
    fn test_case_insensitive_matching() {
        let input = "[Label]: https://example.com

[Good link][LABEL]
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have no violations - case insensitive matching per CommonMark
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_whitespace_normalization() {
        let input = "[  label   with   spaces  ]: https://example.com

[Good link][label with spaces]
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have no violations - whitespace is normalized per CommonMark
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_ignored_definitions_default() {
        let input = "[//]: # (This is a comment)
[unused]: https://example.com

Some text.
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have 1 violation - '//' is ignored by default, but 'unused' is not
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Unused link or image reference definition: \"unused\""));
    }

    #[test]
    fn test_custom_ignored_definitions() {
        let input = "[custom]: https://example.com
[another]: https://example.com
[regular]: https://example.com

[Good link][regular]
";

        let config =
            test_config_with_ignored_definitions(vec!["custom".to_string(), "another".to_string()]);
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have no violations - custom and another are ignored, regular is used
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_mixed_scenarios_comprehensive() {
        let input = "[used-full]: https://example.com/1
[used-collapsed]: https://example.com/2
[used-shortcut]: https://example.com/3
[unused]: https://example.com/4
[duplicate-used]: https://example.com/5
[duplicate-used]: https://example.com/6
[duplicate-unused]: https://example.com/7
[duplicate-unused]: https://example.com/8
[//]: # (Ignored comment)

[Link 1][used-full]
[used-collapsed][]
[used-shortcut]
[Link 2][duplicate-used]
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Expected violations:
        // - unused: unused
        // - duplicate-used (second): duplicate
        // - duplicate-unused (first): unused
        // - duplicate-unused (second): duplicate
        assert_eq!(4, violations.len());

        let messages: Vec<&str> = violations.iter().map(|v| v.message()).collect();
        let unused_count = messages.iter().filter(|m| m.contains("Unused")).count();
        let duplicate_count = messages.iter().filter(|m| m.contains("Duplicate")).count();

        assert_eq!(2, unused_count); // unused + duplicate_unused (first)
        assert_eq!(2, duplicate_count); // duplicate-used (second) + duplicate-unused (second)
    }

    #[test]
    fn test_inline_links_ignored() {
        let input = "[unused]: https://example.com

[Inline link](https://example.com) and [another](https://example.com).
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();

        // Should have 1 violation - unused definition, inline links don't count as references
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Unused link or image reference definition: \"unused\""));
    }
}