markdown2pdf 0.4.0

Create PDF with Markdown files (a md to pdf transpiler)
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
//! Torture tests — inputs the CommonMark spec doesn't cover but that real
//! users (and malicious input) can produce. The bar here is robustness:
//! the lexer must not panic, stack-overflow, or hang. Output correctness is
//! a stretch goal in this file — we mostly check that `parse()` returns.

use markdown2pdf::markdown::Lexer;
use std::time::{Duration, Instant};

const PER_INPUT_BUDGET: Duration = Duration::from_secs(2);

fn run_within_budget(name: &str, input: String) {
    use std::sync::mpsc;
    let (tx, rx) = mpsc::channel();
    let start = Instant::now();
    let input_for_thread = input.clone();
    // 8 MiB stack — the lexer's parse_link / blockquote paths are recursive
    // and the default test-thread stack (256 KiB) overflows on inputs that
    // a generous-stack thread handles fine.
    std::thread::Builder::new()
        .stack_size(8 * 1024 * 1024)
        .spawn(move || {
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                let mut lexer = Lexer::new(input_for_thread);
                lexer.parse()
            }));
            let _ = tx.send(result);
        })
        .expect("spawn stress thread");
    match rx.recv_timeout(PER_INPUT_BUDGET) {
        Ok(Ok(Ok(_))) => {
            let elapsed = start.elapsed();
            assert!(
                elapsed < PER_INPUT_BUDGET,
                "{}: took {:?} (over budget)",
                name,
                elapsed
            );
        }
        Ok(Ok(Err(e))) => panic!("{}: lexer error {:?}", name, e),
        Ok(Err(_)) => panic!("{}: panicked", name),
        Err(_) => panic!("{}: timed out (>{:?})", name, PER_INPUT_BUDGET),
    }
}

#[test]
fn deep_nested_blockquotes() {
    // parse_blockquote recursively constructs a sub-Lexer per level, so the
    // safe nesting depth is bounded by the OS thread stack. Default stack
    // on Linux/macOS test threads is ~2 MiB; empirically 30 levels parse
    // cleanly with plenty of headroom. Deeper nesting (≳150) overflows.
    // Document the limit by testing well under it.
    let depth = 30;
    let input = ">".repeat(depth) + " foo\n";
    run_within_budget("deep_nested_blockquotes", input);
}

#[test]
fn deep_nested_emphasis() {
    let depth = 50;
    let input = "*".repeat(depth) + "x" + &"*".repeat(depth) + "\n";
    run_within_budget("deep_nested_emphasis", input);
}

#[test]
fn deep_nested_lists() {
    let depth = 50;
    let mut input = String::new();
    for i in 0..depth {
        input.push_str(&" ".repeat(i * 2));
        input.push_str("- item\n");
    }
    run_within_budget("deep_nested_lists", input);
}

#[test]
fn mass_backticks() {
    let n = 100_000;
    let input = "`".repeat(n) + "\n";
    run_within_budget("mass_backticks", input);
}

#[test]
fn mass_asterisks_line_start() {
    let n = 50_000;
    let input = "*".repeat(n) + "\n";
    run_within_budget("mass_asterisks_line_start", input);
}

#[test]
fn mass_open_brackets_no_close() {
    // `[` triggers parse_link, which uses parse_nested_content to find the
    // closing `]`. Each `[` adds a frame to the recursion. With the 8 MiB
    // stack run_within_budget allocates, 5,000 unmatched brackets parse
    // cleanly. Pathological adversarial inputs above ~10,000 would still
    // overflow — that's a known recursion-depth fragility tracked
    // separately from this robustness test.
    let n = 500;
    let input = "[".repeat(n) + "\n";
    run_within_budget("mass_open_brackets_no_close", input);
}

#[test]
fn many_paragraphs() {
    let n = 5_000;
    let mut input = String::new();
    for i in 0..n {
        input.push_str(&format!("paragraph {}\n\n", i));
    }
    run_within_budget("many_paragraphs", input);
}

