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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
use serde::Deserialize;
use std::collections::HashSet;
use std::rc::Rc;

use once_cell::sync::Lazy;
use regex::Regex;
use tree_sitter::Node;

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

// MD054-specific configuration types
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct MD054LinkImageStyleTable {
    #[serde(default)]
    pub autolink: bool,
    #[serde(default)]
    pub inline: bool,
    #[serde(default)]
    pub full: bool,
    #[serde(default)]
    pub collapsed: bool,
    #[serde(default)]
    pub shortcut: bool,
    #[serde(default)]
    pub url_inline: bool,
}

impl Default for MD054LinkImageStyleTable {
    fn default() -> Self {
        Self {
            autolink: true,
            inline: true,
            full: true,
            collapsed: true,
            shortcut: true,
            url_inline: true,
        }
    }
}

// Combined regular expressions for detecting different link and image styles.
// This improves performance by reducing the number of passes over the text.
// Groups are used to differentiate between image and link matches.

// Style: [text](url)
static RE_INLINE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(!\[([^\]]*)\]\(([^)]*)\))|((?:^|[^!])\[([^\]]*)\]\(([^)]*)\))").unwrap()
});

// Style: [text][ref]
static RE_FULL_REFERENCE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(!\[([^\]]*)\]\[([^\]]+)\])|((?:^|[^!])\[([^\]]*)\]\[([^\]]+)\])").unwrap()
});

// Style: [ref][]
static RE_COLLAPSED_REFERENCE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(!\[([^\]]+)\]\[\])|((?:^|[^!])\[([^\]]+)\]\[\])").unwrap());

// Style: [ref]
static RE_SHORTCUT_REFERENCE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(!\[([^\]]+)\])|((?:^|[^!])\[([^\]]+)\])").unwrap());

// Style: <url>
static RE_AUTOLINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"<(https?://[^>]+)>").unwrap());

/// MD054 - Link and image style
///
/// This rule controls which styles of links and images are allowed in the document.
pub(crate) struct MD054Linter {
    context: Rc<Context>,
    violations: Vec<RuleViolation>,
}

impl MD054Linter {
    pub fn new(context: Rc<Context>) -> Self {
        Self {
            context,
            violations: Vec::new(),
        }
    }
}

impl RuleLinter for MD054Linter {
    fn feed(&mut self, node: &Node) {
        if node.kind() == "inline" {
            self.check_inline_content(node);
        }
    }

    fn finalize(&mut self) -> Vec<RuleViolation> {
        std::mem::take(&mut self.violations)
    }
}

impl MD054Linter {
    fn check_inline_content(&mut self, node: &Node) {
        let content = {
            let document_content = self.context.document_content.borrow();
            node.utf8_text(document_content.as_bytes())
                .unwrap_or("")
                .to_string()
        };

        if !content.is_empty() {
            self.check_content_for_violations(&content, node);
        }
    }

