html-to-markdown-rs 3.19.1

High-performance HTML to Markdown converter using the astral-tl parser. Part of the Xberg ecosystem.
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
use crate::{ConversionOptions, HighlightStyle, OutputFormat, TierStrategy, WhitespaceMode, convert};

#[test]
fn raw_marks_should_preserve_styles_wrapping_and_side_data() {
    let html =
        "<html><head><title>T</title></head><body><div>1. <mark>1. </mark><mark>雪&#49;. </mark>z</div></body></html>";
    let mut rows = 0;
    for output_format in [OutputFormat::Markdown, OutputFormat::Plain, OutputFormat::Djot] {
        for highlight_style in [
            HighlightStyle::None,
            HighlightStyle::DoubleEqual,
            HighlightStyle::Bold,
            HighlightStyle::Html,
        ] {
            for preserved in [false, true] {
                let options = ConversionOptions {
                    output_format,
                    highlight_style,
                    whitespace_mode: WhitespaceMode::Strict,
                    wrap: true,
                    wrap_width: 40,
                    extract_metadata: true,
                    include_document_structure: true,
                    preserve_tags: if preserved { vec!["mark".into()] } else { vec![] },
                    tier_strategy: TierStrategy::Tier2,
                    ..Default::default()
                };
                assert_row(
                    &format!("option/{output_format:?}/{highlight_style:?}/{preserved}"),
                    html,
                    options,
                    None,
                );
                rows += 1;
            }
        }
    }
    assert_eq!(rows, 24);
}

#[test]
fn raw_marks_should_preserve_nested_lists_and_boundary_side_data() {
    let mut rows = 0;
    for (id, html) in [
        (
            "list",
            "<ol start='12'><li><mark><ul><li>b<blockquote><p>c</p></blockquote></li></ul></mark></li></ol>",
        ),
        ("span", "<div>1. <mark><span>1. </span></mark>z</div>"),
        ("hardbreak", "<div>x<br><mark>\r\n1. </mark>z</div>"),
        ("unicode", "<div>\u{2000}1. <mark>雪\r\n&#49;. </mark>z</div>"),
        ("formfeed", "<div>1. <mark>\u{c}1. </mark>z</div>"),
        ("comment", "<div>1. <mark> <!--x-->1. </mark>z</div>"),
        ("code", "<code>1. <mark>1. </mark>z</code>"),
        ("pre", "<pre>1. <mark>1. </mark>z</pre>"),
    ] {
        for output_format in [OutputFormat::Markdown, OutputFormat::Plain, OutputFormat::Djot] {
            assert_row(
                &format!("policy/{id}/{output_format:?}"),
                html,
                ConversionOptions {
                    output_format,
                    highlight_style: HighlightStyle::None,
                    whitespace_mode: WhitespaceMode::Strict,
                    extract_metadata: true,
                    include_document_structure: true,
                    tier_strategy: TierStrategy::Tier2,
                    ..Default::default()
                },
                None,
            );
            rows += 1;
        }
    }
    assert_eq!(rows, 24);
}

fn assert_row(id: &str, html: &str, options: ConversionOptions, calls: Option<(Vec<String>, Vec<String>)>) {
    crate::converter::context::INLINE_BUFFER_COLUMN_CHAR_READS.with(|v| v.set(0));
    let result = convert(html, options).expect("policy conversion");
    assert_result(id, &result, calls);
}
fn assert_result(id: &str, result: &crate::ConversionResult, calls: Option<(Vec<String>, Vec<String>)>) {
    assert_eq!(result.content.as_deref(), Some(expected_content(id)), "{id}");
    assert_eq!(format!("{:?}", result.document), expected_document(id), "{id}");
    assert_eq!(result.tables.len(), 0, "{id}");
    assert_eq!(result.warnings.len(), 0, "{id}");
    #[cfg(feature = "metadata")]
    {
        let expected = crate::metadata::HtmlMetadata {
            document: crate::metadata::DocumentMetadata {
                title: if id.starts_with("policy/") {
                    None
                } else {
                    Some("T".to_string())
                },
                ..Default::default()
            },
            ..Default::default()
        };
        assert_eq!(
            serde_json::to_value(&result.metadata).expect("metadata"),
            serde_json::to_value(expected).expect("expected metadata"),
            "{id}"
        );
    }
    if id.starts_with("policy/list/") {
        assert_eq!(
            crate::converter::context::INLINE_BUFFER_COLUMN_CHAR_READS.with(std::cell::Cell::get),
            4
        );
    }
    #[cfg(feature = "visitor")]
    if let Some(calls) = calls {
        assert_eq!(
            serde_json::to_value(calls).expect("callback JSON"),
            serde_json::from_str::<serde_json::Value>(expected_calls(id)).expect("expected callback JSON"),
            "{id}"
        );
    }
    #[cfg(not(feature = "visitor"))]
    let _ = calls;
}

