mdbook-lint 0.14.2

A fast markdown linter and preprocessor for mdBook
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
//! Tests for batch 1 rule configuration functionality (MD002, MD003, MD007, MD010, MD012)

#[cfg(test)]
mod tests {
    use crate::config::Config;
    use mdbook_lint_core::{Document, PluginRegistry};
    use mdbook_lint_rulesets::StandardRuleProvider;
    use std::path::PathBuf;

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

    #[test]
    fn test_md002_configuration_works() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();

        // Create config with MD002 level = 2 and only MD002 enabled
        let config_toml = r#"
enabled-rules = ["MD002"]
[MD002]
level = 2
"#;
        let config = Config::from_toml_str(config_toml).unwrap();

        // Create engine with configuration
        let engine = registry
            .create_engine_with_config(Some(&config.core))
            .unwrap();

        // Test document with level 2 heading (should pass with level = 2)
        let content = r#"## This is level 2

Some content here.
"#;

        let document = create_test_document(content);
        let violations = engine
            .lint_document_with_config(&document, &config.core)
            .unwrap();

        // Should have no violations since we configured level 2 as acceptable
        let md002_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD002").collect();
        assert_eq!(md002_violations.len(), 0);

        // Test with level 3 - should still violate
        let content_violating = r#"### This is level 3

Some content here.
"#;

        let document_violating = create_test_document(content_violating);
        let violations_violating = engine
            .lint_document_with_config(&document_violating, &config.core)
            .unwrap();

        let md002_violations_violating: Vec<_> = violations_violating
            .iter()
            .filter(|v| v.rule_id == "MD002")
            .collect();
        assert_eq!(md002_violations_violating.len(), 1);
        assert!(
            md002_violations_violating[0]
                .message
                .contains("should be level 2")
        );
        assert!(
            md002_violations_violating[0]
                .message
                .contains("got level 3")
        );
    }

    #[test]
    fn test_md003_configuration_works() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();

        // Create config with MD003 style = "atx" and only MD003 enabled
        let config_toml = r#"
enabled-rules = ["MD003"]
[MD003]
style = "atx"
"#;
        let config = Config::from_toml_str(config_toml).unwrap();

        // Create engine with configuration
        let engine = registry
            .create_engine_with_config(Some(&config.core))
            .unwrap();

        // Test document with Setext heading that should violate ATX-only config
        let content = r#"Main Title
==========

Some content here.
"#;

        let document = create_test_document(content);
        let violations = engine
            .lint_document_with_config(&document, &config.core)
            .unwrap();

        // Should have violations since we configured ATX but document uses Setext
        let md003_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD003").collect();
        assert!(!md003_violations.is_empty());
        assert!(md003_violations[0].message.contains("Expected 'atx' style"));
        assert!(md003_violations[0].message.contains("found 'setext' style"));
    }

    #[test]
    fn test_md007_configuration_works() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();

        // Create config with MD007 custom indentation and only MD007 enabled
        // Using indent=2 because 4+ spaces at line start creates code blocks
        let config_toml = r#"
enabled-rules = ["MD007"]
[MD007]
indent = 2
start-indented = true
start-indent = 2
"#;
        let config = Config::from_toml_str(config_toml).unwrap();

        // Create engine with configuration
        let engine = registry
            .create_engine_with_config(Some(&config.core))
            .unwrap();

        // Test document with proper list that uses configured indentation
        // With start_indented=true and start_indent=4, top level needs exactly 4 spaces before bullet
        // But 4+ spaces triggers code block, so we can't test that configuration properly
        // Let's use indent=2 with start_indent=2 instead
        let content = r#"  * Item 1 (2 spaces as configured for start_indent)
    * Nested item (4 spaces: 2 base + 2 indent)
      * Deep nested item (6 spaces: 2 base + 2*2 indent)
"#;

        let document = create_test_document(content);
        let violations = engine
            .lint_document_with_config(&document, &config.core)
            .unwrap();

        // Should have no violations since the indentation matches our config
        let md007_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD007").collect();
        assert_eq!(md007_violations.len(), 0);

        // Test with wrong indentation - should violate
        // With indent=2 and start_indented=true, top level should have 2 spaces
        let content_violating = r#"* Item 1 (0 spaces - should have 2 with start_indented)
   * Nested item (3 spaces - should have 4!)
"#;

        let document_violating = create_test_document(content_violating);
        let violations_violating = engine
            .lint_document_with_config(&document_violating, &config.core)
            .unwrap();

        let md007_violations_violating: Vec<_> = violations_violating
            .iter()
            .filter(|v| v.rule_id == "MD007")
            .collect();
        // Should have 2 violations: top-level needs 2 spaces, nested needs 4
        assert_eq!(md007_violations_violating.len(), 2);
        // First violation: top level should have 2 spaces (start_indented=true)
        assert!(
            md007_violations_violating[0]
                .message
                .contains("Expected 2 spaces, found 0")
        );
        // Second violation: nested should have 4 spaces (2 base + 2 indent)
        assert!(
            md007_violations_violating[1]
                .message
                .contains("Expected 4 spaces, found 3")
        );
    }

    #[test]
    fn test_md010_configuration_works() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();

        // Create config with MD010 custom spaces per tab and only MD010 enabled
        let config_toml = r#"
