rumdl 0.2.49

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

#[test]
fn test_valid_links() {
    let rule = MD039NoSpaceInLinks;
    let content = "[link](url) and [another link](url) here";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_spaces_both_ends() {
    let rule = MD039NoSpaceInLinks;
    let content = "[ link ](url) and [ another link ](url) here";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 2);
    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(fixed, "[link](url) and [another link](url) here");
}

#[test]
fn test_space_at_start() {
    let rule = MD039NoSpaceInLinks;
    let content = "[ link](url) and [ another link](url) here";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 2);
    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(fixed, "[link](url) and [another link](url) here");
}

#[test]
fn test_space_at_end() {
    let rule = MD039NoSpaceInLinks;
    let content = "[link ](url) and [another link ](url) here";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 2);
    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(fixed, "[link](url) and [another link](url) here");
}

#[test]
fn test_link_in_code_block() {
    let rule = MD039NoSpaceInLinks;
    let content = "```\n[ link ](url)\n```\n[ link ](url)";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1);
    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(fixed, "```\n[ link ](url)\n```\n[link](url)");
}

#[test]
fn test_multiple_links() {
    let rule = MD039NoSpaceInLinks;
    let content = "[ link ](url) and [ another ](url) in one line";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 2);
    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(fixed, "[link](url) and [another](url) in one line");
}

#[test]
fn test_link_with_internal_spaces() {
    let rule = MD039NoSpaceInLinks;
    let content = "[this is link](url) and [ this is also link ](url)";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1);
    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(fixed, "[this is link](url) and [this is also link](url)");
}

#[test]
fn test_link_with_punctuation() {
    let rule = MD039NoSpaceInLinks;
    let content = "[ link! ](url) and [ link? ](url) here";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 2);
    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(fixed, "[link!](url) and [link?](url) here");
}

/// A wikilink has no `[text](dest)` shape to rewrite, so trimming its display
/// text would mean emitting a destination that does not exist. `[[Target| x ]]`
/// used to become `[x]` and `![[a.png| 300 ]]` became `[300]`, destroying the
/// link.
#[test]
fn test_wikilinks_are_left_alone() {
    let rule = MD039NoSpaceInLinks;
    // The last line is the positive control: without it, a byte-identical
    // result would also be what a rule that simply never ran produces.
    let content = "[[Target| Display text ]]\n\
                   [[Target]]\n\
                   ![[a.png| 300 ]]\n\
                   ![[a.png| Some description ]]\n\
                   ![[a.png]]\n\
                   [ plain ](url)";
    for flavor in [
        rumdl_lib::config::MarkdownFlavor::Obsidian,
        rumdl_lib::config::MarkdownFlavor::Standard,
    ] {
        let ctx = LintContext::new(content, flavor, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "{flavor:?}: only the inline link warns, got {result:?}"
        );
        assert_eq!(result[0].line, 6);

        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(
            fixed,
            content.replace("[ plain ](url)", "[plain](url)"),
            "{flavor:?}: wikilinks must survive the fix byte for byte"
        );
    }
}

/// The destination is taken from the `]` that closes the link text, not from
/// the first `](` in the span: a nested image carries its own `](`, and
/// splitting there spliced the image's destination back into the document.
#[test]
fn test_nested_image_destination_is_not_spliced() {
    let rule = MD039NoSpaceInLinks;
    let content = "[ before ![x](real.png) after ](Target.md)\n[ plain ](Target.md \"a title\")";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 2, "got {result:?}");

    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(
        fixed,
        "[before ![x](real.png) after](Target.md)\n[plain](Target.md \"a title\")"
    );
}

/// The nested image sits flush against both trimmed spaces, so the outer text
/// begins and ends with the image's own brackets.
#[test]
fn test_nested_image_flush_against_the_brackets() {
    let rule = MD039NoSpaceInLinks;
    let content = "[ ![x](real.png) ](Target.md)";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    assert_eq!(fixed, "[![x](real.png)](Target.md)");
}

mod parity_with_markdownlint {
    use rumdl_lib::lint_context::LintContext;
    use rumdl_lib::rule::Rule;
    use rumdl_lib::rules::MD039NoSpaceInLinks;

    #[test]
    fn parity_leading_trailing_space() {
        let input = "[ link](url) and [another link ](url)";
        let expected = "[link](url) and [another link](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_both_ends_spaced() {
        let input = "[ link ](url) and [ another link ](url)";
        let expected = "[link](url) and [another link](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_internal_spaces_only() {
        let input = "[this is link](url) and [another link](url)";
        let expected = "[this is link](url) and [another link](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert!(warnings.is_empty());
    }