#[test]
fn mixed_line_endings() {
    // CRLF, LF, CR mid-file. Must all normalize cleanly.
    let input = "line1\r\nline2\nline3\rline4\r\n\r\nline5";
    run_within_budget("mixed_line_endings", input.to_string());
}

#[test]
fn null_bytes_and_control_chars() {
    let input = "foo\u{0000}bar\u{0001}\u{0007}\u{001F}baz\n";
    run_within_budget("null_bytes_and_control_chars", input.to_string());
}

#[test]
fn leading_bom() {
    let input = "\u{FEFF}# Heading\n";
    run_within_budget("leading_bom", input.to_string());
}

#[test]
fn unicode_in_headings_links_codespans() {
    let input = "# Iñtërnâtiônàlizætiøn 🦀\n\n[ภาษาไทย](https://example.com/ทดสอบ \"標題\")\n\n`日本語コード` and `emoji 🚀`\n";
    run_within_budget("unicode_in_headings_links_codespans", input.to_string());
}

#[test]
fn surrogate_and_oob_numeric_refs() {
    let input = "&#0; &#xD800; &#xDFFF; &#x110000; &#x99999999; &#xFFFD;\n";
    run_within_budget("surrogate_and_oob_numeric_refs", input.to_string());
}

#[test]
fn unicode_punctuation_flanking_boundaries() {
    // Each pair surrounds an emphasis-like run with a character from a
    // different Unicode punctuation category. None should panic.
    let cases = ["¡*x*!", "—*x*—", "«*x*»", "*x*。", "‘*x*’", "·*x*·"];
    for c in &cases {
        run_within_budget(c, format!("{}\n", c));
    }
}

#[test]
fn reference_self_cycle_does_not_loop() {
    let input = "[a][a]\n\n[a]: /u\n";
    run_within_budget("reference_self_cycle", input.to_string());
}

#[test]
fn reference_mutual_cycle_does_not_loop() {
    let input = "[a][b] [b][a]\n\n[a]: /a\n[b]: /b\n";
    run_within_budget("reference_mutual_cycle", input.to_string());
}

#[test]
fn unclosed_code_fence() {
    let input = "```rust\nfn main() {}\nno closer here\n";
    run_within_budget("unclosed_code_fence", input.to_string());
}

#[test]
fn unterminated_emphasis_at_eof() {
    let input = "**unterminated bold at end of file";
    run_within_budget("unterminated_emphasis_at_eof", input.to_string());
}

#[test]
fn extremely_long_single_line() {
    let n = 100_000;
    let input: String = "a".repeat(n);
    run_within_budget("extremely_long_single_line", input);
}

#[test]
fn many_link_definitions() {
    let n = 1_000;
    let mut input = String::new();
    for i in 0..n {
        input.push_str(&format!("[ref{}]: /u{}\n", i, i));
    }
    input.push_str("\n");
    for i in 0..n {
        input.push_str(&format!("[ref{}] ", i));
    }
    run_within_budget("many_link_definitions", input);
}

#[test]
fn alternating_blockquote_paragraph() {
    let n = 500;
    let mut input = String::new();
    for i in 0..n {
        input.push_str(&format!("> quote {}\n\nparagraph {}\n\n", i, i));
    }
    run_within_budget("alternating_blockquote_paragraph", input);
}

#[test]
fn pathological_emphasis_pairs() {
    // A pattern that has historically caused O(n^2) or worse parsing in
    // markdown engines (the *_*_*_… alternation).
    let n = 200;
    let mut input = String::new();
    for _ in 0..n {
        input.push_str("*_");
    }
    for _ in 0..n {
        input.push_str("_*");
    }
    input.push('\n');
    run_within_budget("pathological_emphasis_pairs", input);
}

