Skip to main content

docling_core/
markdown.rs

1//! Markdown serializer for [`DoclingDocument`].
2
3use crate::document::{
4    ContentLayer, ContentLayers, DoclingDocument, MarkdownExportOptions, Node, Table,
5};
6
7/// What docling's Markdown serializer writes for an item it has no component
8/// for — a `KeyValueItem` (the XBRL fact graph) is the one such item a backend
9/// produces.
10const MISSING_KEY_VALUE_ITEM: &str = "<!-- missing-key-value-item -->";
11
12/// How pictures are rendered (mirrors docling-core's `ImageRefMode`).
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
14pub enum ImageMode {
15    /// `<!-- image -->` (docling's default, and the only mode without image data).
16    #[default]
17    Placeholder,
18    /// `![Image](data:<mime>;base64,…)` — self-contained.
19    Embedded,
20    /// `![Image](<artifacts>/image_NNNNNN.<ext>)`; the bytes are returned for the
21    /// caller to write.
22    Referenced,
23}
24
25/// Serializer state threaded through the render walk.
26struct Ctx {
27    strict: bool,
28    /// Render pictures' alt text (#676): strict Markdown, or the document's
29    /// `picture_alt_text`.
30    alt_text: bool,
31    /// Emit compact `| a | b |` tables instead of the padded GitHub serializer.
32    compact_tables: bool,
33    images: ImageMode,
34    artifacts_dir: String,
35    /// (relative path, bytes) for each referenced image — written by the caller.
36    artifacts: Vec<(String, Vec<u8>)>,
37    pic_index: usize,
38    /// Rendering the block content of a rich table cell (docling-core 2.94's
39    /// `in_table_cell`, docling-core#540): a heading has no valid Markdown
40    /// form inside a table, so it renders as plain text without `#` markers.
41    in_table_cell: bool,
42    /// docling-core's `MarkdownParams.page_break_placeholder`: the text that
43    /// separates two pages ([`DoclingDocument::page_break_placeholder`]).
44    /// `None` omits page breaks, docling's default.
45    page_break: Option<String>,
46    /// A page boundary has been crossed since the last rendered block, so the
47    /// next block is preceded by the placeholder. docling yields its
48    /// `_PageBreakNode` between two *items* whose `prov.page_no` differ, so a
49    /// boundary before the first block or after the last one emits nothing,
50    /// and a run of empty pages collapses into a single break.
51    pending_page_break: bool,
52    /// Whether any block has been rendered yet — for a streamer, across every
53    /// earlier push too — which is what makes a boundary a *pending* break.
54    emitted_any: bool,
55    /// The content layers rendered ([`MarkdownExportOptions::layers`], #599).
56    layers: ContentLayers,
57    /// The layer the items being rendered sit on: `None` (body) at the
58    /// root, the group's or wrapper's layer inside a non-body group or a
59    /// [`Node::Furniture`] — what the layer gate compares against the set,
60    /// as every item of a hidden sheet carries that sheet's layer upstream.
61    current_layer: Option<ContentLayer>,
62    /// Render a picture's nested text items ([`MarkdownExportOptions::traverse_pictures`]).
63    traverse_pictures: bool,
64    /// Keep the backends' HTML-entity escaping (`&amp;` …) in the output;
65    /// off, it is undone ([`MarkdownExportOptions::escape_html`]).
66    escape_html: bool,
67    /// Keep the backends' `\_` escaping; off, it is undone.
68    escape_underscores: bool,
69    /// What a picture prints as without image data.
70    image_placeholder: String,
71    /// The plain-text export ([`MarkdownExportOptions::plain_text`], #613).
72    plain: bool,
73}
74
75impl Ctx {
76    /// Serializer state for `options` (the page-break setting is the
77    /// document's, the strictness the caller's).
78    fn new(doc: &DoclingDocument, strict: bool, options: &MarkdownExportOptions) -> Self {
79        Ctx {
80            strict,
81            alt_text: strict || doc.picture_alt_text,
82            compact_tables: doc.compact_tables,
83            images: options.image_mode,
84            artifacts_dir: options.artifacts_dir.clone(),
85            artifacts: Vec::new(),
86            pic_index: 0,
87            in_table_cell: false,
88            page_break: doc.page_break_placeholder.clone(),
89            pending_page_break: false,
90            emitted_any: false,
91            layers: options.layers,
92            current_layer: None,
93            traverse_pictures: options.traverse_pictures,
94            escape_html: options.escape_html,
95            escape_underscores: options.escape_underscores,
96            image_placeholder: options.image_placeholder.clone(),
97            plain: options.plain_text,
98        }
99    }
100
101    /// Whether an item on the layer being rendered is in the chosen set.
102    fn layer_visible(&self) -> bool {
103        self.layers.contains(self.current_layer)
104    }
105
106    /// The backends escape inline text for Markdown when they build the
107    /// nodes (`&` → `&amp;`, `_` → `\_`, what docling-core's `post_process`
108    /// does at serialization time); an export that turns an escaping off
109    /// undoes it here, the way the JSON export recovers the raw text. Code,
110    /// formulas and table cells never pass through — upstream leaves those
111    /// unescaped too.
112    fn unescape(&self, text: &str) -> String {
113        let mut out = std::borrow::Cow::Borrowed(text);
114        if !self.escape_html
115            && (out.contains("&amp;") || out.contains("&lt;") || out.contains("&gt;"))
116        {
117            out = std::borrow::Cow::Owned(
118                out.replace("&lt;", "<")
119                    .replace("&gt;", ">")
120                    .replace("&amp;", "&"),
121            );
122        }
123        if !self.escape_underscores && out.contains("\\_") {
124            out = std::borrow::Cow::Owned(out.replace("\\_", "_"));
125        }
126        out.into_owned()
127    }
128
129    /// A text item's inline text: the strict-mode cleanup, then the
130    /// escaping the options turn off undone.
131    fn text(&self, text: &str) -> String {
132        let text = strict_text(text, self.strict);
133        if self.plain {
134            self.unescape(&plain_inline(&self.drop_image_markers(&text)))
135        } else {
136            self.unescape(&text)
137        }
138    }
139
140    /// A caption's text (the backends escape it, but strict mode leaves it
141    /// alone): the escaping the options turn off undone, and, for the plain
142    /// export, its inline markup dropped.
143    fn caption(&self, text: &str) -> String {
144        if self.plain {
145            self.unescape(&plain_inline(text))
146        } else {
147            self.unescape(text)
148        }
149    }
150
151    /// docling-core's `_md_line_breaks` for the Markdown export; the plain
152    /// export's `PlainTextTextSerializer` keeps the text's own newlines.
153    fn breaks(&self, text: &str) -> String {
154        if self.plain {
155            text.to_string()
156        } else {
157            md_line_breaks(text)
158        }
159    }
160
161    /// A table for the plain export: a rich cell's text is its Markdown
162    /// serialization in our model (bold runs, links, hard line breaks),
163    /// where upstream serializes the cell with the plain serializer — so
164    /// the markup goes before the grid is laid out, which also lets a
165    /// once-bold number right-align like a plain one. A simple cell holds
166    /// raw text and passes through unchanged.
167    fn plain_table(&self, table: &Table) -> Table {
168        let mut plain = table.clone();
169        for cell in plain.rows.iter_mut().flatten() {
170            if cell.contains(['*', '~', '`', '[', '\n', '<']) {
171                let text = self.drop_image_markers(&strip_hard_breaks(cell));
172                *cell = plain_inline(&text);
173            }
174        }
175        plain
176    }
177
178    /// Pictures some backends fold into an item's text (an HTML `<img>`
179    /// inside a `<li>`, a LaTeX figure in a paragraph) arrive as baked
180    /// `<!-- image -->` lines — docling's picture *children* of the item,
181    /// which its serializer prints with the active image placeholder. The
182    /// plain export's placeholder is empty, so such a line goes, as a
183    /// picture item would.
184    fn drop_image_markers(&self, text: &str) -> String {
185        const MARKER: &str = "<!-- image -->";
186        if !text.contains(MARKER) && !text.contains("```") {
187            return text.to_string();
188        }
189        // Block by block (`\n\n`, the serializer's part delimiter): a block
190        // left empty goes with its delimiter, as docling joins only the parts
191        // that carry text.
192        text.split("\n\n")
193            .filter_map(|block| {
194                let lines: Vec<String> = block
195                    .split('\n')
196                    .filter_map(|line| {
197                        // A folded code block's fences go too: the plain
198                        // export prints code unfenced.
199                        if line.trim() == MARKER || is_fence_line(line.trim()) {
200                            return None;
201                        }
202                        Some(line.replace(MARKER, &self.image_placeholder))
203                    })
204                    .collect();
205                (!lines.is_empty()).then(|| lines.join("\n"))
206            })
207            .collect::<Vec<_>>()
208            .join("\n\n")
209    }
210}
211
212/// Render a document to a Markdown string (pictures as placeholders).
213///
214/// `strict` selects the serializer-level behaviours that differ between
215/// docling-legacy output and cleaner Markdown — currently the code-fence
216/// language (legacy drops it, strict keeps it).
217pub fn to_markdown(doc: &DoclingDocument, strict: bool) -> String {
218    to_markdown_images(doc, strict, ImageMode::Placeholder, "artifacts").0
219}
220
221/// Render to Markdown with an explicit picture [`ImageMode`]. Returns the
222/// Markdown and, for [`ImageMode::Referenced`], the `(path, bytes)` of each image
223/// the caller should write (relative to the Markdown file).
224pub fn to_markdown_images(
225    doc: &DoclingDocument,
226    strict: bool,
227    images: ImageMode,
228    artifacts_dir: &str,
229) -> (String, Vec<(String, Vec<u8>)>) {
230    to_markdown_with_options(
231        doc,
232        strict,
233        &MarkdownExportOptions {
234            image_mode: images,
235            artifacts_dir: artifacts_dir.to_string(),
236            ..MarkdownExportOptions::default()
237        },
238    )
239}
240
241/// Render to Markdown per `options` ([`MarkdownExportOptions`], #599):
242/// content layers, picture traversal, escaping, the image placeholder and
243/// the image mode. Returns the Markdown and the referenced-image artifacts.
244pub fn to_markdown_with_options(
245    doc: &DoclingDocument,
246    strict: bool,
247    options: &MarkdownExportOptions,
248) -> (String, Vec<(String, Vec<u8>)>) {
249    let mut ctx = Ctx::new(doc, strict, options);
250    let mut blocks: Vec<String> = Vec::new();
251    render(&doc.nodes, &mut blocks, &mut ctx);
252    let mut body = blocks.join("\n\n");
253    // Strict mode only: turn recovered source hyperlinks into Markdown links.
254    // docling's standard pipeline drops them, so doing this in legacy mode would
255    // diverge from docling — hence strict-only, leaving conformance output intact.
256    if strict && !options.plain_text && !doc.links.is_empty() {
257        body = apply_links(&body, &doc.links);
258    }
259    // The YAML front matter (#679) opens the document when its output mode
260    // asks for it; never in the plain-text export.
261    let front = if doc.metadata_output.front_matter() && !options.plain_text {
262        doc.metadata.to_front_matter()
263    } else {
264        String::new()
265    };
266    let md = match (front.is_empty(), body.is_empty()) {
267        (true, true) => String::new(),
268        (true, false) => format!("{body}\n"),
269        (false, true) => front,
270        (false, false) => format!("{front}\n{body}\n"),
271    };
272    (md, ctx.artifacts)
273}
274
275/// Render the block content of a *rich table cell* to Markdown — what
276/// docling-core's table serializer does for a `RichTableCell`
277/// (`doc_serializer.serialize(item, in_table_cell=True)`): the cell's
278/// paragraphs, lists and flattened nested tables render as in a document, but a
279/// heading loses its `#` markers (docling-core#540 — the Markdown spec has no
280/// headings inside tables). Pictures stay placeholders. The caller flattens the
281/// result into its cell text; the table serializer later turns the newlines
282/// into spaces.
283pub fn to_markdown_table_cell(doc: &DoclingDocument, strict: bool) -> String {
284    let mut ctx = Ctx::new(doc, strict, &MarkdownExportOptions::default());
285    ctx.in_table_cell = true;
286    ctx.artifacts_dir = String::new();
287    // A rich cell is one page's content; its sub-document carries no page
288    // boundaries and docling's `_iterate_items` runs the page-break scan
289    // over the document root only.
290    ctx.page_break = None;
291    let mut blocks: Vec<String> = Vec::new();
292    render(&doc.nodes, &mut blocks, &mut ctx);
293    blocks.join("\n\n")
294}
295
296/// Wrap each recovered link's anchor text in Markdown `[anchor](href)`. Anchors
297/// arrive cleaned (curly quotes/dashes already normalized) but un-escaped, so we
298/// match against the body's HTML-escaped (`&`/`<`/`>`) form, the way prose nodes
299/// were serialized. Links are consumed in document order from a moving cursor, so
300/// a repeated anchor (e.g. two "issues") links its successive occurrences rather
301/// than all pointing at the first. An anchor that can't be located is skipped
302/// (its text may have been split across a line wrap or table cell).
303fn apply_links(body: &str, links: &[(String, String)]) -> String {
304    let mut out = body.to_string();
305    let mut cursor = 0usize;
306    for (anchor, href) in links {
307        let anchor = anchor
308            .replace('&', "&amp;")
309            .replace('<', "&lt;")
310            .replace('>', "&gt;");
311        if anchor.is_empty() {
312            continue;
313        }
314        if let Some(rel) = out[cursor..].find(&anchor) {
315            let at = cursor + rel;
316            // Don't relink inside an already-emitted `](` Markdown link target.
317            // An unsafe destination (#860) leaves the anchor as prose.
318            let replacement = crate::hyperlink::markdown_link(&anchor, href);
319            out.replace_range(at..at + anchor.len(), &replacement);
320            cursor = at + replacement.len();
321        }
322    }
323    out
324}
325
326/// Like [`apply_links`] but over a single chunk, consuming from a shared queue so
327/// the same `[anchor](href)` rewriting can be applied incrementally as Markdown is
328/// streamed out. Each queued link is matched (in document order) against `chunk`
329/// and rewritten in place; a link whose anchor is not in this chunk is carried
330/// forward in the queue for a later chunk. Anchors are recovered in document
331/// order and a chunk is always a contiguous run of whole blocks, so this
332/// reproduces [`apply_links`]' single moving cursor: the link lands in whichever
333/// chunk contains its anchor, identically to the buffered path. (A link whose
334/// anchor never appears is carried to the end and dropped — the same no-op
335/// `apply_links` performs for an unlocatable anchor.)
336fn apply_links_chunk(chunk: &str, queue: &mut Vec<(String, String)>) -> String {
337    let mut out = chunk.to_string();
338    let mut cursor = 0usize;
339    let mut carried: Vec<(String, String)> = Vec::new();
340    for (anchor_raw, href) in std::mem::take(queue) {
341        let anchor = anchor_raw
342            .replace('&', "&amp;")
343            .replace('<', "&lt;")
344            .replace('>', "&gt;");
345        if anchor.is_empty() {
346            continue;
347        }
348        if let Some(rel) = out[cursor..].find(&anchor) {
349            let at = cursor + rel;
350            let replacement = crate::hyperlink::markdown_link(&anchor, &href);
351            out.replace_range(at..at + anchor.len(), &replacement);
352            cursor = at + replacement.len();
353        } else {
354            // Not in this chunk; try again when its block is flushed.
355            carried.push((anchor_raw, href));
356        }
357    }
358    *queue = carried;
359    out
360}
361
362/// Incremental Markdown serializer: feed finalized, in-document-order batches of
363/// [`Node`]s and receive Markdown chunks whose concatenation is **byte-identical**
364/// to [`to_markdown_images`] over the same nodes. This is the streaming
365/// counterpart of the buffered serializer — used to emit a document's Markdown in
366/// chunks (e.g. page by page, as the parallel PDF pipeline finishes pages) instead
367/// of building the whole string up front.
368///
369/// [`ImageMode::Placeholder`] and [`ImageMode::Embedded`] render inline.
370/// [`ImageMode::Referenced`] additionally hands each picture's bytes out through
371/// [`take_artifacts`](Self::take_artifacts) — construct with
372/// [`with_artifacts`](Self::with_artifacts) and drain after every push so the
373/// bytes can be written to disk as pages finish instead of accumulating for the
374/// whole document (issue #80's memory-bounded image handling).
375///
376/// Each [`push`](Self::push) must contain whole blocks in reading order: a caller
377/// must not split a run of list items across two pushes (the run would render as
378/// two separate lists). Finalized PDF page batches already satisfy this.
379pub struct MarkdownStreamer {
380    strict: bool,
381    images: ImageMode,
382    compact_tables: bool,
383    /// [`DoclingDocument::picture_alt_text`] (#676).
384    picture_alt_text: bool,
385    /// The YAML front matter (#679) still to be written, ahead of the first
386    /// chunk — or alone, from [`finish`](Self::finish), when nothing else
387    /// comes.
388    front_matter: Option<String>,
389    /// Whether any non-empty chunk has been emitted yet (drives `\n\n` joins and
390    /// the trailing newline).
391    emitted_any: bool,
392    /// Recovered links not yet placed (strict mode), consumed in document order.
393    links: Vec<(String, String)>,
394    /// Referenced mode: the link prefix, the not-yet-drained `(path, bytes)`
395    /// artifacts, and the running image number (continues across pushes so the
396    /// stream matches the buffered serializer's `image_000000…` numbering).
397    artifacts_dir: String,
398    artifacts: Vec<(String, Vec<u8>)>,
399    pic_index: usize,
400    /// [`DoclingDocument::page_break_placeholder`] and the boundary carried
401    /// over from the previous push (a page batch opens with its page marker,
402    /// so the break it implies is paid by that batch's first block).
403    page_break: Option<String>,
404    pending_page_break: bool,
405    /// Whether an earlier push rendered an *item* — the page-break scan's
406    /// state ([`Ctx`]'s `emitted_any`). It differs from `emitted_any` (text
407    /// written) only when every block of the earlier pushes was empty and
408    /// dropped (#605): a page break before the next item is still owed then,
409    /// exactly as in the buffered export.
410    items_emitted: bool,
411    /// A picture a push ended on, in the placeholder mode: whether its
412    /// marker prints depends on the item after it (#858, a `mermaid` code
413    /// item takes its place), which the next push brings — it joins that
414    /// push, or [`finish`](Self::finish) renders it.
415    held: Option<Node>,
416    /// The export options beyond the image mode (#599): layers, picture
417    /// traversal, escaping, the placeholder — [`with_export_options`](Self::with_export_options).
418    layers: ContentLayers,
419    traverse_pictures: bool,
420    escape_html: bool,
421    escape_underscores: bool,
422    image_placeholder: String,
423    plain: bool,
424}
425
426impl MarkdownStreamer {
427    /// Create a streamer. `compact_tables` mirrors [`DoclingDocument::compact_tables`].
428    /// For [`ImageMode::Referenced`] use [`with_artifacts`](Self::with_artifacts).
429    pub fn new(strict: bool, images: ImageMode, compact_tables: bool) -> Self {
430        debug_assert!(
431            images != ImageMode::Referenced,
432            "referenced image mode needs an artifacts dir; use with_artifacts"
433        );
434        Self::with_artifacts(strict, images, compact_tables, "artifacts")
435    }
436
437    /// Like [`new`](Self::new) but with the artifacts link prefix, allowing
438    /// [`ImageMode::Referenced`]: pictures render as
439    /// `![Image](<artifacts_dir>/image_NNNNNN.<ext>)` and each push's image
440    /// bytes wait in [`take_artifacts`](Self::take_artifacts) for the caller to
441    /// write. The concatenated chunks and the artifact list match the buffered
442    /// [`to_markdown_images`] byte-for-byte.
443    pub fn with_artifacts(
444        strict: bool,
445        images: ImageMode,
446        compact_tables: bool,
447        artifacts_dir: &str,
448    ) -> Self {
449        Self {
450            strict,
451            images,
452            compact_tables,
453            picture_alt_text: false,
454            front_matter: None,
455            emitted_any: false,
456            links: Vec::new(),
457            artifacts_dir: artifacts_dir.to_string(),
458            artifacts: Vec::new(),
459            pic_index: 0,
460            page_break: None,
461            pending_page_break: false,
462            items_emitted: false,
463            held: None,
464            layers: ContentLayers::BODY,
465            traverse_pictures: false,
466            escape_html: true,
467            escape_underscores: true,
468            image_placeholder: "<!-- image -->".to_string(),
469            plain: false,
470        }
471    }
472
473    /// Take every setting of `options` ([`MarkdownExportOptions`], #599):
474    /// the image mode and artifacts directory the constructors took, plus the
475    /// content layers, picture traversal, escaping and image placeholder.
476    /// The concatenated chunks match the buffered
477    /// [`to_markdown_with_options`] byte-for-byte. Set before the first
478    /// [`push`](Self::push).
479    pub fn with_export_options(mut self, options: &MarkdownExportOptions) -> Self {
480        self.images = options.image_mode;
481        self.artifacts_dir = options.artifacts_dir.clone();
482        self.layers = options.layers;
483        self.traverse_pictures = options.traverse_pictures;
484        self.escape_html = options.escape_html;
485        self.escape_underscores = options.escape_underscores;
486        self.image_placeholder = options.image_placeholder.clone();
487        self.plain = options.plain_text;
488        self
489    }
490
491    /// Insert `placeholder` between pages, mirroring
492    /// [`DoclingDocument::page_break_placeholder`] for the buffered path (the
493    /// concatenated chunks stay byte-identical to it). `None` — the default —
494    /// omits page breaks. Set before the first [`push`](Self::push).
495    pub fn with_page_break_placeholder(mut self, placeholder: Option<String>) -> Self {
496        self.page_break = placeholder;
497        self
498    }
499
500    /// Render pictures' alt text (#676) — [`DoclingDocument::picture_alt_text`]
501    /// for the stream; strict Markdown renders it regardless. Set before the
502    /// first [`push`](Self::push).
503    pub fn with_picture_alt_text(mut self, on: bool) -> Self {
504        self.picture_alt_text = on;
505        self
506    }
507
508    /// Open the stream with the YAML front matter of `metadata` (#679) —
509    /// what the buffered export writes for a document whose
510    /// [`DoclingDocument::metadata_output`] is `Frontmatter`. Empty
511    /// metadata writes nothing; the plain-text export never writes it.
512    pub fn with_metadata(mut self, metadata: &crate::metadata::DocumentMetadata) -> Self {
513        self.front_matter = (!self.plain)
514            .then(|| metadata.to_front_matter())
515            .filter(|f| !f.is_empty());
516        self
517    }
518
519    /// The `(relative path, bytes)` of images rendered by pushes since the last
520    /// drain ([`ImageMode::Referenced`] only — empty otherwise). Paths are
521    /// relative to the Markdown file, i.e. they start with the configured
522    /// artifacts dir.
523    pub fn take_artifacts(&mut self) -> Vec<(String, Vec<u8>)> {
524        std::mem::take(&mut self.artifacts)
525    }
526
527    /// Render one finalized batch of nodes (plus any links recovered from the same
528    /// span, in document order) into the next Markdown chunk. Returns an empty
529    /// string when the batch produces no output (e.g. empty tables/pictures), in
530    /// which case nothing should be written.
531    pub fn push(&mut self, nodes: &[Node], links: &[(String, String)]) -> String {
532        self.push_holding(nodes, links, true)
533    }
534
535    /// [`push`](Self::push); with `hold`, a picture the batch ends on is
536    /// held back for the next push (`finish` renders without holding).
537    fn push_holding(&mut self, nodes: &[Node], links: &[(String, String)], hold: bool) -> String {
538        self.links.extend(links.iter().cloned());
539        // A picture held back from the last push leads this one; a picture
540        // this push ends on is held back in turn (placeholder mode only —
541        // the one in which the next item can change what it prints).
542        let images = self.images;
543        let ends_on_picture = |nodes: &[Node]| {
544            matches!(images, ImageMode::Placeholder)
545                && matches!(nodes.last(), Some(Node::Picture { .. }))
546        };
547        let mut owned: Vec<Node> = Vec::new();
548        let mut nodes = nodes;
549        if self.held.is_some() || (hold && ends_on_picture(nodes)) {
550            owned.extend(self.held.take());
551            owned.extend(nodes.iter().cloned());
552            if hold && ends_on_picture(&owned) {
553                self.held = owned.pop();
554            }
555            nodes = &owned;
556        }
557        let mut ctx = Ctx {
558            strict: self.strict,
559            alt_text: self.strict || self.picture_alt_text,
560            compact_tables: self.compact_tables,
561            images: self.images,
562            artifacts_dir: std::mem::take(&mut self.artifacts_dir),
563            artifacts: std::mem::take(&mut self.artifacts),
564            pic_index: self.pic_index,
565            in_table_cell: false,
566            page_break: std::mem::take(&mut self.page_break),
567            pending_page_break: self.pending_page_break,
568            emitted_any: self.items_emitted,
569            layers: self.layers,
570            current_layer: None,
571            traverse_pictures: self.traverse_pictures,
572            escape_html: self.escape_html,
573            escape_underscores: self.escape_underscores,
574            image_placeholder: std::mem::take(&mut self.image_placeholder),
575            plain: self.plain,
576        };
577        let mut blocks: Vec<String> = Vec::new();
578        render(nodes, &mut blocks, &mut ctx);
579        self.image_placeholder = std::mem::take(&mut ctx.image_placeholder);
580        self.artifacts_dir = std::mem::take(&mut ctx.artifacts_dir);
581        self.artifacts = std::mem::take(&mut ctx.artifacts);
582        self.pic_index = ctx.pic_index;
583        self.page_break = std::mem::take(&mut ctx.page_break);
584        self.pending_page_break = ctx.pending_page_break;
585        self.items_emitted = ctx.emitted_any;
586        if blocks.is_empty() {
587            return String::new();
588        }
589        let mut body = blocks.join("\n\n");
590        if self.strict && !self.plain && !self.links.is_empty() {
591            body = apply_links_chunk(&body, &mut self.links);
592        }
593        let chunk = if let Some(front) = self.front_matter.take() {
594            format!("{front}\n{body}")
595        } else if self.emitted_any {
596            format!("\n\n{body}")
597        } else {
598            body
599        };
600        self.emitted_any = true;
601        chunk
602    }
603
604    /// Emit the trailing newline that finishes the document (empty if no content
605    /// was produced). Call exactly once, after the final [`push`](Self::push).
606    pub fn finish(mut self) -> String {
607        let mut out = String::new();
608        if self.held.is_some() {
609            out.push_str(&self.push_holding(&[], &[], false));
610        }
611        if let Some(front) = self.front_matter {
612            out.push_str(&front);
613        } else if self.emitted_any {
614            out.push('\n');
615        }
616        out
617    }
618}
619
620/// In `strict` mode, rewrite inline text for readability rather than byte-for-byte
621/// docling fidelity: undo the legacy `\_` underscore escaping, and tighten stray
622/// spaces around punctuation (`[ 37 , 36 ]` → `[37, 36]`, `( x )` → `(x)`). This
623/// cleans up both the PDF backend's glyph-split spacing and the space the legacy
624/// emphasis serialization leaves before punctuation (`*a* ,` → `*a*,`).
625/// Legacy/default output keeps docling's spacing untouched. Only inline text
626/// nodes pass through here — code blocks and table cells are left alone.
627fn strict_text(text: &str, strict: bool) -> String {
628    if !strict {
629        return text.to_string();
630    }
631    // A list item's leading GFM task box (`[ ] todo`, a docling-JSON check
632    // list item, #1033) is markup, not a stray space inside brackets.
633    if let Some(rest) = text.strip_prefix("[ ] ") {
634        return format!("[ ] {}", strict_text(rest, strict));
635    }
636    text.replace("\\_", "_")
637        .replace(" ,", ",")
638        .replace(" .", ".")
639        .replace(" ;", ";")
640        .replace(" )", ")")
641        .replace("( ", "(")
642        .replace(" ]", "]")
643        .replace("[ ", "[")
644}
645
646/// The longest run of consecutive backticks in `text` (docling-core
647/// 2.103.1's `_longest_backtick_run`, #859).
648pub fn longest_backtick_run(text: &str) -> usize {
649    let (mut best, mut run) = (0, 0);
650    for c in text.chars() {
651        if c == '`' {
652            run += 1;
653            best = best.max(run);
654        } else {
655            run = 0;
656        }
657    }
658    best
659}
660
661/// `text` as a Markdown code span no backtick run inside can close
662/// (docling-core's `_code_span`, #859): one backtick more than the longest
663/// run in it, and a space on both sides when the text starts or ends with
664/// a backtick — the pad CommonMark strips again — so `` a`b `` is
665/// ``` ``a`b`` ``` and `` `x `` is ``` `` `x `` ```.
666pub fn code_span(text: &str) -> String {
667    let ticks = "`".repeat(longest_backtick_run(text) + 1);
668    let pad = if text.starts_with('`') || text.ends_with('`') {
669        " "
670    } else {
671        ""
672    };
673    format!("{ticks}{pad}{text}{pad}{ticks}")
674}
675
676/// A fenced code block whose fence out-lengths every backtick run in `body`
677/// (docling-core's `_code_block`, #859): three backticks, or one more than
678/// the longest run inside, so a body quoting a ``` fence cannot close the
679/// block early. `language` rides on the opening fence (empty for none).
680pub fn code_fence(language: &str, body: &str) -> String {
681    let fence = "`".repeat(longest_backtick_run(body).max(2) + 1);
682    format!("{fence}{language}\n{body}\n{fence}")
683}
684
685/// The code span (or flattened fence) opening at `chars[i]`, a run of
686/// backticks: `(content start, content end, index after the closing run)`.
687/// The closing run is the first later run of exactly the same length — a
688/// run of another length is literal inside the span, as in CommonMark. A
689/// span of two or more backticks sheds the one-space pad [`code_span`]
690/// adds; a single-backtick span keeps its content as it stands, the shape
691/// every reader of the baked text relied on.
692pub(crate) fn code_span_bounds(chars: &[char], i: usize) -> Option<(usize, usize, usize)> {
693    let n = chars[i..].iter().take_while(|&&c| c == '`').count();
694    if n == 0 {
695        return None;
696    }
697    let mut k = i + n;
698    while k < chars.len() {
699        if chars[k] != '`' {
700            k += 1;
701            continue;
702        }
703        let run = chars[k..].iter().take_while(|&&c| c == '`').count();
704        if run == n {
705            let (mut s, mut e) = (i + n, k);
706            if n > 1
707                && e - s >= 2
708                && chars[s] == ' '
709                && chars[e - 1] == ' '
710                && chars[s..e].iter().any(|&c| c != ' ')
711            {
712                s += 1;
713                e -= 1;
714            }
715            return Some((s, e, k + run));
716        }
717        k += run;
718    }
719    None
720}
721
722/// A line that is a code fence: three or more backticks and nothing else.
723fn is_fence_line(line: &str) -> bool {
724    line.len() >= 3 && line.bytes().all(|b| b == b'`')
725}
726
727/// The plain export's inline text (#613): the Markdown markers docling-core's
728/// `PlainTextDocSerializer` never writes — `serialize_bold` / `_italic` /
729/// `_strikethrough` return the text, `serialize_hyperlink` the label, and
730/// `format_code_blocks=False` drops inline code's backticks. The backends
731/// bake those markers into an item's Markdown text (`***x***`, `**x**`,
732/// `*x*`, `~~x~~`, `` `x` ``, `[label](url)`, the set DocLang's run parser
733/// reads too), so they are taken out here, nested ones included. A marker
734/// counts only when it closes and its content is tight against both
735/// markers (CommonMark's flanking rule): `2 * 3 * 4` is arithmetic and IBM
736/// i's `*USE … have *OBJMGT` are special values, not emphasis, and stay as
737/// they are — docling's own emphasis around a space (WebVTT's `* *`) stays
738/// literal too. Inline `$…$` formulas pass through untouched.
739pub(crate) fn plain_inline(text: &str) -> String {
740    if !text.contains(['*', '~', '`', '[']) {
741        return text.to_string();
742    }
743    let chars: Vec<char> = text.chars().collect();
744    let mut out = String::with_capacity(text.len());
745    plain_inline_into(&chars, &mut out);
746    out
747}
748
749fn plain_inline_into(chars: &[char], out: &mut String) {
750    // A backslash-escaped marker (`\*`, the RIS backend's pseudonym stars,
751    // #902) closes no span.
752    let find = |open: usize, pat: &[char]| -> Option<usize> {
753        chars
754            .get(open..)?
755            .windows(pat.len())
756            .enumerate()
757            .position(|(k, w)| w == pat && (open + k == 0 || chars[open + k - 1] != '\\'))
758            .map(|p| open + p)
759    };
760    // An emphasis span opening at `i` with marker `pat`: its closing index
761    // when the content is non-empty and tight against both markers.
762    let span = |i: usize, pat: &[char]| -> Option<usize> {
763        if !chars[i..].starts_with(pat) {
764            return None;
765        }
766        let open = i + pat.len();
767        let end = find(open, pat)?;
768        (end > open && !chars[open].is_whitespace() && !chars[end - 1].is_whitespace())
769            .then_some(end)
770    };
771    let mut i = 0;
772    while i < chars.len() {
773        // `\*` is a literal star, which is what the Markdown renders.
774        if chars[i] == '\\' && chars.get(i + 1) == Some(&'*') {
775            out.push('*');
776            i += 2;
777            continue;
778        }
779        let mut matched = false;
780        for pat in [&['*', '*', '*'][..], &['*', '*'], &['~', '~'], &['*']] {
781            if let Some(end) = span(i, pat) {
782                plain_inline_into(&chars[i + pat.len()..end], out);
783                i = end + pat.len();
784                matched = true;
785                break;
786            }
787        }
788        if matched {
789            continue;
790        }
791        // Inline code: content tight against both backticks, as docling
792        // writes it — a backtick opening a quotation (`` `like this’ ``,
793        // JATS prose) stays.
794        if chars[i] == '`' {
795            if let Some((s, e, after)) = code_span_bounds(chars, i).filter(|&(s, e, _)| {
796                e > s && !chars[s].is_whitespace() && !chars[e - 1].is_whitespace()
797            }) {
798                out.extend(&chars[s..e]);
799                i = after;
800                continue;
801            }
802        }
803        // An inline formula is LaTeX, printed as it is — its brackets and
804        // stars are not Markdown.
805        if chars[i] == '$' {
806            if let Some(end) = find(i + 1, &['$']) {
807                out.extend(&chars[i..=end]);
808                i = end + 1;
809                continue;
810            }
811        }
812        // A link: the `]` closing this `[` (brackets nest) directly followed
813        // by `(` — a citation `[23,24]` before a later link is no anchor.
814        if chars[i] == '[' {
815            if let Some(close) = closing_bracket(chars, i) {
816                if chars.get(close + 1) == Some(&'(') {
817                    if let Some(endp) = crate::doclang::link_dest_end(chars, close + 2) {
818                        plain_inline_into(&chars[i + 1..close], out);
819                        i = endp + 1;
820                        continue;
821                    }
822                }
823            }
824        }
825        // A run of the same marker that opened nothing stays literal as a
826        // whole, so `***` before a lone `*` is not re-read from its middle.
827        let c = chars[i];
828        out.push(c);
829        i += 1;
830        if matches!(c, '*' | '~') {
831            while i < chars.len() && chars[i] == c {
832                out.push(c);
833                i += 1;
834            }
835        }
836    }
837}
838
839/// The index of the `]` matching the `[` at `open`, nested brackets
840/// balanced; `None` when it never closes.
841fn closing_bracket(chars: &[char], open: usize) -> Option<usize> {
842    let mut depth = 0usize;
843    for (k, c) in chars.iter().enumerate().skip(open + 1) {
844        match c {
845            '[' => depth += 1,
846            ']' if depth == 0 => return Some(k),
847            ']' => depth -= 1,
848            _ => {}
849        }
850    }
851    None
852}
853
854/// docling-core 2.92's `_md_line_breaks` (docling-core#721): a single `\n`
855/// inside an item's text becomes a GFM hard line break (`"  \n"`, two trailing
856/// spaces) so renderers honour it; a blank line (`\n\n`) is a paragraph break
857/// and stays as is — the document scope already joins blocks with `\n\n`.
858/// Applied to body text, list items and standalone caption items, never to
859/// code/formulas, nor to the captions a table or picture carries: docling-core's
860/// `serialize_captions` prints those as they are (#769).
861fn md_line_breaks(text: &str) -> String {
862    if !text.contains('\n') {
863        return text.to_string();
864    }
865    text.split("\n\n")
866        .map(|para| para.replace('\n', "  \n"))
867        .collect::<Vec<_>>()
868        .join("\n\n")
869}
870
871/// Undo [`md_line_breaks`] on a rich table cell's flattened Markdown so the
872/// non-Markdown exports (JSON `text`, LaTeX cells) see the cell's raw line
873/// breaks, as docling's do — a rich cell's text is its Markdown serialization
874/// in our model, and the two trailing spaces are a Markdown-only marker.
875pub(crate) fn strip_hard_breaks(text: &str) -> String {
876    if text.contains("  \n") {
877        text.replace("  \n", "\n")
878    } else {
879        text.to_string()
880    }
881}
882
883/// docling-core's `_heading_line_breaks`: a GFM heading cannot span lines, so a
884/// newline inside heading text collapses to a space (`# Hello World`, not a
885/// broken `# Hello\nWorld`).
886fn heading_line_breaks(text: &str) -> String {
887    text.replace('\n', " ")
888}
889
890fn render(nodes: &[Node], blocks: &mut Vec<String>, ctx: &mut Ctx) {
891    let mut i = 0;
892    while i < nodes.len() {
893        let before = blocks.len();
894        match &nodes[i] {
895            // A page boundary: the explicit `PageBreak` (slides, DjVu / DocTags
896            // pages) or the `PageInfo` marker that opens every PDF page and
897            // spreadsheet sheet — a sheet boundary carries both, and the flag
898            // absorbs the pair into one break. docling's `_iterate_items`
899            // yields a `_PageBreakNode` only between two items on different
900            // pages (a group's leading item counts for the group), which is
901            // exactly "a boundary between two rendered blocks": nothing before
902            // the first block, nothing after the last, consecutive boundaries
903            // — empty or furniture-only pages — collapsed into one.
904            Node::PageBreak | Node::PageInfo { .. } => {
905                if ctx.page_break.is_some() && ctx.emitted_any {
906                    ctx.pending_page_break = true;
907                }
908                i += 1;
909            }
910            Node::ListItem { .. } => {
911                let start = i;
912                i += 1;
913                loop {
914                    match nodes.get(i) {
915                        Some(Node::ListItem { .. } | Node::ListText { .. }) => i += 1,
916                        // An empty paragraph between two list items is absorbed
917                        // into the run — docling keeps such a ListGroup
918                        // contiguous rather than splitting it.
919                        Some(Node::Paragraph { text })
920                            if text.is_empty()
921                                && matches!(nodes.get(i + 1), Some(Node::ListItem { .. })) =>
922                        {
923                            i += 1
924                        }
925                        _ => break,
926                    }
927                }
928                render_list_run(&nodes[start..i], blocks, ctx);
929            }
930            other => {
931                let before_item = blocks.len();
932                render_one(other, blocks, ctx);
933                // A picture that is the drawing a `mermaid` code item was
934                // extracted from (#858): in the placeholder mode the
935                // `<!-- image -->` says nothing the graph does not, so the
936                // marker — the last block a picture pushes — goes; an
937                // embedded or referenced image is content and stays.
938                if matches!(other, Node::Picture { .. })
939                    && matches!(ctx.images, ImageMode::Placeholder)
940                    && blocks.len() > before_item
941                    && matches!(
942                        nodes.get(i + 1),
943                        Some(Node::Code { language: Some(l), .. }) if l == "mermaid"
944                    )
945                {
946                    blocks.pop();
947                }
948                i += 1;
949            }
950        }
951        if blocks.len() > before {
952            // docling-core joins only the parts that carry text
953            // (`"\n\n".join(p.text for p in parts if p.text)`, at the
954            // document and every group scope), so an item that renders an
955            // empty block — a picture or chart under `image_placeholder: ""`,
956            // an empty field part or inline group — adds no blank lines
957            // (#605). It still counts as an item for the page-break scan:
958            // upstream's page-break marker sits between the *items* of two
959            // pages and is never empty itself, so the placeholder below still
960            // goes in (an empty placeholder keeps its doubled delimiter).
961            let kept: Vec<String> = blocks.drain(before..).filter(|b| !b.is_empty()).collect();
962            blocks.extend(kept);
963            if ctx.pending_page_break {
964                // The placeholder is a block of its own, joined by the document
965                // delimiter like docling's `_PageBreakSerResult` part — an
966                // empty placeholder therefore leaves the doubled `\n\n`
967                // upstream leaves too.
968                if let Some(placeholder) = &ctx.page_break {
969                    blocks.insert(before, placeholder.clone());
970                }
971                ctx.pending_page_break = false;
972            }
973            ctx.emitted_any = true;
974        }
975    }
976}
977
978/// Render a contiguous run of list items.
979///
980/// Ordered items use their explicit `number`. A new sibling list (marked by
981/// `first_in_list`) at the same depth is separated by a blank line, matching
982/// docling-core's serializer.
983fn render_list_run(items: &[Node], blocks: &mut Vec<String>, ctx: &Ctx) {
984    let mut lines: Vec<String> = Vec::new();
985    // Whether a top-level item has been rendered yet — a fresh-list flag on
986    // the very first item opens nothing.
987    let mut any_top = false;
988
989    for item in items {
990        if let Node::ListText { level, text, glue } = item {
991            let text = ctx.text(text);
992            match lines.last_mut() {
993                Some(last) if *glue => last.push_str(&text),
994                _ => lines.push(format!("{}{text}", "    ".repeat(*level as usize))),
995            }
996            continue;
997        }
998        let Node::ListItem {
999            ordered,
1000            number,
1001            first_in_list,
1002            text,
1003            level,
1004            marker: orig_marker,
1005            location: _,
1006            dclx: _,
1007            href: _,
1008            layer,
1009        } = item
1010        else {
1011            continue;
1012        };
1013        // docling's content-layer filtering: an item renders when its layer
1014        // — its own, else the layer of the group it sits in — is in the set
1015        // (body only by default, so a furniture list item is omitted).
1016        if !ctx.layers.contains(layer.or(ctx.current_layer)) {
1017            continue;
1018        }
1019        let level = *level as usize;
1020
1021        // A new sibling list at the top level gets a blank line — and only the
1022        // backend knows where one starts (`first_in_list`: Word's `numId`
1023        // changing, an HTML `<ul>` closing, a Markdown bullet switching
1024        // `-`→`*`). The serializer used to guess it from a kind flip or a
1025        // number gap as well, which split lists docling keeps whole (an
1026        // AsciiDoc `1.` … `5.`, mixed `*`/`1.` markers) — #385. Only at the
1027        // top level: nested sibling groups are children of a list item, and
1028        // docling joins an item's children without blank lines.
1029        if level == 0 {
1030            if any_top && *first_in_list {
1031                lines.push(String::new());
1032            }
1033            any_top = true;
1034        }
1035
1036        let indent = "    ".repeat(level);
1037        lines.push(format!(
1038            "{indent}{} {}",
1039            list_item_marker(orig_marker.as_deref().unwrap_or(""), *ordered, *number),
1040            list_item_text(text, ctx)
1041        ));
1042    }
1043
1044    // A run consisting only of furniture (content-layer-filtered) items yields no
1045    // lines; pushing an empty block here would surface as a stray blank line.
1046    if !lines.is_empty() {
1047        blocks.push(lines.join("\n"));
1048    }
1049}
1050
1051/// docling-core's list item marker (`orig_list_item_marker_mode=AUTO`,
1052/// `ensure_valid_list_item_marker=True`; docling-core 2.100+): a marker that
1053/// is already valid Markdown — `-`, `*`, `+`, or digits and a dot (Python's
1054/// `\d+\.` admits every Unicode decimal digit, so a DOCX `decimalFullWidth`
1055/// `1.` stays as it is, docling#4336) — prints verbatim; any other marker
1056/// holding a letter or digit is informative (`a)`, `①`, `第九条`) and prints
1057/// after a `-`; a markerless item of an enumerated list is numbered by its
1058/// position, and every other item gets a `-`.
1059fn list_item_marker(marker: &str, ordered: bool, number: u64) -> String {
1060    let already_valid = matches!(marker, "-" | "*" | "+")
1061        || marker
1062            .strip_suffix('.')
1063            .is_some_and(|d| !d.is_empty() && d.chars().all(char::is_numeric));
1064    if already_valid {
1065        return marker.to_string();
1066    }
1067    let outer = if ordered && marker.is_empty() {
1068        format!("{number}.")
1069    } else {
1070        "-".to_string()
1071    };
1072    if marker.chars().any(char::is_alphanumeric) {
1073        format!("{outer} {marker}")
1074    } else {
1075        outer
1076    }
1077}
1078
1079/// A list item's Markdown body. The GFM hard-line-break rule (docling-core#721)
1080/// applies to the item's own text; pictures the HTML backend folded into the
1081/// item (`"\n[alt\n]<!-- image -->"` per `<img>` inside the `<li>`) are
1082/// docling's picture *children* of the item, which its serializer prints after
1083/// the item line with plain newlines — so a folded tail keeps its newlines
1084/// unmarked. The tail is recognised structurally: every line after the first is
1085/// an image marker or an alt caption directly followed by one.
1086fn list_item_text(text: &str, ctx: &Ctx) -> String {
1087    let escaped = ctx.text(text);
1088    if ctx.plain {
1089        return escaped;
1090    }
1091    if let Some((own, tail)) = escaped.split_once('\n') {
1092        if is_folded_child_tail(tail) {
1093            return format!("{}\n{tail}", md_line_breaks(own));
1094        }
1095    }
1096    md_line_breaks(&escaped)
1097}
1098
1099/// Whether everything after a list item's own first line is a folded *child*
1100/// block rather than a continuation of the item's text: an image marker
1101/// (optionally preceded by its caption/alt line) or a fenced code block. The
1102/// AsciiDoc backend indents such a block to the item's own depth (as
1103/// docling-core's list serializer does for each part it emits), so a leading
1104/// indent is ignored here.
1105fn is_folded_child_tail(tail: &str) -> bool {
1106    const MARKER: &str = "<!-- image -->";
1107    let mut lines = tail.split('\n').peekable();
1108    let mut any = false;
1109    while let Some(line) = lines.next() {
1110        let line = line.trim_start();
1111        if line == MARKER {
1112            any = true;
1113        } else if is_fence_line(line) {
1114            // Skip the block's body; an unclosed fence is not a folded child.
1115            // The closing fence is the opening one's length (#859).
1116            loop {
1117                match lines.next() {
1118                    Some(l) if l.trim_start() == line => break,
1119                    Some(_) => {}
1120                    None => return false,
1121                }
1122            }
1123            any = true;
1124        } else if lines.next().map(str::trim_start) == Some(MARKER) {
1125            any = true; // an alt caption line, then its marker
1126        } else {
1127            return false;
1128        }
1129    }
1130    any
1131}
1132
1133/// Render one item: docling's content-layer gate first — an item whose layer
1134/// (the group's or wrapper's it sits in, else the body) is outside the set
1135/// renders nothing, while a container still walks its children — then the
1136/// item itself.
1137fn render_one(node: &Node, blocks: &mut Vec<String>, ctx: &mut Ctx) {
1138    match node {
1139        // Containers and layer wrappers decide for their children.
1140        Node::Group { .. }
1141        | Node::Furniture { .. }
1142        | Node::PageFurniture { .. }
1143        | Node::FurnitureText { .. }
1144        | Node::CommentSection { .. }
1145        | Node::PictureChildren(_)
1146        | Node::Commented { .. }
1147        | Node::ScriptText { .. }
1148        | Node::Located { .. }
1149        | Node::Prov { .. }
1150        | Node::Track { .. }
1151        | Node::PageBreak
1152        | Node::PageInfo { .. }
1153        | Node::DoclangOnly(_) => {}
1154        _ if !ctx.layer_visible() => return,
1155        _ => {}
1156    }
1157    render_item(node, blocks, ctx)
1158}
1159
1160fn render_item(node: &Node, blocks: &mut Vec<String>, ctx: &mut Ctx) {
1161    match node {
1162        Node::Heading { level, text } => {
1163            if ctx.plain {
1164                // `PlainTextTextSerializer`: the heading's text as it is —
1165                // no `#`, its newlines kept.
1166                blocks.push(ctx.text(text));
1167                return;
1168            }
1169            let text = heading_line_breaks(&ctx.text(text));
1170            if ctx.in_table_cell {
1171                // docling-core#540: no `#` markers inside a table cell.
1172                blocks.push(text);
1173            } else {
1174                let hashes = "#".repeat((*level).clamp(1, 6) as usize);
1175                blocks.push(format!("{hashes} {text}"));
1176            }
1177        }
1178        // An empty body paragraph (docling's blank-line text item) contributes
1179        // nothing to Markdown — only DocLang/JSON keep it.
1180        Node::Paragraph { text } if text.is_empty() => {}
1181        Node::Paragraph { text } => blocks.push(ctx.breaks(&ctx.text(text))),
1182        Node::LabeledText { .. } => render_item(&node.labeled_as_paragraph(), blocks, ctx),
1183        // A standalone caption item renders like a text item; its hyperlink
1184        // annotation becomes a Markdown link around the whole caption.
1185        Node::Caption { text, .. } if text.is_empty() => {}
1186        Node::Caption { text, href } => {
1187            let body = ctx.breaks(&ctx.text(text));
1188            blocks.push(match href {
1189                Some(url) if !ctx.plain => crate::hyperlink::markdown_link(&body, url),
1190                _ => body,
1191            });
1192        }
1193        Node::CheckboxItem { checked, text } => {
1194            // docling-core#955: the box needs a space after it, unless the
1195            // text brings its own (a DocLang `<checkbox/> y` reads `" y"`).
1196            let mark = if *checked { "- [x]" } else { "- [ ]" };
1197            let sep = if text.starts_with(' ') { "" } else { " " };
1198            blocks.push(ctx.breaks(&ctx.text(&format!("{mark}{sep}{text}"))));
1199        }
1200        Node::Code {
1201            language,
1202            text,
1203            pretty,
1204            ..
1205        } => {
1206            // Legacy docling never emits a language on the fence; strict keeps
1207            // it. A `mermaid` block (#858, an extraction docling has no
1208            // counterpart for) keeps its tag in either mode: without it the
1209            // graph is a code block no renderer draws.
1210            let lang = match language {
1211                Some(l) if ctx.strict || l == "mermaid" => l.as_str(),
1212                _ => "",
1213            };
1214            // Strict prefers the line-preserving rendering when the backend
1215            // supplied one (PDF); legacy stays on docling's flat `text`.
1216            let body = match pretty {
1217                Some(p) if ctx.strict => p.as_str(),
1218                _ => text.as_str(),
1219            };
1220            // The plain export (`format_code_blocks=False`) prints the code
1221            // as it is.
1222            blocks.push(if ctx.plain {
1223                body.to_string()
1224            } else {
1225                code_fence(lang, body)
1226            });
1227        }
1228        // A CodeFormula-decoded display formula renders as docling's `$$…$$`
1229        // (the un-enriched pipeline emits a placeholder paragraph instead).
1230        Node::Formula { latex, .. } => blocks.push(format!("$${latex}$$")),
1231        Node::Table(table) => {
1232            // docling renders a table's caption as a text line before the grid.
1233            // `caption` is already escaped (backend convention), like a paragraph.
1234            if let Some(cap) = &table.caption {
1235                if !cap.is_empty() {
1236                    blocks.push(ctx.text(cap));
1237                }
1238            }
1239            let rendered = if ctx.plain {
1240                render_table(&ctx.plain_table(table), ctx.compact_tables)
1241            } else {
1242                render_table(table, ctx.compact_tables)
1243            };
1244            if !rendered.is_empty() {
1245                blocks.push(rendered);
1246            }
1247            // docling-core#675: the footnotes follow the grid, one paragraph
1248            // each (`serialize_footnotes` with a `\n\n` delimiter).
1249            for note in table.footnotes.iter().filter(|n| !n.is_empty()) {
1250                blocks.push(ctx.text(note));
1251            }
1252        }
1253        // Classification predictions don't affect docling's Markdown output;
1254        // a description (the picture-OCR text, #645) prints between the
1255        // caption and the image placeholder — docling's
1256        // `MarkdownPictureSerializer` order: captions, annotations, image.
1257        // Alt text (#676) is rendered only under strict Markdown or the
1258        // document's `picture_alt_text`, and then where Markdown puts it: as
1259        // the `![alt](…)` of an image line, or — with the placeholder, which
1260        // has no alt slot — as the line a description takes.
1261        Node::Picture {
1262            caption,
1263            image,
1264            description,
1265            ..
1266        } => {
1267            if let Some(cap) = caption {
1268                if !cap.is_empty() {
1269                    blocks.push(ctx.caption(cap));
1270                }
1271            }
1272            let alt = description
1273                .as_ref()
1274                .filter(|d| d.is_alt_text() && ctx.alt_text && !d.text.is_empty());
1275            let marker = picture_marker(image.as_ref(), ctx, alt.map(|d| d.text.as_str()));
1276            // A docling-native `meta.description` (docling#5015) is the
1277            // item's meta, which docling-core's serializer appends *after*
1278            // the item; the legacy-annotation kinds precede the image.
1279            let native = description
1280                .as_ref()
1281                .filter(|d| d.is_docling_meta() && !d.text.is_empty());
1282            if let Some(desc) = description.as_ref().filter(|d| !d.is_docling_meta()) {
1283                let shown = !desc.is_alt_text() || (ctx.alt_text && !marker.starts_with("!["));
1284                if !desc.text.is_empty() && shown {
1285                    blocks.push(ctx.breaks(&ctx.text(&desc.text)));
1286                }
1287            }
1288            blocks.push(marker);
1289            if let Some(desc) = native {
1290                blocks.push(ctx.breaks(&ctx.text(&desc.text)));
1291            }
1292        }
1293        // A chart renders as docling's picture-with-meta markdown: the caption,
1294        // the placeholder, the humanized classification ("line_chart" ->
1295        // "Line chart"), then the chart's data grid as a regular table.
1296        Node::Chart {
1297            kind,
1298            table,
1299            caption,
1300            ..
1301        } => {
1302            if let Some(cap) = caption {
1303                if !cap.is_empty() {
1304                    blocks.push(ctx.caption(cap));
1305                }
1306            }
1307            blocks.push(picture_marker(None, ctx, None));
1308            blocks.push(humanize_label(kind));
1309            let rendered = if ctx.plain {
1310                render_table(&ctx.plain_table(table), false)
1311            } else {
1312                render_table(table, false)
1313            };
1314            if !rendered.is_empty() {
1315                blocks.push(rendered);
1316            }
1317        }
1318        // A DocLang-only node is omitted from Markdown.
1319        Node::DoclangOnly(_) => {}
1320        // A group on a non-body layer (a hidden spreadsheet sheet): its items
1321        // sit on that layer, as every item of such a sheet does upstream — so
1322        // they render exactly when the set includes it (#599), nothing by
1323        // default.
1324        Node::Group {
1325            layer: Some(layer),
1326            children,
1327            ..
1328        } => {
1329            let outer = ctx.current_layer.replace(*layer);
1330            render(children, blocks, ctx);
1331            ctx.current_layer = outer;
1332        }
1333        Node::Group { children, .. } => render(children, blocks, ctx),
1334        Node::FieldRegion { items } => {
1335            // The region container and each field item carry no text of their
1336            // own; docling-core 2.93 (#724) serializes them to nothing (older
1337            // releases emitted a `<!-- missing-text -->` marker for each), so
1338            // only an item's marker/key/value appear, as separate paragraphs.
1339            for item in items {
1340                for part in [&item.marker, &item.key, &item.value].into_iter().flatten() {
1341                    blocks.push(ctx.breaks(&ctx.text(part)));
1342                }
1343            }
1344        }
1345        // docling's Markdown serializer has no component for a `KeyValueItem`
1346        // and writes its fallback placeholder in the item's place.
1347        Node::KeyValueGraph { .. } => blocks.push(MISSING_KEY_VALUE_ITEM.to_string()),
1348        // A rich inline group renders exactly like a paragraph of its Markdown
1349        // text — the structured runs are DocLang-only.
1350        Node::InlineGroup { md_text, .. } => blocks.push(ctx.breaks(&ctx.text(md_text))),
1351        // A plain-text backend dump renders verbatim as a single block.
1352        Node::TextDump(text) => {
1353            if !text.is_empty() {
1354                blocks.push(text.clone());
1355            }
1356        }
1357        // Furniture (page headers/footers, HTML `<title>`, notes) is out of
1358        // the default body-only export, mirroring docling; a set that
1359        // includes its layer renders the wrapped item like a body item (#599).
1360        Node::Furniture { layer, inner } => {
1361            let outer = ctx.current_layer.replace(*layer);
1362            render_one(inner, blocks, ctx);
1363            ctx.current_layer = outer;
1364        }
1365        // A page header/footer or a `.doc` furniture paragraph: upstream's
1366        // `page_header` / `page_footer` / `footnote` text item, a plain
1367        // paragraph when the furniture layer renders.
1368        Node::PageFurniture { text, .. } | Node::FurnitureText { text, .. } => {
1369            if ctx.layers.furniture && !text.is_empty() {
1370                blocks.push(ctx.breaks(&ctx.text(text)));
1371            }
1372        }
1373        // A picture's contained text: docling's Markdown picture serializer
1374        // prints the caption and the image, never the children — unless the
1375        // export traverses pictures, when the item walk yields them after
1376        // the picture and they render as the text items they are (#599).
1377        Node::PictureChildren(children) => {
1378            if ctx.traverse_pictures {
1379                render(children, blocks, ctx);
1380            }
1381        }
1382        // A comment lives in the notes layer — out like other furniture
1383        // unless that layer renders (the note's `[author: …]: text` as a
1384        // paragraph); the annotation on a body item is JSON-only, so render
1385        // the item.
1386        Node::CommentSection { text, .. } => {
1387            if ctx.layers.notes && !text.is_empty() {
1388                blocks.push(ctx.breaks(&ctx.text(text)));
1389            }
1390        }
1391        Node::Commented { inner, .. } => render_one(inner, blocks, ctx),
1392        // The item's text is already the runs' text (docling's Markdown of
1393        // its inline group: the runs joined, no markup — `H2O`).
1394        Node::ScriptText { inner, .. } => render_one(inner, blocks, ctx),
1395        // Layout provenance is DocLang-only; render the wrapped node.
1396        Node::Located { inner, .. } | Node::Prov { inner, .. } | Node::Track { inner, .. } => {
1397            render_one(inner, blocks, ctx)
1398        }
1399        // Page breaks are DocLang-only; docling omits them from Markdown.
1400        Node::PageBreak => {}
1401        // Page markers feed the JSON export only.
1402        Node::PageInfo { .. } => {}
1403        // A link destination (#852) feeds the JSON export only.
1404        Node::Anchor { .. } => {}
1405        // Runs of adjacent list items are merged by `render`; a stray single
1406        // item (a hand-built document, or a `Located` wrapper around one)
1407        // still renders as its own one-item list instead of panicking —
1408        // `nodes` is public API, so every representable tree must serialize.
1409        Node::ListItem { .. } => render_list_run(std::slice::from_ref(node), blocks, ctx),
1410        // Outside a list run (a hand-built document): a paragraph.
1411        Node::ListText { text, .. } if text.is_empty() => {}
1412        Node::ListText { text, .. } => blocks.push(ctx.breaks(&ctx.text(text))),
1413    }
1414}
1415
1416/// The Markdown for a picture under the active [`ImageMode`]; Referenced mode also
1417/// records the bytes in `ctx.artifacts` for the caller to write.
1418/// docling-core's `_humanize_text`: underscores to spaces, first letter
1419/// capitalized ("line_chart" -> "Line chart").
1420fn humanize_label(label: &str) -> String {
1421    let text = label.replace('_', " ");
1422    let mut chars = text.chars();
1423    match chars.next() {
1424        Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
1425        None => text,
1426    }
1427}
1428
1429/// The image line's alt: docling's `Image`, or the picture's alt text (#676)
1430/// on one line with its `]` escaped, so it cannot close the bracket.
1431fn image_alt(alt: Option<&str>) -> String {
1432    match alt {
1433        Some(text) => text
1434            .split_whitespace()
1435            .collect::<Vec<_>>()
1436            .join(" ")
1437            .replace('\\', "\\\\")
1438            .replace(']', "\\]"),
1439        None => "Image".to_string(),
1440    }
1441}
1442
1443fn picture_marker(image: Option<&crate::PictureImage>, ctx: &mut Ctx, alt: Option<&str>) -> String {
1444    match (ctx.images, image) {
1445        // docling embeds the `ImageRef`'s PNG (a JPEG re-encoded).
1446        (ImageMode::Embedded, Some(img)) => {
1447            format!(
1448                "![{}]({})",
1449                image_alt(alt),
1450                crate::pixel_digest::docling_data_uri(img).1
1451            )
1452        }
1453        (ImageMode::Referenced, Some(img)) => {
1454            let path = format!(
1455                "{}/image_{:06}.{}",
1456                ctx.artifacts_dir,
1457                ctx.pic_index,
1458                ext_for(&img.mimetype)
1459            );
1460            ctx.pic_index += 1;
1461            ctx.artifacts.push((path.clone(), img.data.clone()));
1462            format!("![{}]({})", image_alt(alt), escape_uri_path(&path))
1463        }
1464        // Placeholder, or any mode with no extracted image.
1465        _ => ctx.image_placeholder.clone(),
1466    }
1467}
1468
1469/// Encode a URL or filesystem path as a Markdown link destination —
1470/// docling-core's `MarkdownPictureSerializer._escape_uri_path`
1471/// (docling-core#698, 2.94). Handles URLs of any scheme as well as POSIX and
1472/// Windows paths, keeps relative paths relative and never double-encodes:
1473/// backslashes become `/` (a backslash is both the Windows separator and a
1474/// Markdown escape), a UNC share `//host/…` and an absolute Windows path
1475/// `C:/…` become RFC 8089 `file://` URLs (the one spelling a renderer cannot
1476/// misread as a scheme-relative URL or a `C:` scheme), a URL keeps its
1477/// scheme / authority / delimiters with only the components encoded, and
1478/// everything else is percent-encoded as a path. `%` is kept so an
1479/// already-encoded destination stays as it is; spaces and parentheses are
1480/// encoded because they would end (or unbalance) a Markdown inline link.
1481pub(crate) fn escape_uri_path(value: &str) -> String {
1482    const KEEP: &str = "/%:@+,;=~$!&'*";
1483    let s = value.replace('\\', "/");
1484    if let Some(rest) = s.strip_prefix("//") {
1485        // A fileshare: `file://<host>/<path>`, the host possibly empty.
1486        let rest = rest.trim_start_matches('/');
1487        let (host, tail) = rest.split_once('/').unwrap_or((rest, ""));
1488        return format!("file://{host}{}", percent_quote(&format!("/{tail}"), KEEP));
1489    }
1490    let bytes = s.as_bytes();
1491    if bytes.len() >= 3 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':' && bytes[2] == b'/' {
1492        // A Windows path with a drive letter: `file:///C:/…`.
1493        return format!("file:///{}", percent_quote(&s, KEEP));
1494    }
1495    // A URL keeps its scheme, authority and delimiters; only its components are
1496    // encoded. A single-character scheme cannot be real (it is a drive letter,
1497    // handled above), so it is read as a path — like `urlsplit`.
1498    if let Some((scheme, rest)) = s.split_once(':') {
1499        let valid_scheme = scheme.len() > 1
1500            && scheme.as_bytes()[0].is_ascii_alphabetic()
1501            && scheme
1502                .bytes()
1503                .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'));
1504        if valid_scheme {
1505            let (authority, rest) = match rest.strip_prefix("//") {
1506                Some(r) => {
1507                    let end = r.find(['/', '?', '#']).unwrap_or(r.len());
1508                    (Some(&r[..end]), &r[end..])
1509                }
1510                None => (None, rest),
1511            };
1512            let (before_frag, fragment) = rest.split_once('#').unwrap_or((rest, ""));
1513            let (path, query) = before_frag.split_once('?').unwrap_or((before_frag, ""));
1514            let mut out = format!("{scheme}:");
1515            if let Some(a) = authority {
1516                out.push_str("//");
1517                out.push_str(a);
1518            }
1519            out.push_str(&percent_quote(path, KEEP));
1520            if !query.is_empty() {
1521                out.push('?');
1522                out.push_str(&percent_quote(query, KEEP));
1523            }
1524            if !fragment.is_empty() {
1525                out.push('#');
1526                out.push_str(&percent_quote(fragment, KEEP));
1527            }
1528            return out;
1529        }
1530    }
1531    // A relative or root-relative local path.
1532    percent_quote(&s, KEEP)
1533}
1534
1535/// `urllib.parse.quote(s, safe)`: unreserved ASCII (`A–Z a–z 0–9 _ . - ~`) and
1536/// the `safe` set stay, every other byte of the UTF-8 encoding becomes `%XX`.
1537fn percent_quote(s: &str, safe: &str) -> String {
1538    let mut out = String::with_capacity(s.len());
1539    for &b in s.as_bytes() {
1540        let keep = b.is_ascii_alphanumeric()
1541            || matches!(b, b'_' | b'.' | b'-' | b'~')
1542            || (b.is_ascii() && safe.contains(b as char));
1543        if keep {
1544            out.push(b as char);
1545        } else {
1546            out.push_str(&format!("%{b:02X}"));
1547        }
1548    }
1549    out
1550}
1551
1552pub(crate) fn ext_for(mimetype: &str) -> &str {
1553    match mimetype {
1554        "image/jpeg" => "jpg",
1555        "image/gif" => "gif",
1556        "image/webp" => "webp",
1557        "image/bmp" => "bmp",
1558        "image/tiff" => "tif",
1559        "image/svg+xml" => "svg",
1560        _ => "png",
1561    }
1562}
1563
1564/// Render a table. `compact` selects between two serializers:
1565///
1566/// - **padded** (default) — docling-core's `tabulate(tablefmt="github")`: columns
1567///   are padded to a fixed width (header width + a minimum padding of 2, or the
1568///   widest data cell); numeric columns (every data cell parses as a number) are
1569///   right-aligned, others left-aligned; separators are plain dashes of
1570///   `width + 2`. Matches current published docling (DOCX/HTML conformance).
1571/// - **compact** — `| a | b |` cells with single-dash `| - | - |` separators, no
1572///   width padding. Matches the committed PDF groundtruth corpus, which predates
1573///   the padded serializer.
1574///
1575/// Each cell is first escaped (`\n` → space, `|` → `&#124;`) so it can't break the
1576/// table. The header row is the table's leading `column_header` block flattened
1577/// to one row ([`Table::header_row_count`] + [`flatten_header_rows`],
1578/// docling-core#723); alignment and widths are computed over the body rows.
1579/// Whether a table cell counts as a number for column alignment, matching
1580/// `tabulate`'s detection: an ordinary float/int (`f64`-parseable, covering
1581/// `1e2`/`inf`/`+1.5`) **or** a thousands-separated number like `7,015`.
1582fn is_number_cell(t: &str) -> bool {
1583    t.parse::<f64>().is_ok() || is_thousands_number(t)
1584}
1585
1586/// A number with comma thousands-separators, per `tabulate`'s
1587/// `_float_with_thousands_separators` regex
1588/// (`^(([+-]?[0-9]{1,3})(?:,([0-9]{3}))*)?(?(1)\.[0-9]*|\.[0-9]+)?$`): the
1589/// integer part is 1–3 digits then any number of `,ddd` groups; the fraction is
1590/// optional (and, without an integer part, must have at least one digit).
1591fn is_thousands_number(t: &str) -> bool {
1592    let b = t.as_bytes();
1593    let mut i = 0;
1594    let start = i;
1595    if i < b.len() && (b[i] == b'+' || b[i] == b'-') {
1596        i += 1;
1597    }
1598    // First digit chunk: 1–3 digits.
1599    let d0 = i;
1600    while i < b.len() && b[i].is_ascii_digit() && i - d0 < 3 {
1601        i += 1;
1602    }
1603    let has_int = i > d0;
1604    if has_int {
1605        // Subsequent `,ddd` groups (exactly three digits each).
1606        while i + 3 < b.len() + 1
1607            && b.get(i) == Some(&b',')
1608            && b.get(i + 1).is_some_and(u8::is_ascii_digit)
1609            && b.get(i + 2).is_some_and(u8::is_ascii_digit)
1610            && b.get(i + 3).is_some_and(u8::is_ascii_digit)
1611        {
1612            i += 4;
1613        }
1614    } else {
1615        // A sign only counts with an integer part.
1616        i = start;
1617    }
1618    // Optional fraction.
1619    if i < b.len() && b[i] == b'.' {
1620        i += 1;
1621        let f0 = i;
1622        while i < b.len() && b[i].is_ascii_digit() {
1623            i += 1;
1624        }
1625        if !has_int && i == f0 {
1626            return false; // `.` with no digits and no integer part
1627        }
1628    } else if !has_int {
1629        return false; // neither integer nor fractional part
1630    }
1631    i == b.len()
1632}
1633
1634/// The single GFM header row for a table: the leading header rows (see
1635/// [`Table::header_row_count`]) flattened per column, texts joined with
1636/// `" - "` after dropping consecutive duplicates — docling-core's
1637/// `_flatten_header_rows` (docling-core#723). The duplicate rule is what
1638/// keeps a row-spanning header from being joined to itself (the grid repeats
1639/// its text into every row it covers); it is position-based, so two stacked
1640/// levels sharing a label collapse too — GFM has one header row, and upstream
1641/// accepts that loss. No header rows → one empty header cell per column.
1642fn flatten_header_rows(header_rows: &[Vec<String>], num_cols: usize) -> Vec<String> {
1643    (0..num_cols)
1644        .map(|c| {
1645            let mut parts: Vec<&str> = Vec::new();
1646            for row in header_rows {
1647                let text = row.get(c).map(String::as_str).unwrap_or("");
1648                if !text.is_empty() && parts.last() != Some(&text) {
1649                    parts.push(text);
1650                }
1651            }
1652            parts.join(" - ")
1653        })
1654        .collect()
1655}
1656
1657pub(crate) fn render_table(table: &Table, compact: bool) -> String {
1658    if table.rows.is_empty() {
1659        return String::new();
1660    }
1661    let num_cols = table.rows.iter().map(Vec::len).max().unwrap_or(0);
1662    if num_cols == 0 {
1663        return String::new();
1664    }
1665
1666    // Escaped, rectangular grid (ragged rows padded with empty cells). The
1667    // header block is resolved to the one row GFM allows (docling-core#723);
1668    // `tabulate` strips data cells of surrounding whitespace but leaves the
1669    // header texts as-is.
1670    let num_headers = table.header_row_count().min(table.rows.len());
1671    let escaped = |r: usize| -> Vec<String> {
1672        (0..num_cols)
1673            .map(|c| escape_cell(table.rows[r].get(c).map(String::as_str).unwrap_or("")))
1674            .collect()
1675    };
1676    let header_rows: Vec<Vec<String>> = (0..num_headers).map(escaped).collect();
1677    let header = flatten_header_rows(&header_rows, num_cols);
1678    let body: Vec<Vec<String>> = (num_headers..table.rows.len())
1679        .map(|r| {
1680            escaped(r)
1681                .into_iter()
1682                .map(|c| c.trim().to_string())
1683                .collect()
1684        })
1685        .collect();
1686
1687    if compact {
1688        // Compact: cells joined by " | ", no padding, single-dash separators.
1689        let render_row = |row: &[String]| -> String { format!("| {} |", row.join(" | ")) };
1690        let mut lines = Vec::with_capacity(body.len() + 2);
1691        lines.push(render_row(&header));
1692        let sep: Vec<&str> = (0..num_cols).map(|_| "-").collect();
1693        lines.push(format!("| {} |", sep.join(" | ")));
1694        for row in &body {
1695            lines.push(render_row(row));
1696        }
1697        return lines.join("\n");
1698    }
1699
1700    // Display width (Unicode scalar count — good enough for now).
1701    let dw = |s: &str| s.chars().count();
1702
1703    // A column is right-aligned when at least one body cell is numeric and every
1704    // non-empty body cell is numeric — matching `tabulate`'s column typing, where
1705    // empty cells are "missing" (ignored) and a number may carry thousands
1706    // separators (`7,015`), which a plain `f64` parse rejects.
1707    let right: Vec<bool> = (0..num_cols)
1708        .map(|c| {
1709            let mut any = false;
1710            for row in &body {
1711                let t = row[c].trim();
1712                if t.is_empty() {
1713                    continue;
1714                }
1715                if !is_number_cell(t) {
1716                    return false;
1717                }
1718                any = true;
1719            }
1720            any
1721        })
1722        .collect();
1723
1724    // Column width = max(header_width + MIN_PADDING(2), max body-cell width).
1725    let width: Vec<usize> = (0..num_cols)
1726        .map(|c| {
1727            let mut w = dw(&header[c]) + 2;
1728            for row in &body {
1729                w = w.max(dw(&row[c]));
1730            }
1731            w
1732        })
1733        .collect();
1734
1735    let fmt_cell = |s: &str, c: usize| -> String {
1736        let pad = " ".repeat(width[c].saturating_sub(dw(s)));
1737        let body = if right[c] {
1738            format!("{pad}{s}")
1739        } else {
1740            format!("{s}{pad}")
1741        };
1742        format!(" {body} ")
1743    };
1744    let render_row = |row: &[String]| -> String {
1745        let cells: Vec<String> = (0..num_cols).map(|c| fmt_cell(&row[c], c)).collect();
1746        format!("|{}|", cells.join("|"))
1747    };
1748
1749    let mut lines = Vec::with_capacity(body.len() + 2);
1750    lines.push(render_row(&header));
1751    let sep: Vec<String> = (0..num_cols).map(|c| "-".repeat(width[c] + 2)).collect();
1752    lines.push(format!("|{}|", sep.join("|")));
1753    for row in &body {
1754        lines.push(render_row(row));
1755    }
1756    lines.join("\n")
1757}
1758
1759/// Escape a table cell so it can't break the markdown table: newlines become
1760/// spaces and pipes become the `&#124;` HTML entity (matches docling-core).
1761fn escape_cell(s: &str) -> String {
1762    s.replace('\n', " ").replace('|', "&#124;")
1763}
1764
1765#[cfg(test)]
1766mod tests {
1767    use super::*;
1768    use crate::{PictureImage, TableCell, TableStructure};
1769
1770    /// #385: where one list ends and the next begins is the backend's call
1771    /// (`first_in_list`), never the serializer's. An ordered run `1.` → `5.`
1772    /// is one list (an AsciiDoc numbered list around a nested one), and so are
1773    /// mixed bullet/ordered items the backend did not separate; only a flagged
1774    /// item opens a new list and earns the blank line.
1775    #[test]
1776    fn list_boundaries_come_from_the_backend_not_the_numbering() {
1777        let item = |ordered: bool, number: u64, first_in_list: bool, text: &str| Node::ListItem {
1778            ordered,
1779            number,
1780            first_in_list,
1781            text: text.into(),
1782            level: 0,
1783            marker: None,
1784            location: None,
1785            dclx: None,
1786            href: None,
1787            layer: None,
1788        };
1789        let md = |items: Vec<Node>| {
1790            let mut doc = DoclingDocument::new("t");
1791            for n in items {
1792                doc.push(n);
1793            }
1794            doc.export_to_markdown()
1795        };
1796        // A number gap alone is not a boundary.
1797        assert_eq!(
1798            md(vec![
1799                item(true, 1, true, "one"),
1800                item(true, 5, false, "five")
1801            ]),
1802            "1. one\n5. five\n"
1803        );
1804        // Nor is a kind flip the backend did not flag …
1805        assert_eq!(
1806            md(vec![
1807                item(false, 0, true, "bullet"),
1808                item(true, 1, false, "one"),
1809                item(false, 0, false, "bullet two"),
1810            ]),
1811            "- bullet\n1. one\n- bullet two\n"
1812        );
1813        // … while a flagged item is one, whatever its number says.
1814        assert_eq!(
1815            md(vec![
1816                item(true, 1, true, "a"),
1817                item(true, 2, false, "b"),
1818                item(true, 3, true, "new list, continuing count"),
1819            ]),
1820            "1. a\n2. b\n\n3. new list, continuing count\n"
1821        );
1822    }
1823
1824    #[test]
1825    fn renders_headings_paragraphs_and_lists() {
1826        let mut doc = DoclingDocument::new("demo");
1827        doc.add_heading(1, "Title");
1828        doc.add_paragraph("Hello world.");
1829        doc.push(Node::ListItem {
1830            ordered: false,
1831            number: 1,
1832            first_in_list: true,
1833            text: "first".into(),
1834            level: 0,
1835            marker: None,
1836            location: None,
1837            dclx: None,
1838            href: None,
1839            layer: None,
1840        });
1841        doc.push(Node::ListItem {
1842            ordered: false,
1843            number: 2,
1844            first_in_list: false,
1845            text: "second".into(),
1846            level: 0,
1847            marker: None,
1848            location: None,
1849            dclx: None,
1850            href: None,
1851            layer: None,
1852        });
1853        let md = doc.export_to_markdown();
1854        assert_eq!(md, "# Title\n\nHello world.\n\n- first\n- second\n");
1855    }
1856
1857    /// docling-core 2.92 (#721): a single newline inside an item's text is a
1858    /// GFM hard line break, a blank line stays a paragraph break, and a heading
1859    /// collapses its newline to a space. Nested-table dumps stay verbatim.
1860    #[test]
1861    fn single_newlines_become_gfm_hard_line_breaks() {
1862        let mut doc = DoclingDocument::new("t");
1863        doc.push(Node::Heading {
1864            level: 1,
1865            text: "Hello\nWorld".into(),
1866        });
1867        doc.push(Node::Paragraph {
1868            text: "line one\nline two\n\npara two".into(),
1869        });
1870        doc.push(Node::ListItem {
1871            ordered: false,
1872            number: 1,
1873            first_in_list: true,
1874            text: "item\ncontinued".into(),
1875            level: 0,
1876            marker: None,
1877            location: None,
1878            dclx: None,
1879            href: None,
1880            layer: None,
1881        });
1882        doc.push(Node::TextDump("A1 B1 \n\n\nC1".into()));
1883        assert_eq!(
1884            doc.export_to_markdown(),
1885            "# Hello World\n\nline one  \nline two\n\npara two\n\n- item  \ncontinued\n\nA1 B1 \n\n\nC1\n"
1886        );
1887    }
1888
1889    /// docling-core#540: inside a rich table cell a heading is plain text;
1890    /// docling-core#724: a field region renders only its items' key/value text.
1891    #[test]
1892    fn table_cell_mode_and_field_regions() {
1893        let mut doc = DoclingDocument::new("t");
1894        doc.push(Node::Heading {
1895            level: 2,
1896            text: "A  text".into(),
1897        });
1898        doc.push(Node::Paragraph {
1899            text: "body".into(),
1900        });
1901        assert_eq!(to_markdown_table_cell(&doc, false), "A  text\n\nbody");
1902        assert_eq!(doc.export_to_markdown(), "## A  text\n\nbody\n");
1903
1904        let mut doc = DoclingDocument::new("f");
1905        doc.push(Node::FieldRegion {
1906            items: vec![crate::FieldItem {
1907                marker: None,
1908                key: Some("Name:".into()),
1909                value: Some("John Doe".into()),
1910                value_kind: None,
1911            }],
1912        });
1913        assert_eq!(doc.export_to_markdown(), "Name:\n\nJohn Doe\n");
1914    }
1915
1916    #[test]
1917    fn strict_renders_recovered_links_legacy_does_not() {
1918        let mut doc = DoclingDocument::new("cv");
1919        doc.add_paragraph("Find me on LinkedIn or GitHub.");
1920        doc.links = vec![
1921            ("LinkedIn".into(), "https://www.linkedin.com/in/x/".into()),
1922            ("GitHub".into(), "https://github.com/x/".into()),
1923        ];
1924        // Legacy/docling mode: links are left untouched (conformance preserved).
1925        assert_eq!(doc.export_to_markdown(), "Find me on LinkedIn or GitHub.\n");
1926        // Strict mode: anchors become Markdown links.
1927        assert_eq!(
1928            doc.export_to_markdown_with(true),
1929            "Find me on [LinkedIn](https://www.linkedin.com/in/x/) or [GitHub](https://github.com/x/).\n"
1930        );
1931    }
1932
1933    #[test]
1934    fn strict_links_match_escaped_anchor_and_consume_in_order() {
1935        let mut doc = DoclingDocument::new("d");
1936        // The PDF assembler HTML-escapes prose, so by serialization time the body
1937        // already carries `&amp;`; the anchor is stored un-escaped. The matcher must
1938        // escape the anchor to find it. Two identical anchors link in document order.
1939        doc.add_paragraph("AI &amp; ML here, and issues here, then issues there.");
1940        doc.links = vec![
1941            ("AI & ML".into(), "https://a/".into()),
1942            ("issues".into(), "https://first/".into()),
1943            ("issues".into(), "https://second/".into()),
1944        ];
1945        assert_eq!(
1946            doc.export_to_markdown_with(true),
1947            "[AI &amp; ML](https://a/) here, and [issues](https://first/) here, then [issues](https://second/) there.\n"
1948        );
1949    }
1950
1951    /// docling-core#698: the referenced-image destination is percent-encoded —
1952    /// upstream's own case table (paths, Windows flavours, UNC, URLs) plus
1953    /// idempotency on the encoded result.
1954    #[test]
1955    fn referenced_image_destinations_are_escaped() {
1956        let cases = [
1957            (
1958                "doc_artifacts/image_000001_ab12.png",
1959                "doc_artifacts/image_000001_ab12.png",
1960            ),
1961            (
1962                "My Report_artifacts/img.png",
1963                "My%20Report_artifacts/img.png",
1964            ),
1965            ("artifacts/img (1).png", "artifacts/img%20%281%29.png"),
1966            ("100%_scale/a#b?c.png", "100%_scale/a%23b%3Fc.png"),
1967            ("/home/a b/img.png", "/home/a%20b/img.png"),
1968            (
1969                "My Report_artifacts\\img.png",
1970                "My%20Report_artifacts/img.png",
1971            ),
1972            (
1973                "C:/Users/me/My Docs/img.png",
1974                "file:///C:/Users/me/My%20Docs/img.png",
1975            ),
1976            ("C:\\Users\\me\\img.png", "file:///C:/Users/me/img.png"),
1977            (
1978                "//server/share/My Docs/img.png",
1979                "file://server/share/My%20Docs/img.png",
1980            ),
1981            ("\\\\server\\share\\img.png", "file://server/share/img.png"),
1982            ("file:///home/a b/img.png", "file:///home/a%20b/img.png"),
1983            (
1984                "s3://bucket/My Report_artifacts/img.png",
1985                "s3://bucket/My%20Report_artifacts/img.png",
1986            ),
1987            (
1988                "https://example.com:8080/a b.png?w=1&h=2#frag",
1989                "https://example.com:8080/a%20b.png?w=1&h=2#frag",
1990            ),
1991            (
1992                "https://example.com/img (1).png",
1993                "https://example.com/img%20%281%29.png",
1994            ),
1995            ("caf\u{e9}/im\u{e4}ge.png", "caf%C3%A9/im%C3%A4ge.png"),
1996        ];
1997        for (input, expected) in cases {
1998            assert_eq!(escape_uri_path(input), expected, "input {input:?}");
1999            assert_eq!(
2000                escape_uri_path(expected),
2001                expected,
2002                "idempotent {expected:?}"
2003            );
2004        }
2005        // The whole marker, through the referenced-image export.
2006        let mut doc = DoclingDocument::new("t");
2007        doc.push(Node::Picture {
2008            caption: None,
2009            caption_href: None,
2010            image: Some(PictureImage {
2011                dpi: PictureImage::DEFAULT_DPI,
2012                mimetype: "image/png".into(),
2013                width: 1,
2014                height: 1,
2015                data: b"x".to_vec(),
2016                display_size: None,
2017            }),
2018            classification: None,
2019            description: None,
2020            caption_parent: Default::default(),
2021            caption_location: None,
2022        });
2023        let (md, files) = doc
2024            .export_to_markdown_with_images(ImageMode::Referenced, "My Report (final)_artifacts");
2025        assert!(
2026            md.contains("![Image](My%20Report%20%28final%29_artifacts/image_000000.png)"),
2027            "got:\n{md}"
2028        );
2029        // The file path handed back for writing stays unescaped.
2030        assert_eq!(files[0].0, "My Report (final)_artifacts/image_000000.png");
2031    }
2032
2033    /// Pictures the HTML backend folds into a list item print after the item
2034    /// line with plain newlines; a `<br>` newline in the item's own text is
2035    /// still a GFM hard line break.
2036    #[test]
2037    fn folded_list_item_pictures_keep_plain_newlines() {
2038        let doc = DoclingDocument::new("t");
2039        let ctx = Ctx::new(&doc, false, &MarkdownExportOptions::default());
2040        assert_eq!(
2041            list_item_text("Step\n<!-- image -->", &ctx),
2042            "Step\n<!-- image -->"
2043        );
2044        assert_eq!(
2045            list_item_text("Step\nAlt text\n<!-- image -->\n<!-- image -->", &ctx),
2046            "Step\nAlt text\n<!-- image -->\n<!-- image -->"
2047        );
2048        assert_eq!(
2049            list_item_text("line one\nline two", &ctx),
2050            "line one  \nline two"
2051        );
2052    }
2053
2054    /// docling-core#723: the header block is the leading run of rows on which a
2055    /// `column_header` cell starts, flattened per column with " - ".
2056    #[test]
2057    fn stacked_header_rows_flatten_into_one() {
2058        let mut t = Table {
2059            rows: vec![
2060                vec!["".into(), "% of Total".into(), "% of Total".into()],
2061                vec!["class".into(), "Train".into(), "Test".into()],
2062                vec!["Caption".into(), "2.04".into(), "1.77".into()],
2063            ],
2064            ..Default::default()
2065        };
2066        t.structure = Some(TableStructure {
2067            header_row: vec![true, true, false],
2068            col_continuation: vec![
2069                vec![false, false, true],
2070                vec![false, false, false],
2071                vec![false, false, false],
2072            ],
2073            ..Default::default()
2074        });
2075        assert_eq!(t.header_row_count(), 2);
2076        assert_eq!(
2077            render_table(&t, true),
2078            "| class | % of Total - Train | % of Total - Test |\n| - | - | - |\n| Caption | 2.04 | 1.77 |"
2079        );
2080        // padded: widths from the flattened header, alignment from body rows
2081        assert_eq!(
2082            render_table(&t, false),
2083            "| class   |   % of Total - Train |   % of Total - Test |\n\
2084             |---------|----------------------|---------------------|\n\
2085             | Caption |                 2.04 |                1.77 |"
2086        );
2087    }
2088
2089    /// A header spanning two rows is repeated into the second row by the grid;
2090    /// that row is not a header row unless another header cell starts there.
2091    #[test]
2092    fn vertically_spanning_header_does_not_extend_the_block() {
2093        let mut t = Table {
2094            rows: vec![
2095                vec!["Name".into(), "Value".into()],
2096                vec!["Name".into(), "1".into()],
2097                vec!["x".into(), "2".into()],
2098            ],
2099            ..Default::default()
2100        };
2101        t.structure = Some(TableStructure {
2102            col_header: vec![vec![true, true], vec![true, false], vec![false, false]],
2103            row_continuation: vec![vec![false, false], vec![true, false], vec![false, false]],
2104            ..Default::default()
2105        });
2106        assert_eq!(t.header_row_count(), 1);
2107        assert_eq!(
2108            render_table(&t, true),
2109            "| Name | Value |\n| - | - |\n| Name | 1 |\n| x | 2 |"
2110        );
2111    }
2112
2113    /// Flags that begin on a later row promote nothing: every row stays in the
2114    /// body under an empty header row (tabulate's `headers=["", ""]`).
2115    #[test]
2116    fn header_flags_not_on_row_zero_keep_all_rows_in_the_body() {
2117        let mut t = Table {
2118            rows: vec![
2119                vec!["1".into(), "2".into()],
2120                vec!["a".into(), "b".into()],
2121                vec!["333".into(), "4".into()],
2122            ],
2123            ..Default::default()
2124        };
2125        t.structure = Some(TableStructure {
2126            header_row: vec![false, true, false],
2127            ..Default::default()
2128        });
2129        assert_eq!(t.header_row_count(), 0);
2130        assert_eq!(
2131            render_table(&t, false),
2132            "|     |    |\n|-----|----|\n| 1   | 2  |\n| a   | b  |\n| 333 | 4  |"
2133        );
2134    }
2135
2136    /// A pivot table's row headers (`<th rowspan>`) carry `row_header`, not
2137    /// `column_header` (docling#4216), so the data row beside them is not
2138    /// pulled into the header block — what this port used to reach with a
2139    /// deviation now falls out of the flags themselves.
2140    #[test]
2141    fn pivot_row_headers_do_not_extend_the_header() {
2142        let mut t = Table {
2143            rows: vec![
2144                vec!["Year".into(), "Month".into()],
2145                vec!["2025".into(), "January".into()],
2146                vec!["2025".into(), "February".into()],
2147            ],
2148            ..Default::default()
2149        };
2150        t.structure = Some(TableStructure {
2151            col_header: vec![vec![true, true], vec![false, false], vec![false, false]],
2152            row_header: vec![vec![false, false], vec![true, false], vec![true, false]],
2153            row_continuation: vec![vec![false, false], vec![false, false], vec![true, false]],
2154            ..Default::default()
2155        });
2156        assert_eq!(t.header_row_count(), 1);
2157        assert_eq!(
2158            render_table(&t, true),
2159            "| Year | Month |\n| - | - |\n| 2025 | January |\n| 2025 | February |"
2160        );
2161    }
2162
2163    /// No `column_header` anywhere (first-class cells without flags) → row 0
2164    /// stays the header, as before.
2165    #[test]
2166    fn unflagged_cells_keep_row_zero_as_header() {
2167        let mut t = Table {
2168            rows: vec![vec!["h".into()], vec!["d".into()]],
2169            ..Default::default()
2170        };
2171        t.cells = Some(
2172            [(0usize, "h"), (1, "d")]
2173                .into_iter()
2174                .map(|(r, text)| TableCell {
2175                    text: text.into(),
2176                    bbox: None,
2177                    start_row: r,
2178                    start_col: 0,
2179                    row_span: 1,
2180                    col_span: 1,
2181                    column_header: false,
2182                    row_header: false,
2183                    row_section: false,
2184                })
2185                .collect(),
2186        );
2187        assert_eq!(t.header_row_count(), 1);
2188        assert_eq!(render_table(&t, true), "| h |\n| - |\n| d |");
2189    }
2190
2191    /// A table with first-class 1×1 cells, `column_header` where `ched` says.
2192    fn flagged_table(rows: &[&[&str]], ched: impl Fn(usize, usize) -> bool) -> Table {
2193        let mut cells = Vec::new();
2194        for (r, row) in rows.iter().enumerate() {
2195            for (c, text) in row.iter().enumerate() {
2196                cells.push(TableCell {
2197                    text: (*text).into(),
2198                    bbox: None,
2199                    start_row: r,
2200                    start_col: c,
2201                    row_span: 1,
2202                    col_span: 1,
2203                    column_header: ched(r, c),
2204                    row_header: false,
2205                    row_section: false,
2206                });
2207            }
2208        }
2209        Table {
2210            rows: rows
2211                .iter()
2212                .map(|row| row.iter().map(|t| (*t).to_string()).collect())
2213                .collect(),
2214            cells: Some(cells),
2215            ..Default::default()
2216        }
2217    }
2218
2219    /// #604 (docling-core#766, 2.97): a row whose first-column label is a
2220    /// `column_header` but whose other cells carry body text stops the header
2221    /// block. Expected outputs are docling-core 2.99's, byte for byte.
2222    #[test]
2223    fn rows_with_body_text_stop_the_header_block() {
2224        // The issue's form: labels flagged on every row, a real header on top.
2225        let t = flagged_table(
2226            &[
2227                &["Item", "Q1", "Q2"],
2228                &["Sales", "10", "20"],
2229                &["Costs", "5", "7"],
2230            ],
2231            |r, c| r == 0 || c == 0,
2232        );
2233        assert_eq!(t.header_row_count(), 1);
2234        assert_eq!(
2235            render_table(&t, false),
2236            "| Item   |   Q1 |   Q2 |\n\
2237             |--------|------|------|\n\
2238             | Sales  |   10 |   20 |\n\
2239             | Costs  |    5 |    7 |"
2240        );
2241        // Labels on every row and no header row: row 0 stops, later rows are
2242        // flagged → nothing promotable, the body keeps every row.
2243        let t = flagged_table(&[&["Sales", "10", "20"], &["Costs", "5", "7"]], |_, c| {
2244            c == 0
2245        });
2246        assert_eq!(t.header_row_count(), 0);
2247        assert_eq!(
2248            render_table(&t, false),
2249            "|       |    |    |\n\
2250             |-------|----|----|\n\
2251             | Sales | 10 | 20 |\n\
2252             | Costs |  5 |  7 |"
2253        );
2254        let t = flagged_table(
2255            &[&["a", "1", "2"], &["b", "3", "4"], &["c", "5", "6"]],
2256            |r, c| c == 0 && r < 2,
2257        );
2258        assert_eq!(t.header_row_count(), 0);
2259        // Row 0 stops on its body text, but no later row carries a flag:
2260        // row 0 is the header anyway, as for an unflagged table.
2261        let t = flagged_table(&[&["a", "1", "2"], &["b", "3", "4"]], |r, c| {
2262            r == 0 && c == 0
2263        });
2264        assert_eq!(t.header_row_count(), 1);
2265        assert_eq!(
2266            render_table(&t, false),
2267            "| a   |   1 |   2 |\n\
2268             |-----|-----|-----|\n\
2269             | b   |   3 |   4 |"
2270        );
2271        // Blank cells beside a flagged label are not body text.
2272        let t = flagged_table(&[&["Item", "", ""], &["x", "1", "2"]], |_, c| c == 0);
2273        assert_eq!(t.header_row_count(), 1);
2274        assert_eq!(
2275            render_table(&t, false),
2276            "| Item   |    |    |\n\
2277             |--------|----|----|\n\
2278             | x      |  1 |  2 |"
2279        );
2280    }
2281
2282    #[test]
2283    fn renders_compact_table() {
2284        let mut doc = DoclingDocument::new("t");
2285        // The compact form is opt-in (the PDF backend sets it); default output uses
2286        // the padded GitHub serializer (covered by the regression fixtures).
2287        doc.compact_tables = true;
2288        doc.push(Node::Table(Table {
2289            footnotes: Vec::new(),
2290            rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
2291            location: None,
2292            structure: None,
2293            cell_blocks: None,
2294            cells: None,
2295            caption: None,
2296            caption_parent: Default::default(),
2297            caption_location: None,
2298        }));
2299        let md = doc.export_to_markdown();
2300        assert_eq!(md, "| a | b |\n| - | - |\n| 1 | 2 |\n");
2301    }
2302
2303    #[test]
2304    fn renders_padded_github_table_by_default() {
2305        let mut doc = DoclingDocument::new("t");
2306        doc.push(Node::Table(Table {
2307            footnotes: Vec::new(),
2308            rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
2309            location: None,
2310            structure: None,
2311            cell_blocks: None,
2312            cells: None,
2313            caption: None,
2314            caption_parent: Default::default(),
2315            caption_location: None,
2316        }));
2317        let md = doc.export_to_markdown();
2318        // Numeric data columns are right-aligned; columns padded to header+2.
2319        assert_eq!(md, "|   a |   b |\n|-----|-----|\n|   1 |   2 |\n");
2320    }
2321
2322    #[test]
2323    fn strict_unescapes_inline_underscores_legacy_keeps_them() {
2324        let mut doc = DoclingDocument::new("t");
2325        doc.add_heading(1, "a\\_b");
2326        doc.add_paragraph("x\\_y");
2327        doc.push(Node::ListItem {
2328            ordered: false,
2329            number: 1,
2330            first_in_list: true,
2331            text: "i\\_j".into(),
2332            level: 0,
2333            marker: None,
2334            location: None,
2335            dclx: None,
2336            href: None,
2337            layer: None,
2338        });
2339        // Legacy reproduces docling's `\_` escaping byte-for-byte.
2340        assert_eq!(doc.export_to_markdown(), "# a\\_b\n\nx\\_y\n\n- i\\_j\n");
2341        // Strict prefers literal underscores (Rust-only readability mode).
2342        assert_eq!(doc.export_to_markdown_with(true), "# a_b\n\nx_y\n\n- i_j\n");
2343    }
2344
2345    /// Drive a document's nodes through [`MarkdownStreamer`] in the given page
2346    /// splits and assert the concatenated chunks equal the buffered serializer.
2347    fn assert_stream_matches(
2348        doc: &DoclingDocument,
2349        strict: bool,
2350        images: ImageMode,
2351        splits: &[usize],
2352    ) {
2353        let (want, want_artifacts) = to_markdown_images(doc, strict, images, "artifacts");
2354        let mut streamer =
2355            MarkdownStreamer::with_artifacts(strict, images, doc.compact_tables, "artifacts")
2356                .with_page_break_placeholder(doc.page_break_placeholder.clone());
2357        let mut got = String::new();
2358        let mut got_artifacts = Vec::new();
2359        let mut start = 0;
2360        for &end in splits {
2361            // Links only matter in strict mode; feed them all with the first batch
2362            // that has content (document order is preserved by the queue).
2363            let links = if start == 0 {
2364                doc.links.as_slice()
2365            } else {
2366                &[]
2367            };
2368            got.push_str(&streamer.push(&doc.nodes[start..end], links));
2369            // Referenced mode: drain per push, as a real caller writing files
2370            // page by page would — numbering must continue across drains.
2371            got_artifacts.extend(streamer.take_artifacts());
2372            start = end;
2373        }
2374        got.push_str(&streamer.push(
2375            &doc.nodes[start..],
2376            if start == 0 {
2377                doc.links.as_slice()
2378            } else {
2379                &[]
2380            },
2381        ));
2382        got_artifacts.extend(streamer.take_artifacts());
2383        got.push_str(&streamer.finish());
2384        assert_eq!(
2385            got, want,
2386            "streamed output diverged (splits={splits:?}, strict={strict})"
2387        );
2388        assert_eq!(
2389            got_artifacts, want_artifacts,
2390            "streamed artifacts diverged (splits={splits:?}, strict={strict})"
2391        );
2392    }
2393
2394    #[test]
2395    fn streaming_is_byte_identical_to_buffered() {
2396        let mut doc = DoclingDocument::new("d");
2397        doc.add_heading(1, "Title");
2398        doc.add_paragraph("First paragraph.");
2399        doc.push(Node::ListItem {
2400            ordered: false,
2401            number: 1,
2402            first_in_list: true,
2403            text: "a".into(),
2404            level: 0,
2405            marker: None,
2406            location: None,
2407            dclx: None,
2408            href: None,
2409            layer: None,
2410        });
2411        doc.push(Node::ListItem {
2412            ordered: false,
2413            number: 2,
2414            first_in_list: false,
2415            text: "b".into(),
2416            level: 0,
2417            marker: None,
2418            location: None,
2419            dclx: None,
2420            href: None,
2421            layer: None,
2422        });
2423        doc.push(Node::Code {
2424            language: Some("rust".into()),
2425            text: "let x = 1;".into(),
2426            orig: None,
2427            pretty: None,
2428        });
2429        doc.push(Node::Table(Table {
2430            footnotes: Vec::new(),
2431            rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
2432            location: None,
2433            structure: None,
2434            cell_blocks: None,
2435            cells: None,
2436            caption: None,
2437            caption_parent: Default::default(),
2438            caption_location: None,
2439        }));
2440        doc.push(Node::Picture {
2441            caption: Some("Fig 1".into()),
2442            caption_href: None,
2443            image: Some(PictureImage {
2444                dpi: PictureImage::DEFAULT_DPI,
2445                mimetype: "image/png".into(),
2446                width: 2,
2447                height: 2,
2448                data: b"png-one".to_vec(),
2449                display_size: None,
2450            }),
2451            classification: None,
2452            description: None,
2453            caption_parent: Default::default(),
2454            caption_location: None,
2455        });
2456        doc.add_paragraph("Last paragraph.");
2457        // A second embedded picture, so referenced mode must keep numbering
2458        // (`image_000001`) across chunk boundaries.
2459        doc.push(Node::Picture {
2460            caption: None,
2461            caption_href: None,
2462            image: Some(PictureImage {
2463                dpi: PictureImage::DEFAULT_DPI,
2464                mimetype: "image/png".into(),
2465                width: 2,
2466                height: 2,
2467                data: b"png-two".to_vec(),
2468                display_size: None,
2469            }),
2470            classification: None,
2471            description: None,
2472            caption_parent: Default::default(),
2473            caption_location: None,
2474        });
2475
2476        // A run of list items must never straddle a split, so try splits that fall
2477        // on safe block boundaries (the streaming PDF assembler guarantees this).
2478        for &strict in &[false, true] {
2479            for &images in &[
2480                ImageMode::Placeholder,
2481                ImageMode::Embedded,
2482                ImageMode::Referenced,
2483            ] {
2484                for splits in [&[][..], &[1][..], &[2][..], &[4][..], &[1, 4, 6, 7][..]] {
2485                    assert_stream_matches(&doc, strict, images, splits);
2486                }
2487            }
2488        }
2489    }
2490
2491    #[test]
2492    fn streaming_applies_recovered_links_in_strict_mode() {
2493        let mut doc = DoclingDocument::new("d");
2494        doc.add_paragraph("See LinkedIn for details.");
2495        doc.add_paragraph("And GitHub too.");
2496        doc.links = vec![
2497            ("LinkedIn".into(), "https://lnkd/".into()),
2498            ("GitHub".into(), "https://gh/".into()),
2499        ];
2500        // The second anchor lives in the second block, so it must be carried across
2501        // the page boundary and placed when that block streams out.
2502        assert_stream_matches(&doc, true, ImageMode::Placeholder, &[1]);
2503    }
2504
2505    /// A three-page document with one empty page in the middle and page
2506    /// markers of both kinds, as the backends emit them.
2507    fn paged_doc() -> DoclingDocument {
2508        let mut doc = DoclingDocument::new("p");
2509        doc.push(Node::PageInfo {
2510            page_no: 1,
2511            width: 100.0,
2512            height: 100.0,
2513        });
2514        doc.add_heading(1, "Title");
2515        doc.add_paragraph("Page one.");
2516        // Page two: a marker plus furniture only — renders nothing.
2517        doc.push(Node::PageBreak);
2518        doc.push(Node::PageInfo {
2519            page_no: 2,
2520            width: 100.0,
2521            height: 100.0,
2522        });
2523        doc.push(Node::PageFurniture {
2524            footer: true,
2525            location: [0, 500, 511, 511],
2526            text: "2".into(),
2527        });
2528        doc.push(Node::PageBreak);
2529        doc.push(Node::PageInfo {
2530            page_no: 3,
2531            width: 100.0,
2532            height: 100.0,
2533        });
2534        doc.add_paragraph("Page three.");
2535        // A trailing boundary with nothing after it.
2536        doc.push(Node::PageBreak);
2537        doc
2538    }
2539
2540    #[test]
2541    fn page_break_placeholder_lands_between_pages_only() {
2542        let mut doc = paged_doc();
2543        // Off by default: docling's Markdown carries no page breaks.
2544        assert_eq!(
2545            doc.export_to_markdown(),
2546            "# Title\n\nPage one.\n\nPage three.\n"
2547        );
2548        doc.page_break_placeholder = Some("<!-- page break -->".into());
2549        // One break for the 1→3 transition (the empty page 2 and the doubled
2550        // PageBreak+PageInfo markers collapse), none before the first block,
2551        // none for the trailing boundary.
2552        assert_eq!(
2553            doc.export_to_markdown(),
2554            "# Title\n\nPage one.\n\n<!-- page break -->\n\nPage three.\n"
2555        );
2556        // An empty placeholder is still a (blank) part, as upstream's
2557        // `str.replace(marker, "")` leaves the delimiters around it.
2558        doc.page_break_placeholder = Some(String::new());
2559        assert_eq!(
2560            doc.export_to_markdown(),
2561            "# Title\n\nPage one.\n\n\n\nPage three.\n"
2562        );
2563    }
2564
2565    #[test]
2566    fn page_break_placeholder_never_leads_a_single_page() {
2567        let mut doc = DoclingDocument::new("one");
2568        doc.page_break_placeholder = Some("---".into());
2569        doc.push(Node::PageBreak);
2570        doc.push(Node::PageInfo {
2571            page_no: 1,
2572            width: 10.0,
2573            height: 10.0,
2574        });
2575        doc.add_paragraph("Only page.");
2576        assert_eq!(doc.export_to_markdown(), "Only page.\n");
2577        // Two boundaries with no content between them: still one break.
2578        doc.push(Node::PageBreak);
2579        doc.push(Node::PageBreak);
2580        doc.add_paragraph("Next.");
2581        assert_eq!(doc.export_to_markdown(), "Only page.\n\n---\n\nNext.\n");
2582    }
2583
2584    #[test]
2585    fn page_break_placeholder_streams_byte_identical() {
2586        let mut doc = paged_doc();
2587        doc.page_break_placeholder = Some("<!-- page break -->".into());
2588        // Split at every page marker (how the PDF pipeline pushes page batches)
2589        // and at odd places inside a page: the pending break must survive a
2590        // push that renders nothing (page two) and land on page three's block.
2591        for splits in [
2592            &[3usize][..],
2593            &[3, 6],
2594            &[3, 6, 8],
2595            &[1, 2, 3, 4, 5, 6, 7, 8, 9],
2596            &[8],
2597        ] {
2598            assert_stream_matches(&doc, false, ImageMode::Placeholder, splits);
2599            assert_stream_matches(&doc, true, ImageMode::Placeholder, splits);
2600        }
2601    }
2602
2603    #[test]
2604    fn strict_tightens_punctuation_spacing_legacy_keeps_it() {
2605        let mut doc = DoclingDocument::new("t");
2606        doc.add_paragraph("see [ 37 , 36 ] and ( x ) .");
2607        // Legacy keeps docling's spacing byte-for-byte.
2608        assert_eq!(doc.export_to_markdown(), "see [ 37 , 36 ] and ( x ) .\n");
2609        // Strict tightens punctuation for readable Markdown.
2610        assert_eq!(doc.export_to_markdown_with(true), "see [37, 36] and (x).\n");
2611    }
2612
2613    // ----- MarkdownExportOptions (#599) ------------------------------------
2614
2615    /// A document touching every option: furniture, a comment, a hidden
2616    /// sheet group, a picture with nested text, escaped text.
2617    fn options_doc() -> DoclingDocument {
2618        use crate::document::ContentLayer;
2619        let mut doc = DoclingDocument::new("opts");
2620        doc.push(Node::PageFurniture {
2621            footer: false,
2622            location: [0, 0, 0, 0],
2623            text: "FAX COVER \\_ R&amp;D".into(),
2624        });
2625        doc.add_heading(1, "R&amp;D \\_ report");
2626        doc.add_paragraph("Body a\\_b &lt;tag&gt;");
2627        doc.push(Node::Picture {
2628            caption: Some("Figure 1".into()),
2629            caption_href: None,
2630            image: None,
2631            classification: None,
2632            description: None,
2633            caption_parent: Default::default(),
2634            caption_location: None,
2635        });
2636        doc.push(Node::PictureChildren(vec![
2637            Node::Paragraph {
2638                text: "Field: Name".into(),
2639            },
2640            Node::Paragraph {
2641                text: "Field: Date".into(),
2642            },
2643        ]));
2644        doc.push(Node::CommentSection {
2645            name: "comment-1".into(),
2646            text: "[author: A]: note".into(),
2647            refs_note_text: false,
2648            grouped: true,
2649        });
2650        doc.push(Node::Group {
2651            label: "section".into(),
2652            name: Some("sheet: Hidden".into()),
2653            layer: Some(ContentLayer::Invisible),
2654            children: vec![Node::Paragraph {
2655                text: "hidden cell".into(),
2656            }],
2657        });
2658        doc.push(Node::Furniture {
2659            layer: ContentLayer::Furniture,
2660            inner: Box::new(Node::Paragraph {
2661                text: "Page 1 of 2".into(),
2662            }),
2663        });
2664        doc.push(Node::FurnitureText {
2665            label: "page_footer".into(),
2666            text: "footer line".into(),
2667        });
2668        doc
2669    }
2670
2671    /// The defaults are docling's: the options export is the plain export,
2672    /// byte for byte — and the plain export is unchanged (furniture, notes,
2673    /// hidden sheets and picture children out; escaping kept).
2674    #[test]
2675    fn default_options_are_the_default_export() {
2676        let doc = options_doc();
2677        let plain = doc.export_to_markdown();
2678        assert_eq!(
2679            plain,
2680            "# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n"
2681        );
2682        let (with_default, artifacts) =
2683            doc.export_to_markdown_with_options(&MarkdownExportOptions::default());
2684        assert_eq!(with_default, plain);
2685        assert!(artifacts.is_empty());
2686    }
2687
2688    #[test]
2689    fn layers_render_furniture_notes_and_hidden_sheets() {
2690        use crate::document::ContentLayer;
2691        let doc = options_doc();
2692        let md = |layers: ContentLayers| {
2693            doc.export_to_markdown_with_options(&MarkdownExportOptions {
2694                layers,
2695                ..MarkdownExportOptions::default()
2696            })
2697            .0
2698        };
2699        assert_eq!(
2700            md(ContentLayers::BODY.with(ContentLayer::Furniture)),
2701            "FAX COVER \\_ R&amp;D\n\n# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n\nPage 1 of 2\n\nfooter line\n",
2702            "a page header, a furniture-wrapped paragraph and a furniture text item render as paragraphs"
2703        );
2704        assert_eq!(
2705            md(ContentLayers::BODY.with(ContentLayer::Notes)),
2706            "# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n\n[author: A]: note\n"
2707        );
2708        assert_eq!(
2709            md(ContentLayers::BODY.with(ContentLayer::Invisible)),
2710            "# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n\nhidden cell\n",
2711            "a hidden sheet's items sit on the sheet's layer"
2712        );
2713        assert_eq!(
2714            md(ContentLayers::NONE.with(ContentLayer::Furniture)),
2715            "FAX COVER \\_ R&amp;D\n\nPage 1 of 2\n\nfooter line\n",
2716            "without the body layer only the furniture prints"
2717        );
2718        assert_eq!(md(ContentLayers::NONE), "");
2719        assert_eq!(
2720            md(ContentLayers::ALL),
2721            "FAX COVER \\_ R&amp;D\n\n# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n\n[author: A]: note\n\nhidden cell\n\nPage 1 of 2\n\nfooter line\n"
2722        );
2723    }
2724
2725    /// A list item carries its own layer: a furniture list renders with the
2726    /// furniture layer and stays out of the body export.
2727    #[test]
2728    fn layers_apply_to_list_items() {
2729        use crate::document::ContentLayer;
2730        let mut doc = DoclingDocument::new("lists");
2731        doc.push(Node::ListItem {
2732            ordered: false,
2733            number: 1,
2734            first_in_list: true,
2735            text: "nav link".into(),
2736            level: 0,
2737            marker: None,
2738            location: None,
2739            dclx: None,
2740            href: None,
2741            layer: Some(ContentLayer::Furniture),
2742        });
2743        doc.add_paragraph("body");
2744        assert_eq!(doc.export_to_markdown(), "body\n");
2745        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2746            layers: ContentLayers::BODY.with(ContentLayer::Furniture),
2747            ..MarkdownExportOptions::default()
2748        });
2749        assert_eq!(md, "- nav link\n\nbody\n");
2750    }
2751
2752    #[test]
2753    fn traverse_pictures_prints_the_nested_text_after_the_picture() {
2754        let doc = options_doc();
2755        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2756            traverse_pictures: true,
2757            ..MarkdownExportOptions::default()
2758        });
2759        assert_eq!(
2760            md,
2761            "# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n\nField: Name\n\nField: Date\n"
2762        );
2763    }
2764
2765    #[test]
2766    fn escaping_can_be_turned_off_per_kind() {
2767        let doc = options_doc();
2768        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2769            escape_html: false,
2770            ..MarkdownExportOptions::default()
2771        });
2772        assert_eq!(
2773            md,
2774            "# R&D \\_ report\n\nBody a\\_b <tag>\n\nFigure 1\n\n<!-- image -->\n"
2775        );
2776        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2777            escape_underscores: false,
2778            ..MarkdownExportOptions::default()
2779        });
2780        assert_eq!(
2781            md,
2782            "# R&amp;D _ report\n\nBody a_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n"
2783        );
2784        // Code and table cells are never touched (upstream leaves them
2785        // unescaped too, so nothing was escaped there to undo).
2786        let mut doc = DoclingDocument::new("code");
2787        doc.push(Node::Code {
2788            language: None,
2789            text: "a &amp; b \\_ c".into(),
2790            orig: None,
2791            pretty: None,
2792        });
2793        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2794            escape_html: false,
2795            escape_underscores: false,
2796            ..MarkdownExportOptions::default()
2797        });
2798        assert_eq!(md, "```\na &amp; b \\_ c\n```\n");
2799    }
2800
2801    /// #859 (docling-core 2.103.1): a fence out-lengths every backtick run
2802    /// in the block and a span its text, so neither can be closed early; the
2803    /// readers of the baked spans take the longer form back.
2804    #[test]
2805    fn fences_and_spans_out_length_inner_backticks() {
2806        let code = |text: &str| Node::Code {
2807            language: Some("md".into()),
2808            text: text.into(),
2809            orig: None,
2810            pretty: None,
2811        };
2812        let fence_of = |text: &str| {
2813            let mut doc = DoclingDocument::new("d");
2814            doc.push(code(text));
2815            (doc.export_to_markdown(), doc.export_to_markdown_with(true))
2816        };
2817        assert_eq!(fence_of("x").0, "```\nx\n```\n");
2818        assert_eq!(fence_of("a ` b").0, "```\na ` b\n```\n");
2819        assert_eq!(fence_of("a ``` b").0, "````\na ``` b\n````\n");
2820        assert_eq!(
2821            fence_of("a ``` b ```` c"),
2822            (
2823                "`````\na ``` b ```` c\n`````\n".to_string(),
2824                "`````md\na ``` b ```` c\n`````\n".to_string()
2825            )
2826        );
2827        assert_eq!(code_span("x"), "`x`");
2828        assert_eq!(code_span("a`b"), "``a`b``");
2829        assert_eq!(code_span("a ``` b"), "````a ``` b````");
2830        assert_eq!(code_span("`x"), "`` `x ``");
2831        assert_eq!(code_span("x`"), "`` x` ``");
2832        for (span, text) in [
2833            ("`x`", "x"),
2834            ("``a`b``", "a`b"),
2835            ("`` `x ``", "`x"),
2836            ("````a ``` b````", "a ``` b"),
2837        ] {
2838            assert_eq!(
2839                plain_inline(&format!("see {span} here")),
2840                format!("see {text} here")
2841            );
2842        }
2843        // The plain export prints a folded fence's body unfenced, whatever
2844        // the fence's length.
2845        let mut doc = DoclingDocument::new("d");
2846        doc.push(code("a ``` b"));
2847        assert_eq!(doc.export_to_text(), "a ``` b");
2848        // Streamed and buffered agree on the longer fence.
2849        let mut doc = DoclingDocument::new("d");
2850        doc.add_paragraph("Before.");
2851        doc.push(code("a ``` b ```` c"));
2852        doc.add_paragraph("After.");
2853        assert_stream_matches(&doc, false, ImageMode::Placeholder, &[1, 2]);
2854        assert_stream_matches(&doc, true, ImageMode::Placeholder, &[1, 2]);
2855    }
2856
2857    /// #858: a `mermaid` code item extracted from a drawing takes the
2858    /// place of the drawing's `<!-- image -->` in the placeholder mode —
2859    /// in the buffered export and in a stream cut anywhere around the pair;
2860    /// a picture nothing follows keeps its marker.
2861    #[test]
2862    fn mermaid_code_item_replaces_its_picture_placeholder() {
2863        let picture = || Node::Picture {
2864            caption: None,
2865            caption_href: None,
2866            image: None,
2867            classification: None,
2868            description: None,
2869            caption_parent: Default::default(),
2870            caption_location: None,
2871        };
2872        let mut doc = DoclingDocument::new("d");
2873        doc.add_paragraph("Before.");
2874        doc.push(picture());
2875        doc.push(Node::Code {
2876            language: Some("mermaid".into()),
2877            text: "flowchart TD\n  A --> B".into(),
2878            orig: None,
2879            pretty: None,
2880        });
2881        doc.add_paragraph("After.");
2882        doc.push(picture());
2883        let md = doc.export_to_markdown();
2884        assert_eq!(
2885            md,
2886            "Before.\n\n```mermaid\nflowchart TD\n  A --> B\n```\n\nAfter.\n\n<!-- image -->\n"
2887        );
2888        assert_eq!(doc.export_to_markdown_with(true), md);
2889        for splits in [&[1][..], &[2], &[1, 2], &[2, 3], &[1, 2, 3, 4]] {
2890            assert_stream_matches(&doc, false, ImageMode::Placeholder, splits);
2891            assert_stream_matches(&doc, true, ImageMode::Placeholder, splits);
2892        }
2893        // A stream that ends on the picture still prints it.
2894        let mut doc = DoclingDocument::new("d");
2895        doc.add_paragraph("Only.");
2896        doc.push(picture());
2897        assert_eq!(doc.export_to_markdown(), "Only.\n\n<!-- image -->\n");
2898        assert_stream_matches(&doc, false, ImageMode::Placeholder, &[1]);
2899        assert_stream_matches(&doc, false, ImageMode::Placeholder, &[2]);
2900    }
2901
2902    #[test]
2903    fn image_placeholder_is_configurable() {
2904        let doc = options_doc();
2905        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2906            image_placeholder: "[figure]".into(),
2907            ..MarkdownExportOptions::default()
2908        });
2909        assert!(md.contains("Figure 1\n\n[figure]\n"), "{md}");
2910        assert!(!md.contains("<!-- image -->"));
2911    }
2912
2913    /// #605: an empty `image_placeholder` prints nothing — no empty block, so
2914    /// no extra blank lines — while the page-break scan still counts the
2915    /// picture as an item. Every expectation is docling-core 2.99's own
2916    /// `export_to_markdown(image_placeholder="", page_break_placeholder=…)`
2917    /// (plus the trailing newline this port always writes).
2918    #[test]
2919    fn empty_image_placeholder_prints_nothing() {
2920        fn picture(caption: Option<&str>) -> Node {
2921            Node::Picture {
2922                caption: caption.map(Into::into),
2923                caption_href: None,
2924                image: None,
2925                classification: None,
2926                description: None,
2927                caption_parent: Default::default(),
2928                caption_location: None,
2929            }
2930        }
2931        fn page(no: usize) -> Node {
2932            Node::PageInfo {
2933                page_no: no,
2934                width: 100.0,
2935                height: 100.0,
2936            }
2937        }
2938        fn para(text: &str) -> Node {
2939            Node::Paragraph { text: text.into() }
2940        }
2941        let options = MarkdownExportOptions {
2942            image_placeholder: String::new(),
2943            ..MarkdownExportOptions::default()
2944        };
2945        // (nodes, page-break placeholder, expected)
2946        let cases: Vec<(Vec<Node>, Option<&str>, &str)> = vec![
2947            (
2948                vec![page(1), para("a"), picture(None), para("b")],
2949                None,
2950                "a\n\nb\n",
2951            ),
2952            (
2953                vec![page(1), para("a"), picture(Some("Figure 1")), para("b")],
2954                None,
2955                "a\n\nFigure 1\n\nb\n",
2956            ),
2957            (
2958                vec![
2959                    page(1),
2960                    para("a"),
2961                    page(2),
2962                    picture(None),
2963                    page(3),
2964                    para("b"),
2965                ],
2966                Some("<!-- pb -->"),
2967                "a\n\n<!-- pb -->\n\n<!-- pb -->\n\nb\n",
2968            ),
2969            (
2970                vec![
2971                    page(1),
2972                    para("a"),
2973                    page(2),
2974                    picture(None),
2975                    page(3),
2976                    para("b"),
2977                ],
2978                Some(""),
2979                "a\n\n\n\n\n\nb\n",
2980            ),
2981            (
2982                vec![page(1), picture(None), page(2), para("b")],
2983                Some("<!-- pb -->"),
2984                "<!-- pb -->\n\nb\n",
2985            ),
2986            (
2987                vec![page(1), picture(None), page(2), para("b")],
2988                Some(""),
2989                "\n\nb\n",
2990            ),
2991            (
2992                vec![page(1), para("a"), page(2), picture(None)],
2993                Some("<!-- pb -->"),
2994                "a\n\n<!-- pb -->\n",
2995            ),
2996        ];
2997        for (nodes, placeholder, expected) in cases {
2998            let mut doc = DoclingDocument::new("t");
2999            doc.page_break_placeholder = placeholder.map(Into::into);
3000            for node in nodes {
3001                doc.push(node);
3002            }
3003            let (md, _) = doc.export_to_markdown_with_options(&options);
3004            assert_eq!(md, expected, "page break {placeholder:?}");
3005            // The streamer agrees at every chunking, one node per push
3006            // included — an all-empty push still owes the next page break.
3007            for size in 1..=doc.nodes.len() {
3008                let mut streamer = MarkdownStreamer::new(false, ImageMode::Placeholder, false)
3009                    .with_export_options(&options)
3010                    .with_page_break_placeholder(placeholder.map(Into::into));
3011                let mut out = String::new();
3012                for chunk in doc.nodes.chunks(size) {
3013                    out.push_str(&streamer.push(chunk, &[]));
3014                }
3015                out.push_str(&streamer.finish());
3016                assert_eq!(
3017                    out, expected,
3018                    "chunks of {size}, page break {placeholder:?}"
3019                );
3020            }
3021        }
3022        // The default placeholder is untouched.
3023        let mut doc = DoclingDocument::new("t");
3024        doc.push(para("a"));
3025        doc.push(picture(None));
3026        doc.push(para("b"));
3027        assert_eq!(doc.export_to_markdown(), "a\n\n<!-- image -->\n\nb\n");
3028    }
3029
3030    /// The streamer takes the same options and its chunks concatenate to the
3031    /// buffered export.
3032    #[test]
3033    fn streamer_matches_buffered_export_with_options() {
3034        let doc = options_doc();
3035        let options = MarkdownExportOptions {
3036            layers: ContentLayers::ALL,
3037            traverse_pictures: true,
3038            escape_html: false,
3039            escape_underscores: false,
3040            image_placeholder: "(img)".into(),
3041            ..MarkdownExportOptions::default()
3042        };
3043        let (buffered, _) = doc.export_to_markdown_with_options(&options);
3044        let mut streamer = MarkdownStreamer::new(false, ImageMode::Placeholder, false)
3045            .with_export_options(&options);
3046        let mut out = String::new();
3047        for chunk in doc.nodes.chunks(2) {
3048            out.push_str(&streamer.push(chunk, &[]));
3049        }
3050        out.push_str(&streamer.finish());
3051        assert_eq!(out, buffered);
3052        assert!(
3053            buffered.contains("(img)") && buffered.contains("R&D _ report"),
3054            "{buffered}"
3055        );
3056    }
3057
3058    /// #902: an escaped star is a literal one in the plain text, and closes
3059    /// no emphasis span around it.
3060    #[test]
3061    fn plain_text_reads_escaped_stars() {
3062        assert_eq!(plain_inline("A\\*\\*\\* GmbH"), "A*** GmbH");
3063        assert_eq!(
3064            plain_inline("**FN \\*0\\*271\\*h** and *h\\*.at*"),
3065            "FN *0*271*h and h*.at"
3066        );
3067    }
3068
3069    /// #613: the plain export of a document holding every decoration the
3070    /// Markdown export writes, and the streamer agreeing with it.
3071    #[test]
3072    fn plain_text_drops_the_markdown_decoration() {
3073        let item = |text: &str, ordered: bool, number: u64, level: u8| Node::ListItem {
3074            ordered,
3075            number,
3076            first_in_list: number == 1 && level == 0,
3077            text: text.into(),
3078            level,
3079            marker: None,
3080            location: None,
3081            dclx: None,
3082            href: None,
3083            layer: None,
3084        };
3085        let mut doc = DoclingDocument::new("plain");
3086        doc.add_heading(1, "Title with **bold**");
3087        doc.add_heading(2, "Section");
3088        doc.add_paragraph("Some **bold**, *italic*, ~~gone~~ and `code` with a [link](https://x.org).\nNext line.");
3089        doc.push(item("*one*", false, 1, 0));
3090        doc.push(item("[two](https://y.org)", false, 2, 0));
3091        doc.push(item("nested", true, 1, 1));
3092        doc.push(Node::CheckboxItem {
3093            checked: true,
3094            text: "done".into(),
3095        });
3096        doc.push(Node::Code {
3097            language: Some("rust".into()),
3098            text: "let x = **y**;".into(),
3099            orig: None,
3100            pretty: None,
3101        });
3102        doc.push(Node::Picture {
3103            caption: Some("Figure *1*".into()),
3104            caption_href: None,
3105            image: None,
3106            classification: None,
3107            description: None,
3108            caption_parent: Default::default(),
3109            caption_location: None,
3110        });
3111        doc.push(Node::Caption {
3112            text: "Linked caption".into(),
3113            href: Some("https://z.org".into()),
3114        });
3115        doc.push(Node::Table(Table {
3116            rows: vec![
3117                vec!["**n**".into(), "rich".into()],
3118                vec!["**2**".into(), "a  \nb".into()],
3119            ],
3120            ..Default::default()
3121        }));
3122        doc.add_paragraph("R&amp;D \\_ x");
3123        let text = doc.export_to_text();
3124        assert_eq!(
3125            text,
3126            "Title with bold\n\n\
3127             Section\n\n\
3128             Some bold, italic, gone and code with a link.\nNext line.\n\n\
3129             - one\n- two\n    1. nested\n\n\
3130             - [x] done\n\n\
3131             let x = **y**;\n\n\
3132             Figure 1\n\n\
3133             Linked caption\n\n\
3134             |   n | rich   |\n\
3135             |-----|--------|\n\
3136             |   2 | a b    |\n\n\
3137             R&D _ x"
3138        );
3139
3140        let options = MarkdownExportOptions::plain_text();
3141        let (buffered, _) = doc.export_to_markdown_with_options(&options);
3142        assert_eq!(buffered, format!("{text}\n"));
3143        let mut streamer = MarkdownStreamer::new(false, ImageMode::Placeholder, false)
3144            .with_export_options(&options);
3145        let mut out = String::new();
3146        for chunk in doc.nodes.chunks(3) {
3147            out.push_str(&streamer.push(chunk, &[]));
3148        }
3149        out.push_str(&streamer.finish());
3150        assert_eq!(out, buffered);
3151    }
3152
3153    /// #613: text that only looks like Markdown stays as it is.
3154    #[test]
3155    fn plain_inline_keeps_literal_markers() {
3156        for (input, want) in [
3157            ("**bold** and *it* and ***both***", "bold and it and both"),
3158            ("**[label](https://a.b/c_(d))** end", "label end"),
3159            (
3160                "grant *USE and *OBJMGT authority",
3161                "grant *USE and *OBJMGT authority",
3162            ),
3163            ("2 * 3 * 4", "2 * 3 * 4"),
3164            ("a ** b", "a ** b"),
3165            ("the `quoted’ and `x` term", "the `quoted’ and x term"),
3166            ("see [12] and [docs](u)", "see [12] and docs"),
3167            ("formula $[a](b)$ stays", "formula $[a](b)$ stays"),
3168            ("price $5 and **bold**", "price $5 and bold"),
3169            ("[outer [inner] text](u)", "outer [inner] text"),
3170            ("unclosed **bold", "unclosed **bold"),
3171        ] {
3172            assert_eq!(plain_inline(input), want, "{input}");
3173        }
3174    }
3175}