mdtablefix 0.5.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
//! Tests for the inline wrapping pipeline.
//!
//! These cases exercise `wrap_preserving_code` and
//! `attach_punctuation_to_previous_line` from `crate::wrap::inline`, covering
//! trailing-punctuation coupling with links, hard-break preservation, and the
//! `([n](url))` inline citation attachment introduced in issue #325. The suite
//! uses `proptest` for generated wrapping cases and `rstest` for fixture-driven
//! and snapshot tests, verifying that parenthesized citation links and adjacent
//! citation chains are never split across wrapped lines.

use std::fmt::Write as _;

use rstest::rstest;

use super::{
    super::inline::{attach_punctuation_to_previous_line, wrap_preserving_code},
    TRAILING_PUNCTUATION_CHARS,
};

proptest::proptest! {
    #[test]
    fn wrap_preserving_code_keeps_supported_punctuation_with_links(
        punctuation_index in 0..TRAILING_PUNCTUATION_CHARS.len(),
        pad_len in 0usize..24,
        wrap_width in 24usize..96,
        url_slug in "[a-z]{3,12}",
    ) {
        let punctuation = TRAILING_PUNCTUATION_CHARS[punctuation_index];
        let prefix = "lead ".repeat(pad_len);
        let link = format!("[link](docs/{url_slug}.md){punctuation}");
        let input = format!("{prefix}{link} trailing words force wrapping");
        let lines = wrap_preserving_code(&input, wrap_width);

        assert!(
            lines.iter().any(|line| line.contains(&link)),
            "expected {link:?} to stay attached in {lines:?}",
        );
        assert!(
            lines.iter().all(|line| line.trim() != punctuation.to_string()),
            "punctuation was orphaned in {lines:?}",
        );
    }

    #[test]
    fn wrap_preserving_code_keeps_supported_punctuation_with_code_spans(
        punctuation_index in 0..TRAILING_PUNCTUATION_CHARS.len(),
        pad_len in 0usize..24,
        wrap_width in 24usize..96,
    ) {
        let punctuation = TRAILING_PUNCTUATION_CHARS[punctuation_index];
        let prefix = "lead ".repeat(pad_len);
        let code_span = format!("`code-{pad_len}`{punctuation}");
        let input = format!("{prefix}{code_span} trailing words force wrapping");
        let lines = wrap_preserving_code(&input, wrap_width);

        assert!(
            lines.iter().any(|line| line.contains(&code_span)),
            "expected {code_span:?} to stay attached in {lines:?}",
        );
        assert!(
            lines.iter().all(|line| line.trim() != punctuation.to_string()),
            "punctuation was orphaned in {lines:?}",
        );
    }

    #[test]
    fn wrap_preserving_code_keeps_generated_inline_citations_attached(
        wrap_width in 24usize..96,
        prefix_len in 0usize..32,
        citation_count in 1usize..6,
    ) {
        let citation = inline_citation_chain(citation_count);
        let expected_citation = format!("pattern{citation}");
        let input = format!(
            "{}{expected_citation} trailing words force wrapping",
            "lead ".repeat(prefix_len)
        );
        let lines = wrap_preserving_code(&input, wrap_width);

        assert_inline_citation_invariants(&lines, &expected_citation);
    }
}

#[test]
fn attach_punctuation_appends_to_previous_code_line() {
    let mut lines = vec!["wrap `code`".to_string()];
    let current = String::new();
    assert!(attach_punctuation_to_previous_line(
        lines.as_mut_slice(),
        &current,
        "!",
    ));
    assert_eq!(lines, vec!["wrap `code`!".to_string()]);
}

#[test]
fn attach_punctuation_requires_empty_current_buffer() {
    let mut lines = vec!["`code`".to_string()];
    let current = " pending".to_string();
    assert!(!attach_punctuation_to_previous_line(
        lines.as_mut_slice(),
        &current,
        "!",
    ));
    assert_eq!(lines, vec!["`code`".to_string()]);
}

#[test]
fn attach_punctuation_ignores_non_code_suffix() {
    let mut lines = vec!["plain text".to_string()];
    let current = String::new();
    assert!(!attach_punctuation_to_previous_line(
        lines.as_mut_slice(),
        &current,
        ".",
    ));
    assert_eq!(lines, vec!["plain text".to_string()]);
}

