mdtablefix 0.6.1

`mdtablefix` unb0rks and reflows Markdown tables so that each column has a uniform width. When the `--wrap` option is used, it also wraps paragraphs and list items to 80 columns.
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
//! Tests for fence normalization functionality.

#[macro_use]
#[path = "common/mod.rs"]
mod common;

use mdtablefix::{attach_orphan_specifiers, compress_fences};
use rstest::rstest;

fn assert_fence_snapshot(name: &str, input: &[String]) {
    insta::with_settings!({
        snapshot_path => "snapshots",
        prepend_module_to_snapshot => false,
    }, {
        insta::assert_snapshot!(name, compress_fences(input).join("\n"));
    });
}

#[rstest]
#[case(
    "fences_compress_overlong_backticks",
    lines_vec!["`````rust", "fn main() {}", "`````"]
)]
#[case(
    "fences_compress_tilde_fences",
    lines_vec!["~~~~~python", "print('hello')", "~~~~~"]
)]
#[case(
    "fences_preserve_interior_conflict",
    lines_vec!["````markdown", "```rust", "fn main() {}", "```", "````"]
)]
fn fence_normalization_snapshots(#[case] name: &str, #[case] input: Vec<String>) {
    assert_fence_snapshot(name, &input);
}
#[test]
fn compresses_backtick_fences() {
    let input = lines_vec!["````rust", "code", "````"];
    let out = compress_fences(&input);
    assert_eq!(out, lines_vec!["```rust", "code", "```"]);
}

#[test]
fn compresses_indented_backticks() {
    let input = lines_vec!["    `````foo,bar   "];
    let out = compress_fences(&input);
    assert_eq!(out, lines_vec!["    ```foo,bar"]);
}

#[test]
fn compresses_tilde_fences() {
    let input = lines_vec!["~~~~rust", "code", "~~~~"];
    let out = compress_fences(&input);
    assert_eq!(out, lines_vec!["```rust", "code", "```"]);
}

#[test]
fn preserves_null_language_during_interior_conflict() {
    let input = lines_vec!["````null", "```rust", "fn main() {}", "```", "````"];
    let out = compress_fences(&input);

    assert_eq!(
        out,
        lines_vec!["````", "```rust", "fn main() {}", "```", "````"]
    );
}
#[rstest]
#[case(
    lines_vec!["````markdown", "```rust", "fn main() {}", "```", "````"],
    lines_vec!["````markdown", "```rust", "fn main() {}", "```", "````"]
)]
#[case(
    lines_vec!["~~~~markdown", "~~~rust", "fn main() {}", "~~~", "~~~~"],
    lines_vec!["~~~~markdown", "~~~rust", "fn main() {}", "~~~", "~~~~"]
)]
#[case(
    lines_vec!["~~~markdown", "```rust", "fn main() {}", "```", "~~~"],
    lines_vec!["~~~markdown", "```rust", "fn main() {}", "```", "~~~"]
)]
#[case(
    lines_vec!["````markdown", "~~~rust", "fn main() {}", "~~~", "````"],
    lines_vec!["```markdown", "~~~rust", "fn main() {}", "~~~", "```"]
)]
#[case(
    lines_vec!["``` rust", "~~~example", "code", "~~~", "```"],
    lines_vec!["``` rust", "~~~example", "code", "~~~", "```"]
)]
#[case(
    lines_vec!["```` rust", "code", "````"],
    lines_vec!["```` rust", "code", "````"]
)]
fn preserves_nested_or_spaced_fence_blocks(
    #[case] input: Vec<String>,
    #[case] expected: Vec<String>,
) {
    let out = compress_fences(&input);
    assert_eq!(out, expected);
}

#[test]
fn does_not_compress_mixed_fences() {
    // Each block is unclosed because the trailing delimiter uses a different
    // marker character, and that trailing delimiter is fence-shaped content of
    // the unclosed fence. Rewriting the opener would change the family, or the
    // length, that keeps it literal — the first block compressed to three
    // backticks used to turn its trailing ``` into a closing delimiter — so the
    // preserved-delimiter rule leaves both blocks untouched.
    let input = lines_vec!["~~~rust", "code", "```"];
    let out = compress_fences(&input);
    assert_eq!(out, input);

    let input2 = lines_vec!["```rust", "code", "~~~"];
    let out2 = compress_fences(&input2);
    assert_eq!(out2, input2);
}

#[test]
fn leaves_other_lines_untouched() {
    let input = lines_vec!["~~", "``text``"];
    let out = compress_fences(&input);
    assert_eq!(out, input);
}

