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

use tree_sitter::Node;

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

// MD024-specific configuration types
#[derive(Debug, PartialEq, Clone, Deserialize, Default)]
pub struct MD024MultipleHeadingsTable {
    #[serde(default)]
    pub siblings_only: bool,
    #[serde(default)]
    pub allow_different_nesting: bool,
}

pub(crate) struct MD024Linter {
    context: Rc<Context>,
    violations: Vec<RuleViolation>,
    headings: Vec<HeadingInfo>,
}

#[derive(Debug, Clone)]
struct HeadingInfo {
    content: String,
    level: u8,
    node_range: tree_sitter::Range,
    parent_path: Vec<String>, // Path from root to parent heading
}

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

    fn extract_heading_content(&self, node: &Node) -> String {
        // Extract text content from heading node
        let source = self.context.get_document_content();
        let start_byte = node.start_byte();
        let end_byte = node.end_byte();
        let full_text = &source[start_byte..end_byte];

        // Remove markdown syntax and trim
        match node.kind() {
            "atx_heading" => {
                // Remove leading #s and trailing #s if present
                let text = full_text
                    .trim_start_matches('#')
                    .trim()
                    .trim_end_matches('#')
                    .trim();
                // Normalize whitespace: replace multiple spaces with single space
                text.split_whitespace().collect::<Vec<_>>().join(" ")
            }
            "setext_heading" => {
                // For setext, take first line (before underline)
                if let Some(line) = full_text.lines().next() {
                    let trimmed = line.trim();
                    // Normalize whitespace: replace multiple spaces with single space
                    trimmed.split_whitespace().collect::<Vec<_>>().join(" ")
                } else {
                    String::new()
                }
            }
            _ => String::new(),
        }
    }

    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();
                    if child.kind().starts_with("atx_h") && child.kind().ends_with("_marker") {
                        return child.kind().chars().nth(5).unwrap().to_digit(10).unwrap() as u8;
                    }
                }
                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 build_parent_path(&self, current_level: u8) -> Vec<String> {
        let mut parent_path = Vec::new();

        // Find all headings at levels less than current_level, in reverse order
        for heading in self.headings.iter().rev() {
            if heading.level < current_level {
                parent_path.insert(0, heading.content.clone());
                // Continue looking for higher-level parents
                if heading.level == 1 {
                    break; // Reached the top level
                }
            }
        }

        parent_path
    }

    fn check_for_duplicate(&mut self, current_heading: &HeadingInfo) {
        let config = &self.context.config.linters.settings.multiple_headings;

        for existing_heading in &self.headings {
            if existing_heading.content == current_heading.content {
                let is_violation = if config.siblings_only {
                    // Only report duplicates within same parent
                    existing_heading.parent_path == current_heading.parent_path
                } else if config.allow_different_nesting {
                    // Allow duplicates at different levels
                    existing_heading.level == current_heading.level
                } else {
                    // Standard behavior: any duplicate is a violation
                    true
                };

                if is_violation {
                    self.violations.push(RuleViolation::new(
                        &MD024,
                        format!(
                            "{} [Duplicate heading: '{}']",
                            MD024.description, current_heading.content
                        ),
                        self.context.file_path.clone(),
                        range_from_tree_sitter(&current_heading.node_range),
                    ));
                    break; // Only report once per duplicate
                }
            }
        }
    }
}

impl RuleLinter for MD024Linter {
    fn feed(&mut self, node: &Node) {
        if node.kind() == "atx_heading" || node.kind() == "setext_heading" {
            let content = self.extract_heading_content(node);
            let level = self.extract_heading_level(node);
            let parent_path = self.build_parent_path(level);

            let heading_info = HeadingInfo {
                content: content.clone(),
                level,
                node_range: node.range(),
                parent_path,
            };

            self.check_for_duplicate(&heading_info);
            self.headings.push(heading_info);
        }
    }

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

pub const MD024: Rule = Rule {
    id: "MD024",
    alias: "no-duplicate-heading",
    tags: &["headings"],
    description: "Multiple headings with the same content",
    rule_type: RuleType::Document,
    required_nodes: &["atx_heading", "setext_heading"],
    new_linter: |context| Box::new(MD024Linter::new(context)),
};

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

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

    fn test_config(
        siblings_only: bool,
        allow_different_nesting: bool,
    ) -> crate::config::QuickmarkConfig {
        test_config_with_settings(
            vec![("no-duplicate-heading", RuleSeverity::Error)],
            LintersSettingsTable {
                multiple_headings: MD024MultipleHeadingsTable {
                    siblings_only,
                    allow_different_nesting,
                },
                ..Default::default()
            },
        )
    }

    #[test]
    fn test_basic_duplicate_headings() {
        let config = test_config(false, false);
        let input = "# Introduction

Some text

## Section 1

Content

## Section 1

More 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("Section 1"));
    }

    #[test]
    fn test_no_duplicates() {
        let config = test_config(false, false);
        let input = "# Introduction

## Section 1

### Subsection A

## Section 2

### Subsection B";

        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_siblings_only_different_parents() {
        let config = test_config(true, false);
        let input = "# Chapter 1

## Introduction

# Chapter 2

## Introduction";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 0); // Should allow duplicate under different parents
    }

    #[test]
    fn test_siblings_only_same_parent() {
        let config = test_config(true, false);
        let input = "# Chapter 1

## Introduction

## Introduction";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1); // Should detect duplicate under same parent
    }

    #[test]
    fn test_allow_different_nesting_levels() {
        let config = test_config(false, true);
        let input = "# Introduction

## Introduction";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 0); // Should allow duplicate at different levels
    }

    #[test]
    fn test_allow_different_nesting_same_level() {
        let config = test_config(false, true);
        let input = "# Introduction

# Introduction";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1); // Should detect duplicate at same level
    }

    #[test]
    fn test_setext_headings() {
        let config = test_config(false, false);
        let input = "Introduction
============

Section 1
---------

Section 1
---------
";

        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("Section 1"));
    }

    #[test]
    fn test_mixed_heading_styles() {
        let config = test_config(false, false);
        let input = "Introduction
============

## Section 1

Section 1
---------
";

        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("Section 1"));
    }

    #[test]
    fn test_complex_hierarchy() {
        let config = test_config(true, false);
        let input = "# Part 1

## Chapter 1

### Introduction

## Chapter 2

### Introduction

# Part 2

## Chapter 1

### Introduction";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        // With siblings_only: "Introduction" under different chapters should be allowed
        // "Chapter 1" under different parts should be allowed
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_whitespace_normalization() {
        let config = test_config(false, false);
        let input = "#   Section   1   

##  Section 1  ";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1); // Should normalize whitespace and detect duplicate
    }

    #[test]
    fn test_empty_headings() {
        let config = test_config(false, false);
        let input = "# 

##

##";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(violations.len(), 1); // Empty headings should be treated as duplicates
    }

    #[test]
    fn test_atx_closed_headings() {
        let config = test_config(false, false);
        let input = "# Introduction #

## Section 1 ##

## Section 1 ##";

        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("Section 1"));
    }

    #[test]
    fn test_both_options_enabled() {
        let config = test_config(true, true);
        let input = "# Chapter 1

## Introduction

# Chapter 2 

## Introduction

### Introduction";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        // siblings_only=true allows "Introduction" under different parents
        // allow_different_nesting=true allows same name at different levels within same parent
        assert_eq!(violations.len(), 0);
    }
}