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
//! Integration tests for list renumbering.

use assert_cmd::Command;
use mdtablefix::renumber_lists;
use rstest::rstest;

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

/// Asserts that renumbering `input` gives `expected` and that a second pass
/// then changes nothing, which is the property every regression case shares.
fn assert_is_a_renumbering_fixed_point(input: &[String], expected: &[String]) {
    let once = renumber_lists(input);
    assert_eq!(once, expected);
    assert_eq!(renumber_lists(&once), once, "a second pass changes nothing");
}

/// Asserts that `mdtablefix --renumber` writes `input` back exactly as it read it.
fn assert_cli_leaves_alone(input: &'static str) {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--renumber")
        .write_stdin(input)
        .assert()
        .success()
        .stdout(input);
}

#[test]
fn restart_after_equal_indent_paragraph() {
    // `3. Next` directly follows the paragraph, so it continues it (#573).
    let input = lines_vec!("1. One", "", "Paragraph", "3. Next");
    let expected = lines_vec!("1. One", "", "Paragraph", "3. Next");
    assert_eq!(renumber_lists(&input), expected);
}

#[test]
fn no_restart_without_blank() {
    let input = lines_vec!("1. One", "Paragraph", "3. Next");
    let expected = lines_vec!("1. One", "Paragraph", "2. Next");
    assert_eq!(renumber_lists(&input), expected);
}

#[test]
fn an_indented_paragraph_left_of_the_content_column_ends_the_list() {
    // Two columns is left of the item's content column (three), so the
    // paragraph is outside the item and ends the list; `3. Next` then cannot
    // interrupt that paragraph and stays as written (#573).
    let input = lines_vec!("1. One", "", "  Indented", "3. Next");
    let expected = lines_vec!("1. One", "", "  Indented", "3. Next");
    assert_eq!(renumber_lists(&input), expected);
}

#[test]
fn restart_after_top_heading() {
    let input = lines_vec!("1. One", "", "# Heading", "3. Next");
    let expected = lines_vec!("1. One", "", "# Heading", "1. Next");
    assert_eq!(renumber_lists(&input), expected);
}

#[test]
fn restart_after_nested_paragraph() {
    // `3. Next` directly follows the paragraph, so it continues it (#573).
    let input = lines_vec!("1. One", "    1. Sub", "", "Paragraph", "3. Next");
    let expected = lines_vec!("1. One", "    1. Sub", "", "Paragraph", "3. Next");
    assert_eq!(renumber_lists(&input), expected);
}

#[test]
fn restart_after_nested_equal_indent_paragraph() {
    // `5. Next` directly follows the paragraph, so it continues it (#573).
    let input = lines_vec!("1. One", "    1. Sub", "", "    Paragraph", "    5. Next");
    let expected = lines_vec!("1. One", "    1. Sub", "", "    Paragraph", "    5. Next");
    assert_eq!(renumber_lists(&input), expected);
}

#[test]
fn restart_after_formatting_paragraph() {
    let input = lines_vec!("1. Start", "", "**Bold intro**", "", "4. Next");
    let expected = lines_vec!("1. Start", "", "**Bold intro**", "", "1. Next");
    assert_eq!(renumber_lists(&input), expected);
}

#[test]
fn reset_on_heading_and_thematic_break() {
    let input = lines_vec!("1. a", "2. b", "# Heading", "1. c", "---", "5. d");
    let expected = lines_vec!("1. a", "2. b", "# Heading", "1. c", "---", "1. d");
    assert_eq!(renumber_lists(&input), expected);
}

/// A quoted heading or break is ended by the quote, not read as a heading.
///
/// A block quote interrupts a paragraph, so it ends the list above it even
/// without a blank line; the list after it restarts at one. The quoted
/// structure is not what ends the list: a heading or break would end it by
/// the content-column rule, which the quote prefix takes out of play.
#[rstest::rstest]
#[case::quoted_break("> ---")]
#[case::quoted_heading("> # Heading")]
fn a_quote_ends_the_list_it_interrupts(#[case] quoted_line: &str) {
    let input = lines_vec!("1. first", "2. second", quoted_line, "8. third");
    let expected = lines_vec!("1. first", "2. second", quoted_line, "1. third");

    assert_eq!(renumber_lists(&input), expected);
}
/// Tests the CLI `--renumber` option.
///
/// Ensures that list numbering is corrected when the flag is supplied.
#[test]
fn test_cli_renumber_option() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--renumber")
        .write_stdin("1. a\n4. b\n")
        .assert()
        .success()
        .stdout("1. a\n2. b\n");
}