#[cfg(feature = "visitor")]
#[derive(Debug)]
struct Recorder {
    answer: crate::visitor::VisitResult,
    marks: Vec<String>,
    texts: Vec<String>,
}
#[cfg(feature = "visitor")]
impl crate::visitor::HtmlVisitor for Recorder {
    fn visit_mark(&mut self, _: &crate::visitor::NodeContext<'_>, text: &str) -> crate::visitor::VisitResult {
        self.marks.push(text.into());
        self.answer.clone()
    }
    fn visit_text(&mut self, _: &crate::visitor::NodeContext<'_>, text: &str) -> crate::visitor::VisitResult {
        self.texts.push(text.into());
        crate::visitor::VisitResult::Custom("12. replacement\n- literal".into())
    }
}
#[cfg(feature = "visitor")]
#[test]
fn raw_marks_should_preserve_hook_replacements_and_callback_sequences() {
    use crate::visitor::VisitResult;
    let mut rows = 0;
    for (id, answer) in [
        ("Continue", VisitResult::Continue),
        ("Skip", VisitResult::Skip),
        ("Custom", VisitResult::Custom("C".into())),
        ("PreserveHtml", VisitResult::PreserveHtml),
    ] {
        for output_format in [OutputFormat::Markdown, OutputFormat::Plain, OutputFormat::Djot] {
            let (result, calls) = convert_recorded_mark(output_format, answer.clone());
            assert_result(&format!("hook/{id}/{output_format:?}"), &result, Some(calls));
            rows += 1;
        }
    }
    assert_eq!(rows, 12);
}

#[cfg(feature = "visitor")]
fn convert_recorded_mark(
    output_format: OutputFormat,
    answer: crate::visitor::VisitResult,
) -> (crate::ConversionResult, (Vec<String>, Vec<String>)) {
    use crate::visitor::VisitorHandle;
    use std::sync::{Arc, Mutex};
    let recorder = Arc::new(Mutex::new(Recorder {
        answer,
        marks: vec![],
        texts: vec![],
    }));
    let handle: VisitorHandle = recorder.clone();
    let result = convert(
        "<html><head><title>T</title></head><body><div>1. <mark>1. </mark><mark>雪&#49;. </mark>z</div></body></html>",
        ConversionOptions {
            output_format,
            highlight_style: HighlightStyle::None,
            whitespace_mode: WhitespaceMode::Strict,
            extract_metadata: true,
            include_document_structure: true,
            tier_strategy: TierStrategy::Tier2,
            visitor: Some(handle),
            ..Default::default()
        },
    )
    .expect("hook conversion");
    let rec = recorder.lock().expect("recorder");
    let calls = (rec.marks.clone(), rec.texts.clone());
    drop(rec);
    (result, calls)
}