#[test]
fn wrap_preserving_code_splits_after_consecutive_whitespace() {
    let lines = wrap_preserving_code("alpha  beta   gamma", 8);
    assert_eq!(
        lines,
        vec![
            "alpha  ".to_string(),
            "beta   ".to_string(),
            "gamma".to_string()
        ]
    );
}

#[test]
fn wrap_preserving_code_keeps_colon_suffixed_footnote_reference_with_prose() {
    let input = concat!(
        "The Lighthouse \"Progressive Web App\" category specifically checks for the technical ",
        "requirements of a PWA. The audits are grouped into subcategories [^96]:",
    );
    let lines = wrap_preserving_code(input, 80);

    assert_eq!(
        lines,
        vec![
            "The Lighthouse \"Progressive Web App\" category specifically checks for the",
            "technical requirements of a PWA. The audits are grouped into",
            "subcategories [^96]:",
        ]
    );
    assert!(lines.iter().all(|line| !line.starts_with("[^96]:")));
}

#[test]
fn wrap_preserving_code_couples_opening_paren_before_inline_code() {
    let text = concat!(
        "- `src/cli/mod.rs` (240 lines): defines the `Cli` struct with ",
        "`#[derive(Parser, Serialize, Deserialize, OrthoConfig)]`, its subcommands ",
        "(`Commands` enum), and the `parse_with_localizer_from` function that creates ",
        "a localized clap command and parses arguments."
    );
    let lines = wrap_preserving_code(text, 80);
    for window in lines.windows(2) {
        assert!(
            !window[0].ends_with('('),
            "opening parenthesis must not be stranded at line end: {lines:?}"
        );
    }
}

#[rstest]
#[case("(`code`)", 10)]
#[case("[`code`]", 10)]
#[case("(`code`)", 10)]
#[case("「`code`」", 10)]
#[case("([label](url))", 10)]
#[case("[[label](url)]", 10)]
fn wrap_preserving_code_keeps_opening_bracket_with_inline_code(
    #[case] fragment: &str,
    #[case] width: usize,
) {
    let text = format!("prefix text {fragment} suffix.");
    let lines = wrap_preserving_code(&text, width);
    for line in &lines {
        if line.contains('`') || line.contains("](") {
            assert!(
                !line.ends_with('(')
                    && !line.ends_with('[')
                    && !line.ends_with('(')
                    && !line.ends_with('「'),
                "opening bracket must stay with atomic span on line: {line:?}"
            );
        }
    }
}

#[test]
fn wrap_preserving_code_keeps_reference_link_opening_bracket_with_label() {
    let input = concat!(
        "Implicit fixture injection removes the need for attributes in most cases. ",
        "[user guide][implicit-fixture-guide] · [trybuild][implicit-fixture-trybuild]",
    );
    let lines = wrap_preserving_code(input, 80);

    assert_eq!(
        lines,
        vec![
            "Implicit fixture injection removes the need for attributes in most cases.",
            concat!(
                "[user guide][implicit-fixture-guide] · ",
                "[trybuild][implicit-fixture-trybuild]",
            ),
        ]
    );
    assert!(lines.iter().all(|line| !line.ends_with('[')));
}

fn citation_link_starts(expected_citation: &str) -> Vec<String> {
    let mut markers = Vec::new();
    let mut remaining = expected_citation;
    while let Some(open_index) = remaining.find('[') {
        let candidate = &remaining[open_index..];
        let Some(close_index) = candidate.find("](") else {
            break;
        };
        markers.push(candidate[..close_index + 2].to_string());
        remaining = &candidate[close_index + 2..];
    }
    markers
}

fn inline_citation_chain(citation_count: usize) -> String {
    let mut citation = String::new();
    for index in 1..=citation_count {
        write!(citation, "([{index}](https://example.com/ref{index}))")
            .expect("writing to String cannot fail");
    }
    citation
}