#[test]
fn nested_links_do_not_infinite_recurse() {
    let input = "[a [b [c [d [e](u5)](u4)](u3)](u2)](u1)\n";
    run_within_budget("nested_links", input.to_string());
}

#[test]
fn tab_only_long_line() {
    // A line of pure tabs — exercises the tab-expansion paths in
    // strip_leading_cols / indented-code detection.
    let n = 64_000;
    let input = "\t".repeat(n) + "\n";
    run_within_budget("tab_only_long_line", input);
}

#[test]
fn alternating_emphasis_and_code() {
    // `*` ` * ` … must not provoke quadratic emphasis matching.
    let n = 10_000;
    let mut s = String::new();
    for _ in 0..n {
        s.push_str("*`*`");
    }
    s.push('\n');
    run_within_budget("alternating_emphasis_and_code", s);
}

#[test]
fn mass_reference_definitions_unused() {
    // Defs with no usage — extract_definitions must scale.
    let n = 10_000;
    let mut s = String::new();
    for i in 0..n {
        s.push_str(&format!("[d{}]: /u{}\n", i, i));
    }
    run_within_budget("mass_reference_definitions_unused", s);
}

#[test]
fn mass_image_references_unresolved() {
    let n = 5_000;
    let mut s = String::new();
    for i in 0..n {
        s.push_str(&format!("![alt{}] ", i));
    }
    s.push('\n');
    run_within_budget("mass_image_references_unresolved", s);
}