const CONTENT_ORACLES: &[(&str, &str)] = &[
    (
        "hook/Continue/Markdown",
        concat!(
            "---\ntitle: T\n---\n\n12\\. replacement\n- literal12. replacement\n- lit",
            "eral12. replacement\n- literal12. replacement\n- literal\n",
        ),
    ),
    (
        "hook/Continue/Plain",
        concat!(
            "12. replacement\n- literal12. replacement\n- literal12. replacement",
            "\n- literal12. replacement\n- literal\n",
        ),
    ),
    (
        "hook/Continue/Djot",
        concat!(
            "---\ntitle: T\n---\n\n12. replacement\n- literal12. replacement\n- lite",
            "ral12. replacement\n- literal12. replacement\n- literal\n",
        ),
    ),
    (
        "hook/Skip/Markdown",
        "---\ntitle: T\n---\n\n12\\. replacement\n- literal12. replacement\n- literal\n",
    ),
    (
        "hook/Skip/Plain",
        concat!(
            "12. replacement\n- literal12. replacement\n- literal12. replacement",
            "\n- literal12. replacement\n- literal\n",
        ),
    ),
    (
        "hook/Skip/Djot",
        "---\ntitle: T\n---\n\n12. replacement\n- literal12. replacement\n- literal\n",
    ),
    (
        "hook/Custom/Markdown",
        "---\ntitle: T\n---\n\n12\\. replacement\n- literalCC12. replacement\n- literal\n",
    ),
    (
        "hook/Custom/Plain",
        concat!(
            "12. replacement\n- literal12. replacement\n- literal12. replacement",
            "\n- literal12. replacement\n- literal\n",
        ),
    ),
    (
        "hook/Custom/Djot",
        "---\ntitle: T\n---\n\n12. replacement\n- literalCC12. replacement\n- literal\n",
    ),
    (
        "hook/PreserveHtml/Markdown",
        concat!(
            "---\ntitle: T\n---\n\n12\\. replacement\n- literal<mark>1. </mark><mark",
            ">雪&#49;. </mark>12. replacement\n- literal\n",
        ),
    ),
    (
        "hook/PreserveHtml/Plain",
        concat!(
            "12. replacement\n- literal12. replacement\n- literal12. replacement",
            "\n- literal12. replacement\n- literal\n",
        ),
    ),
    (
        "hook/PreserveHtml/Djot",
        concat!(
            "---\ntitle: T\n---\n\n12. replacement\n- literal<mark>1. </mark><mark>",
            "雪&#49;. </mark>12. replacement\n- literal\n",
        ),
    ),
    ("option/Markdown/None/false", "---\ntitle: T\n---\n\n1\\. 1. 雪1. z\n\n"),
    (
        "option/Markdown/None/true",
        "---\ntitle: T\n---\n\n1\\. <mark>1. </mark><mark>雪&#49;.\n</mark>z\n\n",
    ),
    (
        "option/Markdown/DoubleEqual/false",
        "---\ntitle: T\n---\n\n1\\. ==1.== ==雪1.== z\n\n",
    ),
    (
        "option/Markdown/DoubleEqual/true",
        "---\ntitle: T\n---\n\n1\\. <mark>1. </mark><mark>雪&#49;.\n</mark>z\n\n",
    ),
    (
        "option/Markdown/Bold/false",
        "---\ntitle: T\n---\n\n1\\. **1.** **雪1.** z\n\n",
    ),
    (
        "option/Markdown/Bold/true",
        "---\ntitle: T\n---\n\n1\\. <mark>1. </mark><mark>雪&#49;.\n</mark>z\n\n",
    ),
    (
        "option/Markdown/Html/false",
        "---\ntitle: T\n---\n\n1\\. <mark>1. </mark><mark>雪1. </mark>z\n\n",
    ),
    (
        "option/Markdown/Html/true",
        "---\ntitle: T\n---\n\n1\\. <mark>1. </mark><mark>雪&#49;.\n</mark>z\n\n",
    ),
    ("option/Plain/None/false", "1. 1. 雪1. z\n"),
    ("option/Plain/None/true", "1. 1. 雪1. z\n"),
    ("option/Plain/DoubleEqual/false", "1. 1. 雪1. z\n"),
    ("option/Plain/DoubleEqual/true", "1. 1. 雪1. z\n"),
    ("option/Plain/Bold/false", "1. 1. 雪1. z\n"),
    ("option/Plain/Bold/true", "1. 1. 雪1. z\n"),
    ("option/Plain/Html/false", "1. 1. 雪1. z\n"),
    ("option/Plain/Html/true", "1. 1. 雪1. z\n"),
    ("option/Djot/None/false", "---\ntitle: T\n---\n\n1. 1. 雪1. z\n"),
    (
        "option/Djot/None/true",
        "---\ntitle: T\n---\n\n1. <mark>1. </mark><mark>雪&#49;.\n   </mark>z\n",
    ),
    (
        "option/Djot/DoubleEqual/false",
        "---\ntitle: T\n---\n\n1. {=1.=} {=雪1.=} z\n",
    ),
    (
        "option/Djot/DoubleEqual/true",
        "---\ntitle: T\n---\n\n1. <mark>1. </mark><mark>雪&#49;.\n   </mark>z\n",
    ),
    ("option/Djot/Bold/false", "---\ntitle: T\n---\n\n1. **1.** **雪1.** z\n"),
    (
        "option/Djot/Bold/true",
        "---\ntitle: T\n---\n\n1. <mark>1. </mark><mark>雪&#49;.\n   </mark>z\n",
    ),
    (
        "option/Djot/Html/false",
        "---\ntitle: T\n---\n\n1. <mark>1. </mark><mark>雪1. </mark>z\n",
    ),
    (
        "option/Djot/Html/true",
        "---\ntitle: T\n---\n\n1. <mark>1. </mark><mark>雪&#49;.\n   </mark>z\n",
    ),
    ("policy/list/Markdown", "12. - b\n      > c\n"),
    ("policy/list/Plain", "12.\n- b\n\nc\n"),
    ("policy/list/Djot", "12. - b\n      > c\n"),
    ("policy/span/Markdown", "1\\. 1. z\n"),
    ("policy/span/Plain", "1. 1. z\n"),
    ("policy/span/Djot", "1. 1. z\n"),
    ("policy/hardbreak/Markdown", "x  \n1. z\n"),
    ("policy/hardbreak/Plain", "x\n\n1. z\n"),
    ("policy/hardbreak/Djot", "x\\\n1. z\n"),
    ("policy/unicode/Markdown", "\u{2000}1. 雪\n1. z\n"),
    ("policy/unicode/Plain", " 1. 雪\n1. z\n"),
    ("policy/unicode/Djot", "\u{2000}1. 雪\n1. z\n"),
    ("policy/formfeed/Markdown", "1\\. \u{c}1. z\n"),
    ("policy/formfeed/Plain", "1. \u{c}1. z\n"),
    ("policy/formfeed/Djot", "1. \u{c}1. z\n"),
    ("policy/comment/Markdown", "1\\.  1. z\n"),
    ("policy/comment/Plain", "1.  1. z\n"),
    ("policy/comment/Djot", "1.  1. z\n"),
    ("policy/code/Markdown", "`1. 1. z`\n"),
    ("policy/code/Plain", "1. 1. z\n"),
    ("policy/code/Djot", "`1. 1. z`\n"),
    ("policy/pre/Markdown", "```\n1. 1. z\n```\n"),
    ("policy/pre/Plain", "1. 1. z\n"),
    ("policy/pre/Djot", "```\n1. 1. z\n```\n"),
];

