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

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

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

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

/// MD011 Reversed Link Syntax Rule Linter
///
/// **SINGLE-USE CONTRACT**: This linter is designed for one-time use only.
/// After processing a document (via feed() calls and finalize()), the linter
/// should be discarded. The violations state is not cleared between uses.
pub(crate) struct MD011Linter {
    context: Rc<Context>,
    violations: Vec<RuleViolation>,
    line_offsets: Vec<usize>,
}

impl MD011Linter {
    pub fn new(context: Rc<Context>) -> Self {
        let line_offsets = context
            .lines
            .borrow()
            .iter()
            .scan(0, |state, line| {
                let offset = *state;
                // Assuming LF line endings. The +1 accounts for the newline character.
                *state += line.len() + 1;
                Some(offset)
            })
            .collect();

        Self {
            context,
            violations: Vec::new(),
            line_offsets,
        }
    }

    /// Analyze all lines and store all violations for reporting via finalize().
    /// Context cache is already initialized by MultiRuleLinter.
    fn analyze_all_lines(&mut self) {
        let lines = self.context.lines.borrow();
        let excluded_lines = self.get_excluded_lines();

        for (line_index, line) in lines.iter().enumerate() {
            let line_number = line_index + 1;

            if excluded_lines.contains(&line_number) {
                continue;
            }

            // Find all reversed link patterns in the line and create violations.
            for caps in REVERSED_LINK_REGEX.captures_iter(line) {
                let full_match = caps.get(0).unwrap();
                let pre_char = caps.get(1).unwrap().as_str();
                let link_text = caps.get(2).unwrap().as_str();
                let link_destination = caps.get(3).unwrap().as_str();

                // Skip if either link text or destination ends with backslash (escaped)
                if link_text.ends_with("\\") || link_destination.ends_with("\\") {
                    continue;
                }

                // Manual negative lookahead: skip if followed by opening parenthesis
                let match_end_byte = full_match.end();
                if line.as_bytes().get(match_end_byte) == Some(&b'(') {
                    continue;
                }

                // Calculate position accounting for pre_char
                let match_start_byte = full_match.start() + pre_char.len();
                let match_length_byte = full_match.len() - pre_char.len();

                // Check if this match overlaps with any inline code spans
                if self.overlaps_with_inline_code(line_index, match_start_byte, match_length_byte) {
                    continue;
                }

                let violation =
                    self.create_violation(line_index, match_start_byte, match_length_byte);
                self.violations.push(violation);
            }
        }
    }

    /// Returns a set of line numbers that should be excluded from checking.
    /// This includes code blocks.
    fn get_excluded_lines(&self) -> std::collections::HashSet<usize> {
        let node_cache = self.context.node_cache.borrow();

        ["indented_code_block", "fenced_code_block"]
            .iter()
            .filter_map(|block_type| node_cache.get(*block_type))
            .flatten()
            .flat_map(|node_info| (node_info.line_start + 1)..=(node_info.line_end + 1))
            .collect()
    }

    /// Check if a match overlaps with any inline code spans on the same line.
    fn overlaps_with_inline_code(
        &self,
        line_index: usize,
        match_start: usize,
        match_length: usize,
    ) -> bool {
        let lines = self.context.lines.borrow();
        if let Some(line) = lines.get(line_index) {
            let match_end = match_start + match_length;

            for code_match in INLINE_CODE_REGEX.find_iter(line) {
                let code_start = code_match.start();
                let code_end = code_match.end();

                if match_start < code_end && match_end > code_start {
                    return true;
                }
            }
        }

        false
    }

    /// Creates a RuleViolation for a reversed link at the specified position.
    fn create_violation(
        &self,
        line_index: usize,
        match_start: usize,
        match_length: usize,
    ) -> RuleViolation {
        let message = "Reversed link syntax".to_string();
        let line_start_byte = self.line_offsets[line_index];
        let start_byte = line_start_byte + match_start;
        let end_byte = line_start_byte + match_start + match_length;

        RuleViolation::new(
            &MD011,
            message,
            self.context.file_path.clone(),
            range_from_tree_sitter(&tree_sitter::Range {
                start_byte,
                end_byte,
                start_point: tree_sitter::Point {
                    row: line_index,
                    column: match_start,
                },
                end_point: tree_sitter::Point {
                    row: line_index,
                    column: match_start + match_length,
                },
            }),
        )
    }
}