fn assert_inline_citation_invariants(lines: &[String], expected_citation: &str) {
    let citation_link_starts = citation_link_starts(expected_citation);
    assert!(
        !citation_link_starts.is_empty(),
        "expected citation fixture must contain at least one inline link",
    );
    assert!(
        lines.iter().any(|line| line.contains(expected_citation)),
        "expected citation to stay attached in {lines:?}",
    );
    assert!(
        lines.iter().all(|line| !line.ends_with('(')),
        "opening citation punctuation must not be stranded at line end: {lines:?}",
    );
    assert!(
        lines.iter().all(|line| {
            let trimmed = line.trim_start();
            citation_link_starts
                .iter()
                .all(|marker| !trimmed.starts_with(marker))
        }),
        "citation link must not start a continuation line: {lines:?}",
    );
    assert!(
        lines.iter().all(|line| line.trim() != ")("),
        "adjacent citation punctuation must not be orphaned: {lines:?}",
    );
}

#[rstest]
#[case(
    "The formatter keeps pattern([1](https://example.com/ref)) attached while wrapping.",
    32,
    "pattern([1](https://example.com/ref))"
)]
#[case(
    concat!(
        "The formatter keeps runtime([6](https://example.com/command))",
        "([7](https://example.com/event)) attached while wrapping."
    ),
    34,
    "runtime([6](https://example.com/command))([7](https://example.com/event))",
)]
fn wrap_preserving_code_keeps_inline_citation_links_attached(
    #[case] input: &str,
    #[case] width: usize,
    #[case] expected_citation: &str,
) {
    let lines = wrap_preserving_code(input, width);
    assert_inline_citation_invariants(&lines, expected_citation);
}

