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
use serde::Deserialize;
use std::rc::Rc;

use regex::Regex;
use tree_sitter::Node;

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

// MD041-specific configuration types
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct MD041FirstLineHeadingTable {
    #[serde(default)]
    pub allow_preamble: bool,
    #[serde(default)]
    pub front_matter_title: String,
    #[serde(default)]
    pub level: u8,
}

impl Default for MD041FirstLineHeadingTable {
    fn default() -> Self {
        Self {
            allow_preamble: false,
            front_matter_title: r"^\s*title\s*[:=]".to_string(),
            level: 1,
        }
    }
}

#[derive(Debug)]
enum FirstElement {
    Heading(u8, tree_sitter::Range), // level, range
    Content(tree_sitter::Range),
    None,
}

pub(crate) struct MD041Linter {
    context: Rc<Context>,
    violations: Vec<RuleViolation>,
    first_element: FirstElement,
    front_matter_end_byte: Option<usize>,
    title_regex: Option<Regex>,
}

impl MD041Linter {
    pub fn new(context: Rc<Context>) -> Self {
        let content = context.get_document_content();
        let front_matter_end_byte = Self::calculate_front_matter_end_byte(&content);

        let config = &context.config.linters.settings.first_line_heading;
        let title_regex = if !config.front_matter_title.is_empty() {
            Some(
                Regex::new(&config.front_matter_title)
                    .unwrap_or_else(|_| Regex::new(r"^\s*title\s*[:=]").unwrap()),
            )
        } else {
            None
        };

        Self {
            context: context.clone(),
            violations: Vec::new(),
            first_element: FirstElement::None,
            front_matter_end_byte,
            title_regex,
        }
    }

    /// Calculates the end byte of the front matter, including the final newline.
    /// This is done by iterating through the lines of the content.
    fn calculate_front_matter_end_byte(content: &str) -> Option<usize> {
        if !content.starts_with("---") {
            return None;
        }

        let mut byte_pos = 0;
        let mut found_start = false;

        let mut remaining = content;
        while let Some(newline_pos) = remaining.find('\n') {
            let line = &remaining[..newline_pos];
            let line_to_check = line.trim_end_matches('\r');

            if line_to_check.trim() == "---" {
                if !found_start {
                    found_start = true;
                } else {
                    return Some(byte_pos + newline_pos + 1);
                }
            }
            byte_pos += newline_pos + 1;
            remaining = &remaining[newline_pos + 1..];
        }

        // Check last line if no newline at end
        if !remaining.is_empty() && remaining.trim() == "---" && found_start {
            return Some(content.len());
        }

        None
    }

    fn extract_heading_level(&self, node: &Node) -> u8 {
        match node.kind() {
            "atx_heading" => {
                for i in 0..node.child_count() {
                    let child = node.child(i).unwrap();
                    let kind = child.kind();
                    if kind.starts_with("atx_h") && kind.ends_with("_marker") {
                        let level_str = &kind["atx_h".len()..kind.len() - "_marker".len()];
                        return level_str.parse::<u8>().unwrap_or(1);
                    }
                }
                1 // fallback
            }
            "setext_heading" => {
                for i in 0..node.child_count() {
                    let child = node.child(i).unwrap();
                    if child.kind() == "setext_h1_underline" {
                        return 1;
                    } else if child.kind() == "setext_h2_underline" {
                        return 2;
                    }
                }
                1 // fallback
            }
            _ => 1,
        }
    }

    fn check_front_matter_has_title(&self) -> bool {
        let Some(title_regex) = &self.title_regex else {
            return false; // Front matter title checking disabled
        };

        let Some(fm_end) = self.front_matter_end_byte else {
            return false; // No front matter found
        };

        let content = self.context.get_document_content();
        let front_matter_content = &content[..fm_end];

        front_matter_content
            .lines()
            .skip(1) // Skip the initial "---"
            .take_while(|line| line.trim() != "---")
            .any(|line| title_regex.is_match(line))
    }

    fn is_html_comment(&self, node: &Node) -> bool {
        if node.kind() == "html_flow" {
            let source = self.context.get_document_content();
            let content = &source[node.start_byte()..node.end_byte()];
            content.trim_start().starts_with("<!--")
        } else {
            false
        }
    }

    fn is_in_front_matter(&self, node: &Node) -> bool {
        if let Some(fm_end) = self.front_matter_end_byte {
            node.start_byte() < fm_end
        } else {
            false
        }
    }

    fn should_ignore_node(&self, node: &Node) -> bool {
        // Ignore front matter nodes
        if self.is_in_front_matter(node) {
            return true;
        }

        // Ignore HTML comments
        if self.is_html_comment(node) {
            return true;
        }

        false
    }

    fn is_content_node(&self, node: &Node) -> bool {
        matches!(
            node.kind(),
            "paragraph"
                | "list"
                | "list_item"
                | "code_block"
                | "fenced_code_block"
                | "blockquote"
                | "table"
                | "thematic_break"
        )
    }
}

impl RuleLinter for MD041Linter {
    fn feed(&mut self, node: &Node) {
        // Skip if we already processed the first element
        if !matches!(self.first_element, FirstElement::None) {
            return;
        }

        // Skip nodes that should be ignored
        if self.should_ignore_node(node) {
            return;
        }

        // Check if this is a heading
        if node.kind() == "atx_heading" || node.kind() == "setext_heading" {
            let level = self.extract_heading_level(node);
            self.first_element = FirstElement::Heading(level, node.range());
            return;
        }

        // Check if this is content
        if self.is_content_node(node) {
            self.first_element = FirstElement::Content(node.range());
        }
    }