enabled-rules = ["MD010"]
[MD010]
spaces-per-tab = 8
"#;
        let config = Config::from_toml_str(config_toml).unwrap();

        // Create engine with configuration
        let engine = registry
            .create_engine_with_config(Some(&config.core))
            .unwrap();

        // Test document with tab character
        let content = "Line with\ttab character";

        let document = create_test_document(content);
        let violations = engine
            .lint_document_with_config(&document, &config.core)
            .unwrap();

        // Should have violations mentioning 8 spaces
        let md010_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD010").collect();
        assert_eq!(md010_violations.len(), 1);
        assert!(md010_violations[0].message.contains("8 spaces"));
    }

    #[test]
    fn test_md012_configuration_works() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();

        // Create config with MD012 maximum = 3 and only MD012 enabled
        let config_toml = r#"
enabled-rules = ["MD012"]
[MD012]
maximum = 3
"#;
        let config = Config::from_toml_str(config_toml).unwrap();

        // Create engine with configuration
        let engine = registry
            .create_engine_with_config(Some(&config.core))
            .unwrap();

        // Test document with 3 consecutive blank lines (should be allowed)
        let content = "# Heading\n\n\n\nParagraph.";

        let document = create_test_document(content);
        let violations = engine
            .lint_document_with_config(&document, &config.core)
            .unwrap();

        // Should have no violations since we allow up to 3 blank lines
        let md012_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD012").collect();
        assert_eq!(md012_violations.len(), 0);

        // Test with 4 consecutive blank lines (should violate)
        let content_violating = "# Heading\n\n\n\n\nParagraph.";

        let document_violating = create_test_document(content_violating);
        let violations_violating = engine
            .lint_document_with_config(&document_violating, &config.core)
            .unwrap();

        let md012_violations_violating: Vec<_> = violations_violating
            .iter()
            .filter(|v| v.rule_id == "MD012")
            .collect();
        assert_eq!(md012_violations_violating.len(), 1);
        assert!(
            md012_violations_violating[0]
                .message
                .contains("4 found, 3 allowed")
        );
    }

    #[test]
    fn test_multiple_batch1_rules_configuration() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();

        // Create config with multiple batch 1 rules
        let config_toml = r#"
enabled-rules = ["MD002", "MD010", "MD012"]
[MD002]
level = 3
[MD010]
spaces-per-tab = 2
[MD012]
maximum = 0
"#;
        let config = Config::from_toml_str(config_toml).unwrap();

        // Create engine with configuration
        let engine = registry
            .create_engine_with_config(Some(&config.core))
            .unwrap();

        // Test document that would trigger multiple configured rules
        let content = "## This is level 2 (should violate MD002 configured for level 3)\n\nThis would normally be fine for MD012 but not with maximum=0";

        let document = create_test_document(content);
        let violations = engine
            .lint_document_with_config(&document, &config.core)
            .unwrap();

        // Should have violations from both MD002 and MD012
        let md002_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD002").collect();
        let md012_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD012").collect();

        assert_eq!(md002_violations.len(), 1);
        assert!(md002_violations[0].message.contains("should be level 3"));

        assert_eq!(md012_violations.len(), 1);
        assert!(md012_violations[0].message.contains("1 found, 0 allowed"));
    }

    #[test]
    fn test_batch1_rules_fallback_to_defaults() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();

        // Create config without any rule-specific configuration
        let config_toml = r#"
enabled-rules = ["MD002"]
"#;
        let config = Config::from_toml_str(config_toml).unwrap();

        // Create engine with configuration
        let engine = registry
            .create_engine_with_config(Some(&config.core))
            .unwrap();

        // Test that rules use their default values
        let content = r#"## This is level 2

Some content here.
"#;

        let document = create_test_document(content);
        let violations = engine
            .lint_document_with_config(&document, &config.core)
            .unwrap();

        // Should have MD002 violation since default level is 1
        let md002_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD002").collect();
        assert_eq!(md002_violations.len(), 1);
        assert!(md002_violations[0].message.contains("should be level 1"));
        assert!(md002_violations[0].message.contains("got level 2"));
    }

    #[test]
    fn test_batch1_rules_with_underscore_config_keys() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();

        // Test that underscore keys also work (start_indent vs start-indent)
        let config_toml = r#"
enabled-rules = ["MD007", "MD010"]
[MD007]
start_indent = 6
start_indented = true
[MD010]
spaces_per_tab = 3
"#;
        let config = Config::from_toml_str(config_toml).unwrap();

        // Create engine with configuration
        let engine = registry
            .create_engine_with_config(Some(&config.core))
            .unwrap();

        // Test MD010 configuration with underscore key
        let content_tab = "Line with\ttab";
        let document_tab = create_test_document(content_tab);
        let violations_tab = engine
            .lint_document_with_config(&document_tab, &config.core)
            .unwrap();

        let md010_violations: Vec<_> = violations_tab
            .iter()
            .filter(|v| v.rule_id == "MD010")
            .collect();
        assert_eq!(md010_violations.len(), 1);
        assert!(md010_violations[0].message.contains("3 spaces")); // Should use underscore config

        // Test MD007 configuration with underscore keys
        let content_list = r#"      * Item 1 (6 spaces start)
        * Nested item (should be 6+2=8 spaces)
"#;
        let document_list = create_test_document(content_list);
        let violations_list = engine
            .lint_document_with_config(&document_list, &config.core)
            .unwrap();

        let md007_violations: Vec<_> = violations_list
            .iter()
            .filter(|v| v.rule_id == "MD007")
            .collect();
        // Should have no violations if the 6-space start indent is correctly applied
        assert_eq!(md007_violations.len(), 0);
    }
}