    fn check_content_for_violations(&mut self, content: &str, node: &Node) {
        let config = self
            .context
            .config
            .linters
            .settings
            .link_image_style
            .clone();
        let mut found_violations = HashSet::new();

        // Check for autolinks
        if !config.autolink {
            for caps in RE_AUTOLINK.captures_iter(content) {
                let start = caps.get(0).unwrap().start();
                if found_violations.insert(("autolink", start)) {
                    self.create_violation_at_offset(
                        node,
                        content,
                        start,
                        "Autolinks are not allowed".to_string(),
                    );
                }
            }
        }

        // Check for inline style
        for caps in RE_INLINE.captures_iter(content) {
            // Group 1: Image match `![]()`
            if let Some(image_match) = caps.get(1) {
                if !config.inline && found_violations.insert(("inline_image", image_match.start()))
                {
                    self.create_violation_at_offset(
                        node,
                        content,
                        image_match.start(),
                        "Inline images are not allowed".to_string(),
                    );
                }
            }
            // Group 4: Link match `[]()`
            else if let Some(link_match) = caps.get(4) {
                let mut start = link_match.start();
                if !link_match.as_str().starts_with('[') {
                    start += 1; // Adjust for `(?:^|[^!])`
                }

                if !config.inline {
                    if found_violations.insert(("inline_link", start)) {
                        self.create_violation_at_offset(
                            node,
                            content,
                            start,
                            "Inline links are not allowed".to_string(),
                        );
                    }
                    continue; // If disallowed, no need for further checks
                }

                // Check for url_inline: [https://...](https://...)
                if !config.url_inline {
                    if let (Some(text), Some(url)) = (caps.get(5), caps.get(6)) {
                        if text.as_str() == url.as_str()
                            && found_violations.insert(("url_inline", start))
                        {
                            self.create_violation_at_offset(
                                node,
                                content,
                                start,
                                "Inline links with matching URL text are not allowed".to_string(),
                            );
                        }
                    }
                }
            }
        }

        // Check for full reference style
        if !config.full {
            for caps in RE_FULL_REFERENCE.captures_iter(content) {
                if let Some(image_match) = caps.get(1) {
                    if found_violations.insert(("full_image", image_match.start())) {
                        self.create_violation_at_offset(
                            node,
                            content,
                            image_match.start(),
                            "Full reference images are not allowed".to_string(),
                        );
                    }
                } else if let Some(link_match) = caps.get(4) {
                    let mut start = link_match.start();
                    if !link_match.as_str().starts_with('[') {
                        start += 1;
                    }
                    if found_violations.insert(("full_link", start)) {
                        self.create_violation_at_offset(
                            node,
                            content,
                            start,
                            "Full reference links are not allowed".to_string(),
                        );
                    }
                }
            }
        }

        // Check for collapsed reference style
        if !config.collapsed {
            for caps in RE_COLLAPSED_REFERENCE.captures_iter(content) {
                if let Some(image_match) = caps.get(1) {
                    if found_violations.insert(("collapsed_image", image_match.start())) {
                        self.create_violation_at_offset(
                            node,
                            content,
                            image_match.start(),
                            "Collapsed reference images are not allowed".to_string(),
                        );
                    }
                } else if let Some(link_match) = caps.get(3) {
                    let mut start = link_match.start();
                    if !link_match.as_str().starts_with('[') {
                        start += 1;
                    }
                    if found_violations.insert(("collapsed_link", start)) {
                        self.create_violation_at_offset(
                            node,
                            content,
                            start,
                            "Collapsed reference links are not allowed".to_string(),
                        );
                    }
                }
            }
        }

        // Check for shortcut reference style
        if !config.shortcut {
            for caps in RE_SHORTCUT_REFERENCE.captures_iter(content) {
                let whole_match = caps.get(0).unwrap();
                let end_offset = whole_match.end();

                // Check character after match to avoid false positives for other link types
                if end_offset < content.len() {
                    if let Some(next_char) = content[end_offset..].chars().next() {
                        if next_char == '(' || next_char == '[' {
                            continue;
                        }
                    }
                }

                if let Some(image_match) = caps.get(1) {
                    if found_violations.insert(("shortcut_image", image_match.start())) {
                        self.create_violation_at_offset(
                            node,
                            content,
                            image_match.start(),
                            "Shortcut reference images are not allowed".to_string(),
                        );
                    }
                } else if let Some(link_match) = caps.get(3) {
                    let mut start = link_match.start();
                    if !link_match.as_str().starts_with('[') {
                        start += 1;
                    }
                    if found_violations.insert(("shortcut_link", start)) {
                        self.create_violation_at_offset(
                            node,
                            content,
                            start,
                            "Shortcut reference links are not allowed".to_string(),
                        );
                    }
                }
            }
        }
    }

    fn create_violation_at_offset(
        &mut self,
        node: &Node,
        content: &str,
        offset: usize,
        message: String,
    ) {
        // Calculate the line number within the content where the violation occurs
        let lines_before_offset = content[..offset].matches('\n').count();
        let node_start_row = node.start_position().row;
        let violation_row = node_start_row + lines_before_offset;

        // Create a custom range for this specific violation
        let violation_range = tree_sitter::Range {
            start_byte: node.start_byte() + offset,
            end_byte: node.start_byte() + offset + 1, // Just mark the start of the violation
            start_point: tree_sitter::Point {
                row: violation_row,
                column: if lines_before_offset == 0 {
                    node.start_position().column + offset
                } else {
                    // Calculate column position for this line
                    let line_start = content[..offset].rfind('\n').map(|i| i + 1).unwrap_or(0);
                    offset - line_start
                },
            },
            end_point: tree_sitter::Point {
                row: violation_row,
                column: if lines_before_offset == 0 {
                    node.start_position().column + offset + 1
                } else {
                    let line_start = content[..offset].rfind('\n').map(|i| i + 1).unwrap_or(0);
                    offset - line_start + 1
                },
            },
        };

        self.violations.push(RuleViolation::new(
            &MD054,
            message,
            self.context.file_path.clone(),
            range_from_tree_sitter(&violation_range),
        ));
    }
}

