mdbook-lint-rulesets 0.14.4

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
use mdbook_lint_core::Document;
use mdbook_lint_core::error::Result;
use mdbook_lint_core::rule::{Rule, RuleCategory, RuleMetadata};
use mdbook_lint_core::violation::{Fix, Position, Severity, Violation};

/// MD006 - Consider starting bulleted lists at the beginning of the line
pub struct MD006;

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

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

    fn description(&self) -> &'static str {
        "Consider starting bulleted lists at the beginning of the line"
    }

    fn metadata(&self) -> RuleMetadata {
        RuleMetadata::deprecated(
            RuleCategory::Formatting,
            "Removed from markdownlint; MD007 covers list indentation more comprehensively",
            Some("MD007"),
        )
    }

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

    fn check_with_ast<'a>(
        &self,
        document: &Document,
        _ast: Option<&'a comrak::nodes::AstNode<'a>>,
    ) -> Result<Vec<Violation>> {
        let mut violations = Vec::new();
        let lines: Vec<&str> = document.content.lines().collect();
        let in_code_block = self.get_code_block_ranges(&lines);

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

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

            // Skip lines inside code blocks
            if in_code_block[line_number - 1] {
                continue;
            }

            // Check for unordered list markers (*, +, -) that are indented
            if let Some(first_char_pos) = line.find(|c: char| !c.is_whitespace())
                && first_char_pos > 0
            {
                let remaining = &line[first_char_pos..];

                // Check if this is a list item (starts with *, +, or - followed by space)
                if let Some(first_char) = remaining.chars().next()
                    && matches!(first_char, '*' | '+' | '-')
                    && remaining.len() > 1
                {
                    let second_char = remaining.chars().nth(1).unwrap();
                    if second_char.is_whitespace() {
                        // This is an indented unordered list item
                        // Create fix by removing the indentation
                        let fixed_line = format!("{}\n", &line[first_char_pos..]);
                        let fix = Fix {
                            description: format!("Remove {} spaces of indentation", first_char_pos),
                            replacement: Some(fixed_line),
                            start: Position {
                                line: line_number,
                                column: 1,
                            },
                            end: Position {
                                line: line_number,
                                column: line.len() + 1,
                            },
                        };

                        violations.push(
                            self.create_violation_with_fix(
                                "Consider starting bulleted lists at the beginning of the line"
                                    .to_string(),
                                line_number,
                                1,
                                Severity::Warning,
                                fix,
                            ),
                        );
                    }
                }
            }
        }

        Ok(violations)
    }
}

impl MD006 {
    /// Get code block ranges to exclude from checking
    fn get_code_block_ranges(&self, lines: &[&str]) -> Vec<bool> {
        let mut in_code_block = vec![false; lines.len()];
        let mut in_fenced_block = false;
        let mut in_indented_block = false;

        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            // Check for fenced code blocks
            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
                in_fenced_block = !in_fenced_block;
                in_code_block[i] = true;
                continue;
            }

            if in_fenced_block {
                in_code_block[i] = true;
                continue;
            }