#[test]
fn content_oracles_should_have_sixty_unique_case_ids() {
    let ids: std::collections::BTreeSet<_> = CONTENT_ORACLES.iter().map(|(id, _)| *id).collect();
    assert_eq!(CONTENT_ORACLES.len(), 60);
    assert_eq!(ids.len(), 60);
}

fn expected_content(id: &str) -> &'static str {
    CONTENT_ORACLES
        .iter()
        .find_map(|(key, value)| (*key == id).then_some(*value))
        .expect("known output oracle")
}

fn expected_document(id: &str) -> &'static str {
    match id {
        "policy/list/Markdown" | "policy/list/Plain" | "policy/list/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"list-02eef3c51aa026ea\", content: ",
            "List { ordered: true }, parent: None, children: [], annotations: [], attributes: None ",
            "}, DocumentNode { id: \"list-464921dfa5d83f29\", content: List { ordered: false }, parent: ",
            "None, children: [3], annotations: [], attributes: None }, DocumentNode { id: ",
            "\"paragraph-d2944f1eeac9d1c2\", content: Paragraph { text: \"c\" }, parent: None, children: [], ",
            "annotations: [TextAnnotation { start: 0, end: 1, kind: Highlight }], attributes: None }, ",
            "DocumentNode { id: \"list_item-631f7e0c3decaac2\", content: ListItem { text: \"bc\" }, ",
            "parent: Some(1), children: [], annotations: [TextAnnotation { start: 0, end: 2, kind: Highlight }], ",
            "attributes: None }], source_format: Some(\"html\") })",
        ),
        "policy/pre/Markdown" | "policy/pre/Plain" | "policy/pre/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"code-d0b73273c9b72376\", content: ",
            "Code { text: \"1. 1. z\", language: None }, parent: None, children: [], annotations: [],",
            " attributes: None }], source_format: Some(\"html\") })",
        ),
        "policy/code/Markdown" | "policy/code/Plain" | "policy/code/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-61e035ffadb575be\", cont",
            "ent: Paragraph { text: \"1. 1. z\" }, parent: None, children: [], annotations: [TextAnno",
            "tation { start: 3, end: 6, kind: Highlight }, TextAnnotation { start: 0, end: 7, kind:",
            " Code }], attributes: None }], source_format: Some(\"html\") })",
        ),
        "policy/span/Markdown" | "policy/span/Plain" | "policy/span/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-61e035ffadb575be\", cont",
            "ent: Paragraph { text: \"1. 1. z\" }, parent: None, children: [], annotations: [TextAnno",
            "tation { start: 3, end: 6, kind: Highlight }], attributes: None }], source_format: Som",
            "e(\"html\") })",
        ),
        "policy/comment/Markdown" | "policy/comment/Plain" | "policy/comment/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-babeb6d326434b3a\", cont",
            "ent: Paragraph { text: \"1.  1. z\" }, parent: None, children: [], annotations: [TextAnn",
            "otation { start: 3, end: 7, kind: Highlight }], attributes: None }], source_format: So",
            "me(\"html\") })",
        ),
        "policy/formfeed/Markdown" | "policy/formfeed/Plain" | "policy/formfeed/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-e6849dcc850c10ef\", cont",
            "ent: Paragraph { text: \"1. \\u{c}1. z\" }, parent: None, children: [], annotations: [Tex",
            "tAnnotation { start: 3, end: 7, kind: Highlight }], attributes: None }], source_format",
            ": Some(\"html\") })",
        ),
        "policy/hardbreak/Markdown" | "policy/hardbreak/Plain" | "policy/hardbreak/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-c0b45f8016f28814\", cont",
            "ent: Paragraph { text: \"x\\n1. z\" }, parent: None, children: [], annotations: [TextAnnota",
            "tion { start: 2, end: 5, kind: Highlight }], attributes: None }], source_format: Some(",
            "\"html\") })",
        ),
        "policy/unicode/Markdown" | "policy/unicode/Plain" | "policy/unicode/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-c6aa62790f20263c\", cont",
            "ent: Paragraph { text: \"1. 雪\\n1. z\" }, parent: None, children: [], annotations: [TextA",
            "nnotation { start: 3, end: 10, kind: Highlight }], attributes: None }], source_format:",
            " Some(\"html\") })",
        ),
        "hook/Continue/Markdown" | "hook/Continue/Plain" | "hook/Continue/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-980c3207ebc778c5\", cont",
            "ent: Paragraph { text: \"12. replacement\\n- literal12. replacement\\n- literal12. replac",
            "ement\\n- literal12. replacement\\n- literal\" }, parent: None, children: [], annotations",
            ": [TextAnnotation { start: 25, end: 50, kind: Highlight }, TextAnnotation { start: 50,",
            " end: 75, kind: Highlight }], attributes: None }], source_format: Some(\"html\") })",
        ),
        "hook/Custom/Markdown" | "hook/Custom/Plain" | "hook/Custom/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-7026c1435a4ff9ae\", cont",
            "ent: Paragraph { text: \"12. replacement\\n- literalCC12. replacement\\n- literal\" }, par",
            "ent: None, children: [], annotations: [], attributes: None }], source_format: Some(\"ht",
            "ml\") })",
        ),
        "hook/PreserveHtml/Markdown" | "hook/PreserveHtml/Plain" | "hook/PreserveHtml/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-1df09ac2e49fc3d2\", cont",
            "ent: Paragraph { text: \"12. replacement\\n- literal<mark>1. </mark><mark>雪&#49;. </mark",
            ">12. replacement\\n- literal\" }, parent: None, children: [], annotations: [], attribute",
            "s: None }], source_format: Some(\"html\") })",
        ),
        "hook/Skip/Markdown" | "hook/Skip/Plain" | "hook/Skip/Djot" => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-c36e99f0380f77a3\", cont",
            "ent: Paragraph { text: \"12. replacement\\n- literal12. replacement\\n- literal\" }, paren",
            "t: None, children: [], annotations: [], attributes: None }], source_format: Some(\"html",
            "\") })",
        ),
        _ if id.ends_with("/true") => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-2c5bbf766ba79092\", cont",
            "ent: Paragraph { text: \"1. z\" }, parent: None, children: [], annotations: [], attribut",
            "es: None }], source_format: Some(\"html\") })",
        ),
        _ => concat!(
            "Some(DocumentStructure { nodes: [DocumentNode { id: \"paragraph-4ee8a13de33b1247\", cont",
            "ent: Paragraph { text: \"1. 1. 雪1. z\" }, parent: None, children: [], annotations: [Text",
            "Annotation { start: 3, end: 6, kind: Highlight }, TextAnnotation { start: 6, end: 12, ",
            "kind: Highlight }], attributes: None }], source_format: Some(\"html\") })",
        ),
    }
}

