rumdl 0.1.71

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
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
use rumdl_lib::lint_context::LintContext;
use rumdl_lib::rule::Rule;
use rumdl_lib::rules::MD031BlanksAroundFences;

#[test]
fn test_valid_fenced_blocks() {
    let rule = MD031BlanksAroundFences::default();
    let content = "Text before\n\n```\ncode block\n```\n\nText after";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_no_blank_before() {
    let rule = MD031BlanksAroundFences::default();
    let content = "Text before\n```\ncode block\n```\n\nText after";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].line, 2);
    assert_eq!(result[0].column, 1);
}

#[test]
fn test_no_blank_after() {
    let rule = MD031BlanksAroundFences::default();
    let content = "Text before\n\n```\ncode block\n```\nText after";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].line, 5);
    assert_eq!(result[0].column, 1);
}

#[test]
fn test_fix_missing_blanks() {
    let rule = MD031BlanksAroundFences::default();
    let content = "Text before\n```\ncode block\n```\nText after";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.fix(&ctx).unwrap();
    let fixed_ctx = LintContext::new(&result, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed_result = rule.check(&fixed_ctx).unwrap();
    assert_eq!(fixed_result, Vec::new());
}

#[test]
fn test_nested_code_blocks_no_internal_blanks() {
    let rule = MD031BlanksAroundFences::default();

    // Test nested markdown code blocks (4 backticks containing 3 backticks)
    let content = "# Test\n\n````markdown\nHere's some text.\n\n```python\ndef hello():\n    print(\"Hello!\")\n```\n\nMore text.\n````\n\nAfter.";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.fix(&ctx).unwrap();

    // Verify that the inner ```python block has NO internal blank lines
    assert!(result.contains("```python\ndef hello():\n    print(\"Hello!\")\n```"));
    assert!(!result.contains("```python\n\ndef hello()"));
    assert!(!result.contains("print(\"Hello!\")\n\n```"));

    // Verify that blank lines are only added around the outer ````markdown block
    let lines: Vec<&str> = result.lines().collect();
    let markdown_start = lines.iter().position(|&line| line.starts_with("````markdown")).unwrap();
    let markdown_end = lines.iter().rposition(|&line| line.starts_with("````")).unwrap();

    // Should have blank line before ````markdown
    assert_eq!(lines[markdown_start - 1], "");
    // Should have blank line after closing ````
    assert_eq!(lines[markdown_end + 1], "");
}

#[test]
fn test_nested_code_blocks_different_fence_types() {
    let rule = MD031BlanksAroundFences::default();

    // Test ~~~ containing ```
    let content = "Text\n~~~markdown\n```python\ncode\n```\n~~~\nAfter";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.fix(&ctx).unwrap();

    // Inner ```python should not get blank lines (it's content inside ~~~)
    assert!(result.contains("```python\ncode\n```"));
    assert!(!result.contains("```python\n\ncode"));
    assert!(!result.contains("code\n\n```"));
}

#[test]
fn test_multiple_nested_levels() {
    let rule = MD031BlanksAroundFences::default();

    // Test 5 backticks containing 4 backticks containing 3 backticks
    let content = "`````text\n````markdown\n```python\ncode\n```\n````\n`````";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.fix(&ctx).unwrap();

    // Only the outermost fence should be treated as a real code block
    // Everything inside should be preserved as-is
    assert!(result.contains("````markdown\n```python\ncode\n```\n````"));
    assert!(!result.contains("```python\n\ncode"));
}

#[test]
fn test_nested_vs_standalone_distinction() {
    let rule = MD031BlanksAroundFences::default();

    // Test that standalone ``` blocks still get blank lines, but nested ones don't
    let content = "# Test\nStandalone:\n```python\ncode1\n```\nNested:\n````markdown\n```python\ncode2\n```\n````\nEnd";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.fix(&ctx).unwrap();

    // Standalone ```python should get blank lines around it
    assert!(result.contains("Standalone:\n\n```python\ncode1\n```\n\nNested:"));

    // Nested ```python should NOT get blank lines (it's inside ````markdown)
    assert!(result.contains("```python\ncode2\n```"));
    assert!(!result.contains("```python\n\ncode2"));

    // Outer ````markdown should get blank lines
    assert!(result.contains("Nested:\n\n````markdown"));
    assert!(result.contains("````\n\nEnd"));
}

#[test]
fn test_mixed_fence_markers_nested() {
    let rule = MD031BlanksAroundFences::default();

    // Test ``` inside ~~~ and ~~~ inside ```
    let content = "Test1:\n~~~text\n```python\ncode\n```\n~~~\nTest2:\n````text\n~~~bash\nscript\n~~~\n````\nEnd";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.fix(&ctx).unwrap();

    // Inner fences should not get blank lines
    assert!(result.contains("```python\ncode\n```"));
    assert!(result.contains("~~~bash\nscript\n~~~"));
    assert!(!result.contains("```python\n\ncode"));
    assert!(!result.contains("~~~bash\n\nscript"));

    // Outer fences should get blank lines
    assert!(result.contains("Test1:\n\n~~~text"));
    assert!(result.contains("Test2:\n\n````text"));
}

#[test]
fn test_documentation_example_scenario() {
    let rule = MD031BlanksAroundFences::default();

    // Test the exact scenario from docs/md031.md that was causing issues
    let content = "### Example\n\n````markdown\nHere's some text explaining the code.\n\n```python\ndef hello():\n    print(\"Hello, world!\")\n```\n\nAnd here's more text after the code.\n````\n\n## Next section";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.fix(&ctx).unwrap();

    // The ```python block should remain clean (no internal blank lines)
    assert!(result.contains("```python\ndef hello():\n    print(\"Hello, world!\")\n```"));

    // Should NOT contain internal blank lines in the code block
    assert!(!result.contains("```python\n\ndef hello()"));
    assert!(!result.contains("print(\"Hello, world!\")\n\n```"));

    // The outer ````markdown block should have proper spacing
    assert!(result.contains("### Example\n\n````markdown"));
    assert!(result.contains("````\n\n## Next section"));
}

#[test]
fn test_fence_length_specificity() {
    let rule = MD031BlanksAroundFences::default();

    // Test that fence length matters - ``` inside ```` should not close the outer block
    let content = "````markdown\n```python\ncode\n```\nmore content\n````";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.fix(&ctx).unwrap();

    // The ```python should be treated as content, not as opening/closing a block
    let lines: Vec<&str> = result.lines().collect();
    let python_line = lines.iter().position(|&line| line == "```python").unwrap();
    let close_python_line = lines.iter().position(|&line| line == "```").unwrap();
    let more_content_line = lines.iter().position(|&line| line == "more content").unwrap();

    // Should maintain the order and not treat ``` as block delimiters
    assert!(python_line < close_python_line);
    assert!(close_python_line < more_content_line);
}

#[test]
fn test_code_blocks_in_lists() {
    let rule = MD031BlanksAroundFences::default();

    // Test code blocks inside list items - this was causing issues in docs/md031.md
    let content = r#"# Test

1. First item with code:

   ```python
   code_in_list()
   ```

2. Second item

3. Third item with code:
   ```javascript
   console.log("test");
   ```
   More text in item 3.

Regular paragraph."#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();

    // Code blocks in lists should still require blank lines
    assert!(
        !result.is_empty(),
        "Should detect missing blank lines around code blocks in lists"
    );

    // Test the fix
    let fixed = rule.fix(&ctx).unwrap();

    // Should add blank lines around code blocks in lists
    assert!(fixed.contains("1. First item with code:\n\n   ```python"));
    assert!(fixed.contains("   ```\n\n2. Second item"));
    assert!(fixed.contains("3. Third item with code:\n\n   ```javascript"));
    assert!(fixed.contains("   ```\n\n   More text"));
}

#[test]
fn test_issue_284_blockquote_blank_lines() {
    // Issue #284: Empty blockquote lines (like ">") should be treated as blank lines
    // MD031 should not report false positives for code blocks in blockquotes
    let rule = MD031BlanksAroundFences::default();
    let content = r#"# Blockquote with code

> Some content
>
> ```python
> def hello():
>     print("Hello")
> ```
>
> More content
"#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();

    // Should NOT report missing blank lines - `>` is effectively a blank line in blockquote context
    assert!(
        result.is_empty(),
        "Empty blockquote lines should be treated as blank lines: {result:?}"
    );
}

#[test]
fn test_blockquote_with_blank_marker_only() {
    // Test blockquote with just ">" as blank line separator
    let rule = MD031BlanksAroundFences::default();
    let content = "> Text before\n>\n> ```\n> code\n> ```\n>\n> Text after";

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();

    assert!(
        result.is_empty(),
        "Blockquote with > as blank line should not trigger MD031: {result:?}"
    );
}

#[test]
fn test_blockquote_with_trailing_space_blank() {
    // Test blockquote with "> " (with trailing space) as blank line separator
    let rule = MD031BlanksAroundFences::default();
    let content = "> Text before\n> \n> ```\n> code\n> ```\n> \n> Text after";

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();

    assert!(
        result.is_empty(),
        "Blockquote with '> ' as blank line should not trigger MD031: {result:?}"
    );
}

#[test]
fn test_nested_blockquote_blank_lines() {
    // Test nested blockquotes with blank lines
    let rule = MD031BlanksAroundFences::default();
    let content = r#">> Nested content
>>
>> ```python
>> code here
>> ```
>>
>> More nested content
"#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();

    assert!(
        result.is_empty(),
        "Nested blockquote blank lines should work: {result:?}"
    );
}

#[test]
fn test_blockquote_still_detects_missing_blanks() {
    // Verify that MD031 still detects issues when blank lines are truly missing in blockquotes
    let rule = MD031BlanksAroundFences::default();
    let content = "> Text before\n> ```\n> code\n> ```\n> Text after";

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let _result = rule.check(&ctx).unwrap();

    // Should still detect issues when there's no blank line (not even `>`)
    // Note: This behavior depends on how the rule handles blockquote context
    // Currently, MD031 does not require blank lines in blockquote context
    // because blockquote content is handled separately
}

#[test]
fn test_roundtrip_fix_then_recheck_basic() {
    // Roundtrip: fix then re-check should yield zero warnings
    let rule = MD031BlanksAroundFences::default();
    let content = "Text before\n```\ncode block\n```\nText after";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let warnings = rule.check(&ctx2).unwrap();
    assert!(
        warnings.is_empty(),
        "Roundtrip (basic): fix then re-check should produce 0 warnings, got {warnings:?}"
    );
}

#[test]
fn test_roundtrip_fix_then_recheck_list_items() {
    let rule = MD031BlanksAroundFences::new(true);
    let content = "1. First item\n   ```python\n   code_in_list()\n   ```\n2. Second item";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let warnings = rule.check(&ctx2).unwrap();
    assert!(
        warnings.is_empty(),
        "Roundtrip (list items): fix then re-check should produce 0 warnings, got {warnings:?}"
    );
}

#[test]
fn test_roundtrip_fix_then_recheck_blockquote() {
    let rule = MD031BlanksAroundFences::default();
    let content = "> Text before\n> ```\n> code\n> ```\n> Text after";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let warnings = rule.check(&ctx2).unwrap();
    assert!(
        warnings.is_empty(),
        "Roundtrip (blockquote): fix then re-check should produce 0 warnings, got {warnings:?}"
    );
}

#[test]
fn test_roundtrip_fix_then_recheck_nested_blockquote() {
    let rule = MD031BlanksAroundFences::default();
    let content = ">> Nested quote\n>> ```\n>> code\n>> ```\n>> More text";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let warnings = rule.check(&ctx2).unwrap();
    assert!(
        warnings.is_empty(),
        "Roundtrip (nested blockquote): fix then re-check should produce 0 warnings, got {warnings:?}"
    );
}

#[test]
fn test_roundtrip_fix_then_recheck_multiple_blocks() {
    let rule = MD031BlanksAroundFences::default();
    let content = "Text\n```\ncode1\n```\nMiddle\n```\ncode2\n```\nEnd";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let warnings = rule.check(&ctx2).unwrap();
    assert!(
        warnings.is_empty(),
        "Roundtrip (multiple blocks): fix then re-check should produce 0 warnings, got {warnings:?}"
    );
}

#[test]
fn test_roundtrip_fix_then_recheck_trailing_newline() {
    let rule = MD031BlanksAroundFences::default();
    let content = "Some text\n```\ncode\n```\nMore text\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let warnings = rule.check(&ctx2).unwrap();
    assert!(
        warnings.is_empty(),
        "Roundtrip (trailing newline): fix then re-check should produce 0 warnings, got {warnings:?}"
    );
}

#[test]
fn test_roundtrip_fix_idempotent() {
    // Applying fix twice should produce the same result
    let rule = MD031BlanksAroundFences::default();
    let content = "Text\n```\ncode\n```\nMore";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed1 = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed1, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed2 = rule.fix(&ctx2).unwrap();
    assert_eq!(fixed1, fixed2, "Fix should be idempotent");
}

#[test]
fn test_roundtrip_fix_then_recheck_mkdocs_admonition() {
    let rule = MD031BlanksAroundFences::default();
    let content = "Text before\n!!! note\n    Content\nText after";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::MkDocs, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::MkDocs, None);
    let warnings = rule.check(&ctx2).unwrap();
    assert!(
        warnings.is_empty(),
        "Roundtrip (MkDocs admonition): fix then re-check should produce 0 warnings, got {warnings:?}"
    );
}

