rumdl 0.1.88

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
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
use rumdl_lib::lint_context::LintContext;
use rumdl_lib::rule::Rule;
use rumdl_lib::rules::MD055TablePipeStyle;

#[test]
fn test_name() {
    let rule = MD055TablePipeStyle::default();
    assert_eq!(rule.name(), "MD055");
}

#[test]
fn test_consistent_pipe_styles() {
    let rule = MD055TablePipeStyle::default();

    // Leading and trailing pipes consistently
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
    "#;

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

    // No leading or trailing pipes consistently
    let content = r#"
Header 1 | Header 2
-------- | --------
Cell 1   | Cell 2
    "#;

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

#[test]
fn test_inconsistent_pipe_styles() {
    let rule = MD055TablePipeStyle::default();

    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
Cell 1   | Cell 2
    "#;

    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, 4);
    assert!(result[0].message.contains("Table pipe style"));
}

#[test]
fn test_leading_and_trailing_style() {
    let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());

    // Consistent with leading_and_trailing style
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
    "#;

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

    // Inconsistent with leading_and_trailing style
    let content = r#"
Header 1 | Header 2
-------- | --------
Cell 1   | Cell 2
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 3); // Three rows, all need fixes
}

#[test]
fn test_no_leading_or_trailing_style() {
    let rule = MD055TablePipeStyle::new("no_leading_or_trailing".to_string());

    // Consistent with no_leading_or_trailing style
    let content = r#"
Header 1 | Header 2
-------- | --------
Cell 1   | Cell 2
    "#;

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

    // Inconsistent with no_leading_or_trailing style
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 3); // Three rows, all need fixes
}

#[test]
fn test_leading_only_style() {
    let rule = MD055TablePipeStyle::new("leading_only".to_string());

    // Consistent with leading_only style
    let content = r#"
| Header 1 | Header 2
| -------- | --------
| Cell 1   | Cell 2
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        0,
        "Content with leading pipes only should not trigger warnings with leading_only style"
    );

    // Inconsistent with leading_only style (has trailing pipes)
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        3,
        "Content with both leading and trailing pipes should be flagged when style is leading_only"
    );

    // Fix should correctly convert to leading_only style
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let fixed_ctx = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&fixed_ctx).unwrap();
    assert_eq!(result.len(), 0);
}

#[test]
fn test_trailing_only_style() {
    let rule = MD055TablePipeStyle::new("trailing_only".to_string());

    // Consistent with trailing_only style
    let content = r#"
Header 1 | Header 2 |
-------- | -------- |
Cell 1   | Cell 2   |
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        0,
        "Content with trailing pipes only should not trigger warnings with trailing_only style"
    );

    // Inconsistent with trailing_only style (has leading pipes)
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        3,
        "Content with both leading and trailing pipes should be flagged when style is trailing_only"
    );

    // Fix should correctly convert to trailing_only style
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let fixed_ctx = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&fixed_ctx).unwrap();
    assert_eq!(result.len(), 0);
}

#[test]
fn test_code_blocks_ignored() {
    let rule = MD055TablePipeStyle::default();

    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |

```markdown
| This is a table in a code block |
Header with inconsistent style | that should be ignored
```
    "#;

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

#[test]
fn test_fix() {
    // Test fix for leading_and_trailing style
    let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());
    let content = r#"
Header 1 | Header 2
-------- | --------
Cell 1   | Cell 2
    "#;
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let fixed_ctx = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&fixed_ctx).unwrap();
    assert_eq!(result.len(), 0, "Fixed content should have no warnings");

    // Test fix for no_leading_or_trailing style
    let rule = MD055TablePipeStyle::new("no_leading_or_trailing".to_string());
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
    "#;
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let fixed_ctx = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&fixed_ctx).unwrap();
    assert_eq!(result.len(), 0, "Fixed content should have no warnings");

    // Test fix for leading_only style
    let rule = MD055TablePipeStyle::new("leading_only".to_string());
    let content = r#"
Header 1 | Header 2 |
-------- | -------- |
Cell 1   | Cell 2   |
    "#;
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let fixed_ctx = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&fixed_ctx).unwrap();
    assert_eq!(result.len(), 0, "Fixed content should have no warnings");

    // Test fix for trailing_only style
    let rule = MD055TablePipeStyle::new("trailing_only".to_string());
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
    "#;
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let fixed_ctx = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&fixed_ctx).unwrap();
    assert_eq!(result.len(), 0, "Fixed content should have no warnings");
}

#[test]
fn test_false_positives_not_flagged() {
    let rule = MD055TablePipeStyle::default();

    // Test 1: Regular text mentioning pipe characters should not be flagged
    let content = r#"
# MD055 - Table pipe style

## Description

This rule is triggered when table rows in a Markdown file have inconsistent pipe styles.

In Markdown tables, you can include or omit leading and trailing pipe characters (`|`). This rule enforces a consistent style for these pipes.
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        0,
        "Regular text mentioning pipe characters should not be flagged"
    );

    // Test 2: Bullet points with pipe characters should not be flagged
    let content = r#"
The `style` parameter can have the following values:

- `consistent` (default): All table rows should use the same style as the first table row
- `leading*and*trailing`: All table rows must have both leading and trailing pipes (`| cell | cell |`)
  - `leading*only`: All table rows must have leading pipes and no trailing pipes (`| cell | cell`)
  - `trailing*only`: All table rows must have trailing pipes and no leading pipes (`cell | cell |`)
  - `no*leading*or*trailing`: All table rows must have neither leading nor trailing pipes (`cell | cell`)
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        0,
        "Bullet points with pipe characters should not be flagged"
    );

    // Test 3: Inline code with pipe characters should not be flagged
    let content = r#"