#[cfg(feature = "visitor")]
fn expected_calls(id: &str) -> &'static str {
    match id {
        "hook/Continue/Markdown" => "[[\"1. \", \"雪1. \"], [\"1. \", \"1. \", \"雪1. \", \"z\"]]",
        "hook/Continue/Plain" => concat!(
            "[[\"1. \", \"雪1. \"], [\"1. \", \"1. \", \"雪1. \", \"z\", \"1. \", \"1. \", \"雪1. ",
            "\", \"z\"]]",
        ),
        "hook/Continue/Djot" => "[[\"1. \", \"雪1. \"], [\"1. \", \"1. \", \"雪1. \", \"z\"]]",
        "hook/Skip/Markdown" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\"]]",
        "hook/Skip/Plain" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\", \"1. \", \"1. \", \"雪1. \", \"z\"]]",
        "hook/Skip/Djot" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\"]]",
        "hook/Custom/Markdown" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\"]]",
        "hook/Custom/Plain" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\", \"1. \", \"1. \", \"雪1. \", \"z\"]]",
        "hook/Custom/Djot" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\"]]",
        "hook/PreserveHtml/Markdown" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\"]]",
        "hook/PreserveHtml/Plain" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\", \"1. \", \"1. \", \"雪1. \", \"z\"]]",
        "hook/PreserveHtml/Djot" => "[[\"1. \", \"雪1. \"], [\"1. \", \"z\"]]",
        _ => panic!("unknown callback oracle {id}"),
    }
}

