mdbook-lint-rulesets 0.12.0

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
//! MDBOOK010: Missing or invalid preprocessor configuration
//!
//! This rule checks for invalid preprocessor directives in mdBook files.
//! Preprocessors like mermaid, katex, and others require specific syntax.

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

/// Rule to check for invalid preprocessor configuration
pub struct MDBOOK010;

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

    fn name(&self) -> &'static str {
        "preprocessor-validation"
    }

    fn description(&self) -> &'static str {
        "Missing or invalid preprocessor configuration"
    }

    fn metadata(&self) -> RuleMetadata {
        RuleMetadata::experimental(RuleCategory::MdBook).introduced_in("mdbook-lint v0.11.0")
    }

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

        // Check for common preprocessor patterns
        self.check_mermaid_blocks(document, &mut violations);
        self.check_katex_blocks(document, &mut violations);
        self.check_admonish_blocks(document, &mut violations);

        Ok(violations)
    }
}

impl MDBOOK010 {
    /// Check for invalid mermaid blocks
    fn check_mermaid_blocks(&self, document: &Document, violations: &mut Vec<Violation>) {
        let _mermaid_re = Regex::new(r"```mermaid\s*\n([\s\S]*?)```").unwrap();

        for (line_num, line) in document.lines.iter().enumerate() {
            if line.trim() == "```mermaid" {
                // Check if the block is empty
                if line_num + 1 < document.lines.len() {
                    let next_line = &document.lines[line_num + 1];
                    if next_line.trim() == "```" {
                        violations.push(self.create_violation(
                            "Empty mermaid block detected".to_string(),
                            line_num + 1,
                            1,
                            Severity::Warning,
                        ));
                    }
                }
            }

            // Check for common mermaid syntax errors
            if line.contains("```mermaid") && !line.trim().eq("```mermaid") {
                violations.push(self.create_violation(
                    "Mermaid blocks should start with '```mermaid' on its own line".to_string(),
                    line_num + 1,
                    1,
                    Severity::Error,
                ));
            }
        }
    }

    /// Check for invalid KaTeX blocks
    fn check_katex_blocks(&self, document: &Document, violations: &mut Vec<Violation>) {
        // Check for inline math
        let _inline_math_re = Regex::new(r"\$([^$\n]+)\$").unwrap();
        // Check for display math
        let _display_math_re = Regex::new(r"\$\$([^$]+)\$\$").unwrap();

        let mut in_code_block = false;

        for (line_num, line) in document.lines.iter().enumerate() {
            // Track code block state - skip all processing inside code blocks
            if line.trim_start().starts_with("```") || line.trim_start().starts_with("~~~") {
                in_code_block = !in_code_block;
                continue;
            }

            // Skip all processing if we're inside a code block
            if in_code_block {
                continue;
            }

            // Skip lines that look like shell prompts (start with $ followed by space or common commands)
            let trimmed = line.trim();
            if trimmed.starts_with("$ ") || trimmed == "$" {
                // This looks like a shell prompt, not a math block
                continue;
            }

            // Check for unclosed inline math - but exclude standalone $ at start of line
            let dollar_count = line.chars().filter(|&c| c == '$').count();
            if dollar_count % 2 != 0 && !line.contains("$$") {
                // Additional check: only flag if there's actual math content
                // A single $ at the start followed by non-math content is likely a shell prompt
                if !Self::is_likely_shell_prompt(line) {
                    violations.push(self.create_violation(
                        "Unclosed inline math block (odd number of $ signs)".to_string(),
                        line_num + 1,
                        1,
                        Severity::Error,
                    ));
                }
            }

            // Check for empty math blocks (still only outside code blocks)
            if line.contains("$$$$") {
                violations.push(self.create_violation(
                    "Empty display math block detected".to_string(),
                    line_num + 1,
                    line.find("$$$$").unwrap() + 1,
                    Severity::Warning,
                ));
            }

            if line.contains("$ $") {
                violations.push(self.create_violation(
                    "Empty inline math block detected".to_string(),
                    line_num + 1,
                    line.find("$ $").unwrap() + 1,
                    Severity::Warning,
                ));
            }
        }
    }

    /// Helper function to detect if a line is likely a shell prompt
    fn is_likely_shell_prompt(line: &str) -> bool {
        let trimmed = line.trim();

        // Common shell prompt patterns
        if trimmed.starts_with("$ ") || trimmed == "$" {
            return true;
        }

        // Check for PowerShell prompts
        if trimmed.starts_with("> ") || trimmed == ">" {
            return true;
        }

        // Check for numbered prompts like "1$" or similar
        if trimmed.starts_with(|c: char| c.is_ascii_digit()) && trimmed.contains("$") {
            return true;
        }

        // If line has a single $ and looks like a command (contains common shell keywords)
        if line.matches('$').count() == 1 {
            let after_dollar = line.split('$').nth(1).unwrap_or("");
            let first_word = after_dollar.split_whitespace().next().unwrap_or("");

            // Common shell commands/keywords
            let shell_keywords = [
                "cd", "ls", "pwd", "echo", "mkdir", "rm", "cp", "mv", "cat", "grep", "find",
                "curl", "wget", "git", "npm", "cargo", "rustc", "python", "ruby", "node", "java",
                "gcc", "make", "sudo", "apt", "yum", "brew", "docker", "kubectl", "rustup", "pip",
                "gem", "yarn", "pnpm", "deno", "./", "../", "~/", "/", "\\", ".", "..", "export",
                "source", "bash",
            ];

            if shell_keywords.iter().any(|&kw| first_word.starts_with(kw)) {
                return true;
            }
        }

        false
    }