You can use pipes in inline code like `| cell | cell |` without issues.

Also backticks with pipes: ``| some | code |`` should be ignored.
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        0,
        "Inline code with pipe characters should not be flagged"
    );

    // Test 4: Lines with pipes but no table structure should not be flagged
    let content = r#"
This is a line with | some | pipes | but no table structure.

Another line | with | pipes | that | doesn't | form | a | table.
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        0,
        "Lines with pipes but no table structure should not be flagged"
    );

    // Test 5: Single line with pipes (no delimiter row) should not be flagged
    let content = r#"
| This | looks | like | a | table | row |

But there's no delimiter row, so it's not a table.
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        0,
        "Single line with pipes (no delimiter row) should not be flagged"
    );
}

#[test]
fn test_actual_tables_are_still_flagged() {
    let rule = MD055TablePipeStyle::default();

    // Test that actual tables with inconsistent styles are still flagged
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
Cell 1   | Cell 2
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        1,
        "Actual tables with inconsistent styles should still be flagged"
    );
    assert_eq!(result[0].line, 4, "The inconsistent row should be flagged");
}

#[test]
fn test_table_detection_requires_delimiter_row() {
    let rule = MD055TablePipeStyle::default();

    // Test that a proper table structure is required (header + delimiter + rows)
    let content = r#"
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
| Cell 3   | Cell 4   |
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 0, "Properly formatted table should not be flagged");

    // Test that without delimiter row, it's not considered a table
    let content = r#"
| Header 1 | Header 2 |
| Cell 1   | Cell 2   |
| Cell 3   | Cell 4   |
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        0,
        "Lines without delimiter row should not be considered a table"
    );
}

#[test]
fn test_mixed_content_with_tables() {
    let rule = MD055TablePipeStyle::default();

    // Test content that mixes regular text with pipes and actual tables
    let content = r#"
# Document with mixed content

This line mentions pipe characters (`|`) in regular text.

- Bullet point with pipes: `| cell | cell |`
- Another bullet: uses pipes (`|`) in description

Now here's an actual table:

| Header 1 | Header 2 |
| -------- | -------- |
Cell 1   | Cell 2

And more text with | pipes | that | aren't | tables.
    "#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1, "Only the actual table row should be flagged");
    assert_eq!(result[0].line, 13, "The inconsistent table row should be flagged");
    assert!(
        result[0].message.contains("Table pipe style"),
        "Should be a table pipe style warning"
    );
}

#[test]
fn test_fix_does_not_corrupt_non_tables() {
    let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());

    // Test that fix doesn't corrupt non-table content
    let content = r#"
# MD055 - Table pipe style

In Markdown tables, you can include or omit leading and trailing pipe characters (`|`).

- `leading*and*trailing`: All table rows must have both leading and trailing pipes (`| cell | cell |`)
- `no*leading*or*trailing`: All table rows must have neither leading nor trailing pipes (`cell | cell`)

This line has | some | pipes | but | isn't | a | table.
    "#;

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

    // The content should be unchanged since there are no actual tables
    assert_eq!(
        content.trim(),
        fixed.trim(),
        "Non-table content should not be modified by fix"
    );

    // Verify no warnings are generated
    let fixed_ctx = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&fixed_ctx).unwrap();
    assert_eq!(result.len(), 0, "Fixed content should have no warnings");
}

// --- Continuation table tests (tables on lines after the list marker) ---

#[test]
fn test_md055_continuation_table_preserves_indent() {
    // MD055 with "leading_and_trailing" style should fix pipe style
    // while preserving list indentation
    let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());

    // Table without leading pipes, indented under a list item
    let content = "- Item\n  h1 | h2\n  ---|---\n  d1 | d2";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();

    let lines: Vec<&str> = fixed.lines().collect();
    assert_eq!(lines[0], "- Item", "List text unchanged");
    // All table lines must keep 2-space indent
    for (idx, line) in lines.iter().enumerate().skip(1) {
        assert!(
            line.starts_with("  "),
            "Line {idx} must preserve 2-space indent, got: {line:?}",
        );
    }
    // Should now have leading and trailing pipes
    assert!(
        lines[1].trim().starts_with('|') && lines[1].trim().ends_with('|'),
        "Header should have leading/trailing pipes after fix, got: {:?}",
        lines[1]
    );
}

#[test]
fn test_md055_continuation_table_idempotent() {
    let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());

    let content = "- Item\n  | h1 | h2 |\n  |---|---|\n  | d1 | d2 |";
    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, "MD055 fix must be idempotent for continuation tables");
}

#[test]
fn test_md055_nested_list_continuation_table_at_parent_level() {
    let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());

    let content = "- Parent\n  - Child\n\n  | h1 | h2 |\n  |---|---|\n  | d1 | d2 |";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();

    let lines: Vec<&str> = fixed.lines().collect();
    // Table at parent indent (2 spaces) must be preserved
    assert!(
        lines[3].starts_with("  "),
        "Table header at parent level must keep indent, got: {:?}",
        lines[3]
    );
    assert!(
        lines[4].starts_with("  "),
        "Table delimiter at parent level must keep indent, got: {:?}",
        lines[4]
    );
}