#[test]
fn raw_mark_text_should_not_count_unused_columns_after_bare_markers() {
    use crate::converter::context::INLINE_BUFFER_COLUMN_CHAR_READS;
    let mut cases = 0;
    for (raw, normalized, plain) in [
        ("1. ", "1. ", "1. "),
        ("2) ", "2) ", "2) "),
        ("- ", "- ", "- "),
        ("x 1. ", "x 1. ", "x 1. "),
        ("\u{2000}- ", "- ", " - "),
        ("&#49;. ", "1. ", "1. "),
        ("\u{c}1. ", "\u{c}1. ", "\u{c}1. "),
        ("雪 ", "雪 ", "雪 "),
    ] {
        for output_format in [OutputFormat::Markdown, OutputFormat::Plain, OutputFormat::Djot] {
            for whitespace_mode in [WhitespaceMode::Strict, WhitespaceMode::Normalized] {
                for n in [100, 400] {
                    INLINE_BUFFER_COLUMN_CHAR_READS.with(|v| v.set(0));
                    let result = convert(
                        &format!("<div>{}</div>", format!("<mark>{raw}</mark>").repeat(n)),
                        ConversionOptions {
                            output_format,
                            whitespace_mode,
                            highlight_style: HighlightStyle::None,
                            tier_strategy: TierStrategy::Tier2,
                            extract_metadata: false,
                            ..Default::default()
                        },
                    )
                    .expect("raw mark conversion");
                    let text = if output_format == OutputFormat::Plain {
                        plain
                    } else {
                        normalized
                    };
                    let expected = format!("{}\n", text.repeat(n).trim_end());
                    assert_eq!(
                        result.content.as_deref(),
                        Some(expected.as_str()),
                        "{raw:?}/{output_format:?}/{whitespace_mode:?}/{n}"
                    );
                    assert_eq!(INLINE_BUFFER_COLUMN_CHAR_READS.with(std::cell::Cell::get), 0);
                    cases += 1;
                }
            }
        }
    }
    assert_eq!(cases, 96);
}