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
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Deserialize;
use std::collections::HashSet;
use std::rc::Rc;
use tree_sitter::Node;

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

// MD052-specific configuration types
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct MD052ReferenceLinksImagesTable {
    #[serde(default)]
    pub shortcut_syntax: bool,
    #[serde(default)]
    pub ignored_labels: Vec<String>,
}

impl Default for MD052ReferenceLinksImagesTable {
    fn default() -> Self {
        Self {
            shortcut_syntax: false,
            ignored_labels: vec!["x".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 ReferenceLink {
    label: String,
    range: tree_sitter::Range,
    is_shortcut: bool,
}

pub(crate) struct MD052Linter {
    context: Rc<Context>,
    definitions: HashSet<String>,
    references: Vec<ReferenceLink>,
}

impl MD052Linter {
    pub fn new(context: Rc<Context>) -> Self {
        Self {
            context,
            definitions: HashSet::new(),
            references: Vec::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
        label
            .to_lowercase()
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn extract_reference_definition(&self, node: &Node) -> Vec<String> {
        // 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];

        let mut definitions = Vec::new();
        for cap in REFERENCE_DEFINITION_PATTERN.captures_iter(content) {
            if let Some(label) = cap.get(1) {
                definitions.push(self.normalize_reference(label.as_str()));
            }
        }
        definitions
    }

    fn extract_reference_links(&self, node: &Node) -> Vec<(String, bool)> {
        // 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), false));
                }
            }
        }

        // 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()), false));
            }
        }

        // Shortcut reference: [label] (only if not caught by other patterns and not inline links)
        if links.is_empty() {
            for cap in SHORTCUT_REFERENCE_PATTERN.captures_iter(content) {
                if let Some(label) = cap.get(1) {
                    // Only consider it a shortcut if it doesn't look like a full/collapsed reference
                    // and there's no second bracket pair after this one
                    let match_end = cap.get(0).unwrap().end();
                    let remaining = &content[match_end..];
                    if !remaining.trim_start().starts_with('[') {
                        links.push((self.normalize_reference(label.as_str()), true));
                    }
                }
            }
        }

        links
    }
}

impl RuleLinter for MD052Linter {
    fn feed(&mut self, node: &Node) {
        match node.kind() {
            // Handle reference definitions like [label]: url
            "paragraph" => {
                let definitions = self.extract_reference_definition(node);
                for definition in definitions {
                    self.definitions.insert(definition);
                }

                // Also check for reference links in paragraphs
                let links = self.extract_reference_links(node);
                for (label, is_shortcut) in links {
                    self.references.push(ReferenceLink {
                        label,
                        range: node.range(),
                        is_shortcut,
                    });
                }
            }
            // Handle reference links [text][label], [label][], [label]
            "link" | "image" => {
                let links = self.extract_reference_links(node);
                for (label, is_shortcut) in links {
                    self.references.push(ReferenceLink {
                        label,
                        range: node.range(),
                        is_shortcut,
                    });
                }
            }
            _ => {
                // Check all other node types for reference definitions
                let definitions = self.extract_reference_definition(node);
                for definition in definitions {
                    self.definitions.insert(definition);
                }
            }
        }
    }

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

        for reference in &self.references {
            // Skip shortcut syntax unless explicitly enabled
            if reference.is_shortcut && !config.shortcut_syntax {
                continue;
            }

            let normalized_label = self.normalize_reference(&reference.label);

            // Skip if label is in ignored list
            if ignored_labels.contains(&normalized_label) {
                continue;
            }

            // Check if definition exists
            if !self.definitions.contains(&normalized_label) {
                violations.push(RuleViolation::new(
                    &MD052,
                    format!(
                        "Missing link or image reference definition: \"{}\"",
                        reference.label
                    ),
                    self.context.file_path.clone(),
                    range_from_tree_sitter(&reference.range),
                ));
            }
        }

        violations
    }
}

