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
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},
};

// Pre-compiled regex patterns for image parsing
static IMG_TAG_REGEX: Lazy<Regex> = Lazy::new(|| {
    // Use DOTALL flag to match across newlines and case-insensitive flag
    Regex::new(r"(?si)<(/?)img\b[^>]*>").expect("Invalid img tag regex")
});

static ALT_ATTRIBUTE_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"(?si)\balt\s*=\s*(?:[\"']([^\"']*)['"]|([^\s>]+))"#)
        .expect("Invalid alt attribute regex")
});

static ARIA_HIDDEN_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"(?si)aria-hidden\s*=\s*(?:[\"']([^\"']*)['"]|([^\s>]+))"#)
        .expect("Invalid aria-hidden regex")
});

// Regex patterns for Markdown images
static MARKDOWN_IMAGE_REGEX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"!\[([^\]]*)\]\([^)]+\)").expect("Invalid markdown image regex"));

static MARKDOWN_REFERENCE_IMAGE_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"!\[([^\]]*)\]\[([^\]]*)\]").expect("Invalid markdown reference image regex")
});

static MARKDOWN_REFERENCE_IMAGE_SHORTCUT_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"!\[([^\]]*)\]\[]").expect("Invalid markdown reference image shortcut regex")
});

pub(crate) struct MD045Linter {
    context: Rc<Context>,
    violations: Vec<RuleViolation>,
    line_starts: Vec<usize>,
}

