mdbook-lint-rulesets 0.14.3

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
//! MD010: Hard tabs
//!
//! This rule checks for hard tab characters in the document.
//!
//! ## Why This Rule Exists
//!
//! Hard tabs can cause formatting inconsistencies:
//!
//! - Tab width varies between editors (2, 4, or 8 spaces)
//! - Mixing tabs and spaces leads to misaligned text
//! - Different markdown renderers may handle tabs differently
//! - Code blocks and indentation become unpredictable
//!
//! ## Examples
//!
//! ### ❌ Incorrect (violates rule)
//!
//! ```text
//! →   This line starts with a tab
//! -→  List item with tab after marker
//! ```→   Code block with tab indent
//! ```
//!
//! (Where → represents a tab character)
//!
//! ### ✅ Correct
//!
//! ```markdown
//!     This line uses spaces for indentation
//! -   List item with spaces after marker
//! ```    Code block with space indent
//! ```
//!
//! ## Configuration
//!
//! ```toml
//! [rules.MD010]
//! code_blocks = true  # Check for tabs in code blocks (default: true)
//! spaces_per_tab = 4  # Number of spaces to replace each tab with (default: 4)
//! ```
//!
//! ## Automatic Fix
//!
//! This rule supports automatic fixing. The fix will:
//!
//! - Replace each tab character with the configured number of spaces
//! - Preserve the visual indentation of your content
//! - Handle tabs in all contexts (text, lists, code blocks)
//!
//! ## When to Disable
//!
//! Consider disabling this rule if:
//!
//! - Your project standard requires tabs
//! - You're working with tab-delimited data files
//! - You're documenting makefiles or other tab-sensitive formats

use mdbook_lint_core::error::Result;
use mdbook_lint_core::rule::{Rule, RuleCategory, RuleMetadata};
use mdbook_lint_core::{
    Document,
    violation::{Fix, Position, Severity, Violation},
};

/// Rule to check for hard tab characters
pub struct MD010 {
    /// Number of spaces that a tab character is equivalent to (for reporting)
    spaces_per_tab: usize,
    /// Whether to check for tabs inside code blocks (default: true)
    code_blocks: bool,
}

impl MD010 {
    /// Create a new MD010 rule with default settings
    pub fn new() -> Self {
        Self {
            spaces_per_tab: 4,
            code_blocks: true,
        }
    }

    /// Create a new MD010 rule with custom tab size
    #[allow(dead_code)]
    pub fn with_spaces_per_tab(spaces_per_tab: usize) -> Self {
        Self {
            spaces_per_tab,
            code_blocks: true,
        }
    }

    /// Set whether to check for tabs inside code blocks
    #[allow(dead_code)]
    pub fn with_code_blocks(mut self, check_code_blocks: bool) -> Self {
        self.code_blocks = check_code_blocks;
        self
    }

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

        if let Some(spaces) = config
            .get("spaces-per-tab")
            .or_else(|| config.get("spaces_per_tab"))
            .and_then(|v| v.as_integer())
        {
            rule.spaces_per_tab = spaces as usize;
        }

        if let Some(code_blocks) = config
            .get("code-blocks")
            .or_else(|| config.get("code_blocks"))
            .and_then(|v| v.as_bool())
        {
            rule.code_blocks = code_blocks;
        }