pub const MD052: Rule = Rule {
    id: "MD052",
    alias: "reference-links-images",
    tags: &["links", "images"],
    description: "Reference links and images should use a label that is defined",
    rule_type: RuleType::Document,
    required_nodes: &["link", "image", "paragraph"],
    new_linter: |context| Box::new(MD052Linter::new(context)),
};

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

    use crate::config::{LintersSettingsTable, MD052ReferenceLinksImagesTable, 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![("reference-links-images", RuleSeverity::Error)])
    }

    fn test_config_with_settings(
        shortcut_syntax: bool,
        ignored_labels: Vec<String>,
    ) -> crate::config::QuickmarkConfig {
        crate::test_utils::test_helpers::test_config_with_settings(
            vec![("reference-links-images", RuleSeverity::Error)],
            LintersSettingsTable {
                reference_links_images: MD052ReferenceLinksImagesTable {
                    shortcut_syntax,
                    ignored_labels,
                },
                ..Default::default()
            },
        )
    }

    #[test]
    fn test_valid_full_reference() {
        let input = "[Good link][label]

[label]: 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 no violations - valid reference
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_invalid_full_reference() {
        let input = "[Bad link][missing]

[label]: 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 - missing reference definition
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Missing link or image reference definition: \"missing\""));
    }

    #[test]
    fn test_valid_collapsed_reference() {
        let input = "[label][]

[label]: 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 no violations - valid collapsed reference
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_invalid_collapsed_reference() {
        let input = "[missing][]

[label]: 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 - missing reference definition
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Missing link or image reference definition: \"missing\""));
    }

    #[test]
    fn test_shortcut_syntax_disabled_by_default() {
        let input = "[undefined]

[label]: 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 no violations - shortcut syntax ignored by default
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_shortcut_syntax_enabled() {
        let input = "[undefined]

[label]: https://example.com
";

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

        // Should have 1 violation - shortcut syntax enabled and undefined
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Missing link or image reference definition: \"undefined\""));
    }

    #[test]
    fn test_valid_shortcut_syntax_enabled() {
        let input = "[label]

[label]: https://example.com
";

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

        // Should have no violations - shortcut syntax enabled and defined
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_ignored_labels_default_x() {
        let input = "[x] Task item

[label]: 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 no violations - 'x' is ignored by default (GitHub task list)
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_custom_ignored_labels() {
        let input = "[custom] Some text
[another] More text

[label]: https://example.com
";

        let config =
            test_config_with_settings(true, 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 labels are ignored
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_case_insensitive_matching() {
        let input = "[Good Link][LABEL]

[label]: 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 no violations - case insensitive matching per CommonMark
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_whitespace_normalization() {
        let input = "[Good Link][  label   with   spaces  ]

[label with spaces]: 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 no violations - whitespace is normalized per CommonMark
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_images_full_reference() {
        let input = "![Alt text][image]

[image]: https://example.com/image.png
";

        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 - valid image reference
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_images_invalid_reference() {
        let input = "![Alt text][missing]

[image]: https://example.com/image.png
";

        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 - missing image reference definition
        assert_eq!(1, violations.len());
        assert!(violations[0]
            .message()
            .contains("Missing link or image reference definition: \"missing\""));
    }

    #[test]
    fn test_multiple_violations() {
        let input = "[Bad link][missing1]
[Another bad][missing2]
[Good link][valid]

[valid]: 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 2 violations - two missing reference definitions
        assert_eq!(2, violations.len());
    }

    #[test]
    fn test_mixed_link_types() {
        let input = "[Full][label1]
[Collapsed][]
[Shortcut]
![Image][image1]
![Collapsed image][]

[label1]: https://example.com/1
[collapsed]: https://example.com/2
[shortcut]: https://example.com/3
[image1]: https://example.com/image1.png
[collapsed image]: https://example.com/image2.png
";

        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 - all references defined (shortcut ignored by default)
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_duplicate_definitions() {
        let input = "[Good link][label]

[label]: https://example.com/1
[label]: https://example.com/2
";

        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 - first definition wins per CommonMark spec
        assert_eq!(0, violations.len());
    }
}