#[test]
fn nested_lists_respect_fence_tracker() {
    let input = lines_vec![
        "1. Outer list",
        "   ```",
        "   1. Code block list",
        "   ```",
        "9. Outer list continued",
        "   4. Nested list",
        "      ```",
        "      - Malformed fence",
        "      ```",
        "   8. Nested list continued",
    ];
    let expected = lines_vec![
        "1. Outer list",
        "   ```",
        "   1. Code block list",
        "   ```",
        "2. Outer list continued",
        // `4.` continues the item's text, so it is not a list (#573); the
        // fence ends that paragraph, so `8.` starts a list, at one.
        "   4. Nested list",
        "      ```",
        "      - Malformed fence",
        "      ```",
        "   1. Nested list continued",
    ];
    assert_eq!(renumber_lists(&input), expected);
}

#[test]
fn malformed_fences_do_not_break_list_renumbering() {
    let input = lines_vec![
        "1. List before fence",
        "   ```",
        "   1. Inside code block",
        "   ``",
        "   still inside fence",
        "   ```",
        "7. List after fence",
    ];
    let expected = lines_vec![
        "1. List before fence",
        "   ```",
        "   1. Inside code block",
        "   ``",
        "   still inside fence",
        "   ```",
        "2. List after fence",
    ];
    assert_eq!(renumber_lists(&input), expected);
}

#[rstest(
    input,
    expected,
    case::basic(
        lines_vec!["1. first", "2. second", "7. third"],
        lines_vec!["1. first", "2. second", "3. third"]
    ),
    // A fence at the list's marker column ends the list (issue #563).
    case::with_fence(
        lines_vec!["1. item", "```", "code", "```", "9. next"],
        lines_vec!["1. item", "```", "code", "```", "1. next"]
    ),
    case::nested_lists(
        lines_vec!["1. first", "    1. sub first", "    3. sub second", "2. second"],
        lines_vec!["1. first", "    1. sub first", "    2. sub second", "2. second"]
    ),
    case::tabs_in_indent(
        lines_vec!["1. first", "\t1. sub first", "\t5. sub second", "2. second"],
        lines_vec!["1. first", "\t1. sub first", "\t2. sub second", "2. second"]
    ),
    case::mult_paragraph_items(
        lines_vec!["1. first", "", "    still first paragraph", "", "2. second"],
        lines_vec!["1. first", "", "    still first paragraph", "", "2. second"]
    ),
    case::table_in_list(
        lines_vec!["1. first", "    | A | B |", "    | 1 | 2 |", "5. second"],
        lines_vec!["1. first", "    | A | B |", "    | 1 | 2 |", "2. second"]
    ),
    case::restart_after_paragraph(
        include_lines!("data/renumber_paragraph_restart_input.txt"),
        include_lines!("data/renumber_paragraph_restart_expected.txt")
    ),
    case::restart_after_formatting(
        include_lines!("data/renumber_formatting_paragraph_input.txt"),
        include_lines!("data/renumber_formatting_paragraph_expected.txt")
    ),
    case::restart_after_break(
        include_lines!("data/renumber_break_restart_input.txt"),
        include_lines!("data/renumber_break_restart_expected.txt")
    ),
    case::restart_after_heading(
        include_lines!("data/renumber_heading_restart_input.txt"),
        include_lines!("data/renumber_heading_restart_expected.txt")
    ),
    case::restart_after_break_and_heading(
        include_lines!("data/renumber_break_heading_restart_input.txt"),
        include_lines!("data/renumber_break_heading_restart_expected.txt")
    ),
    case::blank_lines(
        include_lines!("data/renumber_blank_lines_input.txt"),
        include_lines!("data/renumber_blank_lines_expected.txt")
    ),
    case::ordered_list(
        include_lines!("data/renumber_ordered_list_input.txt"),
        include_lines!("data/renumber_ordered_list_expected.txt")
    )
)]
fn test_renumber_cases(input: Vec<String>, expected: Vec<String>) {
    assert_eq!(renumber_lists(&input), expected);
}

/// Regression cases for issue #450: a heading indented inside a list item
/// does not end the ordered list.
///
/// Issue #450 was introduced by #106 (1b41d7f), which made every ATX heading
/// and thematic break up to three spaces deep reset all list state. A heading
/// or break indented to an item's content column is a child block of that
/// item, so the items after it keep their numbers.
#[rstest]
#[case::nested_heading(include_lines!("data/issue_450_nested_heading_input.txt"))]
#[case::nested_break(include_lines!("data/issue_450_nested_break_input.txt"))]
fn renumber_issue_450_block_inside_an_item_keeps_the_list_counting(#[case] input: Vec<String>) {
    assert_is_a_renumbering_fixed_point(&input, &input);
}