#[test]
fn fixes_orphaned_specifier() {
    let input = lines_vec!["Rust", "```", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[rstest]
#[case(
    lines_vec!["````markdown", "Rust", "```", "fn main() {}", "```", "````"],
    lines_vec!["````markdown", "Rust", "```", "fn main() {}", "```", "````"]
)]
#[case(
    lines_vec!["````markdown", "```", "Rust", "fn main() {}", "```", "````"],
    lines_vec!["````markdown", "```", "Rust", "fn main() {}", "```", "````"]
)]
#[case(
    lines_vec!["Rust", "````", "```", "fn main() {}", "```", "````"],
    lines_vec!["````rust", "```", "fn main() {}", "```", "````"]
)]
fn handles_orphan_specifiers_around_outer_fences(
    #[case] input: Vec<String>,
    #[case] expected: Vec<String>,
) {
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, expected);
}

#[test]
fn attaches_orphan_specifier_unit() {
    let input = lines_vec!["Rust", "```", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&input);
    assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[test]
fn attaches_orphan_specifier_with_blank_line_unit() {
    let input = lines_vec!["Rust", "", "```", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&input);
    assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[rstest]
#[case(lines_vec!["Rust", "", "```", "fn main() {}", "```"])]
#[case(lines_vec!["Rust", "", "", "```", "fn main() {}", "```"])]
fn fixes_orphaned_specifier_with_blank_lines(#[case] input: Vec<String>) {
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[rstest]
#[case(lines_vec!["Rust", "", "", "```python", "print('hi')", "```"])]
#[case(lines_vec!["Rust", "", "not a fence"])]
fn leaves_orphan_specifier_and_blank_lines_unchanged_when_not_attachable(
    #[case] input: Vec<String>,
) {
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, input);
}

#[test]
fn fixes_multiple_orphaned_specifiers() {
    let input = lines_vec![
        "Rust",
        "```",
        "fn main() {}",
        "```",
        "",
        "Python",
        "```",
        "print('hi')",
        "```",
    ];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(
        out,
        lines_vec![
            "```rust",
            "fn main() {}",
            "```",
            "",
            "```python",
            "print('hi')",
            "```"
        ]
    );
}

#[test]
fn does_not_attach_non_orphan_lines_before_fences() {
    let input = lines_vec![
        "Rust code",
        "```",
        "fn main() {}",
        "```",
        "rust!",
        "```",
        "println!(\"hi\");",
        "```",
    ];
    let out = attach_orphan_specifiers(&input);
    assert_eq!(out, input);
}

#[test]
fn does_not_overwrite_existing_fence() {
    let input = lines_vec!["ruby", "```rust", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["ruby", "```rust", "fn main() {}", "```"]);
}

#[test]
fn does_not_attach_specifier_without_preceding_blank_line() {
    let input = lines_vec!["intro", "Rust", "```", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(
        out,
        lines_vec!["intro", "Rust", "```", "fn main() {}", "```"]
    );
}

#[test]
fn attaches_orphan_specifier_with_symbols() {
    let input = lines_vec!["C++", "```", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["```c++", "fn main() {}", "```"]);
}

#[test]
fn attaches_orphan_specifier_with_hyphen_and_dot() {
    let input = lines_vec!["objective-c", "```", "int main() {}", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["```objective-c", "int main() {}", "```"]);
}

#[test]
fn does_not_attach_specifier_with_trailing_period() {
    let input = lines_vec!["rust.", "```", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&input);
    assert_eq!(out, input);
}

#[test]
fn does_not_attach_specifier_with_trailing_question_mark() {
    let input = lines_vec!["rust?", "```", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&input);
    assert_eq!(out, input);
}

#[test]
fn attaches_orphan_specifier_preserves_indent() {
    let input = lines_vec!["  Rust", "", "  ```", "  fn main() {}", "  ```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["  ```rust", "  fn main() {}", "  ```"]);
}

#[test]
fn attaches_orphan_specifier_preserves_tab_indent() {
    let input = lines_vec!["\tRust", "", "\t```", "\tfn main() {}", "\t```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["\t```rust", "\tfn main() {}", "\t```"]);
}

#[test]
fn attaches_orphan_specifier_mixed_indent() {
    let input = lines_vec![" \tRust", "", " \t```", " \tfn main() {}", " \t```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec![" \t```rust", " \tfn main() {}", " \t```"]);
}

#[test]
fn attaches_orphan_specifier_uses_candidate_indent_when_fence_unindented() {
    let input = lines_vec!["  Rust", "", "```", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["  ```rust", "fn main() {}", "```"]);
}

#[rstest]
#[case(lines_vec!["  Rust", "", "	```", "	fn main() {}", "	```"], lines_vec!["	```rust", "	fn main() {}", "	```"])]
#[case(lines_vec!["    Rust", "", "  ```", "  fn main() {}", "  ```"], lines_vec!["    ```rust", "  fn main() {}", "  ```"])]
fn attaches_orphan_specifier_with_mismatched_indent(
    #[case] input: Vec<String>,
    #[case] expected: Vec<String>,
) {
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, expected);
}

#[test]
fn attaches_orphan_specifier_allows_spaces() {
    let input = lines_vec!["TOML, Ini", "```", "a=1", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(out, lines_vec!["```toml,ini", "a=1", "```"]);
}

#[rstest]
#[case("````null", "````")]
#[case("````NULL", "````")]
#[case("````Null", "````")]
#[case("````null  ", "````")]
#[case("````   ", "````")]
#[case("````NULL  ", "````")]
#[case("````Null  ", "````")]
#[case("~~~~null", "~~~~")]
#[case("~~~~NULL", "~~~~")]
#[case("~~~~Null", "~~~~")]
#[case("~~~~null  ", "~~~~")]
#[case("~~~~   ", "~~~~")]
#[case("~~~~NULL  ", "~~~~")]
#[case("~~~~Null  ", "~~~~")]
fn compresses_null_language_to_empty(#[case] open: &str, #[case] close: &str) {
    let input = lines_vec![open, "code", close];
    let out = compress_fences(&input);
    assert_eq!(out, lines_vec!["```", "code", "```"]);
}

#[rstest]
#[case("```null", "```")]
#[case("```NULL", "```")]
#[case("```Null", "```")]
#[case("```null  ", "```")]
#[case("```NULL  ", "```")]
#[case("```Null  ", "```")]
#[case("~~~~null", "~~~~")]
#[case("~~~~NULL", "~~~~")]
#[case("~~~~Null", "~~~~")]
#[case("~~~~null  ", "~~~~")]
#[case("~~~~NULL  ", "~~~~")]
#[case("~~~~Null  ", "~~~~")]
fn attaches_orphan_specifier_when_null_language(#[case] fence: &str, #[case] opener: &str) {
    // A backtick opener with the backtick delimiter below it closes, so that
    // block is matched and both of its delimiters compress. A tilde opener with
    // the same backtick delimiter below it never closes, and that delimiter is
    // fence-shaped content of the unclosed block, so the opener keeps its own
    // family and length. Either way the null language is dropped, which is what
    // this test is about.
    let input = lines_vec!["Rust", fence, "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(
        out,
        lines_vec![&format!("{opener}rust"), "fn main() {}", "```"]
    );
}

#[test]
fn attaches_orphan_specifier_null_language_without_compression() {
    let input = lines_vec!["Rust", "```null", "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&input);
    assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[rstest]
#[case("```   ", "```")]
#[case("~~~~   ", "~~~~")]
fn attaches_orphan_specifier_whitespace_language(#[case] fence: &str, #[case] opener: &str) {
    // As above: the backtick block closes and compresses, the tilde block does
    // not and keeps its opener, and the whitespace-only language is dropped.
    let input = lines_vec!["Rust", fence, "fn main() {}", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));
    assert_eq!(
        out,
        lines_vec![&format!("{opener}rust"), "fn main() {}", "```"]
    );
}

#[test]
fn compresses_matched_fence_reusing_cached_opening_and_closing_rewrites() {
    // Both delimiters are rewritable and are emitted from the per-line cache,
    // while the interior content lines pass through untouched.
    let input = lines_vec!["`````rust", "let x = 1;", "let y = 2;", "`````"];
    let out = compress_fences(&input);
    assert_eq!(
        out,
        lines_vec!["```rust", "let x = 1;", "let y = 2;", "```"]
    );
}

#[test]
fn unclosed_fence_rewrites_only_the_opening_delimiter() {
    // No closing delimiter matches the six-backtick opener, so the block is
    // emitted through the unmatched fallback. Nothing inside the block is
    // fence-shaped, so only the opening delimiter is rewritten and the interior
    // lines are preserved verbatim.
    let input = lines_vec!["``````rust", "plain text", "more text"];
    let out = compress_fences(&input);
    assert_eq!(out, lines_vec!["```rust", "plain text", "more text"]);
}

#[test]
fn unclosed_fence_keeps_the_opener_reported_for_an_interior_shorter_fence() {
    // The four-backtick opener is what makes the three-backtick line below it
    // literal content rather than a closing delimiter. Compressing the opener
    // closed the block early, so the payload below moved out of the literal
    // region and a later pass rewrote it — `--ellipsis` turned `literal...`
    // into `literal…`. The opener keeps its four backticks and the payload
    // stays inside the unclosed fence.
    let input = lines_vec!["````", "```", "literal..."];
    let out = compress_fences(&input);
    assert_eq!(out, input);
}

#[test]
fn unclosed_fence_keeps_the_opener_that_makes_its_interior_literal() {
    // The interior run is fence-shaped and strictly shorter than the opener, so
    // it is literal content only for as long as the opener stays six markers
    // long. Rewriting the opener to three markers would promote that run to the
    // closing delimiter and end the block a line early, then rewrite the
    // remainder on the next pass, so the unmatched path applies the same
    // preserved-delimiter rule as the matched one and the document round-trips.
    let input = lines_vec!["``````rust", "````", "~~~"];
    let out = compress_fences(&input);
    assert_eq!(out, input);
}

#[rstest]
#[case(lines_vec!["> ```rust", "> code", "> ```"])]
#[case(lines_vec!["> > ````toml", "> > data", "> > ````"])]
#[case(lines_vec![">```", ">code", ">```"])]
fn preserves_quoted_and_nested_fences(#[case] input: Vec<String>) {
    // Blockquoted delimiters are not normalization-compatible, so quoted and
    // nested fenced blocks round-trip unchanged while remaining correctly opened
    // and closed at their blockquote depth.
    let out = compress_fences(&input);
    assert_eq!(out, input);
}

#[test]
fn compresses_top_level_fence_after_quoted_block() {
    // A quoted block passes through untouched; the following unquoted block is
    // compressed, proving depth transitions do not leak between the two.
    let input = lines_vec!["> ````rust", "> code", "> ````", "````rust", "top", "````",];
    let out = compress_fences(&input);
    assert_eq!(
        out,
        lines_vec!["> ````rust", "> code", "> ````", "```rust", "top", "```",]
    );
}

#[rstest]
#[case::hyphen_run("---")]
#[case::asterisk_run("***")]
#[case::underscore_run("___")]
#[case::spaced_hyphen_run("- - -")]
#[case::indented_hyphen_run("   ---")]
#[case::normalised_break(&"_".repeat(mdtablefix::THEMATIC_BREAK_LEN))]
fn does_not_attach_a_thematic_break_as_a_specifier(#[case] break_line: &str) {
    // `--breaks` writes a seventy-underscore line for every thematic break, and
    // an underscore run is shaped like a language specifier. Attaching it would
    // delete the break, so the break line survives and the fence stays unlabelled.
    let input = lines_vec![break_line, "```", "aa", "```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));

    assert_eq!(out, input);
}

#[test]
fn does_not_attach_a_thematic_break_through_blank_lines() {
    let break_line = "_".repeat(mdtablefix::THEMATIC_BREAK_LEN);
    let input = lines_vec![
        break_line.as_str(),
        "",
        "```",
        "aa",
        "```",
        "",
        "Rust",
        "```",
        "bb",
        "```",
    ];
    let out = attach_orphan_specifiers(&compress_fences(&input));

    // The break survives, and the ordinary specifier below it still attaches.
    assert_eq!(
        out,
        lines_vec![
            break_line.as_str(),
            "",
            "```",
            "aa",
            "```",
            "",
            "```rust",
            "bb",
            "```",
        ]
    );
}

#[test]
fn attaches_a_tab_indented_specifier_that_is_not_a_break() {
    // A tab indent is indented code, not a thematic break, so the line remains
    // eligible for attachment exactly as before.
    let input = lines_vec!["\tRust", "", "\t```", "\tfn main() {}", "\t```"];
    let out = attach_orphan_specifiers(&compress_fences(&input));

    assert_eq!(out, lines_vec!["\t```rust", "\tfn main() {}", "\t```"]);
}

#[test]
fn keeps_info_string_marker_as_literal_content() {
    // A same-marker line carrying an info string is not a closing fence
    // (CommonMark), so the block stays open through the pipe line and the whole
    // fenced block round-trips verbatim rather than closing early.
    let input = lines_vec![
        "```rust",
        "let table = build();",
        "```rust extra info",
        "| tee /tmp/test.log",
        "```",
    ];
    let out = compress_fences(&input);
    assert_eq!(out, input);
}