    /// Check for invalid admonish blocks
    fn check_admonish_blocks(&self, document: &Document, violations: &mut Vec<Violation>) {
        let admonish_re = Regex::new(r"```admonish\s+(\w+)(.*)").unwrap();
        let valid_types = [
            "note",
            "tip",
            "info",
            "warning",
            "danger",
            "important",
            "caution",
            "bug",
            "example",
            "quote",
        ];

        for (line_num, line) in document.lines.iter().enumerate() {
            if line.starts_with("```admonish") {
                if let Some(captures) = admonish_re.captures(line) {
                    if let Some(admonish_type) = captures.get(1) {
                        let type_str = admonish_type.as_str();
                        if !valid_types.contains(&type_str) {
                            violations.push(self.create_violation(
                                format!(
                                    "Invalid admonish type '{}'. Valid types are: {}",
                                    type_str,
                                    valid_types.join(", ")
                                ),
                                line_num + 1,
                                admonish_type.start() + 1,
                                Severity::Error,
                            ));
                        }
                    }
                } else if line.trim() == "```admonish" {
                    violations.push(self.create_violation(
                        "Admonish block missing type. Use format: ```admonish <type>".to_string(),
                        line_num + 1,
                        1,
                        Severity::Error,
                    ));
                }
            }
        }
    }
}

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

    #[test]
    fn test_valid_preprocessors() {
        let content = r#"# Chapter

Here's a mermaid diagram:

```mermaid
graph TD
    A --> B
```

Some math: $x = y^2$

Display math:
$$
E = mc^2
$$

```admonish note
This is a note.
```
"#;
        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

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

    #[test]
    fn test_empty_mermaid_block() {
        let content = r#"```mermaid
```"#;
        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Empty mermaid block"));
    }

    #[test]
    fn test_invalid_mermaid_syntax() {
        let content = "```mermaid with extra text";
        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        assert_eq!(violations.len(), 1);
        assert!(
            violations[0]
                .message
                .contains("should start with '```mermaid' on its own line")
        );
    }

    #[test]
    fn test_unclosed_inline_math() {
        let content = "This is $unclosed math";
        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Unclosed inline math"));
    }

    #[test]
    fn test_empty_math_blocks() {
        let content = r#"Empty inline: $ $
Empty display: $$$$"#;
        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        assert_eq!(violations.len(), 2);
        assert!(violations[0].message.contains("Empty inline math"));
        assert!(violations[1].message.contains("Empty display math"));
    }

    #[test]
    fn test_invalid_admonish_type() {
        let content = "```admonish invalid";
        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        assert_eq!(violations.len(), 1);
        assert!(
            violations[0]
                .message
                .contains("Invalid admonish type 'invalid'")
        );
    }

    #[test]
    fn test_missing_admonish_type() {
        let content = "```admonish";
        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        assert_eq!(violations.len(), 1);
        assert!(
            violations[0]
                .message
                .contains("Admonish block missing type")
        );
    }

    #[test]
    fn test_shell_prompts_not_math() {
        let content = r#"# Shell Commands

$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
$ cargo build --release
$ rustc main.rs
$ echo $PATH

Some actual math: $x = y^2$
But this is unclosed math: $x = y"#;

        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        // Should only flag the last line as unclosed math, not the shell commands
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Unclosed inline math"));
        assert_eq!(violations[0].line, 9); // The "But this is unclosed math" line
    }

    #[test]
    fn test_powershell_prompts() {
        let content = r#"> rustc main.rs
> echo %PATH%
> dir /B"#;

        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        // PowerShell prompts should not be flagged
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_dollar_signs_in_code_blocks() {
        let content = r#"# Shell Examples

```bash
echo "Database status: $status"
if [ "$status" = "active" ]; then
    echo "System is $status"
    export PATH=$PATH:/usr/local/bin
fi

# Variables with dollar signs
name=$1
echo "Hello $name"
result=$(echo $value | grep pattern)
```

Regular text with math: $x = y^2$

~~~sh
# Another code block style
echo $HOME
echo $USER
~~~

More text after code block"#;

        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        // Should not flag any violations for dollar signs in code blocks
        assert_eq!(
            violations.len(),
            0,
            "Dollar signs in code blocks should be ignored"
        );
    }

    #[test]
    fn test_mixed_code_and_math() {
        let content = r#"# Mixed Content

Some inline math: $x = y^2$

```bash
# Shell code with dollars
echo $PATH
export VAR=$VALUE
```

Unclosed math: $x = y

```python
# Python code (no dollars but testing code block boundaries)
print("Hello")
```

Valid display math:
$$
E = mc^2
$$"#;

        let doc = Document::new(content.to_string(), PathBuf::from("chapter.md")).unwrap();
        let rule = MDBOOK010;
        let violations = rule.check(&doc).unwrap();

        // Should only flag the unclosed math outside code blocks
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Unclosed inline math"));
        assert_eq!(violations[0].line, 11); // Line with "Unclosed math: $x = y"
    }
}