    #[test]
    fn parity_code_block_containing_links() {
        let input = "```
[ link ](url)
```
[ link ](url)";
        let expected = "```
[ link ](url)
```
[link](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 1);
    }

    #[test]
    fn parity_multiple_links_per_line() {
        let input = "[ link ](url) and [ another ](url) in one line";
        let expected = "[link](url) and [another](url) in one line";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_punctuation_in_link_text() {
        let input = "[ link! ](url) and [ link? ](url) here";
        let expected = "[link!](url) and [link?](url) here";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_link_text_only_spaces() {
        let input = "[   ](url) and [ ](url)";
        // markdownlint removes all spaces, resulting in empty link text
        let expected = "[](url) and [](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_reference_style_links() {
        let input = "[ link ][ref] and [ another ][ref2]\n\n[ref]: url\n[ref2]: url2";
        // markdownlint does not fix reference-style links for MD039, so output is unchanged
        let expected = "[ link ][ref] and [ another ][ref2]\n\n[ref]: url\n[ref2]: url2";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        // Should not warn on reference-style links
        let warnings = rule.check(&ctx).unwrap();
        assert!(warnings.is_empty());
    }

    #[test]
    fn parity_unicode_whitespace() {
        let input = "[\u{00A0}link\u{00A0}](url) and [\u{2003}another\u{2003}](url)"; // non-breaking space and em space
        let expected = "[link](url) and [another](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_tab_whitespace() {
        let input = "[\tlink\t](url) and [\tanother\t](url)";
        let expected = "[link](url) and [another](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_only_whitespace_and_newlines() {
        let input = "[   \n  ](url) and [\t\n\t](url)";
        let expected = "[](url) and [](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_internal_newlines() {
        let input = "[link\ntext](url) and [ another\nlink ](url)";
        let expected = "[link\ntext](url) and [another\nlink](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 1);
    }

    #[test]
    fn parity_nested_formatting() {
        let input = "[ * link * ](url) and [ _ another _ ](url)";
        let expected = "[* link *](url) and [_ another _](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_escaped_brackets() {
        let input = "[link\\]](url) and [link\\[]](url)";
        let expected = "[link\\]](url) and [link\\[]](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert!(warnings.is_empty());
    }

    #[test]
    fn parity_inline_images() {
        let input = "![ alt ](img.png) and ![ another ](img2.png)";
        let expected = "![alt](img.png) and ![another](img2.png)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2);
    }

    #[test]
    fn parity_html_entities() {
        let input = "[  link  ](url)";
        let expected = "[ link ](url)";
        let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
        let rule = MD039NoSpaceInLinks::new();
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, expected);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 1);
    }
}

/// Helper: fix content, then check the result has zero violations
fn assert_fix_roundtrip(input: &str) {
    let rule = MD039NoSpaceInLinks::new();
    let ctx = LintContext::new(input, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let fixed = rule.fix(&ctx).unwrap();
    let ctx2 = LintContext::new(&fixed, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let remaining = rule.check(&ctx2).unwrap();
    assert!(
        remaining.is_empty(),
        "After fix, check() should return 0 warnings but got {} for input: {:?}\nFixed output: {:?}",
        remaining.len(),
        input,
        fixed,
    );
}

mod roundtrip_tests {
    use super::*;

    #[test]
    fn roundtrip_spaces_both_ends() {
        assert_fix_roundtrip("[ link ](url) and [ another link ](url) here");
    }

    #[test]
    fn roundtrip_space_at_start() {
        assert_fix_roundtrip("[ link](url) and [ another link](url) here");
    }

    #[test]
    fn roundtrip_space_at_end() {
        assert_fix_roundtrip("[link ](url) and [another link ](url) here");
    }

    #[test]
    fn roundtrip_code_block() {
        assert_fix_roundtrip("```\n[ link ](url)\n```\n[ link ](url)");
    }

    #[test]
    fn roundtrip_multiple_links_same_line() {
        assert_fix_roundtrip("[ link ](url) and [ another ](url) in one line");
    }

    #[test]
    fn roundtrip_only_whitespace_text() {
        assert_fix_roundtrip("[   ](url) and [ ](url)");
    }

    #[test]
    fn roundtrip_unicode_whitespace() {
        assert_fix_roundtrip("[\u{00A0}link\u{00A0}](url) and [\u{2003}another\u{2003}](url)");
    }

    #[test]
    fn roundtrip_tab_whitespace() {
        assert_fix_roundtrip("[\tlink\t](url) and [\tanother\t](url)");
    }

    #[test]
    fn roundtrip_inline_images() {
        assert_fix_roundtrip("![ alt ](img.png) and ![ another ](img2.png)");
    }

    #[test]
    fn roundtrip_nested_formatting() {
        assert_fix_roundtrip("[ * link * ](url) and [ _ another _ ](url)");
    }

    #[test]
    fn roundtrip_html_entities() {
        assert_fix_roundtrip("[  link  ](url)");
    }

    #[test]
    fn roundtrip_mixed_links_and_images() {
        assert_fix_roundtrip("[ link ](url) and ![ image ](img.png) together");
    }

    #[test]
    fn roundtrip_link_with_title() {
        assert_fix_roundtrip("[ link ](url \"title\") here");
    }

    #[test]
    fn roundtrip_multiple_spaces() {
        assert_fix_roundtrip("[   link   ](url) text");
    }

    #[test]
    fn roundtrip_unicode_content() {
        assert_fix_roundtrip("[ 日本語 ](url) and [ émojis 🎉 ](url2)");
    }
}