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

/// Line length calculation mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LengthMode {
    /// Strict mode: count all characters
    #[default]
    Strict,
    /// Visual mode: exclude URLs from length calculation
    Visual,
}

/// MD013: Line length should not exceed a specified limit
///
/// This rule is triggered when lines exceed a specified length.
/// The default line length is 80 characters.
pub struct MD013 {
    /// Maximum allowed line length
    pub line_length: usize,
    /// Whether to ignore code blocks
    pub ignore_code_blocks: bool,
    /// Whether to ignore tables
    pub ignore_tables: bool,
    /// Whether to ignore headings
    pub ignore_headings: bool,
    /// Whether to ignore link/image reference definition lines
    /// (e.g. `[label]: https://very-long-destination`), which cannot be wrapped
    pub ignore_reference_definitions: bool,
    /// Line length calculation mode
    pub length_mode: LengthMode,
}

impl MD013 {
    /// Create a new MD013 rule with default settings
    pub fn new() -> Self {
        Self {
            line_length: 80,
            ignore_code_blocks: true,
            ignore_tables: true,
            ignore_headings: true,
            ignore_reference_definitions: false,
            length_mode: LengthMode::default(),
        }
    }

    /// Create a new MD013 rule with custom line length
    #[allow(dead_code)]
    pub fn with_line_length(line_length: usize) -> Self {
        Self {
            line_length,
            ignore_code_blocks: true,
            ignore_tables: true,
            ignore_headings: true,
            ignore_reference_definitions: false,
            length_mode: LengthMode::default(),
        }
    }

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

        if let Some(line_length) = config
            .get("line-length")
            .or_else(|| config.get("line_length"))
            .and_then(|v| v.as_integer())
        {
            rule.line_length = line_length as usize;
        }

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

        if let Some(ignore_tables) = config
            .get("ignore-tables")
            .or_else(|| config.get("ignore_tables"))
            .and_then(|v| v.as_bool())
        {
            rule.ignore_tables = ignore_tables;
        }

        if let Some(ignore_headings) = config
            .get("ignore-headings")
            .or_else(|| config.get("ignore_headings"))
            .and_then(|v| v.as_bool())
        {
            rule.ignore_headings = ignore_headings;
        }

        if let Some(ignore_ref_defs) = config
            .get("ignore-reference-definitions")
            .or_else(|| config.get("ignore_reference_definitions"))
            .and_then(|v| v.as_bool())
        {
            rule.ignore_reference_definitions = ignore_ref_defs;
        }

        if let Some(mode) = config
            .get("length-mode")
            .or_else(|| config.get("length_mode"))
            .and_then(|v| v.as_str())
        {
            rule.length_mode = match mode.to_lowercase().as_str() {
                "visual" => LengthMode::Visual,
                _ => LengthMode::Strict,
            };
        }