pub const MD054: Rule = Rule {
    id: "MD054",
    alias: "link-image-style",
    tags: &["links", "images"],
    description: "Link and image style",
    rule_type: RuleType::Token,
    required_nodes: &["inline"],
    new_linter: |context| Box::new(MD054Linter::new(context)),
};

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

    use crate::config::{MD054LinkImageStyleTable, 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-style", RuleSeverity::Error),
            ("heading-style", RuleSeverity::Off),
            ("heading-increment", RuleSeverity::Off),
            ("line-length", RuleSeverity::Off),
        ])
    }

    fn test_config_with_settings(
        settings: MD054LinkImageStyleTable,
    ) -> crate::config::QuickmarkConfig {
        let mut config = test_config();
        config.linters.settings.link_image_style = settings;
        config
    }

    // Test cases for autolinks
    #[test]
    fn test_autolink_allowed() {
        let input = "<https://example.com>";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            autolink: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_autolink_disallowed() {
        let input = "<https://example.com>";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            autolink: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation.message().to_lowercase().contains("autolink"));
    }

    // Test cases for inline links
    #[test]
    fn test_inline_link_allowed() {
        let input = "[example](https://example.com)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_inline_link_disallowed() {
        let input = "[example](https://example.com)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation.message().to_lowercase().contains("inline"));
    }

    // Test cases for inline images
    #[test]
    fn test_inline_image_allowed() {
        let input = "![alt text](https://example.com/image.jpg)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_inline_image_disallowed() {
        let input = "![alt text](https://example.com/image.jpg)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation.message().to_lowercase().contains("inline"));
    }

    // Test cases for full reference links
    #[test]
    fn test_full_reference_link_allowed() {
        let input = "[example][ref]\n\n[ref]: https://example.com";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            full: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_full_reference_link_disallowed() {
        let input = "[example][ref]\n\n[ref]: https://example.com";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            full: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation
            .message()
            .to_lowercase()
            .contains("full reference"));
    }

    // Test cases for full reference images
    #[test]
    fn test_full_reference_image_allowed() {
        let input = "![alt text][ref]\n\n[ref]: https://example.com/image.jpg";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            full: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_full_reference_image_disallowed() {
        let input = "![alt text][ref]\n\n[ref]: https://example.com/image.jpg";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            full: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation
            .message()
            .to_lowercase()
            .contains("full reference"));
    }

    // Test cases for collapsed reference links
    #[test]
    fn test_collapsed_reference_link_allowed() {
        let input = "[example][]\n\n[example]: https://example.com";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            collapsed: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_collapsed_reference_link_disallowed() {
        let input = "[example][]\n\n[example]: https://example.com";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            collapsed: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation
            .message()
            .to_lowercase()
            .contains("collapsed reference"));
    }

    // Test cases for collapsed reference images
    #[test]
    fn test_collapsed_reference_image_allowed() {
        let input = "![example][]\n\n[example]: https://example.com/image.jpg";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            collapsed: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_collapsed_reference_image_disallowed() {
        let input = "![example][]\n\n[example]: https://example.com/image.jpg";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            collapsed: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation
            .message()
            .to_lowercase()
            .contains("collapsed reference"));
    }

    // Test cases for shortcut reference links
    #[test]
    fn test_shortcut_reference_link_allowed() {
        let input = "[example]\n\n[example]: https://example.com";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            shortcut: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_shortcut_reference_link_disallowed() {
        let input = "[example]\n\n[example]: https://example.com";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            shortcut: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation
            .message()
            .to_lowercase()
            .contains("shortcut reference"));
    }

    // Test cases for shortcut reference images
    #[test]
    fn test_shortcut_reference_image_allowed() {
        let input = "![example]\n\n[example]: https://example.com/image.jpg";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            shortcut: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_shortcut_reference_image_disallowed() {
        let input = "![example]\n\n[example]: https://example.com/image.jpg";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            shortcut: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation
            .message()
            .to_lowercase()
            .contains("shortcut reference"));
    }

    // Test cases for url_inline
    #[test]
    fn test_url_inline_link_allowed() {
        let input = "[https://example.com](https://example.com)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            url_inline: true,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_url_inline_link_disallowed() {
        let input = "[https://example.com](https://example.com)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            url_inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());
        let violation = &violations[0];
        assert_eq!("MD054", violation.rule().id);
        assert!(violation
            .message()
            .to_lowercase()
            .contains("matching url text"));
    }

    // Test multiple configuration options disabled
    #[test]
    fn test_multiple_styles_disallowed() {
        let input = r#"
[inline link](https://example.com)
<https://example.com>
[reference][ref]

[ref]: https://example.com
"#;
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            autolink: false,
            inline: false,
            full: false,
            collapsed: true,
            shortcut: true,
            url_inline: true,
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(3, violations.len()); // Should catch inline, autolink, and full reference
        for violation in &violations {
            assert_eq!("MD054", violation.rule().id);
        }
    }

    // Test all styles allowed (default)
    #[test]
    fn test_all_styles_allowed() {
        let input = r#"
[inline link](https://example.com)
<https://example.com>
[reference][ref]
[collapsed][]
[shortcut]
[https://example.com](https://example.com)

[ref]: https://example.com
[collapsed]: https://example.com
[shortcut]: https://example.com
"#;
        let config = test_config(); // Default config allows all
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    // Test line number accuracy for multiline content
    #[test]
    fn test_line_numbers_multiline_content() {
        let input = "Here is some text.\n\n[Link 1](https://example.com)\n\nSome more text here.\n\n[Link 2](https://github.com)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(2, violations.len());

        // Verify line numbers match exactly what original markdownlint reports
        assert_eq!(2, violations[0].location().range.start.line); // Line 3 (0-indexed)
        assert_eq!(6, violations[1].location().range.start.line); // Line 7 (0-indexed)
    }

    // Test bracket offset calculation with preceding text
    #[test]
    fn test_bracket_offset_with_preceding_text() {
        let input = "Text [link](url) more text";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());

        // Should point to the '[' character, not any preceding character
        assert_eq!(5, violations[0].location().range.start.character); // Column should be at start of line (tree-sitter groups text)
    }

    // Test newline handling in regex patterns
    #[test]
    fn test_newline_before_link() {
        let input = "\n[Link text](https://example.com)\n[GitHub](https://github.com)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(2, violations.len());

        // Line numbers should be correct even with leading newlines
        assert_eq!(1, violations[0].location().range.start.line); // Second line (0-indexed)
        assert_eq!(2, violations[1].location().range.start.line); // Third line (0-indexed)
    }

    // Test multiple links on same line
    #[test]
    fn test_multiple_links_same_line() {
        let input = "[Link1](url1) and [Link2](url2)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(2, violations.len());

        // Both violations should be on line 1
        assert_eq!(0, violations[0].location().range.start.line);
        assert_eq!(0, violations[1].location().range.start.line);
    }

    // Test reference link bracket offset calculation
    #[test]
    fn test_reference_link_bracket_offset() {
        let input = "Text [ref link][reference] more";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            full: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());

        // Should correctly identify the full reference link
        assert!(violations[0].message().contains("Full reference"));
    }

    // Test collapsed reference bracket offset
    #[test]
    fn test_collapsed_reference_bracket_offset() {
        let input = "Text [collapsed][] more text";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            collapsed: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());

        // Should correctly identify the collapsed reference link
        assert!(violations[0].message().contains("Collapsed reference"));
    }

    // Test shortcut reference bracket offset
    #[test]
    fn test_shortcut_reference_bracket_offset() {
        let input = "Text [shortcut] more text\n\n[shortcut]: https://example.com";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            shortcut: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());

        // Should correctly identify the shortcut reference link
        assert!(violations[0].message().contains("Shortcut reference"));
    }

    // Test regex patterns that start with non-bracket characters
    #[test]
    fn test_regex_non_bracket_start() {
        let input = "!\n[Link after exclamation](url)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());

        // Should be reported on line 2 where the actual link is, not line 1 with the !
        assert_eq!(1, violations[0].location().range.start.line);
    }

    // Test line number accuracy parity with original markdownlint
    #[test]
    fn test_parity_line_numbers() {
        // This input matches what we tested against original markdownlint
        let input = "# MD054 Violations Test Cases\n\nThis file contains examples.\n\n<https://example.com>\n[Link text](https://example.com)";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            autolink: false,
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(2, violations.len());

        // These line numbers should match exactly what original markdownlint reports
        assert_eq!(4, violations[0].location().range.start.line); // Autolink on line 5 (0-indexed: 4)
        assert_eq!(5, violations[1].location().range.start.line); // Inline link on line 6 (0-indexed: 5)
    }

    // Test image bracket offset calculation (images don't use the (?:^|[^!]) pattern)
    #[test]
    fn test_image_bracket_offset() {
        let input = "Text ![alt](image.jpg) more text";
        let config = test_config_with_settings(MD054LinkImageStyleTable {
            inline: false,
            ..Default::default()
        });
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(1, violations.len());

        // Should correctly identify the inline image
        assert!(violations[0].message().contains("Inline images"));
        assert_eq!(0, violations[0].location().range.start.line);
    }
}