impl MD045Linter {
    pub fn new(context: Rc<Context>) -> Self {
        // Pre-calculate line starts for efficient line/col lookup
        let line_starts: Vec<usize> = std::iter::once(0)
            .chain(
                context
                    .document_content
                    .borrow()
                    .match_indices('\n')
                    .map(|(i, _)| i + 1),
            )
            .collect();

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

    fn is_in_code_context(&self, node: &Node) -> bool {
        // Check if this node is inside a code span or code block
        let mut current = node.parent();
        while let Some(parent) = current {
            match parent.kind() {
                "code_span" | "fenced_code_block" | "indented_code_block" => {
                    return true;
                }
                _ => {
                    current = parent.parent();
                }
            }
        }
        false
    }

    fn contains_inline_code_with_images(&self, content: &str) -> bool {
        // Check if the entire content is a single inline code span containing images
        static CODE_SPAN_WITH_IMG_REGEX: Lazy<Regex> = Lazy::new(|| {
            Regex::new(r"^`[^`]*(?:<img|!\[)[^`]*`\s*(?:and\s*`[^`]*(?:<img|!\[)[^`]*`\s*)*$")
                .expect("Invalid code span with image regex")
        });
        CODE_SPAN_WITH_IMG_REGEX.is_match(content.trim())
    }

    fn find_markdown_image_violations(&self, content: &str) -> Vec<(usize, usize)> {
        let mut ranges = Vec::new();

        // Check inline images: ![alt](url)
        for captures in MARKDOWN_IMAGE_REGEX.captures_iter(content) {
            if let (Some(alt_text), Some(full_match)) = (captures.get(1), captures.get(0)) {
                if alt_text.as_str().is_empty() {
                    ranges.push((full_match.start(), full_match.end()));
                }
            }
        }

        // Check reference images: ![alt][ref]
        for captures in MARKDOWN_REFERENCE_IMAGE_REGEX.captures_iter(content) {
            if let (Some(alt_text), Some(full_match)) = (captures.get(1), captures.get(0)) {
                if alt_text.as_str().is_empty() {
                    ranges.push((full_match.start(), full_match.end()));
                }
            }
        }

        // Check shortcut reference images: ![alt][]
        for captures in MARKDOWN_REFERENCE_IMAGE_SHORTCUT_REGEX.captures_iter(content) {
            if let (Some(alt_text), Some(full_match)) = (captures.get(1), captures.get(0)) {
                if alt_text.as_str().is_empty() {
                    ranges.push((full_match.start(), full_match.end()));
                }
            }
        }

        ranges
    }

    fn find_html_image_violations(&self, content: &str) -> Vec<(usize, usize)> {
        let mut ranges = Vec::new();
        for img_match in IMG_TAG_REGEX.find_iter(content) {
            let img_tag = img_match.as_str();

            // Skip closing tags
            if img_tag.starts_with("</") {
                continue;
            }

            // Check for aria-hidden="true" first
            if let Some(aria_cap) = ARIA_HIDDEN_REGEX.captures(img_tag) {
                let value = aria_cap.get(1).or(aria_cap.get(2));
                if let Some(value_match) = value {
                    if value_match.as_str().to_lowercase() == "true" {
                        continue; // Skip images with aria-hidden="true"
                    }
                }
            }

            // Check for alt attribute with value
            let has_valid_alt = ALT_ATTRIBUTE_REGEX.captures(img_tag).is_some();

            if !has_valid_alt {
                ranges.push((img_match.start(), img_match.end()));
            }
        }
        ranges
    }

    fn add_violation(&mut self, node: &Node, start_offset: usize, end_offset: usize) {
        let start_byte = node.start_byte() + start_offset;
        let end_byte = node.start_byte() + end_offset;

        let (start_line, start_col) = self.byte_to_line_col(start_byte);
        let (end_line, end_col) = self.byte_to_line_col(end_byte);

        let range = range_from_tree_sitter(&tree_sitter::Range {
            start_byte,
            end_byte,
            start_point: tree_sitter::Point {
                row: start_line,
                column: start_col,
            },
            end_point: tree_sitter::Point {
                row: end_line,
                column: end_col,
            },
        });

        let violation = RuleViolation::new(
            &MD045,
            MD045.description.to_string(),
            self.context.file_path.clone(),
            range,
        );
        self.violations.push(violation);
    }

    fn byte_to_line_col(&self, byte_pos: usize) -> (usize, usize) {
        let line = match self.line_starts.binary_search(&byte_pos) {
            Ok(line) => line,
            Err(line) => line - 1,
        };
        let line_start = self.line_starts[line];
        let col = byte_pos - line_start;
        (line, col)
    }
}

pub const MD045: Rule = Rule {
    id: "MD045",
    alias: "no-alt-text",
    tags: &["accessibility", "images"],
    description: "Images should have alternate text (alt text)",
    rule_type: RuleType::Token,
    required_nodes: &["inline", "html_block"],
    new_linter: |context| Box::new(MD045Linter::new(context)),
};

impl RuleLinter for MD045Linter {
    fn feed(&mut self, node: &Node) {
        match node.kind() {
            "inline" | "html_block" => {
                if self.is_in_code_context(node) {
                    return;
                }

                let (markdown_ranges, html_ranges) = {
                    let document_content = self.context.document_content.borrow();
                    let content = &document_content[node.start_byte()..node.end_byte()];

                    if self.contains_inline_code_with_images(content) {
                        (vec![], vec![])
                    } else if node.kind() == "inline" {
                        (
                            self.find_markdown_image_violations(content),
                            self.find_html_image_violations(content),
                        )
                    } else {
                        // html_block
                        (vec![], self.find_html_image_violations(content))
                    }
                };

                for (start, end) in markdown_ranges {
                    self.add_violation(node, start, end);
                }

                for (start, end) in html_ranges {
                    self.add_violation(node, start, end);
                }
            }
            _ => {}
        }
    }

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

#[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-alt-text", RuleSeverity::Error),
            ("no-inline-html", RuleSeverity::Off),
        ])
    }

    #[test]
    fn test_markdown_images_with_alt_text_no_violations() {
        let input = "# Test\n\n![Valid alt text](image.jpg)\n\n![Another valid image](image.jpg \"Title\")\n\n![Reference image with alt][ref]\n\nReference image with alt text ![Alt text reference][ref2]\n\n[ref]: image.jpg\n[ref2]: image.jpg \"Title\"\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();
        assert_eq!(md045_violations.len(), 0);
    }

    #[test]
    fn test_markdown_images_without_alt_text_violations() {
        let input = "# Test\n\n![](image.jpg)\n\n![](image.jpg \"Title\")\n\n![Empty alt](image.jpg) and ![](inline-image.jpg) in text\n\nReference image without alt ![][ref]\n\n[ref]: image.jpg\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();

        // Should find 4 violations:
        // Line 2: ![](image.jpg)
        // Line 4: ![](image.jpg "Title")
        // Line 6: ![](inline-image.jpg)
        // Line 8: ![][ref]
        assert_eq!(md045_violations.len(), 4);
    }

    #[test]
    fn test_html_images_with_alt_attribute_no_violations() {
        let input = "# Test\n\n<img src=\"image.jpg\" alt=\"Valid alt text\" />\n\n<img src=\"image.jpg\" alt=\"Another valid\" >\n\n<IMG SRC=\"image.jpg\" ALT=\"Case insensitive\" />\n\n<img \n  src=\"image.jpg\" \n  alt=\"Multi-line\" \n  />\n\n<img src=\"image.jpg\" alt=\"\" />\n\n<img src=\"image.jpg\" alt='' />\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();
        assert_eq!(md045_violations.len(), 0);
    }

    #[test]
    fn test_html_images_without_alt_attribute_violations() {
        let input = "# Test\n\n<img src=\"image.jpg\" />\n\n<img src=\"image.jpg\" alt>\n\n<IMG SRC=\"image.jpg\" />\n\n<img \n  src=\"image.jpg\" \n  title=\"Title only\" />\n\n<p><img src=\"nested.jpg\"></p>\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();

        // Should find 4 violations:
        // Line 2: <img src="image.jpg" />
        // Line 4: <img src="image.jpg" alt>
        // Line 6: <IMG SRC="image.jpg" />
        // Line 8-10: Multi-line img tag
        // Line 12: nested img tag
        assert_eq!(md045_violations.len(), 5);
    }

    #[test]
    fn test_html_images_with_aria_hidden_no_violations() {
        let input = "# Test\n\n<img src=\"image.jpg\" aria-hidden=\"true\" />\n\n<img src=\"image.jpg\" ARIA-HIDDEN=\"TRUE\" />\n\n<img \n  src=\"image.jpg\" \n  aria-hidden=\"true\"
  />\n\n<img src=\"image.jpg\" aria-hidden='true' />\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();
        assert_eq!(md045_violations.len(), 0);
    }

    #[test]
    fn test_html_images_with_aria_hidden_false_violations() {
        let input = "# Test\n\n<img src=\"image.jpg\" aria-hidden=\"false\" />\n\n<img src=\"image.jpg\" aria-hidden=\"\" />\n\n<img src=\"image.jpg\" aria-hidden=\"other\" />\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();

        // Should find 3 violations (aria-hidden != \"true\")
        assert_eq!(md045_violations.len(), 3);
    }

    #[test]
    fn test_mixed_image_types() {
        let input = "# Test\n\n![Valid alt](image.jpg)\n\n![](no-alt.jpg)\n\n<img src=\"valid.jpg\" alt=\"Valid\" />\n\n<img src=\"no-alt.jpg\" />\n\n<img src=\"hidden.jpg\" aria-hidden=\"true\" />\n\n![Reference valid][ref1]\n\n![][ref2]\n\n[ref1]: image.jpg\n[ref2]: image.jpg\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();

        // Should find 3 violations:
        // Line 4: ![](no-alt.jpg)
        // Line 8: <img src="no-alt.jpg" />
        // Line 14: ![][ref2]
        assert_eq!(md045_violations.len(), 3);
    }

    #[test]
    fn test_multiline_markdown_images() {
        let input = "# Test\n\n![Alt text](image.jpg 
\"Title\")\n\n![](image.jpg 
\"Title\")\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();

        // Should find 1 violation (the second image without alt text)
        assert_eq!(md045_violations.len(), 1);
    }

    #[test]
    fn test_images_in_links() {
        let input = "# Test\n\n[![Alt text](image.jpg)](link.html)\n\n[![](no-alt.jpg)](link.html)\n\n[<img src=\"alt.jpg\" alt=\"Alt\" />](link.html)\n\n[<img src=\"no-alt.jpg\" />](link.html)\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();

        // Should find 2 violations:
        // Line 4: [![](no-alt.jpg)](link.html) - markdown image without alt
        // Line 8: [<img src="no-alt.jpg" />](link.html) - HTML img without alt
        assert_eq!(md045_violations.len(), 2);
    }

    #[test]
    fn test_no_false_positives_in_code_blocks() {
        let input = "# Test\n\n```html\n![](image.jpg)\n<img src=\"image.jpg\" />\n```\n\n    ![](indented-code.jpg)\n    <img src=\"indented.jpg\" />\n\n`![](inline-code.jpg)` and `<img src=\"inline.jpg\" />`\n\nRegular text with ![](actual-image.jpg) should trigger.\n";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md045_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD045")
            .collect();

        // Should only find 1 violation (the actual image outside code blocks)
        assert_eq!(md045_violations.len(), 1);
    }
}