    fn finalize(&mut self) -> Vec<RuleViolation> {
        // Check if front matter has title - if so, no violation
        if self.check_front_matter_has_title() {
            return Vec::new();
        }

        let config = &self.context.config.linters.settings.first_line_heading;

        match &self.first_element {
            FirstElement::Heading(level, range) => {
                // First element is a heading - check if it has the correct level
                if *level != config.level {
                    self.violations.push(RuleViolation::new(
                        &MD041,
                        format!(
                            "Expected first heading to be level {}, but found level {}",
                            config.level, level
                        ),
                        self.context.file_path.clone(),
                        range_from_tree_sitter(range),
                    ));
                }
            }
            FirstElement::Content(range) => {
                // First element is content - only a violation if preamble is not allowed
                if !config.allow_preamble {
                    self.violations.push(RuleViolation::new(
                        &MD041,
                        "First line in a file should be a top-level heading".to_string(),
                        self.context.file_path.clone(),
                        range_from_tree_sitter(range),
                    ));
                }
            }
            FirstElement::None => {
                // No content found - this is valid (empty document)
            }
        }

        std::mem::take(&mut self.violations)
    }
}

pub const MD041: Rule = Rule {
    id: "MD041",
    alias: "first-line-heading",
    tags: &["headings"],
    description: "First line in a file should be a top-level heading",
    rule_type: RuleType::Document,
    required_nodes: &[
        "atx_heading",
        "setext_heading",
        "paragraph",
        "list",
        "list_item",
        "code_block",
        "fenced_code_block",
        "blockquote",
        "table",
        "thematic_break",
    ],
    new_linter: |context| Box::new(MD041Linter::new(context)),
};

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

    use crate::config::{LintersSettingsTable, MD041FirstLineHeadingTable, RuleSeverity};
    use crate::linter::MultiRuleLinter;
    use crate::test_utils::test_helpers::test_config_with_settings;

    fn test_config(
        level: u8,
        front_matter_title: &str,
        allow_preamble: bool,
    ) -> crate::config::QuickmarkConfig {
        test_config_with_settings(
            vec![("first-line-heading", RuleSeverity::Error)],
            LintersSettingsTable {
                first_line_heading: MD041FirstLineHeadingTable {
                    level,
                    front_matter_title: front_matter_title.to_string(),
                    allow_preamble,
                },
                ..Default::default()
            },
        )
    }

    #[test]
    fn test_valid_first_line_heading() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "# Title

Some content

## Section 1

Content";

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

    #[test]
    fn test_no_first_line_heading() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "This is some text

# Title

Content";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1);
        assert!(violations[0]
            .message()
            .contains("First line in a file should be a top-level heading"));
    }

    #[test]
    fn test_wrong_level_first_heading() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "## Title

Content";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1);
        assert!(violations[0]
            .message()
            .contains("Expected first heading to be level 1, but found level 2"));
    }

    #[test]
    fn test_custom_level() {
        let config = test_config(2, r"^\s*title\s*[:=]", false);
        let input = "## Title

Content";

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

    #[test]
    fn test_custom_level_wrong_level() {
        let config = test_config(2, r"^\s*title\s*[:=]", false);
        let input = "# Title

Content";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1);
        assert!(violations[0]
            .message()
            .contains("Expected first heading to be level 2, but found level 1"));
    }

    #[test]
    fn test_setext_heading_valid() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "Title
=====

Content";

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

    #[test]
    fn test_setext_heading_wrong_level() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "Title
-----

Content";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1);
        assert!(violations[0]
            .message()
            .contains("Expected first heading to be level 1, but found level 2"));
    }

    #[test]
    fn test_allow_preamble_true() {
        let config = test_config(1, r"^\s*title\s*[:=]", true);
        let input = "This is some preamble text

# Title

Content";

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

    #[test]
    fn test_allow_preamble_false() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "This is some preamble text

# Title

Content";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1);
        assert!(violations[0]
            .message()
            .contains("First line in a file should be a top-level heading"));
    }

    #[test]
    fn test_front_matter_with_title() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "---
layout: post
title: \"Welcome to Jekyll!\"
date: 2015-11-17 16:16:01 -0600
---

This is content without a heading";

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

    #[test]
    fn test_front_matter_without_title() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "---
layout: post
author: John Doe
date: 2015-11-17 16:16:01 -0600
---

This is content without a heading";

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

    #[test]
    fn test_front_matter_title_disabled() {
        let config = test_config(1, "", false); // Empty pattern disables front matter checking
        let input = "---
title: \"Welcome to Jekyll!\"
---

This is content without a heading";

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

    #[test]
    fn test_custom_front_matter_title_regex() {
        let config = test_config(1, r"^\s*heading\s*:", false);
        let input = "---
layout: post
heading: \"My Custom Title\"
---

This is content without a heading";

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

    #[test]
    fn test_comments_before_heading() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "<!-- This is a comment -->

# Title

Content";

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

    #[test]
    fn test_empty_document() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "";

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

    #[test]
    fn test_whitespace_only() {
        let config = test_config(1, r"^\s*title\s*[:=]", false);
        let input = "   \n\n  \n\n# Title\n\nContent";

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