mdbook-lint-rulesets 0.16.1

Modular rulesets for mdbook-lint - standard and mdBook-specific linting rules
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
use comrak::nodes::{AstNode, NodeValue};
use mdbook_lint_core::Document;
use mdbook_lint_core::error::Result;
use mdbook_lint_core::rule::{AstRule, RuleCategory, RuleMetadata};
use mdbook_lint_core::violation::{Fix, Position, Severity, Violation};

/// MD007 - Unordered list indentation
pub struct MD007 {
    /// Number of spaces for indent (default: 2)
    pub indent: usize,
    /// Spaces for first level indent when start_indented is set (default: 2)
    pub start_indent: usize,
    /// Whether to indent the first level of the list (default: false)
    pub start_indented: bool,
}

impl MD007 {
    pub fn new() -> Self {
        Self {
            indent: 2,
            start_indent: 2,
            start_indented: false,
        }
    }

    #[allow(dead_code)]
    pub fn with_indent(mut self, indent: usize) -> Self {
        self.indent = indent;
        self
    }

    #[allow(dead_code)]
    pub fn with_start_indent(mut self, start_indent: usize) -> Self {
        self.start_indent = start_indent;
        self
    }

    #[allow(dead_code)]
    pub fn with_start_indented(mut self, start_indented: bool) -> Self {
        self.start_indented = start_indented;
        self
    }

    /// Create MD007 from configuration
    pub fn from_config(config: &toml::Value) -> Self {
        let mut rule = Self::new();

        if let Some(indent) = config.get("indent").and_then(|v| v.as_integer()) {
            rule.indent = indent as usize;
        }

        if let Some(start_indent) = config
            .get("start-indent")
            .or_else(|| config.get("start_indent"))
            .and_then(|v| v.as_integer())
        {
            rule.start_indent = start_indent as usize;
        }

        if let Some(start_indented) = config
            .get("start-indented")
            .or_else(|| config.get("start_indented"))
            .and_then(|v| v.as_bool())
        {
            rule.start_indented = start_indented;
        }

        rule
    }

    fn calculate_expected_indent(&self, depth: usize) -> usize {
        if depth == 0 {
            if self.start_indented {
                self.start_indent
            } else {
                0
            }
        } else {
            let base = if self.start_indented {
                self.start_indent
            } else {
                0
            };
            base + depth * self.indent
        }
    }

    fn parse_list_item(&self, line: &str) -> Option<(usize, char, bool)> {
        let mut indent = 0;
        let mut chars = line.chars();

        // Count leading spaces
        while let Some(ch) = chars.next() {
            if ch == ' ' {
                indent += 1;
            } else if ch == '\t' {
                indent += 4; // Treat tab as 4 spaces
            } else if matches!(ch, '*' | '+' | '-') {
                // Check if followed by whitespace (valid list marker)
                if let Some(next_ch) = chars.next()
                    && next_ch.is_whitespace()
                {
                    return Some((indent, ch, false)); // false = unordered
                }
                break;
            } else if ch.is_ascii_digit() {
                // Check for ordered list (digit followed by . or ))
                let mut temp_chars = chars.as_str().chars();
                while let Some(digit_ch) = temp_chars.next() {
                    if digit_ch == '.' || digit_ch == ')' {
                        if let Some(next_ch) = temp_chars.next()
                            && next_ch.is_whitespace()
                        {
                            return Some((indent, ch, true)); // true = ordered
                        }
                        break;
                    } else if !digit_ch.is_ascii_digit() {
                        break;
                    }
                }
                break;
            } else {
                break;
            }
        }

        None
    }

    fn calculate_depth(&self, list_stack: &[(usize, char, bool)], current_indent: usize) -> usize {
        // Find the depth based on indentation level
        for (i, &(stack_indent, _, _)) in list_stack.iter().enumerate() {
            if current_indent <= stack_indent {
                return i;
            }
        }
        list_stack.len()
    }

    fn update_list_stack(
        &self,
        list_stack: &mut Vec<(usize, char, bool)>,
        indent: usize,
        marker: char,
        is_ordered: bool,
    ) {
        // Remove items with greater or equal indentation
        list_stack.retain(|&(stack_indent, _, _)| stack_indent < indent);

        // Add current item
        list_stack.push((indent, marker, is_ordered));
    }

    fn has_ordered_ancestors(&self, list_stack: &[(usize, char, bool)]) -> bool {
        list_stack.iter().any(|&(_, _, is_ordered)| is_ordered)
    }