impl RuleLinter for MD011Linter {
    fn feed(&mut self, node: &Node) {
        // This rule is line-based and only needs to run once.
        // We trigger the analysis on seeing the top-level `document` node.
        if node.kind() == "document" {
            self.analyze_all_lines();
        }
    }

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

pub const MD011: Rule = Rule {
    id: "MD011",
    alias: "no-reversed-links",
    tags: &["links"],
    description: "Reversed link syntax",
    rule_type: RuleType::Line,
    required_nodes: &["indented_code_block", "fenced_code_block"],
    new_linter: |context| Box::new(MD011Linter::new(context)),
};

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

    use crate::config::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![
            ("no-reversed-links", RuleSeverity::Error),
            ("heading-style", RuleSeverity::Off),
            ("heading-increment", RuleSeverity::Off),
        ])
    }

    #[test]
    fn test_basic_reversed_link_violation() {
        let input = "This is a (reversed)[link] example.";

        let config = test_config();
        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!("MD011", violation.rule().id);
        assert_eq!("Reversed link syntax", violation.message());
    }

    #[test]
    fn test_no_violations_correct_syntax() {
        let input = "This is a [correct](link) example.";

        let config = test_config();
        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_multiple_reversed_links() {
        let input = "Here is (one)[link] and (another)[example].";

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

        for violation in &violations {
            assert_eq!("MD011", violation.rule().id);
            assert_eq!("Reversed link syntax", violation.message());
        }
    }

    #[test]
    fn test_escaped_reversed_link_not_flagged() {
        let input = r"This is an escaped \(not)[a-link] example.";

        let config = test_config();
        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_link_text_ending_with_backslash() {
        let input = r"(text\)[link] should not be flagged.";

        let config = test_config();
        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_link_destination_ending_with_backslash() {
        let input = r"(text)[link\\] should not be flagged.";

        let config = test_config();
        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_reversed_link_in_fenced_code_block_ignored() {
        let input = r###"```
This (reversed)[link] should be ignored in code block.
```"###;

        let config = test_config();
        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_reversed_link_in_indented_code_block_ignored() {
        let input = "    This (reversed)[link] should be ignored in indented code block.";

        let config = test_config();
        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_mixed_content_with_some_violations() {
        let input = r###"# Heading

This is a (reversed)[link] example.

```
This (code)[link] should be ignored.
```

And another [correct](link).

Another (bad)[example] here."###;

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(2, violations.len()); // Only the two reversed links outside code blocks

        for violation in &violations {
            assert_eq!("MD011", violation.rule().id);
            assert_eq!("Reversed link syntax", violation.message());
        }
    }

    #[test]
    fn test_markdown_extra_footnote_style() {
        // Footnote references like [^1] should not be flagged
        let input = "For (example)[^1] this should not be flagged.";

        let config = test_config();
        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_complex_urls() {
        let input = "Visit (GitHub)[https://github.com/user/repo#section] for more info.";

        let config = test_config();
        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!("MD011", violation.rule().id);
        assert_eq!("Reversed link syntax", violation.message());
    }

    #[test]
    fn test_at_start_of_line() {
        let input = "(reversed)[link] at start of line.";

        let config = test_config();
        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!("MD011", violation.rule().id);
        assert_eq!("Reversed link syntax", violation.message());
    }

    #[test]
    fn test_nested_parentheses_not_matched() {
        let input = "This (text (with parens))[link] should not match because of nested parens.";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(0, violations.len()); // Regex excludes nested parentheses
    }

    #[test]
    fn test_link_destination_starting_with_caret_or_bracket() {
        // Link destinations starting with ] or ^ should not match
        let input1 = "(text)[^footnote] should not match.";
        let input2 = "(text)[]bracket] should not match.";

        let config = test_config();

        let mut linter =
            MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config.clone(), input1);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());

        let mut linter =
            MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input2);
        let violations = linter.analyze();
        assert_eq!(0, violations.len());
    }

    #[test]
    fn test_followed_by_parenthesis_not_matched() {
        // Pattern followed by opening parenthesis should not match
        let input = "(text)[link](more) should not match.";

        let config = test_config();
        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_reversed_link_in_inline_code_ignored() {
        let input = "This is `a (reversed)[link]` in inline code.";

        let config = test_config();
        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_reversed_link_partially_in_inline_code_ignored() {
        let input = "This is `a (reversed`)[link] in inline code.";

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