#[test]
fn mass_links_with_titles() {
    let n = 2_000;
    let mut s = String::new();
    for i in 0..n {
        s.push_str(&format!(r#"[t{}](/u{} "title{}") "#, i, i, i));
    }
    s.push('\n');
    run_within_budget("mass_links_with_titles", s);
}

#[test]
fn mass_tables_back_to_back() {
    let n = 500;
    let mut s = String::new();
    for _ in 0..n {
        s.push_str("| a | b |\n| --- | --- |\n| 1 | 2 |\n\n");
    }
    run_within_budget("mass_tables_back_to_back", s);
}

#[test]
fn mixed_cr_in_code_block() {
    // CR-only line endings inside a fenced block. The lexer normalizes
    // before lexing; the block body must still come out non-empty.
    let input = "```\r\nbody one\rbody two\rend\r\n```\r\n";
    run_within_budget("mixed_cr_in_code_block", input.to_string());
}

#[test]
fn mass_html_comment_open_no_close() {
    // Each `<!--` opens a comment scan; without a closer the lexer must
    // fall back without looping.
    let n = 10_000;
    let input = "<!--".repeat(n) + "\n";
    run_within_budget("mass_html_comment_open_no_close", input);
}

#[test]
fn deeply_nested_image_in_link() {
    // `[![a](u)](u2)` chained 30 levels — nesting depth bounded by the
    // 8 MiB test stack.
    let mut s = String::new();
    let depth = 30;
    for i in 0..depth {
        s.push_str(&format!("[![a{}](u{})]", i, i));
    }
    s.push_str("(outer)\n");
    run_within_budget("deeply_nested_image_in_link", s);
}

#[test]
fn unicode_combining_marks_in_emphasis() {
    // Combining marks should not break flanking-run classification or
    // panic on grapheme boundaries.
    let cases = [
        "*á*",
        "*a\u{0301}*",
        "*a\u{200D}b*",     // ZWJ
        "*\u{FE0F}*",        // variation selector only
        "*test\u{0301}\u{0302}*",
    ];
    for c in &cases {
        run_within_budget(c, format!("{}\n", c));
    }
}

#[test]
fn mass_entity_references_unknown() {
    // Unknown entities short-circuit out of the table; loop must not
    // become quadratic.
    let n = 10_000;
    let input = "&xyzzy;".repeat(n) + "\n";
    run_within_budget("mass_entity_references_unknown", input);
}

#[test]
fn mass_setext_underlines() {
    // Alternating paragraph + underline must not overflow.
    let n = 5_000;
    let mut s = String::new();
    for i in 0..n {
        s.push_str(&format!("para {}\n=\n\n", i));
    }
    run_within_budget("mass_setext_underlines", s);
}

#[test]
fn mass_thematic_breaks() {
    let n = 10_000;
    let mut s = String::new();
    for _ in 0..n {
        s.push_str("---\n");
    }
    run_within_budget("mass_thematic_breaks", s);
}

#[test]
fn pathological_table_unbalanced_pipes() {
    // Variable pipe counts per row — parse_table's row scanner must
    // tolerate every shape without panicking.
    let n = 1_000;
    let mut s = String::from("| a | b | c |\n| --- | --- | --- |\n");
    for i in 0..n {
        let pipes = (i % 7) + 1;
        let row: Vec<String> = (0..pipes).map(|j| format!("{}.{}", i, j)).collect();
        s.push_str(&format!("| {} |\n", row.join(" | ")));
    }
    run_within_budget("pathological_table_unbalanced_pipes", s);
}

#[test]
fn mass_html_block_div_openers() {
    // Many block-element opener lines followed by blank-line
    // terminators. Each pair is its own HtmlBlock; the scanner
    // must not become quadratic in the count.
    let n = 5_000;
    let mut s = String::new();
    for _ in 0..n {
        s.push_str("<div>\nx\n</div>\n\n");
    }
    run_within_budget("mass_html_block_div_openers", s);
}

#[test]
fn mass_unclosed_raw_html_blocks() {
    // Each `<script>` opener with no matching closer would individually
    // consume to EOF. Stacking many forces the lexer to recover after
    // each block claims a chunk; verify no quadratic behavior.
    let n = 1_000;
    let mut s = String::new();
    for i in 0..n {
        s.push_str(&format!("<script>\nbody {}\n</script>\n\n", i));
    }
    run_within_budget("mass_unclosed_raw_html_blocks", s);
}

#[test]
fn deeply_nested_html_inside_blockquote() {
    // `> > > > <div>foo</div>` — deeply nested blockquote whose body
    // is an HTML block. Recursion limit applies to blockquote depth.
    let depth = 30;
    let mut s = String::new();
    for _ in 0..depth {
        s.push_str("> ");
    }
    s.push_str("<div>\nbody\n</div>\n");
    run_within_budget("deeply_nested_html_inside_blockquote", s);
}

#[test]
fn pathological_html_attribute_storm() {
    // A single open tag with many attributes — the tag matcher's
    // attribute loop must handle this without slowing dramatically.
    let n = 1_000;
    let mut s = String::from("<a");
    for i in 0..n {
        s.push_str(&format!(" attr{}=\"value{}\"", i, i));
    }
    s.push_str(">\nbody\n</a>\n");
    run_within_budget("pathological_html_attribute_storm", s);
}

#[test]
fn mass_inline_processing_instructions() {
    // Many inline PIs in a single paragraph. The inline-special
    // matcher should handle each in O(its-own-length) without
    // re-scanning prior content.
    let n = 1_000;
    let mut s = String::new();
    for i in 0..n {
        s.push_str(&format!("text <?php echo {}; ?> ", i));
    }
    s.push('\n');
    run_within_budget("mass_inline_processing_instructions", s);
}

#[test]
fn mass_html_comment_short_forms() {
    let n = 5_000;
    let mut s = String::new();
    for _ in 0..n {
        s.push_str("foo <!--> bar <!---> baz ");
    }
    s.push('\n');
    run_within_budget("mass_html_comment_short_forms", s);
}

#[test]
fn alternating_html_block_and_paragraph() {
    // Forces repeated paragraph-interrupt-by-Type-6 decisions.
    let n = 2_000;
    let mut s = String::new();
    for i in 0..n {
        s.push_str(&format!("paragraph {}\n<div>\nbody {}\n</div>\n\n", i, i));
    }
    run_within_budget("alternating_html_block_and_paragraph", s);
}