/// Regression cases for issue #450, the neighbouring shapes #106 touched: a
/// heading or break ends exactly the lists whose current item it is not
/// indented into.
///
/// #106 reset on every ATX heading and thematic break indented zero to three
/// spaces. Under `1. `, whose content starts at column 3, a heading at three
/// spaces is inside the item and the list continues; at one or two spaces it
/// is not, so the list ends as it does at column 0, which #106 intended. A
/// block inside the outer item but left of a nested item's content ends the
/// nested list only.
#[rstest]
#[case::heading_at_nested_marker_column(
    lines_vec![
        "1. Outer", "   1. Inner", "   2. Inner", "", "   ## Heading in the outer item", "",
        "   5. Inner again", "7. Outer again",
    ],
    lines_vec![
        "1. Outer", "   1. Inner", "   2. Inner", "", "   ## Heading in the outer item", "",
        "   1. Inner again", "2. Outer again",
    ]
)]
#[case::break_at_nested_marker_column(
    lines_vec!["1. Outer", "   1. Inner", "", "   ---", "", "   4. Inner again", "6. Outer again"],
    lines_vec!["1. Outer", "   1. Inner", "", "   ---", "", "   1. Inner again", "2. Outer again"]
)]
#[case::heading_three_spaces_inside_item(
    lines_vec!["1. a", "", "   ### h", "", "5. b"],
    lines_vec!["1. a", "", "   ### h", "", "2. b"]
)]
#[case::heading_two_spaces_ends_list(
    lines_vec!["1. a", "", "  ### h", "", "5. b"],
    lines_vec!["1. a", "", "  ### h", "", "1. b"]
)]
#[case::heading_one_space_ends_list(
    lines_vec!["1. a", "", " ### h", "", "5. b"],
    lines_vec!["1. a", "", " ### h", "", "1. b"]
)]
#[case::break_two_spaces_ends_list(
    lines_vec!["1. a", "", "  ***", "", "5. b"],
    lines_vec!["1. a", "", "  ***", "", "1. b"]
)]
// Inside the outer item but left of the nested item's content: the heading
// reads as a heading relative to the outer item, so only the nested list ends.
#[case::heading_at_column_four_between_items(
    lines_vec![
        "1. Outer", "   1. Inner", "", "    #### Heading", "", "   5. Inner again", "7. Outer again",
    ],
    lines_vec![
        "1. Outer", "   1. Inner", "", "    #### Heading", "", "   1. Inner again", "2. Outer again",
    ]
)]
// A tab in the separator advances to the next tab stop, so `1. \t` puts the
// content at column 4 and a three-space heading is outside the item.
#[case::heading_left_of_a_tab_separated_item(
    lines_vec!["1. \titem", "", "   ### h", "", "5. b"],
    lines_vec!["1. \titem", "", "   ### h", "", "1. b"]
)]
// The content column is measured on the emitted marker, so `10.` becoming
// `2.` puts the heading inside the item on the first pass as on the second.
#[case::heading_after_a_narrowed_marker(
    lines_vec!["9. a", "10. b", "", "   ### h", "", "4. c"],
    lines_vec!["1. a", "2. b", "", "   ### h", "", "3. c"]
)]
#[case::heading_at_margin(
    lines_vec!["1. a", "4. b", "", "# Title", "", "6. c", "9. d"],
    lines_vec!["1. a", "2. b", "", "# Title", "", "1. c", "2. d"]
)]
#[case::break_at_margin(
    lines_vec!["1. a", "   1. sub", "", "---", "", "6. c"],
    lines_vec!["1. a", "   1. sub", "", "---", "", "1. c"]
)]
fn renumber_issue_450_neighbouring_shapes(
    #[case] input: Vec<String>,
    #[case] expected: Vec<String>,
) {
    assert_is_a_renumbering_fixed_point(&input, &expected);
}

/// Regression case for issue #450 through the CLI: `--renumber` keeps the
/// list counting past a heading nested in an item.
#[test]
fn renumber_issue_450_cli_keeps_counting_past_a_nested_heading() {
    assert_cli_leaves_alone(include_str!("data/issue_450_nested_heading_input.txt"));
}

