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
//! MD039: Spaces inside link text
//!
//! This rule checks for unnecessary spaces at the beginning or end of link text.

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

/// Rule to check for spaces inside link text
pub struct MD039;

impl MD039 {
    /// Find link violations in a line
    fn check_line_links(
        &self,
        document: &Document,
        line: &str,
        line_number: usize,
    ) -> Vec<Violation> {
        let mut violations = Vec::new();
        let chars: Vec<char> = line.chars().collect();
        let mut i = 0;

        while i < chars.len() {
            if chars[i] == '[' {
                // Skip if this is an image (preceded by !)
                if i > 0 && chars[i - 1] == '!' {
                    i += 1;
                    continue;
                }

                // Look for closing bracket
                if let Some(end_bracket) = self.find_closing_bracket(&chars, i + 1) {
                    let link_text = &chars[i + 1..end_bracket];

                    // Check if this is followed by a link URL or reference
                    let is_link = if end_bracket + 1 < chars.len() {
                        chars[end_bracket + 1] == '(' || chars[end_bracket + 1] == '['
                    } else {
                        false
                    };

                    if is_link && self.has_unnecessary_spaces(link_text) {
                        // Create fix by trimming the link text
                        let fixed_text = self.fix_link_text(link_text);

                        // Convert character indices to byte indices for string slicing
                        let byte_start: usize = chars[..i].iter().map(|c| c.len_utf8()).sum();
                        let byte_end: usize =
                            chars[..end_bracket + 1].iter().map(|c| c.len_utf8()).sum();

                        let mut replacement = String::new();
                        replacement.push_str(&line[..byte_start]);
                        replacement.push('[');
                        replacement.push_str(&fixed_text);
                        replacement.push(']');
                        replacement.push_str(&line[byte_end..]);
                        let fix = Fix::line_replacement(
                            "Remove spaces inside link text",
                            replacement,
                            line_number,
                            line,
                            document.line_ending(line_number),
                        );

                        violations.push(self.create_violation_with_fix(
                            "Spaces inside link text".to_string(),
                            line_number,
                            i + 1, // Convert to 1-based column
                            Severity::Warning,
                            fix,
                        ));
                    }

                    i = end_bracket + 1;
                } else {
                    i += 1;
                }
            } else {
                i += 1;
            }
        }

        violations
    }

    /// Find the closing bracket for a link
    fn find_closing_bracket(&self, chars: &[char], start: usize) -> Option<usize> {
        let mut bracket_count = 1;
        let mut i = start;

        while i < chars.len() && bracket_count > 0 {
            match chars[i] {
                '[' => bracket_count += 1,
                ']' => bracket_count -= 1,
                '\\' => {
                    // Skip escaped character
                    i += 1;
                }
                _ => {}
            }

            if bracket_count == 0 {
                return Some(i);
            }

            i += 1;
        }

        None
    }

    /// Check if link text has unnecessary leading or trailing spaces
    fn has_unnecessary_spaces(&self, link_text: &[char]) -> bool {
        if link_text.is_empty() {
            return false;
        }

        // Check for leading space
        let has_leading_space = link_text[0].is_whitespace();

        // Check for trailing space
        let has_trailing_space = link_text[link_text.len() - 1].is_whitespace();

        has_leading_space || has_trailing_space
    }

    /// Fix link text by trimming leading and trailing spaces
    fn fix_link_text(&self, link_text: &[char]) -> String {
        let text: String = link_text.iter().collect();
        text.trim().to_string()
    }

    /// 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;

        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;
            }
        }

        in_code_block
    }
}

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

    fn name(&self) -> &'static str {
        "no-space-in-links"
    }

    fn description(&self) -> &'static str {
        "Spaces inside link text"
    }

    fn metadata(&self) -> RuleMetadata {
        RuleMetadata::stable(RuleCategory::Content).introduced_in("mdbook-lint v0.1.0")
    }

    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 lines inside code blocks
            if in_code_block[line_number - 1] {
                continue;
            }

            violations.extend(self.check_line_links(document, line, line_number));
        }

        Ok(violations)
    }
}

#[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_md039_normal_links_valid() {
        let content = r#"Here is a [normal link](http://example.com).