#[test]
fn test_roundtrip_fix_then_recheck_4space_list() {
    let rule = MD031BlanksAroundFences::new(true);
    let content =
        "1. First item\n2. Second item with code:\n    ```python\n    print(\"Hello\")\n    ```\n3. Third item";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let warnings = rule.check(&ctx2).unwrap();
    assert!(
        warnings.is_empty(),
        "Roundtrip (4-space list): fix then re-check should produce 0 warnings, got {warnings:?}"
    );
}

#[test]
fn test_mixed_blockquote_and_regular_content() {
    // Test that regular content outside blockquotes still requires blank lines
    let rule = MD031BlanksAroundFences::default();
    let content = r#"# Mixed Content

> Blockquote with proper spacing
>
> ```python
> inside_quote()
> ```
>
> End of quote

Regular text without blank line
```javascript
outside_quote();
```
More text
"#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();

    // The blockquote section should NOT trigger warnings
    // But the non-blockquote section should trigger warnings
    assert!(
        !result.is_empty(),
        "Should still detect missing blanks outside blockquotes"
    );

    // Verify the warnings are for the right lines
    let warning_lines: Vec<usize> = result.iter().map(|w| w.line).collect();
    // Line 12 is "```javascript" without blank before
    // Line 14 is after "```" without blank after
    assert!(
        warning_lines.iter().all(|&l| l >= 12),
        "Warnings should be for non-blockquote section: {warning_lines:?}"
    );
}