        rule
    }

    /// Calculate line length based on the configured mode
    fn calculate_length(&self, line: &str) -> usize {
        match self.length_mode {
            LengthMode::Strict => line.len(),
            LengthMode::Visual => self.visual_length(line),
        }
    }

    /// Calculate visual line length by excluding URLs
    fn visual_length(&self, line: &str) -> usize {
        // Regex pattern for URLs (both bare and in markdown links)
        let mut length = line.len();
        let mut search_start = 0;

        // Find and subtract URL lengths
        while let Some(url_start) = line[search_start..]
            .find("http://")
            .or_else(|| line[search_start..].find("https://"))
        {
            let abs_start = search_start + url_start;
            // Find the end of the URL (whitespace, ), ], or end of line)
            let url_end = line[abs_start..]
                .find(|c: char| {
                    c.is_whitespace() || c == ')' || c == ']' || c == '>' || c == '"' || c == '\''
                })
                .map(|pos| abs_start + pos)
                .unwrap_or(line.len());

            let url_len = url_end - abs_start;
            length = length.saturating_sub(url_len);
            search_start = url_end;
        }

        length
    }

    /// Check if a line should be ignored based on rule settings
    fn should_ignore_line(&self, line: &str, in_code_block: bool, in_table: bool) -> bool {
        let trimmed = line.trim_start();

        // Ignore code blocks if configured
        if in_code_block && self.ignore_code_blocks {
            return true;
        }

        // Ignore tables if configured
        if in_table && self.ignore_tables {
            return true;
        }

        // Ignore headings if configured
        if self.ignore_headings && trimmed.starts_with('#') {
            return true;
        }

        // Always ignore lines that are just URLs (common in markdown)
        if trimmed.starts_with("http://") || trimmed.starts_with("https://") {
            return true;
        }

        // Ignore link/image reference definitions if configured. These are of
        // the form `[label]: destination` and cannot be wrapped, so a long
        // destination would otherwise trigger MD013 with no way to fix it.
        if self.ignore_reference_definitions && Self::is_reference_definition(trimmed) {
            return true;
        }

        false
    }

    /// Return true if `trimmed` (already left-trimmed) is a link/image
    /// reference definition line: `[label]: destination [optional title]`.
    fn is_reference_definition(trimmed: &str) -> bool {
        // Must start with a non-empty label in brackets.
        if !trimmed.starts_with('[') {
            return false;
        }
        let Some(close) = trimmed.find(']') else {
            return false;
        };
        let label = &trimmed[1..close];
        if label.is_empty() {
            return false;
        }
        // The label must be immediately followed by ':' and then a destination.
        let rest = &trimmed[close + 1..];
        let Some(after_colon) = rest.strip_prefix(':') else {
            return false;
        };
        !after_colon.trim().is_empty()
    }
}

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

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

    fn name(&self) -> &'static str {
        "line-length"
    }

    fn description(&self) -> &'static str {
        "Line length should not exceed a specified limit"
    }

    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>> {
        // MD013 is line-based and doesn't need AST, so we ignore the ast parameter
        let mut violations = Vec::new();
        let mut in_code_block = false;
        let mut in_table = false;

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

            // Track code block state
            if line.trim_start().starts_with("```") {
                in_code_block = !in_code_block;
                continue;
            }

            // Track table state (simplified - tables have | characters)
            let trimmed = line.trim();
            if !in_code_block && (trimmed.starts_with('|') || trimmed.contains(" | ")) {
                in_table = true;
            } else if in_table && trimmed.is_empty() {
                in_table = false;
            }

            // Check if we should ignore this line
            if self.should_ignore_line(line, in_code_block, in_table) {
                continue;
            }

            // Check line length
            let effective_length = self.calculate_length(line);
            if effective_length > self.line_length {
                let message = match self.length_mode {
                    LengthMode::Strict => format!(
                        "Line length is {} characters, expected no more than {}",
                        effective_length, self.line_length
                    ),
                    LengthMode::Visual => format!(
                        "Line length is {} characters (visual), expected no more than {}",
                        effective_length, self.line_length
                    ),
                };

                violations.push(self.create_violation(
                    message,
                    line_num,
                    self.line_length + 1, // Point to the first character that exceeds limit
                    Severity::Warning,
                ));
            }
        }

        Ok(violations)
    }
}

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

    #[test]
    fn test_md013_reference_definition_option() {
        // A long reference-link definition line (#407).
        let content = "# Title\n\n[this-triggers-md013-when-long-link-and-label]: https://github.com/mdbook-lint/issues\n";

        // Default: the long reference definition IS flagged (backward compatible).
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let flagged = MD013::new().check(&document).unwrap();
        assert_eq!(
            flagged.len(),
            1,
            "reference definitions are flagged by default"
        );
        assert_eq!(flagged[0].line, 3);

        // With ignore_reference_definitions = true: not flagged.
        let cfg: toml::Value = toml::from_str("ignore_reference_definitions = true").unwrap();
        let rule = MD013::from_config(&cfg);
        assert!(rule.ignore_reference_definitions);
        let violations = rule.check(&document).unwrap();
        assert_eq!(
            violations.len(),
            0,
            "long reference definitions are ignored when the option is enabled"
        );

        // A normal long prose line is still flagged with the option on.
        let prose = format!("# Title\n\n{}\n", "word ".repeat(30));
        let doc2 = Document::new(prose, PathBuf::from("test.md")).unwrap();
        assert_eq!(rule.check(&doc2).unwrap().len(), 1);
    }

    #[test]
    fn test_md013_is_reference_definition() {
        assert!(MD013::is_reference_definition(
            "[label]: https://example.com"
        ));
        assert!(MD013::is_reference_definition(
            "[a-b_c]: /local/path \"Title\""
        ));
        // Inline links and images are not reference definitions.
        assert!(!MD013::is_reference_definition(
            "[text](https://example.com)"
        ));
        assert!(!MD013::is_reference_definition("just some prose"));
        assert!(!MD013::is_reference_definition("[]: https://example.com"));
        assert!(!MD013::is_reference_definition("[label]:"));
    }

    #[test]
    fn test_md013_short_lines() {
        let content = "# Short title\n\nThis is a short line.\nAnother short line.";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD013::new();
        let violations = rule.check(&document).unwrap();

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

    #[test]
    fn test_md013_long_line() {
        let long_line = "a".repeat(100);
        let content = format!("# Title\n\n{long_line}");
        let document = Document::new(content, PathBuf::from("test.md")).unwrap();
        let rule = MD013::new();
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].rule_id, "MD013");
        assert_eq!(violations[0].line, 3);
        assert_eq!(violations[0].column, 81);
        assert_eq!(violations[0].severity, Severity::Warning);
        assert!(violations[0].message.contains("100 characters"));
        assert!(violations[0].message.contains("no more than 80"));
    }

    #[test]
    fn test_md013_custom_line_length() {
        let content = "This line is exactly fifty characters long here.";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD013::with_line_length(40);
        let violations = rule.check(&document).unwrap();

        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("48 characters"));
        assert!(violations[0].message.contains("no more than 40"));
    }

    #[test]
    fn test_md013_ignore_headings() {
        let long_heading = format!("# {}", "a".repeat(100));
        let document = Document::new(long_heading, PathBuf::from("test.md")).unwrap();
        let rule = MD013::new(); // ignore_headings is true by default
        let violations = rule.check(&document).unwrap();

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

    #[test]
    fn test_md013_ignore_code_blocks() {
        let content = r#"# Title

```rust
let very_long_line_of_code_that_exceeds_the_normal_line_length_limit_but_should_be_ignored = "value";
```

This is a normal line that should be checked."#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD013::new(); // ignore_code_blocks is true by default
        let violations = rule.check(&document).unwrap();

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

    #[test]
    fn test_md013_ignore_urls() {
        let content = "https://example.com/very/long/path/that/exceeds/normal/line/length/limits/but/should/be/ignored";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD013::new();
        let violations = rule.check(&document).unwrap();

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

    #[test]
    fn test_md013_ignore_tables() {
        let content = r#"# Title

| Column 1 with very long content | Column 2 with very long content | Column 3 with very long content |
|----------------------------------|----------------------------------|----------------------------------|
| Data 1 with very long content   | Data 2 with very long content   | Data 3 with very long content   |

This is a normal line that should be checked if it's too long."#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD013::new(); // ignore_tables is true by default
        let violations = rule.check(&document).unwrap();

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

    #[test]
    fn test_md013_multiple_violations() {
        let long_line = "a".repeat(100);
        let content = format!("Normal line\n{long_line}\nAnother normal line\n{long_line}");
        let document = Document::new(content, PathBuf::from("test.md")).unwrap();
        let rule = MD013::new();
        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_md013_visual_mode_excludes_urls() {
        // Line with URL that would be too long in strict mode but OK in visual mode
        // "See docs at " (12) + URL (60) + " for details" (12) = 84 chars total
        // Visual length = 84 - 60 = 24 chars (under 80)
        let content = "See docs at https://example.com/very/long/path/to/documentation for details";
        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();

        // Strict mode should flag it if over limit
        let strict_rule = MD013::new();
        let strict_violations = strict_rule.check(&document).unwrap();
        // This line is 75 chars, so no violation in strict mode either
        assert_eq!(strict_violations.len(), 0);

        // Now test with a longer line
        let long_content = "See the documentation at https://example.com/very/long/path/to/documentation/that/goes/on/and/on for more details about this feature";
        let long_doc = Document::new(long_content.to_string(), PathBuf::from("test.md")).unwrap();

        // Strict mode should flag it (136 chars)
        let strict_violations = strict_rule.check(&long_doc).unwrap();
        assert_eq!(strict_violations.len(), 1);

        // Visual mode should not flag it (URL is ~90 chars, remaining text is ~46 chars)
        let mut visual_rule = MD013::new();
        visual_rule.length_mode = LengthMode::Visual;
        let visual_violations = visual_rule.check(&long_doc).unwrap();
        assert_eq!(visual_violations.len(), 0);
    }

    #[test]
    fn test_md013_visual_mode_multiple_urls() {
        // Line with multiple URLs
        let content = "Check https://example.com/path1 and https://example.com/path2 for info";

        let mut rule = MD013::new();
        rule.length_mode = LengthMode::Visual;

        // Visual length should exclude both URLs
        let visual_len = rule.visual_length(content);
        // Total: 70 chars, URL1: 27 chars, URL2: 27 chars
        // Visual: 70 - 27 - 27 = 16 chars (just "Check ", " and ", " for info")
        assert!(
            visual_len < 25,
            "Visual length should be small: {}",
            visual_len
        );
    }

    #[test]
    fn test_md013_visual_mode_markdown_link() {
        // Markdown link with long URL
        let content =
            "See [the docs](https://example.com/very/long/path/to/documentation) for details";

        let mut rule = MD013::new();
        rule.length_mode = LengthMode::Visual;

        let visual_len = rule.visual_length(content);
        // URL should be excluded from count
        assert!(
            visual_len < 40,
            "Visual length should exclude URL: {}",
            visual_len
        );
    }

    #[test]
    fn test_md013_from_config_length_mode() {
        let config: toml::Value = toml::from_str(
            r#"
            line-length = 100
            length-mode = "visual"
        "#,
        )
        .unwrap();

        let rule = MD013::from_config(&config);
        assert_eq!(rule.line_length, 100);
        assert_eq!(rule.length_mode, LengthMode::Visual);
    }

    #[test]
    fn test_md013_from_config_length_mode_strict() {
        let config: toml::Value = toml::from_str(
            r#"
            length-mode = "strict"
        "#,
        )
        .unwrap();

        let rule = MD013::from_config(&config);
        assert_eq!(rule.length_mode, LengthMode::Strict);
    }

    #[test]
    fn test_md013_visual_length_no_urls() {
        let rule = MD013::new();
        let line = "This is a normal line without any URLs in it.";
        assert_eq!(rule.visual_length(line), line.len());
    }
}