Another [link with text](http://example.com) works fine.

Reference link [with text][ref] is also okay.

[ref]: http://example.com
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md039_leading_space_violation() {
        let content = r#"Here is a [ leading space](http://example.com) link.

Another [ spaced link](http://example.com) here.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].rule_id, "MD039");
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 3);
    }

    #[test]
    fn test_md039_trailing_space_violation() {
        let content = r#"Here is a [trailing space ](http://example.com) link.

Another [spaced link ](http://example.com) here.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 3);
    }

    #[test]
    fn test_md039_both_spaces_violation() {
        let content = r#"Here is a [ both spaces ](http://example.com) link.

Multiple [ spaced   ](http://example.com) spaces.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 3);
    }

    #[test]
    fn test_md039_reference_links() {
        let content = r#"Good [reference link][good] here.

Bad [ spaced reference][bad] link.

Another [reference with space ][also-bad] here.

[good]: http://example.com
[bad]: http://example.com
[also-bad]: http://example.com
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 3);
        assert_eq!(violations[1].line, 5);
    }

    #[test]
    fn test_md039_nested_brackets() {
        let content = r#"This has [link with [nested] brackets](http://example.com).

This has [ link with [nested] and space](http://example.com).
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 3);
    }

    #[test]
    fn test_md039_not_links() {
        let content = r#"This has [brackets] but no link.

This has [ spaced brackets] but no link.

This has [reference] but no definition.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0); // Not links, so no violations
    }

    #[test]
    fn test_md039_images_ignored() {
        let content = r#"This has ![ spaced alt text](image.png) which is an image.

And ![normal alt](image.png) text.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0); // Images are not checked by this rule
    }

    #[test]
    fn test_md039_code_blocks_ignored() {
        let content = r#"This has [normal link](http://example.com).

```
This has [ spaced link](http://example.com) in code.
```

This has [ spaced link](http://example.com) that should be flagged.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 7);
    }

    #[test]
    fn test_md039_escaped_brackets() {
        let content = r#"This has [link with \] escaped bracket](http://example.com).

This has [ link with \] and space](http://example.com).
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 3);
    }

    #[test]
    fn test_md039_autolinks() {
        let content = r#"Autolinks like <http://example.com> are not checked.

Email autolinks <user@example.com> are also not checked.

Regular [normal link](http://example.com) is fine.

Bad [ spaced link](http://example.com) is flagged.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 7);
    }

    #[test]
    fn test_md039_empty_link_text() {
        let content = r#"Empty link [](http://example.com) is not flagged for spaces.

Link with just space [ ](http://example.com) is flagged.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 3);
    }

    #[test]
    fn test_md039_multiple_links_per_line() {
        let content = r#"Multiple [good link](http://example.com) and [ bad link](http://example.com) on same line.

More [good](http://example.com) and [also good](http://example.com) links.
"#;

        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 1);
    }

    #[test]
    fn test_md039_fix_leading_space() {
        let content = "Here is a [ leading space](http://example.com) link.\n";
        let document = create_test_document(content);
        let rule = MD039;
        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 spaces inside link text");
        assert_eq!(
            fix.replacement,
            Some("Here is a [leading space](http://example.com) link.\n".to_string())
        );
    }

    #[test]
    fn test_md039_fix_trailing_space() {
        let content = "Here is a [trailing space ](http://example.com) link.\n";
        let document = create_test_document(content);
        let rule = MD039;
        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("Here is a [trailing space](http://example.com) link.\n".to_string())
        );
    }

    #[test]
    fn test_md039_fix_both_spaces() {
        let content = "Here is a [ both spaces ](http://example.com) link.\n";
        let document = create_test_document(content);
        let rule = MD039;
        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("Here is a [both spaces](http://example.com) link.\n".to_string())
        );
    }

    #[test]
    fn test_md039_fix_multiple_spaces() {
        let content = "Link with [   multiple spaces   ](http://example.com) here.\n";
        let document = create_test_document(content);
        let rule = MD039;
        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("Link with [multiple spaces](http://example.com) here.\n".to_string())
        );
    }

    #[test]
    fn test_md039_fix_reference_link() {
        let content = "Bad [ spaced reference][bad] link.\n";
        let document = create_test_document(content);
        let rule = MD039;
        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("Bad [spaced reference][bad] link.\n".to_string())
        );
    }

    #[test]
    fn test_md039_fix_multiple_links() {
        let content =
            "Has [ first bad](http://example.com) and [second bad ](http://example.com) links.\n";
        let document = create_test_document(content);
        let rule = MD039;
        let violations = rule.check(&document).unwrap();

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

        // Both should have fixes
        for violation in &violations {
            assert!(violation.fix.is_some());
        }
    }

    #[test]
    fn test_md039_fix_preserves_nested_brackets() {
        let content = "Has [ link with [nested] and space](http://example.com).\n";
        let document = create_test_document(content);
        let rule = MD039;
        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("Has [link with [nested] and space](http://example.com).\n".to_string())
        );
    }

    #[test]
    fn test_md039_fix_preserves_escaped_brackets() {
        let content = "Has [ link with \\] and space](http://example.com).\n";
        let document = create_test_document(content);
        let rule = MD039;
        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("Has [link with \\] and space](http://example.com).\n".to_string())
        );
    }

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