/// Regression cases for issue #563: a block that ends an ordered list resets
/// numbering for the next list.
///
/// Each fixture is a list, then a block at the list's marker column, then a
/// second list that the source starts at one. `CommonMark` ends the first list
/// at that block, so renumbering must leave the second list at one; carrying
/// the count across it changes the rendered `start` of the second list.
#[rstest]
#[case::fence(include_lines!("data/issue_563_fence_input.txt"))]
#[case::bullet_list(include_lines!("data/issue_563_bullet_list_input.txt"))]
#[case::table(include_lines!("data/issue_563_table_input.txt"))]
#[case::block_quote(include_lines!("data/issue_563_block_quote_input.txt"))]
#[case::html_comment(include_lines!("data/issue_563_html_comment_input.txt"))]
#[case::link_paragraph(include_lines!("data/issue_563_link_paragraph_input.txt"))]
fn renumber_issue_563_block_at_marker_column_ends_the_list(#[case] input: Vec<String>) {
    assert_is_a_renumbering_fixed_point(&input, &input);
}

/// Regression cases for issue #563, the neighbouring shapes: a block
/// indented into an item, or a bullet item that interrupts without a blank
/// line, is decided by its column alone.
///
/// A block right of the list's marker column belongs to the item and the
/// list keeps counting; one at a nested list's marker column ends only that
/// nested list; a bullet item at the marker column ends the list even with
/// no blank line before it.
#[rstest]
#[case::fence_inside_item(
    lines_vec!["1. a", "   ```", "   code", "   ```", "5. b"],
    lines_vec!["1. a", "   ```", "   code", "   ```", "2. b"]
)]
#[case::bullet_inside_item(
    lines_vec!["1. a", "", "   - sub", "", "5. b"],
    lines_vec!["1. a", "", "   - sub", "", "2. b"]
)]
#[case::fence_at_nested_marker_column(
    lines_vec![
        "1. Outer", "   1. Inner", "   2. Inner", "", "   ```", "   code", "   ```", "",
        "   4. Inner again", "5. Outer again",
    ],
    lines_vec![
        "1. Outer", "   1. Inner", "   2. Inner", "", "   ```", "   code", "   ```", "",
        "   1. Inner again", "2. Outer again",
    ]
)]
// The bullet ends the list without a blank line above it, so the list after
// the blank line restarts. (A `3. c` directly below the bullet is not a list
// item at all: it lazily continues the bullet's paragraph, see #573.)
#[case::bullet_interrupts_without_blank(
    lines_vec!["1. a", "2. b", "- bullet", "", "3. c"],
    lines_vec!["1. a", "2. b", "- bullet", "", "1. c"]
)]
// A block quote or an HTML block that can interrupt a paragraph ends the list
// without a blank line; an inline tag, a bare `-` and a `-` followed by a
// non-breaking space are paragraph text and leave it open.
#[case::quote_interrupts_without_blank(
    lines_vec!["1. a", "> quote", "", "5. b"],
    lines_vec!["1. a", "> quote", "", "1. b"]
)]
#[case::html_block_interrupts_without_blank(
    lines_vec!["1. a", "<div>", "", "5. b"],
    lines_vec!["1. a", "<div>", "", "1. b"]
)]
#[case::inline_tag_is_lazy_text(
    lines_vec!["1. a", "<span>x", "4. b"],
    lines_vec!["1. a", "<span>x", "2. b"]
)]
#[case::bare_bullet_is_lazy_text(lines_vec!["1. a", "-", "4. b"], lines_vec!["1. a", "-", "2. b"])]
#[case::non_breaking_space_is_no_bullet(
    lines_vec!["1. a", "-\u{a0}text", "7. next"],
    lines_vec!["1. a", "-\u{a0}text", "2. next"]
)]
// Two columns is left of the content column of `1. `, so the paragraph is not
// in the item and ends the list.
#[case::paragraph_left_of_the_content_column(
    lines_vec!["1. a", "", "  para", "", "5. b"],
    lines_vec!["1. a", "", "  para", "", "1. b"]
)]
#[case::lazy_paragraph_continues(
    lines_vec!["1. a", "[lazy](u) continuation", "3. b"],
    lines_vec!["1. a", "[lazy](u) continuation", "2. b"]
)]
fn renumber_issue_563_neighbouring_shapes(
    #[case] input: Vec<String>,
    #[case] expected: Vec<String>,
) {
    assert_is_a_renumbering_fixed_point(&input, &expected);
}

/// Regression case for issue #563 through the CLI: `--renumber` leaves the
/// list after an ending fence at one.
#[test]
fn renumber_issue_563_cli_keeps_the_restart_after_a_fence() {
    assert_cli_leaves_alone(include_str!("data/issue_563_fence_input.txt"));
}