        rule
    }
}

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

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

    fn name(&self) -> &'static str {
        "no-hard-tabs"
    }

    fn description(&self) -> &'static str {
        "Hard tabs are not allowed"
    }

    fn metadata(&self) -> RuleMetadata {
        RuleMetadata::stable(RuleCategory::Formatting).introduced_in("markdownlint v0.1.0")
    }

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

        for (line_number, line) in document.lines.iter().enumerate() {
            let line_num = line_number + 1; // Convert to 1-based line numbers

            // Track fenced code block boundaries
            let trimmed = line.trim_start();
            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
                in_code_block = !in_code_block;
                // Still check the fence line itself for tabs
            }

            // Skip code block content if code_blocks is false
            if !self.code_blocks
                && in_code_block
                && !trimmed.starts_with("```")
                && !trimmed.starts_with("~~~")
            {
                continue;
            }

            // Check for tab characters
            if let Some(tab_pos) = line.find('\t') {
                let column = tab_pos + 1; // Convert to 1-based column

                // Create replacement string with spaces
                let replacement = " ".repeat(self.spaces_per_tab);

                // Find all tabs in the line to create a complete fix
                let fixed_line = line.replace('\t', &replacement);

                let fix = Fix {
                    description: format!("Replace tab with {} spaces", self.spaces_per_tab),
                    replacement: Some(fixed_line),
                    start: Position {
                        line: line_num,
                        column: 1,
                    },
                    end: Position {
                        line: line_num,
                        column: line.len() + 1,
                    },
                };

                violations.push(self.create_violation_with_fix(
                    format!(
                        "Hard tab character found (consider using {} spaces)",
                        self.spaces_per_tab
                    ),
                    line_num,
                    column,
                    Severity::Warning,
                    fix,
                ));
            }
        }

        Ok(violations)
    }

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

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

    fn create_test_document(content: &str) -> Document {
        Document::new(content.to_string(), PathBuf::from("test.md")).unwrap()
    }

    #[test]
    fn test_md010_no_tabs() {
        let content = "# Heading\n\nNo tabs here.\nJust spaces.";
        let document = create_test_document(content);
        let rule = MD010::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md010_single_tab() {
        let content = "# Heading\n\nLine with\ttab.\nClean line.";
        let document = create_test_document(content);
        let rule = MD010::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].rule_id, "MD010");
        assert_eq!(violations[0].line, 3);
        assert_eq!(violations[0].column, 10);
        assert!(violations[0].message.contains("Hard tab character"));
        assert!(violations[0].message.contains("4 spaces"));

        // Check fix is present
        assert!(violations[0].fix.is_some());
        let fix = violations[0].fix.as_ref().unwrap();
        assert_eq!(fix.description, "Replace tab with 4 spaces");
        assert_eq!(fix.replacement, Some("Line with    tab.".to_string()));
    }

    #[test]
    fn test_md010_multiple_tabs() {
        let content = "# Heading\n\nLine\twith\ttabs.\nAnother\ttab line.";
        let document = create_test_document(content);
        let rule = MD010::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 3);
        assert_eq!(violations[0].column, 5); // First tab position
        assert_eq!(violations[1].line, 4);
        assert_eq!(violations[1].column, 8); // First tab in second line
    }

    #[test]
    fn test_md010_custom_spaces_per_tab() {
        let content = "Line with\ttab.";
        let document = create_test_document(content);
        let rule = MD010::with_spaces_per_tab(2);
        let violations = rule.check(&document).unwrap();

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

    #[test]
    fn test_md010_tab_at_beginning() {
        let content = "\tIndented with tab";
        let document = create_test_document(content);
        let rule = MD010::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].column, 1);
    }

    #[test]
    fn test_md010_only_first_tab_reported() {
        let content = "Line\twith\tmultiple\ttabs";
        let document = create_test_document(content);
        let rule = MD010::new();
        let violations = rule.check(&document).unwrap();

        // Should only report the first tab per line
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].column, 5);
    }

    #[test]
    fn test_md010_fix_single_tab() {
        let content = "Line with\ttab here.";
        let document = create_test_document(content);
        let rule = MD010::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, "Replace tab with 4 spaces");
        assert_eq!(fix.replacement, Some("Line with    tab here.".to_string()));
        assert_eq!(fix.start.line, 1);
        assert_eq!(fix.start.column, 1);
    }

    #[test]
    fn test_md010_fix_multiple_tabs() {
        let content = "Text\twith\tmultiple\ttabs.";
        let document = create_test_document(content);
        let rule = MD010::new();
        let violations = rule.check(&document).unwrap();

        // Only first tab is reported
        assert_eq!(violations.len(), 1);
        assert!(violations[0].fix.is_some());

        let fix = violations[0].fix.as_ref().unwrap();
        // Fix should replace ALL tabs on the line
        assert_eq!(
            fix.replacement,
            Some("Text    with    multiple    tabs.".to_string())
        );
    }

    #[test]
    fn test_md010_fix_custom_spaces() {
        let content = "\tIndented line.";
        let document = create_test_document(content);
        let rule = MD010::with_spaces_per_tab(2);
        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, "Replace tab with 2 spaces");
        assert_eq!(fix.replacement, Some("  Indented line.".to_string()));
    }

    #[test]
    fn test_md010_check_code_blocks_by_default() {
        let content = "```\ncode\twith\ttab\n```\nText\twith tab.";
        let document = create_test_document(content);
        let rule = MD010::new();
        let violations = rule.check(&document).unwrap();

        // By default, code_blocks = true, so tabs in code blocks are flagged
        // This finds tabs on line 2 and line 4
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 2);
        assert_eq!(violations[1].line, 4);
        assert!(violations[0].fix.is_some());
        assert!(violations[1].fix.is_some());
    }

    #[test]
    fn test_md010_skip_code_blocks_when_disabled() {
        let content = "```\ncode\twith\ttab\n```\nText\twith tab.";
        let document = create_test_document(content);
        let rule = MD010::new().with_code_blocks(false);
        let violations = rule.check(&document).unwrap();

        // With code_blocks = false, only the tab outside code blocks is flagged
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 4);
    }

    #[test]
    fn test_md010_skip_code_blocks_with_tilde_fence() {
        let content = "~~~rust\ncode\twith\ttab\n~~~\nText\twith tab.";
        let document = create_test_document(content);
        let rule = MD010::new().with_code_blocks(false);
        let violations = rule.check(&document).unwrap();

        // With code_blocks = false, only the tab outside code blocks is flagged
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 4);
    }

    #[test]
    fn test_md010_multiple_code_blocks() {
        let content = "Text\twith tab.\n```\ncode\ttab\n```\nMore\ttext.\n~~~\nanother\tblock\n~~~\nFinal\ttab.";
        let document = create_test_document(content);
        let rule = MD010::new().with_code_blocks(false);
        let violations = rule.check(&document).unwrap();

        // Only tabs outside code blocks: lines 1, 5, 9
        assert_eq!(violations.len(), 3);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 5);
        assert_eq!(violations[2].line, 9);
    }

    #[test]
    fn test_md010_code_blocks_config_from_toml() {
        let config: toml::Value = toml::from_str(
            r#"
            code_blocks = false
            spaces_per_tab = 2
            "#,
        )
        .unwrap();

        let rule = MD010::from_config(&config);
        assert!(!rule.code_blocks);
        assert_eq!(rule.spaces_per_tab, 2);
    }

    #[test]
    fn test_md010_code_blocks_config_with_hyphen() {
        let config: toml::Value = toml::from_str(
            r#"
            code-blocks = false
            "#,
        )
        .unwrap();

        let rule = MD010::from_config(&config);
        assert!(!rule.code_blocks);
    }

    #[test]
    fn test_md010_fix_preserves_content() {
        let content = "Before\t\tmiddle\t\tafter.";
        let document = create_test_document(content);
        let rule = MD010::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();
        // All tabs should be replaced
        assert_eq!(
            fix.replacement,
            Some("Before        middle        after.".to_string())
        );
    }
}