#[test]
fn wrap_preserving_code_snapshots_single_inline_citation() {
    let lines = wrap_preserving_code(
        "The formatter keeps pattern([1](https://example.com/ref)) attached while wrapping.",
        32,
    );
    insta::assert_snapshot!(
        lines.join("\n"),
        @r###"
The formatter keeps
pattern([1](https://example.com/ref))
attached while wrapping.
"###
    );
}

#[test]
fn wrap_preserving_code_snapshots_adjacent_inline_citations() {
    let lines = wrap_preserving_code(
        concat!(
            "The formatter keeps runtime([6](https://example.com/command))",
            "([7](https://example.com/event)) attached while wrapping."
        ),
        34,
    );
    insta::assert_snapshot!(
        lines.join("\n"),
        @r###"
The formatter keeps
runtime([6](https://example.com/command))([7](https://example.com/event))
attached while wrapping.
"###
    );
}

#[test]
fn wrap_preserving_code_glues_punctuation_after_code() {
    let lines = wrap_preserving_code("line with `code` !", 80);
    assert_eq!(lines, vec!["line with `code`!".to_string()]);
}

#[test]
fn wrap_preserving_code_breaks_between_inline_code_spans() {
    let text = "Extensions (`.toml`, `.json`, `.json5`, `.yaml`, `.yml`).";
    // Width 35 sits between the width of the `.json` and `.json5` prefixes,
    // forcing the wrapper to decide whether it can break between separate
    // inline code spans that are spaced apart.
    let lines = wrap_preserving_code(text, 35);
    assert_eq!(
        lines,
        vec![
            "Extensions (`.toml`, `.json`,".to_string(),
            "`.json5`, `.yaml`, `.yml`).".to_string(),
        ]
    );
}

#[test]
fn wrap_preserving_code_retains_punctuation_after_separate_spans() {
    let text = "Alpha `code` `more`, trailing.";
    let lines = wrap_preserving_code(text, 18);
    assert_eq!(
        lines,
        vec!["Alpha `code`".to_string(), "`more`, trailing.".to_string(),]
    );
}

#[rstest]
#[case("alpha beta", 5, &["alpha", "beta"])]
#[case("alpha  beta", 5, &["alpha", "beta"])]
#[case("alpha `beta`", 5, &["alpha", "`beta`"])]
fn wrap_preserving_code_strips_leading_carry_whitespace(
    #[case] input: &str,
    #[case] width: usize,
    #[case] expected: &[&str],
) {
    let lines = wrap_preserving_code(input, width);
    assert_eq!(
        lines,
        expected.iter().map(|&s| s.to_string()).collect::<Vec<_>>()
    );
    for line in lines.iter().skip(1) {
        assert!(
            !line.starts_with(' '),
            "continuation lines must not begin with carry whitespace: {line:?}"
        );
    }
}

#[rstest]
#[case("  alpha beta", 8, &["  alpha", "beta"])]
fn wrap_preserving_code_preserves_intentional_leading_whitespace_on_first_line(
    #[case] input: &str,
    #[case] width: usize,
    #[case] expected: &[&str],
) {
    let lines = wrap_preserving_code(input, width);
    assert_eq!(
        lines,
        expected.iter().map(|&s| s.to_string()).collect::<Vec<_>>()
    );
    assert!(
        lines[0].starts_with("  "),
        "first line must preserve intentional leading whitespace"
    );
    for line in lines.iter().skip(1) {
        assert!(
            !line.starts_with(' '),
            "continuation lines must not begin with carry whitespace: {line:?}"
        );
    }
}

#[rstest]
#[case("trail  ", 80, &["trail  "])]
#[case("`code span`  ", 12, &["`code span`  "])]
#[case("foo  ", 3, &["foo  "])]
#[case("x  ", 1, &["x  "])]
fn preserves_trailing_spaces(#[case] input: &str, #[case] width: usize, #[case] expected: &[&str]) {
    let out = wrap_preserving_code(input, width);
    assert_eq!(
        out,
        expected.iter().map(|&s| s.to_string()).collect::<Vec<_>>()
    );
}

#[rstest]
#[case("aaaaaaaaaaaa", 5, &["aaaaaaaaaaaa"])] // forced flush without split
#[case("abcde", 3, &["abcde"])]
#[case("`codespan`", 6, &["`codespan`"])]
fn no_split_forced_flush_no_trim(
    #[case] input: &str,
    #[case] width: usize,
    #[case] expected: &[&str],
) {
    let out = wrap_preserving_code(input, width);
    assert_eq!(
        out,
        expected.iter().map(|&s| s.to_string()).collect::<Vec<_>>()
    );
}

#[rstest]
#[case("Check this [link](foo.md)!?", 11, "[link](foo.md)!?", "!?")]
#[case("Reference [doc](bar.md):", 9, "[doc](bar.md):", ":")]
#[case("See [note](baz.md)...", 3, "[note](baz.md)...", "...")]
#[case("Alert [warn](warn.md);", 5, "[warn](warn.md);", ";")]
fn wrap_preserving_code_keeps_trailing_link_punctuation(
    #[case] input: &str,
    #[case] width: usize,
    #[case] expected_link: &str,
    #[case] orphaned_punctuation: &str,
) {
    let lines = wrap_preserving_code(input, width);
    assert!(lines.len() > 1, "expected wrapping for {input:?}");
    assert!(
        lines.iter().any(|line| line.contains(expected_link)),
        "expected {expected_link:?} to stay attached in {lines:?}",
    );
    assert!(
        lines.iter().all(|line| line.trim() != orphaned_punctuation),
        "punctuation was orphaned in {lines:?}",
    );
}

#[test]
fn wrap_preserving_code_handles_leading_link_punctuation() {
    let input = concat!(
        "\"[Quoted link](quote.md)\" is important for understanding the ",
        "overall design because it provides context to the guidelines."
    );
    let lines = wrap_preserving_code(input, 80);
    assert!(lines.len() > 1, "expected wrapping for {input:?}");
    assert!(
        lines
            .iter()
            .any(|line| line.starts_with("\"[Quoted link](quote.md)")),
        "expected leading punctuation to stay with link in {lines:?}",
    );
    assert!(
        lines.iter().all(|line| line.trim() != "\""),
        "leading punctuation was orphaned in {lines:?}",
    );
}

#[test]
fn wrap_preserving_code_handles_leading_and_trailing_link_punctuation() {
    let input = concat!(
        "\"[Link](foo.md)!\" demonstrates punctuation around a link and ",
        "includes plenty of extra words to exceed the wrapping limit."
    );
    let lines = wrap_preserving_code(input, 80);
    assert!(lines.len() > 1, "expected wrapping for {input:?}");
    assert!(
        lines.iter().any(|line| line.contains("[Link](foo.md)!\"")),
        "expected trailing punctuation to stay with link in {lines:?}",
    );
    assert!(
        lines.iter().all(|line| line.trim() != "\""),
        "leading punctuation was orphaned in {lines:?}",
    );
}