    /// Get line ranges for code blocks to skip them
    fn get_code_block_line_ranges<'a>(&self, ast: &'a AstNode<'a>) -> Vec<(usize, usize)> {
        let mut ranges = Vec::new();
        self.collect_code_block_ranges(ast, &mut ranges);
        ranges
    }

    /// Recursively collect code block line ranges (both fenced and indented)
    #[allow(clippy::only_used_in_recursion)]
    fn collect_code_block_ranges<'a>(
        &self,
        node: &'a AstNode<'a>,
        ranges: &mut Vec<(usize, usize)>,
    ) {
        if let NodeValue::CodeBlock(_) = &node.data.borrow().value {
            // Fenced or indented code block
            let sourcepos = node.data.borrow().sourcepos;
            if sourcepos.start.line > 0 && sourcepos.end.line > 0 {
                ranges.push((sourcepos.start.line, sourcepos.end.line));
            }
        }

        for child in node.children() {
            self.collect_code_block_ranges(child, ranges);
        }
    }
}

impl Default for MD007 {
    fn default() -> Self {
        Self::new()
    }
}

impl AstRule for MD007 {
    fn id(&self) -> &'static str {
        "MD007"
    }

    fn name(&self) -> &'static str {
        "ul-indent"
    }

    fn description(&self) -> &'static str {
        "Unordered list indentation"
    }

    fn metadata(&self) -> RuleMetadata {
        RuleMetadata::stable(RuleCategory::Formatting)
    }

    fn can_fix(&self) -> bool {
        true
    }

    fn check_ast<'a>(&self, document: &Document, ast: &'a AstNode<'a>) -> Result<Vec<Violation>> {
        let mut violations = Vec::new();
        let lines: Vec<&str> = document.content.lines().collect();

        // YAML frontmatter uses `---` fences and can contain YAML block sequences
        // (`  - item`) that must not be linted as Markdown list indentation.
        let frontmatter_range = document.frontmatter_line_range();

        // Get code block line ranges from AST. comrak renumbers nodes after
        // frontmatter, so shift the ranges back onto the real source lines.
        let frontmatter_offset = document.frontmatter_ast_offset(ast);
        let code_block_lines: Vec<(usize, usize)> = self
            .get_code_block_line_ranges(ast)
            .into_iter()
            .map(|(start, end)| (start + frontmatter_offset, end + frontmatter_offset))
            .collect();

        let mut list_stack: Vec<(usize, char, bool)> = Vec::new(); // (indent, marker, is_ordered)

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

            // Skip frontmatter lines (delimiters and YAML content)
            if let Some((fm_start, fm_end)) = frontmatter_range
                && line_number >= fm_start
                && line_number <= fm_end
            {
                continue;
            }

            // Skip lines inside code blocks
            let in_code_block = code_block_lines
                .iter()
                .any(|(start, end)| line_number >= *start && line_number <= *end);

            if in_code_block {
                continue;
            }

            // Skip empty lines
            if line.trim().is_empty() {
                continue;
            }

            // Check if this line is a list item
            if let Some((indent, marker, is_ordered)) = self.parse_list_item(line) {
                // Only check unordered lists and only if all ancestors are unordered
                if !is_ordered && !self.has_ordered_ancestors(&list_stack) {
                    // Calculate expected indentation
                    let current_depth = self.calculate_depth(&list_stack, indent);
                    let expected_indent = self.calculate_expected_indent(current_depth);

                    if indent != expected_indent {
                        // Create fix by replacing current indentation with expected indentation
                        let fixed_line =
                            format!("{}{}", " ".repeat(expected_indent), &line[indent..]);

                        let fix = Fix {
                            description: format!(
                                "Fix indentation from {} to {} spaces",
                                indent, expected_indent
                            ),
                            replacement: Some(fixed_line),
                            start: Position {
                                line: line_number,
                                column: 1,
                            },
                            end: Position::line_end(line_number, line),
                        };

                        violations.push(self.create_violation_with_fix(
                            format!(
                                "Unordered list indentation: Expected {expected_indent} spaces, found {indent}"
                            ),
                            line_number,
                            indent + 1, // Convert to 1-based column
                            Severity::Warning,
                            fix,
                        ));
                    }
                }

                // Update the list stack
                self.update_list_stack(&mut list_stack, indent, marker, is_ordered);
            } else {
                // Non-list line resets the stack
                list_stack.clear();
            }
        }

        Ok(violations)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mdbook_lint_core::{Document, rule::Rule};
    use std::path::PathBuf;

    #[test]
    fn test_md007_correct_indentation() {
        let content = r#"* Item 1
  * Nested item (2 spaces)
    * Deep nested item (4 spaces)
* Item 2
  * Another nested item
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md007_incorrect_indentation() {
        let content = r#"* Item 1
   * Nested item (3 spaces - wrong!)
     * Deep nested item (5 spaces - wrong!)
* Item 2
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 2);
        assert_eq!(violations[1].line, 3);
        assert!(violations[0].message.contains("Expected 2 spaces, found 3"));
        assert!(violations[1].message.contains("Expected 4 spaces, found 5"));
    }

    #[test]
    fn test_md007_custom_indent() {
        let content = r#"* Item 1
    * Nested item (4 spaces)
        * Deep nested item (8 spaces)
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new().with_indent(4);
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md007_start_indented() {
        let content = r#"  * Item 1 (2 spaces start)
    * Nested item (4 spaces total)
      * Deep nested item (6 spaces total)
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new().with_start_indented(true);
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md007_start_indented_custom() {
        let content = r#"    * Item 1 (4 spaces start)
        * Nested item (8 spaces total)
            * Deep nested item (12 spaces total)
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new()
            .with_start_indented(true)
            .with_start_indent(4)
            .with_indent(4);
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md007_mixed_list_types() {
        let content = r#"1. Ordered item
   * Unordered nested (should be ignored due to ordered parent)
     * Deep nested (should be ignored)
* Unordered item
  * Unordered nested (should be checked)
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0); // Mixed list types are ignored
    }

    #[test]
    fn test_md007_only_unordered_lists() {
        let content = r#"1. Ordered item
   2. Another ordered item (wrong indentation but ignored)
      3. Deep ordered item (also ignored)
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md007_no_indentation_needed() {
        let content = r#"* Item 1
* Item 2
* Item 3
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md007_zero_indentation_with_start_indented() {
        let content = r#"* Item 1 (should be indented)
* Item 2 (should be indented)
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new().with_start_indented(true);
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert!(violations[0].message.contains("Expected 2 spaces, found 0"));
        assert!(violations[1].message.contains("Expected 2 spaces, found 0"));
    }

    #[test]
    fn test_md007_complex_nesting() {
        let content = r#"* Level 1
  * Level 2 correct
    * Level 3 correct
      * Level 4 correct
   * Level 2 wrong (3 spaces)
     * Level 3 wrong (5 spaces)
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 5);
        assert_eq!(violations[1].line, 6);
    }

    #[test]
    fn test_md007_fix_basic_indentation() {
        let content = "* Item 1\n   * Nested item (3 spaces - wrong!)\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);
        assert!(violations[0].fix.is_some());

        let fix = violations[0].fix.as_ref().unwrap();
        assert_eq!(fix.description, "Fix indentation from 3 to 2 spaces");
        assert_eq!(
            fix.replacement,
            Some("  * Nested item (3 spaces - wrong!)".to_string())
        );
        assert_eq!(fix.start.line, 2);
        assert_eq!(fix.start.column, 1);
    }

    #[test]
    fn test_md007_fix_multiple_levels() {
        let content = r#"* Item 1
     * Too many spaces (5)
         * Way too many spaces (9)
"#;
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 2);

        // First violation: 5 spaces should be 2
        assert!(violations[0].fix.is_some());
        let fix1 = violations[0].fix.as_ref().unwrap();
        assert_eq!(fix1.description, "Fix indentation from 5 to 2 spaces");
        assert_eq!(
            fix1.replacement,
            Some("  * Too many spaces (5)".to_string())
        );

        // Second violation: 9 spaces should be 4
        assert!(violations[1].fix.is_some());
        let fix2 = violations[1].fix.as_ref().unwrap();
        assert_eq!(fix2.description, "Fix indentation from 9 to 4 spaces");
        assert_eq!(
            fix2.replacement,
            Some("    * Way too many spaces (9)".to_string())
        );
    }

    #[test]
    fn test_md007_fix_with_custom_indent() {
        let content = r#"* Item 1
  * Wrong for 4-space indent
      * Wrong again
"#;
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new().with_indent(4);
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 2);

        // Should expect 4 spaces
        let fix1 = violations[0].fix.as_ref().unwrap();
        assert_eq!(fix1.description, "Fix indentation from 2 to 4 spaces");
        assert_eq!(
            fix1.replacement,
            Some("    * Wrong for 4-space indent".to_string())
        );

        // Should expect 8 spaces for second level
        let fix2 = violations[1].fix.as_ref().unwrap();
        assert_eq!(fix2.description, "Fix indentation from 6 to 8 spaces");
        assert_eq!(fix2.replacement, Some("        * Wrong again".to_string()));
    }

    #[test]
    fn test_md007_fix_with_start_indented() {
        let content = r#"* No indent (wrong when start_indented)
* Another no indent (also wrong)
  * Correct for level 0 with start_indented
    * Correct for level 1 (4 spaces = 2 base + 2 indent)
"#;
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new().with_start_indented(true);
        let violations = rule.check(&document).unwrap();

        // When start_indented is true, all top-level items without indent are violations
        // Let's just check the first two
        assert!(
            violations.len() >= 2,
            "Expected at least 2 violations, got {}",
            violations.len()
        );

        // First item should have 2 spaces when start_indented is true
        let fix1 = violations[0].fix.as_ref().unwrap();
        assert_eq!(fix1.description, "Fix indentation from 0 to 2 spaces");
        assert_eq!(
            fix1.replacement,
            Some("  * No indent (wrong when start_indented)".to_string())
        );

        // Second item should also have 2 spaces
        let fix2 = violations[1].fix.as_ref().unwrap();
        assert_eq!(fix2.description, "Fix indentation from 0 to 2 spaces");
        assert_eq!(
            fix2.replacement,
            Some("  * Another no indent (also wrong)".to_string())
        );
    }

    #[test]
    fn test_md007_fix_removes_extra_spaces() {
        let content = "* Item\n        * Way too indented (8 spaces for first nested level)\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);

        let fix = violations[0].fix.as_ref().unwrap();
        assert_eq!(fix.description, "Fix indentation from 8 to 2 spaces");
        assert_eq!(
            fix.replacement,
            Some("  * Way too indented (8 spaces for first nested level)".to_string())
        );
    }

    #[test]
    fn test_md007_fix_adds_spaces() {
        let content = "* Item\n * Not enough indent (1 space)\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);

        let fix = violations[0].fix.as_ref().unwrap();
        assert_eq!(fix.description, "Fix indentation from 1 to 2 spaces");
        assert_eq!(
            fix.replacement,
            Some("  * Not enough indent (1 space)".to_string())
        );
    }

    #[test]
    fn test_md007_fix_preserves_content() {
        let content = "* Item\n   * This has **bold** and _italic_ text\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);

        let fix = violations[0].fix.as_ref().unwrap();
        assert_eq!(
            fix.replacement,
            Some("  * This has **bold** and _italic_ text".to_string())
        );
    }

    #[test]
    fn test_md007_fix_with_different_markers() {
        let content = r#"- Dash item
   + Plus item (wrong indent)
     * Star item (wrong indent)
"#;
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 2);

        // Different markers should still get fixed
        let fix1 = violations[0].fix.as_ref().unwrap();
        assert_eq!(
            fix1.replacement,
            Some("  + Plus item (wrong indent)".to_string())
        );

        let fix2 = violations[1].fix.as_ref().unwrap();
        assert_eq!(
            fix2.replacement,
            Some("    * Star item (wrong indent)".to_string())
        );
    }

    #[test]
    fn test_md007_ignores_lists_in_code_blocks() {
        let content = r#"Regular list:
* Item 1
  * Item 2

Code block with list:
```yaml
steps:
  - uses: actions/checkout@v4
  - name: Install mdBook and mdbook-lint
    run: |
      cargo install mdbook
      cargo install mdbook-lint
  - name: Build book
    run: mdbook build
```

Another regular list:
* Item 3
    * Too much indent (4 spaces)
"#;
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        // Should only have 1 violation for the last list item (too much indent)
        // Should NOT have violations for the YAML list inside the code block
        assert_eq!(
            violations.len(),
            1,
            "Should only flag the regular list item with wrong indent, not the YAML in code block"
        );

        // Verify it's the right violation (line 19 is the "    * Too much indent" line)
        assert_eq!(violations[0].line, 19);
        assert!(violations[0].message.contains("Expected 2 spaces, found 4"));
    }

    #[test]
    fn test_md007_ignores_frontmatter_block_sequence() {
        // Regression for issue #406: a YAML block sequence inside frontmatter
        // (`  - item`) must not be linted as mis-indented Markdown list items.
        let content = "---\nstatus: accepted\ndecision-makers:\n  - Alice\n  - Bob\n---\n\n# Title\n\nBody.\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(
            violations.len(),
            0,
            "YAML block sequence in frontmatter should not be flagged"
        );
    }

    #[test]
    fn test_md007_body_list_after_frontmatter_reported_at_real_line() {
        // A genuine list-indentation problem in the body must still be flagged,
        // and reported at the real source line despite the frontmatter above it.
        let content =
            "---\nstatus: accepted\ndeciders:\n  - Alice\n---\n\n# T\n\n* Item\n   * Bad indent\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 10);
        assert!(violations[0].message.contains("Expected 2 spaces, found 3"));
    }

    #[test]
    fn test_md007_ignores_indented_code_blocks() {
        let content = r#"Regular list:
* Item 1
  * Item 2

Indented code block:

    * This is code, not a list
      * Should not be checked
        * Even with multiple levels

Another list:
* Item 3
  * Correct indent
"#;
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD007::new();
        let violations = rule.check(&document).unwrap();

        // Should have no violations - the indented code block should be ignored
        assert_eq!(
            violations.len(),
            0,
            "Should not flag items in indented code blocks"
        );
    }
}