            // Check for indented code blocks (4+ spaces at start of line)
            // But not if it's a list item
            if !line.trim().is_empty() && line.starts_with("    ") {
                let trimmed_after_indent = line[4..].trim_start();
                // Check if this is a list item (starts with *, +, or - followed by space)
                let is_list_item = if let Some(first_char) = trimmed_after_indent.chars().next() {
                    matches!(first_char, '*' | '+' | '-')
                        && trimmed_after_indent.len() > 1
                        && trimmed_after_indent
                            .chars()
                            .nth(1)
                            .is_some_and(|c| c.is_whitespace())
                } else {
                    false
                };

                if !is_list_item {
                    in_indented_block = true;
                    in_code_block[i] = true;
                }
            } else if !line.trim().is_empty() {
                in_indented_block = false;
            } else if in_indented_block {
                // Empty lines continue indented code blocks
                in_code_block[i] = true;
            }
        }

        in_code_block
    }
}

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

    #[test]
    fn test_md006_no_violations() {
        let content = r#"# Heading

* Item 1
* Item 2
* Item 3

Some text

+ Item A
+ Item B

More text

- Item X
- Item Y
"#;

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

    #[test]
    fn test_md006_indented_list() {
        let content = r#"# Heading

Some text
 * Indented item 1
 * Indented item 2

More text
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 4);
        assert_eq!(violations[1].line, 5);
        assert!(
            violations[0]
                .message
                .contains("Consider starting bulleted lists")
        );
    }

    #[test]
    fn test_md006_mixed_indentation() {
        let content = r#"* Good item
 * Bad item
* Good item
  + Another bad item
- Good item
"#;

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

    #[test]
    fn test_md006_nested_lists_valid() {
        let content = r#"* Item 1
  * Nested item (this triggers the rule - it's indented)
  * Another nested item
* Item 2
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2); // The nested items are indented
        assert_eq!(violations[0].line, 2);
        assert_eq!(violations[1].line, 3);
    }

    #[test]
    fn test_md006_code_blocks_ignored() {
        let content = r#"# Heading

```
 * This is in a code block
 * Should not trigger the rule
```

 * But this should trigger it
"#;

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

    #[test]
    fn test_md006_blockquotes_ignored() {
        let content = r#"# Heading

> * This is in a blockquote
> * Should not trigger the rule

 * But this should trigger it
"#;

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

    #[test]
    fn test_md006_different_markers() {
        let content = r#" * Asterisk indented
 + Plus indented
 - Dash indented
"#;

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

    #[test]
    fn test_md006_not_list_markers() {
        let content = r#" * Not followed by space
 *Not followed by space
 - Not followed by space
 -Not followed by space
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        let violations = rule.check(&document).unwrap();
        // First and third lines have space after marker, so they trigger the rule
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 3);
    }

    #[test]
    fn test_md006_tab_indentation() {
        let content = "\t* Tab indented item\n\t+ Another tab indented";

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

    #[test]
    fn test_md006_fix_simple_indentation() {
        let content = " * Single space indented\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        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, "Remove 1 spaces of indentation");
        assert_eq!(
            fix.replacement,
            Some("* Single space indented\n".to_string())
        );
    }

    #[test]
    fn test_md006_fix_multiple_spaces() {
        let content = "    * Four spaces indented\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        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, "Remove 4 spaces of indentation");
        assert_eq!(
            fix.replacement,
            Some("* Four spaces indented\n".to_string())
        );
    }

    #[test]
    fn test_md006_fix_tab_indentation() {
        let content = "\t* Tab indented item\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        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, "Remove 1 spaces of indentation");
        assert_eq!(fix.replacement, Some("* Tab indented item\n".to_string()));
    }

    #[test]
    fn test_md006_fix_multiple_items() {
        let content = " * First item\n  + Second item\n   - Third item\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        let violations = rule.check(&document).unwrap();

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

        // First item - 1 space
        assert!(violations[0].fix.is_some());
        let fix1 = violations[0].fix.as_ref().unwrap();
        assert_eq!(fix1.description, "Remove 1 spaces of indentation");
        assert_eq!(fix1.replacement, Some("* First item\n".to_string()));

        // Second item - 2 spaces
        assert!(violations[1].fix.is_some());
        let fix2 = violations[1].fix.as_ref().unwrap();
        assert_eq!(fix2.description, "Remove 2 spaces of indentation");
        assert_eq!(fix2.replacement, Some("+ Second item\n".to_string()));

        // Third item - 3 spaces
        assert!(violations[2].fix.is_some());
        let fix3 = violations[2].fix.as_ref().unwrap();
        assert_eq!(fix3.description, "Remove 3 spaces of indentation");
        assert_eq!(fix3.replacement, Some("- Third item\n".to_string()));
    }

    #[test]
    fn test_md006_fix_preserves_content() {
        let content = "  * Item with **bold** and *italic* text\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        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.replacement,
            Some("* Item with **bold** and *italic* text\n".to_string())
        );
    }

    #[test]
    fn test_md006_fix_mixed_indentation() {
        let content = " * Space indented\n\t+ Tab indented\n  - Two space indented\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        let violations = rule.check(&document).unwrap();

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

        // All should have fixes that remove indentation
        for violation in &violations {
            assert!(violation.fix.is_some());
        }

        assert_eq!(
            violations[0].fix.as_ref().unwrap().replacement,
            Some("* Space indented\n".to_string())
        );
        assert_eq!(
            violations[1].fix.as_ref().unwrap().replacement,
            Some("+ Tab indented\n".to_string())
        );
        assert_eq!(
            violations[2].fix.as_ref().unwrap().replacement,
            Some("- Two space indented\n".to_string())
        );
    }

    #[test]
    fn test_md006_fix_different_markers() {
        let content = "  * Asterisk item\n  + Plus item\n  - Dash item\n";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD006;
        let violations = rule.check(&document).unwrap();

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

        // All should preserve their original markers
        assert_eq!(
            violations[0].fix.as_ref().unwrap().replacement,
            Some("* Asterisk item\n".to_string())
        );
        assert_eq!(
            violations[1].fix.as_ref().unwrap().replacement,
            Some("+ Plus item\n".to_string())
        );
        assert_eq!(
            violations[2].fix.as_ref().unwrap().replacement,
            Some("- Dash item\n".to_string())
        );
    }

    #[test]
    fn test_md006_can_fix() {
        let rule = MD006;
        assert!(rule.can_fix());
    }
}