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    /// Emit compact `| a | b |` tables instead of the padded GitHub serializer.
29    compact_tables: bool,
30    images: ImageMode,
31    artifacts_dir: String,
32    /// (relative path, bytes) for each referenced image — written by the caller.
33    artifacts: Vec<(String, Vec<u8>)>,
34    pic_index: usize,
35    /// Rendering the block content of a rich table cell (docling-core 2.94's
36    /// `in_table_cell`, docling-core#540): a heading has no valid Markdown
37    /// form inside a table, so it renders as plain text without `#` markers.
38    in_table_cell: bool,
39    /// docling-core's `MarkdownParams.page_break_placeholder`: the text that
40    /// separates two pages ([`DoclingDocument::page_break_placeholder`]).
41    /// `None` omits page breaks, docling's default.
42    page_break: Option<String>,
43    /// A page boundary has been crossed since the last rendered block, so the
44    /// next block is preceded by the placeholder. docling yields its
45    /// `_PageBreakNode` between two *items* whose `prov.page_no` differ, so a
46    /// boundary before the first block or after the last one emits nothing,
47    /// and a run of empty pages collapses into a single break.
48    pending_page_break: bool,
49    /// Whether any block has been rendered yet — for a streamer, across every
50    /// earlier push too — which is what makes a boundary a *pending* break.
51    emitted_any: bool,
52    /// The content layers rendered ([`MarkdownExportOptions::layers`], #599).
53    layers: ContentLayers,
54    /// The layer the items being rendered sit on: `None` (body) at the
55    /// root, the group's or wrapper's layer inside a non-body group or a
56    /// [`Node::Furniture`] — what the layer gate compares against the set,
57    /// as every item of a hidden sheet carries that sheet's layer upstream.
58    current_layer: Option<ContentLayer>,
59    /// Render a picture's nested text items ([`MarkdownExportOptions::traverse_pictures`]).
60    traverse_pictures: bool,
61    /// Keep the backends' HTML-entity escaping (`&amp;` …) in the output;
62    /// off, it is undone ([`MarkdownExportOptions::escape_html`]).
63    escape_html: bool,
64    /// Keep the backends' `\_` escaping; off, it is undone.
65    escape_underscores: bool,
66    /// What a picture prints as without image data.
67    image_placeholder: String,
68}
69
70impl Ctx {
71    /// Serializer state for `options` (the page-break setting is the
72    /// document's, the strictness the caller's).
73    fn new(doc: &DoclingDocument, strict: bool, options: &MarkdownExportOptions) -> Self {
74        Ctx {
75            strict,
76            compact_tables: doc.compact_tables,
77            images: options.image_mode,
78            artifacts_dir: options.artifacts_dir.clone(),
79            artifacts: Vec::new(),
80            pic_index: 0,
81            in_table_cell: false,
82            page_break: doc.page_break_placeholder.clone(),
83            pending_page_break: false,
84            emitted_any: false,
85            layers: options.layers,
86            current_layer: None,
87            traverse_pictures: options.traverse_pictures,
88            escape_html: options.escape_html,
89            escape_underscores: options.escape_underscores,
90            image_placeholder: options.image_placeholder.clone(),
91        }
92    }
93
94    /// Whether an item on the layer being rendered is in the chosen set.
95    fn layer_visible(&self) -> bool {
96        self.layers.contains(self.current_layer)
97    }
98
99    /// The backends escape inline text for Markdown when they build the
100    /// nodes (`&` → `&amp;`, `_` → `\_`, what docling-core's `post_process`
101    /// does at serialization time); an export that turns an escaping off
102    /// undoes it here, the way the JSON export recovers the raw text. Code,
103    /// formulas and table cells never pass through — upstream leaves those
104    /// unescaped too.
105    fn unescape(&self, text: &str) -> String {
106        let mut out = std::borrow::Cow::Borrowed(text);
107        if !self.escape_html
108            && (out.contains("&amp;") || out.contains("&lt;") || out.contains("&gt;"))
109        {
110            out = std::borrow::Cow::Owned(
111                out.replace("&lt;", "<")
112                    .replace("&gt;", ">")
113                    .replace("&amp;", "&"),
114            );
115        }
116        if !self.escape_underscores && out.contains("\\_") {
117            out = std::borrow::Cow::Owned(out.replace("\\_", "_"));
118        }
119        out.into_owned()
120    }
121
122    /// A text item's inline text: the strict-mode cleanup, then the
123    /// escaping the options turn off undone.
124    fn text(&self, text: &str) -> String {
125        self.unescape(&strict_text(text, self.strict))
126    }
127}
128
129/// Render a document to a Markdown string (pictures as placeholders).
130///
131/// `strict` selects the serializer-level behaviours that differ between
132/// docling-legacy output and cleaner Markdown — currently the code-fence
133/// language (legacy drops it, strict keeps it).
134pub fn to_markdown(doc: &DoclingDocument, strict: bool) -> String {
135    to_markdown_images(doc, strict, ImageMode::Placeholder, "artifacts").0
136}
137
138/// Render to Markdown with an explicit picture [`ImageMode`]. Returns the
139/// Markdown and, for [`ImageMode::Referenced`], the `(path, bytes)` of each image
140/// the caller should write (relative to the Markdown file).
141pub fn to_markdown_images(
142    doc: &DoclingDocument,
143    strict: bool,
144    images: ImageMode,
145    artifacts_dir: &str,
146) -> (String, Vec<(String, Vec<u8>)>) {
147    to_markdown_with_options(
148        doc,
149        strict,
150        &MarkdownExportOptions {
151            image_mode: images,
152            artifacts_dir: artifacts_dir.to_string(),
153            ..MarkdownExportOptions::default()
154        },
155    )
156}
157
158/// Render to Markdown per `options` ([`MarkdownExportOptions`], #599):
159/// content layers, picture traversal, escaping, the image placeholder and
160/// the image mode. Returns the Markdown and the referenced-image artifacts.
161pub fn to_markdown_with_options(
162    doc: &DoclingDocument,
163    strict: bool,
164    options: &MarkdownExportOptions,
165) -> (String, Vec<(String, Vec<u8>)>) {
166    let mut ctx = Ctx::new(doc, strict, options);
167    let mut blocks: Vec<String> = Vec::new();
168    render(&doc.nodes, &mut blocks, &mut ctx);
169    let mut body = blocks.join("\n\n");
170    // Strict mode only: turn recovered source hyperlinks into Markdown links.
171    // docling's standard pipeline drops them, so doing this in legacy mode would
172    // diverge from docling — hence strict-only, leaving conformance output intact.
173    if strict && !doc.links.is_empty() {
174        body = apply_links(&body, &doc.links);
175    }
176    let md = if body.is_empty() {
177        String::new()
178    } else {
179        format!("{body}\n")
180    };
181    (md, ctx.artifacts)
182}
183
184/// Render the block content of a *rich table cell* to Markdown — what
185/// docling-core's table serializer does for a `RichTableCell`
186/// (`doc_serializer.serialize(item, in_table_cell=True)`): the cell's
187/// paragraphs, lists and flattened nested tables render as in a document, but a
188/// heading loses its `#` markers (docling-core#540 — the Markdown spec has no
189/// headings inside tables). Pictures stay placeholders. The caller flattens the
190/// result into its cell text; the table serializer later turns the newlines
191/// into spaces.
192pub fn to_markdown_table_cell(doc: &DoclingDocument, strict: bool) -> String {
193    let mut ctx = Ctx::new(doc, strict, &MarkdownExportOptions::default());
194    ctx.in_table_cell = true;
195    ctx.artifacts_dir = String::new();
196    // A rich cell is one page's content; its sub-document carries no page
197    // boundaries and docling's `_iterate_items` runs the page-break scan
198    // over the document root only.
199    ctx.page_break = None;
200    let mut blocks: Vec<String> = Vec::new();
201    render(&doc.nodes, &mut blocks, &mut ctx);
202    blocks.join("\n\n")
203}
204
205/// Wrap each recovered link's anchor text in Markdown `[anchor](href)`. Anchors
206/// arrive cleaned (curly quotes/dashes already normalized) but un-escaped, so we
207/// match against the body's HTML-escaped (`&`/`<`/`>`) form, the way prose nodes
208/// were serialized. Links are consumed in document order from a moving cursor, so
209/// a repeated anchor (e.g. two "issues") links its successive occurrences rather
210/// than all pointing at the first. An anchor that can't be located is skipped
211/// (its text may have been split across a line wrap or table cell).
212fn apply_links(body: &str, links: &[(String, String)]) -> String {
213    let mut out = body.to_string();
214    let mut cursor = 0usize;
215    for (anchor, href) in links {
216        let anchor = anchor
217            .replace('&', "&amp;")
218            .replace('<', "&lt;")
219            .replace('>', "&gt;");
220        if anchor.is_empty() {
221            continue;
222        }
223        if let Some(rel) = out[cursor..].find(&anchor) {
224            let at = cursor + rel;
225            // Don't relink inside an already-emitted `](` Markdown link target.
226            let replacement = format!("[{anchor}]({href})");
227            out.replace_range(at..at + anchor.len(), &replacement);
228            cursor = at + replacement.len();
229        }
230    }
231    out
232}
233
234/// Like [`apply_links`] but over a single chunk, consuming from a shared queue so
235/// the same `[anchor](href)` rewriting can be applied incrementally as Markdown is
236/// streamed out. Each queued link is matched (in document order) against `chunk`
237/// and rewritten in place; a link whose anchor is not in this chunk is carried
238/// forward in the queue for a later chunk. Anchors are recovered in document
239/// order and a chunk is always a contiguous run of whole blocks, so this
240/// reproduces [`apply_links`]' single moving cursor: the link lands in whichever
241/// chunk contains its anchor, identically to the buffered path. (A link whose
242/// anchor never appears is carried to the end and dropped — the same no-op
243/// `apply_links` performs for an unlocatable anchor.)
244fn apply_links_chunk(chunk: &str, queue: &mut Vec<(String, String)>) -> String {
245    let mut out = chunk.to_string();
246    let mut cursor = 0usize;
247    let mut carried: Vec<(String, String)> = Vec::new();
248    for (anchor_raw, href) in std::mem::take(queue) {
249        let anchor = anchor_raw
250            .replace('&', "&amp;")
251            .replace('<', "&lt;")
252            .replace('>', "&gt;");
253        if anchor.is_empty() {
254            continue;
255        }
256        if let Some(rel) = out[cursor..].find(&anchor) {
257            let at = cursor + rel;
258            let replacement = format!("[{anchor}]({href})");
259            out.replace_range(at..at + anchor.len(), &replacement);
260            cursor = at + replacement.len();
261        } else {
262            // Not in this chunk; try again when its block is flushed.
263            carried.push((anchor_raw, href));
264        }
265    }
266    *queue = carried;
267    out
268}
269
270/// Incremental Markdown serializer: feed finalized, in-document-order batches of
271/// [`Node`]s and receive Markdown chunks whose concatenation is **byte-identical**
272/// to [`to_markdown_images`] over the same nodes. This is the streaming
273/// counterpart of the buffered serializer — used to emit a document's Markdown in
274/// chunks (e.g. page by page, as the parallel PDF pipeline finishes pages) instead
275/// of building the whole string up front.
276///
277/// [`ImageMode::Placeholder`] and [`ImageMode::Embedded`] render inline.
278/// [`ImageMode::Referenced`] additionally hands each picture's bytes out through
279/// [`take_artifacts`](Self::take_artifacts) — construct with
280/// [`with_artifacts`](Self::with_artifacts) and drain after every push so the
281/// bytes can be written to disk as pages finish instead of accumulating for the
282/// whole document (issue #80's memory-bounded image handling).
283///
284/// Each [`push`](Self::push) must contain whole blocks in reading order: a caller
285/// must not split a run of list items across two pushes (the run would render as
286/// two separate lists). Finalized PDF page batches already satisfy this.
287pub struct MarkdownStreamer {
288    strict: bool,
289    images: ImageMode,
290    compact_tables: bool,
291    /// Whether any non-empty chunk has been emitted yet (drives `\n\n` joins and
292    /// the trailing newline).
293    emitted_any: bool,
294    /// Recovered links not yet placed (strict mode), consumed in document order.
295    links: Vec<(String, String)>,
296    /// Referenced mode: the link prefix, the not-yet-drained `(path, bytes)`
297    /// artifacts, and the running image number (continues across pushes so the
298    /// stream matches the buffered serializer's `image_000000…` numbering).
299    artifacts_dir: String,
300    artifacts: Vec<(String, Vec<u8>)>,
301    pic_index: usize,
302    /// [`DoclingDocument::page_break_placeholder`] and the boundary carried
303    /// over from the previous push (a page batch opens with its page marker,
304    /// so the break it implies is paid by that batch's first block).
305    page_break: Option<String>,
306    pending_page_break: bool,
307    /// The export options beyond the image mode (#599): layers, picture
308    /// traversal, escaping, the placeholder — [`with_export_options`](Self::with_export_options).
309    layers: ContentLayers,
310    traverse_pictures: bool,
311    escape_html: bool,
312    escape_underscores: bool,
313    image_placeholder: String,
314}
315
316impl MarkdownStreamer {
317    /// Create a streamer. `compact_tables` mirrors [`DoclingDocument::compact_tables`].
318    /// For [`ImageMode::Referenced`] use [`with_artifacts`](Self::with_artifacts).
319    pub fn new(strict: bool, images: ImageMode, compact_tables: bool) -> Self {
320        debug_assert!(
321            images != ImageMode::Referenced,
322            "referenced image mode needs an artifacts dir; use with_artifacts"
323        );
324        Self::with_artifacts(strict, images, compact_tables, "artifacts")
325    }
326
327    /// Like [`new`](Self::new) but with the artifacts link prefix, allowing
328    /// [`ImageMode::Referenced`]: pictures render as
329    /// `![Image](<artifacts_dir>/image_NNNNNN.<ext>)` and each push's image
330    /// bytes wait in [`take_artifacts`](Self::take_artifacts) for the caller to
331    /// write. The concatenated chunks and the artifact list match the buffered
332    /// [`to_markdown_images`] byte-for-byte.
333    pub fn with_artifacts(
334        strict: bool,
335        images: ImageMode,
336        compact_tables: bool,
337        artifacts_dir: &str,
338    ) -> Self {
339        Self {
340            strict,
341            images,
342            compact_tables,
343            emitted_any: false,
344            links: Vec::new(),
345            artifacts_dir: artifacts_dir.to_string(),
346            artifacts: Vec::new(),
347            pic_index: 0,
348            page_break: None,
349            pending_page_break: false,
350            layers: ContentLayers::BODY,
351            traverse_pictures: false,
352            escape_html: true,
353            escape_underscores: true,
354            image_placeholder: "<!-- image -->".to_string(),
355        }
356    }
357
358    /// Take every setting of `options` ([`MarkdownExportOptions`], #599):
359    /// the image mode and artifacts directory the constructors took, plus the
360    /// content layers, picture traversal, escaping and image placeholder.
361    /// The concatenated chunks match the buffered
362    /// [`to_markdown_with_options`] byte-for-byte. Set before the first
363    /// [`push`](Self::push).
364    pub fn with_export_options(mut self, options: &MarkdownExportOptions) -> Self {
365        self.images = options.image_mode;
366        self.artifacts_dir = options.artifacts_dir.clone();
367        self.layers = options.layers;
368        self.traverse_pictures = options.traverse_pictures;
369        self.escape_html = options.escape_html;
370        self.escape_underscores = options.escape_underscores;
371        self.image_placeholder = options.image_placeholder.clone();
372        self
373    }
374
375    /// Insert `placeholder` between pages, mirroring
376    /// [`DoclingDocument::page_break_placeholder`] for the buffered path (the
377    /// concatenated chunks stay byte-identical to it). `None` — the default —
378    /// omits page breaks. Set before the first [`push`](Self::push).
379    pub fn with_page_break_placeholder(mut self, placeholder: Option<String>) -> Self {
380        self.page_break = placeholder;
381        self
382    }
383
384    /// The `(relative path, bytes)` of images rendered by pushes since the last
385    /// drain ([`ImageMode::Referenced`] only — empty otherwise). Paths are
386    /// relative to the Markdown file, i.e. they start with the configured
387    /// artifacts dir.
388    pub fn take_artifacts(&mut self) -> Vec<(String, Vec<u8>)> {
389        std::mem::take(&mut self.artifacts)
390    }
391
392    /// Render one finalized batch of nodes (plus any links recovered from the same
393    /// span, in document order) into the next Markdown chunk. Returns an empty
394    /// string when the batch produces no output (e.g. empty tables/pictures), in
395    /// which case nothing should be written.
396    pub fn push(&mut self, nodes: &[Node], links: &[(String, String)]) -> String {
397        self.links.extend(links.iter().cloned());
398        let mut ctx = Ctx {
399            strict: self.strict,
400            compact_tables: self.compact_tables,
401            images: self.images,
402            artifacts_dir: std::mem::take(&mut self.artifacts_dir),
403            artifacts: std::mem::take(&mut self.artifacts),
404            pic_index: self.pic_index,
405            in_table_cell: false,
406            page_break: std::mem::take(&mut self.page_break),
407            pending_page_break: self.pending_page_break,
408            emitted_any: self.emitted_any,
409            layers: self.layers,
410            current_layer: None,
411            traverse_pictures: self.traverse_pictures,
412            escape_html: self.escape_html,
413            escape_underscores: self.escape_underscores,
414            image_placeholder: std::mem::take(&mut self.image_placeholder),
415        };
416        let mut blocks: Vec<String> = Vec::new();
417        render(nodes, &mut blocks, &mut ctx);
418        self.image_placeholder = std::mem::take(&mut ctx.image_placeholder);
419        self.artifacts_dir = std::mem::take(&mut ctx.artifacts_dir);
420        self.artifacts = std::mem::take(&mut ctx.artifacts);
421        self.pic_index = ctx.pic_index;
422        self.page_break = std::mem::take(&mut ctx.page_break);
423        self.pending_page_break = ctx.pending_page_break;
424        if blocks.is_empty() {
425            return String::new();
426        }
427        let mut body = blocks.join("\n\n");
428        if self.strict && !self.links.is_empty() {
429            body = apply_links_chunk(&body, &mut self.links);
430        }
431        let chunk = if self.emitted_any {
432            format!("\n\n{body}")
433        } else {
434            body
435        };
436        self.emitted_any = true;
437        chunk
438    }
439
440    /// Emit the trailing newline that finishes the document (empty if no content
441    /// was produced). Call exactly once, after the final [`push`](Self::push).
442    pub fn finish(self) -> String {
443        if self.emitted_any {
444            "\n".to_string()
445        } else {
446            String::new()
447        }
448    }
449}
450
451/// In `strict` mode, rewrite inline text for readability rather than byte-for-byte
452/// docling fidelity: undo the legacy `\_` underscore escaping, and tighten stray
453/// spaces around punctuation (`[ 37 , 36 ]` → `[37, 36]`, `( x )` → `(x)`). This
454/// cleans up both the PDF backend's glyph-split spacing and the space the legacy
455/// emphasis serialization leaves before punctuation (`*a* ,` → `*a*,`).
456/// Legacy/default output keeps docling's spacing untouched. Only inline text
457/// nodes pass through here — code blocks and table cells are left alone.
458fn strict_text(text: &str, strict: bool) -> String {
459    if !strict {
460        return text.to_string();
461    }
462    text.replace("\\_", "_")
463        .replace(" ,", ",")
464        .replace(" .", ".")
465        .replace(" ;", ";")
466        .replace(" )", ")")
467        .replace("( ", "(")
468        .replace(" ]", "]")
469        .replace("[ ", "[")
470}
471
472/// docling-core 2.92's `_md_line_breaks` (docling-core#721): a single `\n`
473/// inside an item's text becomes a GFM hard line break (`"  \n"`, two trailing
474/// spaces) so renderers honour it; a blank line (`\n\n`) is a paragraph break
475/// and stays as is — the document scope already joins blocks with `\n\n`.
476/// Applied to body text, list items and captions, never to code/formulas.
477fn md_line_breaks(text: &str) -> String {
478    if !text.contains('\n') {
479        return text.to_string();
480    }
481    text.split("\n\n")
482        .map(|para| para.replace('\n', "  \n"))
483        .collect::<Vec<_>>()
484        .join("\n\n")
485}
486
487/// Undo [`md_line_breaks`] on a rich table cell's flattened Markdown so the
488/// non-Markdown exports (JSON `text`, LaTeX cells) see the cell's raw line
489/// breaks, as docling's do — a rich cell's text is its Markdown serialization
490/// in our model, and the two trailing spaces are a Markdown-only marker.
491pub(crate) fn strip_hard_breaks(text: &str) -> String {
492    if text.contains("  \n") {
493        text.replace("  \n", "\n")
494    } else {
495        text.to_string()
496    }
497}
498
499/// docling-core's `_heading_line_breaks`: a GFM heading cannot span lines, so a
500/// newline inside heading text collapses to a space (`# Hello World`, not a
501/// broken `# Hello\nWorld`).
502fn heading_line_breaks(text: &str) -> String {
503    text.replace('\n', " ")
504}
505
506fn render(nodes: &[Node], blocks: &mut Vec<String>, ctx: &mut Ctx) {
507    let mut i = 0;
508    while i < nodes.len() {
509        let before = blocks.len();
510        match &nodes[i] {
511            // A page boundary: the explicit `PageBreak` (slides, DjVu / DocTags
512            // pages) or the `PageInfo` marker that opens every PDF page and
513            // spreadsheet sheet — a sheet boundary carries both, and the flag
514            // absorbs the pair into one break. docling's `_iterate_items`
515            // yields a `_PageBreakNode` only between two items on different
516            // pages (a group's leading item counts for the group), which is
517            // exactly "a boundary between two rendered blocks": nothing before
518            // the first block, nothing after the last, consecutive boundaries
519            // — empty or furniture-only pages — collapsed into one.
520            Node::PageBreak | Node::PageInfo { .. } => {
521                if ctx.page_break.is_some() && ctx.emitted_any {
522                    ctx.pending_page_break = true;
523                }
524                i += 1;
525            }
526            Node::ListItem { .. } => {
527                let start = i;
528                i += 1;
529                loop {
530                    match nodes.get(i) {
531                        Some(Node::ListItem { .. }) => i += 1,
532                        // An empty paragraph between two list items is absorbed
533                        // into the run — docling keeps such a ListGroup
534                        // contiguous rather than splitting it.
535                        Some(Node::Paragraph { text })
536                            if text.is_empty()
537                                && matches!(nodes.get(i + 1), Some(Node::ListItem { .. })) =>
538                        {
539                            i += 1
540                        }
541                        _ => break,
542                    }
543                }
544                render_list_run(&nodes[start..i], blocks, ctx);
545            }
546            other => {
547                render_one(other, blocks, ctx);
548                i += 1;
549            }
550        }
551        if blocks.len() > before {
552            if ctx.pending_page_break {
553                // The placeholder is a block of its own, joined by the document
554                // delimiter like docling's `_PageBreakSerResult` part — an
555                // empty placeholder therefore leaves the doubled `\n\n`
556                // upstream leaves too.
557                if let Some(placeholder) = &ctx.page_break {
558                    blocks.insert(before, placeholder.clone());
559                }
560                ctx.pending_page_break = false;
561            }
562            ctx.emitted_any = true;
563        }
564    }
565}
566
567/// Render a contiguous run of list items.
568///
569/// Ordered items use their explicit `number`. A new sibling list (marked by
570/// `first_in_list`) at the same depth is separated by a blank line, matching
571/// docling-core's serializer.
572fn render_list_run(items: &[Node], blocks: &mut Vec<String>, ctx: &Ctx) {
573    let mut lines: Vec<String> = Vec::new();
574    // Whether a top-level item has been rendered yet — a fresh-list flag on
575    // the very first item opens nothing.
576    let mut any_top = false;
577
578    for item in items {
579        let Node::ListItem {
580            ordered,
581            number,
582            first_in_list,
583            text,
584            level,
585            marker: orig_marker,
586            location: _,
587            dclx: _,
588            href: _,
589            layer,
590        } = item
591        else {
592            continue;
593        };
594        // docling's content-layer filtering: an item renders when its layer
595        // — its own, else the layer of the group it sits in — is in the set
596        // (body only by default, so a furniture list item is omitted).
597        if !ctx.layers.contains(layer.or(ctx.current_layer)) {
598            continue;
599        }
600        let level = *level as usize;
601
602        // A new sibling list at the top level gets a blank line — and only the
603        // backend knows where one starts (`first_in_list`: Word's `numId`
604        // changing, an HTML `<ul>` closing, a Markdown bullet switching
605        // `-`→`*`). The serializer used to guess it from a kind flip or a
606        // number gap as well, which split lists docling keeps whole (an
607        // AsciiDoc `1.` … `5.`, mixed `*`/`1.` markers) — #385. Only at the
608        // top level: nested sibling groups are children of a list item, and
609        // docling joins an item's children without blank lines.
610        if level == 0 {
611            if any_top && *first_in_list {
612                lines.push(String::new());
613            }
614            any_top = true;
615        }
616
617        let indent = "    ".repeat(level);
618        // docling-core's `case_already_valid`: a marker of digits and a dot
619        // prints verbatim — Python's `\d+\.` admits every Unicode decimal
620        // digit, so a DOCX `decimalFullWidth` marker (`1.`, docling#4336)
621        // is kept as it is rather than renumbered in ASCII.
622        let verbatim = orig_marker.as_deref().filter(|m| {
623            m.strip_suffix('.')
624                .is_some_and(|d| !d.is_empty() && d.chars().all(char::is_numeric))
625        });
626        let marker = match verbatim {
627            Some(m) if *ordered => m.to_string(),
628            _ if *ordered => format!("{number}."),
629            _ => "-".to_string(),
630        };
631        lines.push(format!("{indent}{marker} {}", list_item_text(text, ctx)));
632    }
633
634    // A run consisting only of furniture (content-layer-filtered) items yields no
635    // lines; pushing an empty block here would surface as a stray blank line.
636    if !lines.is_empty() {
637        blocks.push(lines.join("\n"));
638    }
639}
640
641/// A list item's Markdown body. The GFM hard-line-break rule (docling-core#721)
642/// applies to the item's own text; pictures the HTML backend folded into the
643/// item (`"\n[alt\n]<!-- image -->"` per `<img>` inside the `<li>`) are
644/// docling's picture *children* of the item, which its serializer prints after
645/// the item line with plain newlines — so a folded tail keeps its newlines
646/// unmarked. The tail is recognised structurally: every line after the first is
647/// an image marker or an alt caption directly followed by one.
648fn list_item_text(text: &str, ctx: &Ctx) -> String {
649    let escaped = ctx.text(text);
650    if let Some((own, tail)) = escaped.split_once('\n') {
651        if is_folded_child_tail(tail) {
652            return format!("{}\n{tail}", md_line_breaks(own));
653        }
654    }
655    md_line_breaks(&escaped)
656}
657
658/// Whether everything after a list item's own first line is a folded *child*
659/// block rather than a continuation of the item's text: an image marker
660/// (optionally preceded by its caption/alt line) or a fenced code block. The
661/// AsciiDoc backend indents such a block to the item's own depth (as
662/// docling-core's list serializer does for each part it emits), so a leading
663/// indent is ignored here.
664fn is_folded_child_tail(tail: &str) -> bool {
665    const MARKER: &str = "<!-- image -->";
666    const FENCE: &str = "```";
667    let mut lines = tail.split('\n').peekable();
668    let mut any = false;
669    while let Some(line) = lines.next() {
670        let line = line.trim_start();
671        if line == MARKER {
672            any = true;
673        } else if line == FENCE {
674            // Skip the block's body; an unclosed fence is not a folded child.
675            loop {
676                match lines.next() {
677                    Some(l) if l.trim_start() == FENCE => break,
678                    Some(_) => {}
679                    None => return false,
680                }
681            }
682            any = true;
683        } else if lines.next().map(str::trim_start) == Some(MARKER) {
684            any = true; // an alt caption line, then its marker
685        } else {
686            return false;
687        }
688    }
689    any
690}
691
692/// Render one item: docling's content-layer gate first — an item whose layer
693/// (the group's or wrapper's it sits in, else the body) is outside the set
694/// renders nothing, while a container still walks its children — then the
695/// item itself.
696fn render_one(node: &Node, blocks: &mut Vec<String>, ctx: &mut Ctx) {
697    match node {
698        // Containers and layer wrappers decide for their children.
699        Node::Group { .. }
700        | Node::Furniture { .. }
701        | Node::PageFurniture { .. }
702        | Node::FurnitureText { .. }
703        | Node::CommentSection { .. }
704        | Node::PictureChildren(_)
705        | Node::Commented { .. }
706        | Node::Located { .. }
707        | Node::Prov { .. }
708        | Node::PageBreak
709        | Node::PageInfo { .. }
710        | Node::DoclangOnly(_) => {}
711        _ if !ctx.layer_visible() => return,
712        _ => {}
713    }
714    render_item(node, blocks, ctx)
715}
716
717fn render_item(node: &Node, blocks: &mut Vec<String>, ctx: &mut Ctx) {
718    match node {
719        Node::Heading { level, text } => {
720            let text = heading_line_breaks(&ctx.text(text));
721            if ctx.in_table_cell {
722                // docling-core#540: no `#` markers inside a table cell.
723                blocks.push(text);
724            } else {
725                let hashes = "#".repeat((*level).clamp(1, 6) as usize);
726                blocks.push(format!("{hashes} {text}"));
727            }
728        }
729        // An empty body paragraph (docling's blank-line text item) contributes
730        // nothing to Markdown — only DocLang/JSON keep it.
731        Node::Paragraph { text } if text.is_empty() => {}
732        Node::Paragraph { text } => blocks.push(md_line_breaks(&ctx.text(text))),
733        // A standalone caption item renders like a text item; its hyperlink
734        // annotation becomes a Markdown link around the whole caption.
735        Node::Caption { text, .. } if text.is_empty() => {}
736        Node::Caption { text, href } => {
737            let body = md_line_breaks(&ctx.text(text));
738            blocks.push(match href {
739                Some(url) => format!("[{body}]({url})"),
740                None => body,
741            });
742        }
743        Node::CheckboxItem { checked, text } => {
744            let mark = if *checked { "- [x] " } else { "- [ ] " };
745            blocks.push(md_line_breaks(&ctx.text(&format!("{mark}{text}"))));
746        }
747        Node::Code {
748            language,
749            text,
750            pretty,
751            ..
752        } => {
753            // Legacy docling never emits a language on the fence; strict keeps it.
754            let lang = match language {
755                Some(l) if ctx.strict => l.as_str(),
756                _ => "",
757            };
758            // Strict prefers the line-preserving rendering when the backend
759            // supplied one (PDF); legacy stays on docling's flat `text`.
760            let body = match pretty {
761                Some(p) if ctx.strict => p.as_str(),
762                _ => text.as_str(),
763            };
764            blocks.push(format!("```{lang}\n{body}\n```"));
765        }
766        // A CodeFormula-decoded display formula renders as docling's `$$…$$`
767        // (the un-enriched pipeline emits a placeholder paragraph instead).
768        Node::Formula { latex, .. } => blocks.push(format!("$${latex}$$")),
769        Node::Table(table) => {
770            // docling renders a table's caption as a text line before the grid.
771            // `caption` is already escaped (backend convention), like a paragraph.
772            if let Some(cap) = &table.caption {
773                if !cap.is_empty() {
774                    blocks.push(md_line_breaks(&ctx.text(cap)));
775                }
776            }
777            let rendered = render_table(table, ctx.compact_tables);
778            if !rendered.is_empty() {
779                blocks.push(rendered);
780            }
781        }
782        // Classification predictions don't affect docling's Markdown output.
783        Node::Picture { caption, image, .. } => {
784            if let Some(cap) = caption {
785                if !cap.is_empty() {
786                    blocks.push(md_line_breaks(&ctx.unescape(cap)));
787                }
788            }
789            blocks.push(picture_marker(image.as_ref(), ctx));
790        }
791        // A chart renders as docling's picture-with-meta markdown: the caption,
792        // the placeholder, the humanized classification ("line_chart" ->
793        // "Line chart"), then the chart's data grid as a regular table.
794        Node::Chart {
795            kind,
796            table,
797            caption,
798            ..
799        } => {
800            if let Some(cap) = caption {
801                if !cap.is_empty() {
802                    blocks.push(md_line_breaks(&ctx.unescape(cap)));
803                }
804            }
805            blocks.push(picture_marker(None, ctx));
806            blocks.push(humanize_label(kind));
807            let rendered = render_table(table, false);
808            if !rendered.is_empty() {
809                blocks.push(rendered);
810            }
811        }
812        // A DocLang-only node is omitted from Markdown.
813        Node::DoclangOnly(_) => {}
814        // A group on a non-body layer (a hidden spreadsheet sheet): its items
815        // sit on that layer, as every item of such a sheet does upstream — so
816        // they render exactly when the set includes it (#599), nothing by
817        // default.
818        Node::Group {
819            layer: Some(layer),
820            children,
821            ..
822        } => {
823            let outer = ctx.current_layer.replace(*layer);
824            render(children, blocks, ctx);
825            ctx.current_layer = outer;
826        }
827        Node::Group { children, .. } => render(children, blocks, ctx),
828        Node::FieldRegion { items } => {
829            // The region container and each field item carry no text of their
830            // own; docling-core 2.93 (#724) serializes them to nothing (older
831            // releases emitted a `<!-- missing-text -->` marker for each), so
832            // only an item's marker/key/value appear, as separate paragraphs.
833            for item in items {
834                for part in [&item.marker, &item.key, &item.value].into_iter().flatten() {
835                    blocks.push(md_line_breaks(&ctx.text(part)));
836                }
837            }
838        }
839        // docling's Markdown serializer has no component for a `KeyValueItem`
840        // and writes its fallback placeholder in the item's place.
841        Node::KeyValueGraph { .. } => blocks.push(MISSING_KEY_VALUE_ITEM.to_string()),
842        // A rich inline group renders exactly like a paragraph of its Markdown
843        // text — the structured runs are DocLang-only.
844        Node::InlineGroup { md_text, .. } => blocks.push(md_line_breaks(&ctx.text(md_text))),
845        // A plain-text backend dump renders verbatim as a single block.
846        Node::TextDump(text) => {
847            if !text.is_empty() {
848                blocks.push(text.clone());
849            }
850        }
851        // Furniture (page headers/footers, HTML `<title>`, notes) is out of
852        // the default body-only export, mirroring docling; a set that
853        // includes its layer renders the wrapped item like a body item (#599).
854        Node::Furniture { layer, inner } => {
855            let outer = ctx.current_layer.replace(*layer);
856            render_one(inner, blocks, ctx);
857            ctx.current_layer = outer;
858        }
859        // A page header/footer or a `.doc` furniture paragraph: upstream's
860        // `page_header` / `page_footer` / `footnote` text item, a plain
861        // paragraph when the furniture layer renders.
862        Node::PageFurniture { text, .. } | Node::FurnitureText { text, .. } => {
863            if ctx.layers.furniture && !text.is_empty() {
864                blocks.push(md_line_breaks(&ctx.text(text)));
865            }
866        }
867        // A picture's contained text: docling's Markdown picture serializer
868        // prints the caption and the image, never the children — unless the
869        // export traverses pictures, when the item walk yields them after
870        // the picture and they render as the text items they are (#599).
871        Node::PictureChildren(children) => {
872            if ctx.traverse_pictures {
873                render(children, blocks, ctx);
874            }
875        }
876        // A comment lives in the notes layer — out like other furniture
877        // unless that layer renders (the note's `[author: …]: text` as a
878        // paragraph); the annotation on a body item is JSON-only, so render
879        // the item.
880        Node::CommentSection { text, .. } => {
881            if ctx.layers.notes && !text.is_empty() {
882                blocks.push(md_line_breaks(&ctx.text(text)));
883            }
884        }
885        Node::Commented { inner, .. } => render_one(inner, blocks, ctx),
886        // Layout provenance is DocLang-only; render the wrapped node.
887        Node::Located { inner, .. } | Node::Prov { inner, .. } => render_one(inner, blocks, ctx),
888        // Page breaks are DocLang-only; docling omits them from Markdown.
889        Node::PageBreak => {}
890        // Page markers feed the JSON export only.
891        Node::PageInfo { .. } => {}
892        // Runs of adjacent list items are merged by `render`; a stray single
893        // item (a hand-built document, or a `Located` wrapper around one)
894        // still renders as its own one-item list instead of panicking —
895        // `nodes` is public API, so every representable tree must serialize.
896        Node::ListItem { .. } => render_list_run(std::slice::from_ref(node), blocks, ctx),
897    }
898}
899
900/// The Markdown for a picture under the active [`ImageMode`]; Referenced mode also
901/// records the bytes in `ctx.artifacts` for the caller to write.
902/// docling-core's `_humanize_text`: underscores to spaces, first letter
903/// capitalized ("line_chart" -> "Line chart").
904fn humanize_label(label: &str) -> String {
905    let text = label.replace('_', " ");
906    let mut chars = text.chars();
907    match chars.next() {
908        Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
909        None => text,
910    }
911}
912
913fn picture_marker(image: Option<&crate::PictureImage>, ctx: &mut Ctx) -> String {
914    match (ctx.images, image) {
915        // docling embeds the `ImageRef`'s PNG (a JPEG re-encoded).
916        (ImageMode::Embedded, Some(img)) => {
917            format!("![Image]({})", crate::pixel_digest::docling_data_uri(img).1)
918        }
919        (ImageMode::Referenced, Some(img)) => {
920            let path = format!(
921                "{}/image_{:06}.{}",
922                ctx.artifacts_dir,
923                ctx.pic_index,
924                ext_for(&img.mimetype)
925            );
926            ctx.pic_index += 1;
927            ctx.artifacts.push((path.clone(), img.data.clone()));
928            format!("![Image]({})", escape_uri_path(&path))
929        }
930        // Placeholder, or any mode with no extracted image.
931        _ => ctx.image_placeholder.clone(),
932    }
933}
934
935/// Encode a URL or filesystem path as a Markdown link destination —
936/// docling-core's `MarkdownPictureSerializer._escape_uri_path`
937/// (docling-core#698, 2.94). Handles URLs of any scheme as well as POSIX and
938/// Windows paths, keeps relative paths relative and never double-encodes:
939/// backslashes become `/` (a backslash is both the Windows separator and a
940/// Markdown escape), a UNC share `//host/…` and an absolute Windows path
941/// `C:/…` become RFC 8089 `file://` URLs (the one spelling a renderer cannot
942/// misread as a scheme-relative URL or a `C:` scheme), a URL keeps its
943/// scheme / authority / delimiters with only the components encoded, and
944/// everything else is percent-encoded as a path. `%` is kept so an
945/// already-encoded destination stays as it is; spaces and parentheses are
946/// encoded because they would end (or unbalance) a Markdown inline link.
947pub(crate) fn escape_uri_path(value: &str) -> String {
948    const KEEP: &str = "/%:@+,;=~$!&'*";
949    let s = value.replace('\\', "/");
950    if let Some(rest) = s.strip_prefix("//") {
951        // A fileshare: `file://<host>/<path>`, the host possibly empty.
952        let rest = rest.trim_start_matches('/');
953        let (host, tail) = rest.split_once('/').unwrap_or((rest, ""));
954        return format!("file://{host}{}", percent_quote(&format!("/{tail}"), KEEP));
955    }
956    let bytes = s.as_bytes();
957    if bytes.len() >= 3 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':' && bytes[2] == b'/' {
958        // A Windows path with a drive letter: `file:///C:/…`.
959        return format!("file:///{}", percent_quote(&s, KEEP));
960    }
961    // A URL keeps its scheme, authority and delimiters; only its components are
962    // encoded. A single-character scheme cannot be real (it is a drive letter,
963    // handled above), so it is read as a path — like `urlsplit`.
964    if let Some((scheme, rest)) = s.split_once(':') {
965        let valid_scheme = scheme.len() > 1
966            && scheme.as_bytes()[0].is_ascii_alphabetic()
967            && scheme
968                .bytes()
969                .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'));
970        if valid_scheme {
971            let (authority, rest) = match rest.strip_prefix("//") {
972                Some(r) => {
973                    let end = r.find(['/', '?', '#']).unwrap_or(r.len());
974                    (Some(&r[..end]), &r[end..])
975                }
976                None => (None, rest),
977            };
978            let (before_frag, fragment) = rest.split_once('#').unwrap_or((rest, ""));
979            let (path, query) = before_frag.split_once('?').unwrap_or((before_frag, ""));
980            let mut out = format!("{scheme}:");
981            if let Some(a) = authority {
982                out.push_str("//");
983                out.push_str(a);
984            }
985            out.push_str(&percent_quote(path, KEEP));
986            if !query.is_empty() {
987                out.push('?');
988                out.push_str(&percent_quote(query, KEEP));
989            }
990            if !fragment.is_empty() {
991                out.push('#');
992                out.push_str(&percent_quote(fragment, KEEP));
993            }
994            return out;
995        }
996    }
997    // A relative or root-relative local path.
998    percent_quote(&s, KEEP)
999}
1000
1001/// `urllib.parse.quote(s, safe)`: unreserved ASCII (`A–Z a–z 0–9 _ . - ~`) and
1002/// the `safe` set stay, every other byte of the UTF-8 encoding becomes `%XX`.
1003fn percent_quote(s: &str, safe: &str) -> String {
1004    let mut out = String::with_capacity(s.len());
1005    for &b in s.as_bytes() {
1006        let keep = b.is_ascii_alphanumeric()
1007            || matches!(b, b'_' | b'.' | b'-' | b'~')
1008            || (b.is_ascii() && safe.contains(b as char));
1009        if keep {
1010            out.push(b as char);
1011        } else {
1012            out.push_str(&format!("%{b:02X}"));
1013        }
1014    }
1015    out
1016}
1017
1018pub(crate) fn ext_for(mimetype: &str) -> &str {
1019    match mimetype {
1020        "image/jpeg" => "jpg",
1021        "image/gif" => "gif",
1022        "image/webp" => "webp",
1023        "image/bmp" => "bmp",
1024        "image/tiff" => "tif",
1025        _ => "png",
1026    }
1027}
1028
1029/// Render a table. `compact` selects between two serializers:
1030///
1031/// - **padded** (default) — docling-core's `tabulate(tablefmt="github")`: columns
1032///   are padded to a fixed width (header width + a minimum padding of 2, or the
1033///   widest data cell); numeric columns (every data cell parses as a number) are
1034///   right-aligned, others left-aligned; separators are plain dashes of
1035///   `width + 2`. Matches current published docling (DOCX/HTML conformance).
1036/// - **compact** — `| a | b |` cells with single-dash `| - | - |` separators, no
1037///   width padding. Matches the committed PDF groundtruth corpus, which predates
1038///   the padded serializer.
1039///
1040/// Each cell is first escaped (`\n` → space, `|` → `&#124;`) so it can't break the
1041/// table. The header row is the table's leading `column_header` block flattened
1042/// to one row ([`Table::header_row_count`] + [`flatten_header_rows`],
1043/// docling-core#723); alignment and widths are computed over the body rows.
1044/// Whether a table cell counts as a number for column alignment, matching
1045/// `tabulate`'s detection: an ordinary float/int (`f64`-parseable, covering
1046/// `1e2`/`inf`/`+1.5`) **or** a thousands-separated number like `7,015`.
1047fn is_number_cell(t: &str) -> bool {
1048    t.parse::<f64>().is_ok() || is_thousands_number(t)
1049}
1050
1051/// A number with comma thousands-separators, per `tabulate`'s
1052/// `_float_with_thousands_separators` regex
1053/// (`^(([+-]?[0-9]{1,3})(?:,([0-9]{3}))*)?(?(1)\.[0-9]*|\.[0-9]+)?$`): the
1054/// integer part is 1–3 digits then any number of `,ddd` groups; the fraction is
1055/// optional (and, without an integer part, must have at least one digit).
1056fn is_thousands_number(t: &str) -> bool {
1057    let b = t.as_bytes();
1058    let mut i = 0;
1059    let start = i;
1060    if i < b.len() && (b[i] == b'+' || b[i] == b'-') {
1061        i += 1;
1062    }
1063    // First digit chunk: 1–3 digits.
1064    let d0 = i;
1065    while i < b.len() && b[i].is_ascii_digit() && i - d0 < 3 {
1066        i += 1;
1067    }
1068    let has_int = i > d0;
1069    if has_int {
1070        // Subsequent `,ddd` groups (exactly three digits each).
1071        while i + 3 < b.len() + 1
1072            && b.get(i) == Some(&b',')
1073            && b.get(i + 1).is_some_and(u8::is_ascii_digit)
1074            && b.get(i + 2).is_some_and(u8::is_ascii_digit)
1075            && b.get(i + 3).is_some_and(u8::is_ascii_digit)
1076        {
1077            i += 4;
1078        }
1079    } else {
1080        // A sign only counts with an integer part.
1081        i = start;
1082    }
1083    // Optional fraction.
1084    if i < b.len() && b[i] == b'.' {
1085        i += 1;
1086        let f0 = i;
1087        while i < b.len() && b[i].is_ascii_digit() {
1088            i += 1;
1089        }
1090        if !has_int && i == f0 {
1091            return false; // `.` with no digits and no integer part
1092        }
1093    } else if !has_int {
1094        return false; // neither integer nor fractional part
1095    }
1096    i == b.len()
1097}
1098
1099/// The single GFM header row for a table: the leading header rows (see
1100/// [`Table::header_row_count`]) flattened per column, texts joined with
1101/// `" - "` after dropping consecutive duplicates — docling-core's
1102/// `_flatten_header_rows` (docling-core#723). The duplicate rule is what
1103/// keeps a row-spanning header from being joined to itself (the grid repeats
1104/// its text into every row it covers); it is position-based, so two stacked
1105/// levels sharing a label collapse too — GFM has one header row, and upstream
1106/// accepts that loss. No header rows → one empty header cell per column.
1107fn flatten_header_rows(header_rows: &[Vec<String>], num_cols: usize) -> Vec<String> {
1108    (0..num_cols)
1109        .map(|c| {
1110            let mut parts: Vec<&str> = Vec::new();
1111            for row in header_rows {
1112                let text = row.get(c).map(String::as_str).unwrap_or("");
1113                if !text.is_empty() && parts.last() != Some(&text) {
1114                    parts.push(text);
1115                }
1116            }
1117            parts.join(" - ")
1118        })
1119        .collect()
1120}
1121
1122pub(crate) fn render_table(table: &Table, compact: bool) -> String {
1123    if table.rows.is_empty() {
1124        return String::new();
1125    }
1126    let num_cols = table.rows.iter().map(Vec::len).max().unwrap_or(0);
1127    if num_cols == 0 {
1128        return String::new();
1129    }
1130
1131    // Escaped, rectangular grid (ragged rows padded with empty cells). The
1132    // header block is resolved to the one row GFM allows (docling-core#723);
1133    // `tabulate` strips data cells of surrounding whitespace but leaves the
1134    // header texts as-is.
1135    let num_headers = table.header_row_count().min(table.rows.len());
1136    let escaped = |r: usize| -> Vec<String> {
1137        (0..num_cols)
1138            .map(|c| escape_cell(table.rows[r].get(c).map(String::as_str).unwrap_or("")))
1139            .collect()
1140    };
1141    let header_rows: Vec<Vec<String>> = (0..num_headers).map(escaped).collect();
1142    let header = flatten_header_rows(&header_rows, num_cols);
1143    let body: Vec<Vec<String>> = (num_headers..table.rows.len())
1144        .map(|r| {
1145            escaped(r)
1146                .into_iter()
1147                .map(|c| c.trim().to_string())
1148                .collect()
1149        })
1150        .collect();
1151
1152    if compact {
1153        // Compact: cells joined by " | ", no padding, single-dash separators.
1154        let render_row = |row: &[String]| -> String { format!("| {} |", row.join(" | ")) };
1155        let mut lines = Vec::with_capacity(body.len() + 2);
1156        lines.push(render_row(&header));
1157        let sep: Vec<&str> = (0..num_cols).map(|_| "-").collect();
1158        lines.push(format!("| {} |", sep.join(" | ")));
1159        for row in &body {
1160            lines.push(render_row(row));
1161        }
1162        return lines.join("\n");
1163    }
1164
1165    // Display width (Unicode scalar count — good enough for now).
1166    let dw = |s: &str| s.chars().count();
1167
1168    // A column is right-aligned when at least one body cell is numeric and every
1169    // non-empty body cell is numeric — matching `tabulate`'s column typing, where
1170    // empty cells are "missing" (ignored) and a number may carry thousands
1171    // separators (`7,015`), which a plain `f64` parse rejects.
1172    let right: Vec<bool> = (0..num_cols)
1173        .map(|c| {
1174            let mut any = false;
1175            for row in &body {
1176                let t = row[c].trim();
1177                if t.is_empty() {
1178                    continue;
1179                }
1180                if !is_number_cell(t) {
1181                    return false;
1182                }
1183                any = true;
1184            }
1185            any
1186        })
1187        .collect();
1188
1189    // Column width = max(header_width + MIN_PADDING(2), max body-cell width).
1190    let width: Vec<usize> = (0..num_cols)
1191        .map(|c| {
1192            let mut w = dw(&header[c]) + 2;
1193            for row in &body {
1194                w = w.max(dw(&row[c]));
1195            }
1196            w
1197        })
1198        .collect();
1199
1200    let fmt_cell = |s: &str, c: usize| -> String {
1201        let pad = " ".repeat(width[c].saturating_sub(dw(s)));
1202        let body = if right[c] {
1203            format!("{pad}{s}")
1204        } else {
1205            format!("{s}{pad}")
1206        };
1207        format!(" {body} ")
1208    };
1209    let render_row = |row: &[String]| -> String {
1210        let cells: Vec<String> = (0..num_cols).map(|c| fmt_cell(&row[c], c)).collect();
1211        format!("|{}|", cells.join("|"))
1212    };
1213
1214    let mut lines = Vec::with_capacity(body.len() + 2);
1215    lines.push(render_row(&header));
1216    let sep: Vec<String> = (0..num_cols).map(|c| "-".repeat(width[c] + 2)).collect();
1217    lines.push(format!("|{}|", sep.join("|")));
1218    for row in &body {
1219        lines.push(render_row(row));
1220    }
1221    lines.join("\n")
1222}
1223
1224/// Escape a table cell so it can't break the markdown table: newlines become
1225/// spaces and pipes become the `&#124;` HTML entity (matches docling-core).
1226fn escape_cell(s: &str) -> String {
1227    s.replace('\n', " ").replace('|', "&#124;")
1228}
1229
1230#[cfg(test)]
1231mod tests {
1232    use super::*;
1233    use crate::{PictureImage, TableCell, TableStructure};
1234
1235    /// #385: where one list ends and the next begins is the backend's call
1236    /// (`first_in_list`), never the serializer's. An ordered run `1.` → `5.`
1237    /// is one list (an AsciiDoc numbered list around a nested one), and so are
1238    /// mixed bullet/ordered items the backend did not separate; only a flagged
1239    /// item opens a new list and earns the blank line.
1240    #[test]
1241    fn list_boundaries_come_from_the_backend_not_the_numbering() {
1242        let item = |ordered: bool, number: u64, first_in_list: bool, text: &str| Node::ListItem {
1243            ordered,
1244            number,
1245            first_in_list,
1246            text: text.into(),
1247            level: 0,
1248            marker: None,
1249            location: None,
1250            dclx: None,
1251            href: None,
1252            layer: None,
1253        };
1254        let md = |items: Vec<Node>| {
1255            let mut doc = DoclingDocument::new("t");
1256            for n in items {
1257                doc.push(n);
1258            }
1259            doc.export_to_markdown()
1260        };
1261        // A number gap alone is not a boundary.
1262        assert_eq!(
1263            md(vec![
1264                item(true, 1, true, "one"),
1265                item(true, 5, false, "five")
1266            ]),
1267            "1. one\n5. five\n"
1268        );
1269        // Nor is a kind flip the backend did not flag …
1270        assert_eq!(
1271            md(vec![
1272                item(false, 0, true, "bullet"),
1273                item(true, 1, false, "one"),
1274                item(false, 0, false, "bullet two"),
1275            ]),
1276            "- bullet\n1. one\n- bullet two\n"
1277        );
1278        // … while a flagged item is one, whatever its number says.
1279        assert_eq!(
1280            md(vec![
1281                item(true, 1, true, "a"),
1282                item(true, 2, false, "b"),
1283                item(true, 3, true, "new list, continuing count"),
1284            ]),
1285            "1. a\n2. b\n\n3. new list, continuing count\n"
1286        );
1287    }
1288
1289    #[test]
1290    fn renders_headings_paragraphs_and_lists() {
1291        let mut doc = DoclingDocument::new("demo");
1292        doc.add_heading(1, "Title");
1293        doc.add_paragraph("Hello world.");
1294        doc.push(Node::ListItem {
1295            ordered: false,
1296            number: 1,
1297            first_in_list: true,
1298            text: "first".into(),
1299            level: 0,
1300            marker: None,
1301            location: None,
1302            dclx: None,
1303            href: None,
1304            layer: None,
1305        });
1306        doc.push(Node::ListItem {
1307            ordered: false,
1308            number: 2,
1309            first_in_list: false,
1310            text: "second".into(),
1311            level: 0,
1312            marker: None,
1313            location: None,
1314            dclx: None,
1315            href: None,
1316            layer: None,
1317        });
1318        let md = doc.export_to_markdown();
1319        assert_eq!(md, "# Title\n\nHello world.\n\n- first\n- second\n");
1320    }
1321
1322    /// docling-core 2.92 (#721): a single newline inside an item's text is a
1323    /// GFM hard line break, a blank line stays a paragraph break, and a heading
1324    /// collapses its newline to a space. Nested-table dumps stay verbatim.
1325    #[test]
1326    fn single_newlines_become_gfm_hard_line_breaks() {
1327        let mut doc = DoclingDocument::new("t");
1328        doc.push(Node::Heading {
1329            level: 1,
1330            text: "Hello\nWorld".into(),
1331        });
1332        doc.push(Node::Paragraph {
1333            text: "line one\nline two\n\npara two".into(),
1334        });
1335        doc.push(Node::ListItem {
1336            ordered: false,
1337            number: 1,
1338            first_in_list: true,
1339            text: "item\ncontinued".into(),
1340            level: 0,
1341            marker: None,
1342            location: None,
1343            dclx: None,
1344            href: None,
1345            layer: None,
1346        });
1347        doc.push(Node::TextDump("A1 B1 \n\n\nC1".into()));
1348        assert_eq!(
1349            doc.export_to_markdown(),
1350            "# Hello World\n\nline one  \nline two\n\npara two\n\n- item  \ncontinued\n\nA1 B1 \n\n\nC1\n"
1351        );
1352    }
1353
1354    /// docling-core#540: inside a rich table cell a heading is plain text;
1355    /// docling-core#724: a field region renders only its items' key/value text.
1356    #[test]
1357    fn table_cell_mode_and_field_regions() {
1358        let mut doc = DoclingDocument::new("t");
1359        doc.push(Node::Heading {
1360            level: 2,
1361            text: "A  text".into(),
1362        });
1363        doc.push(Node::Paragraph {
1364            text: "body".into(),
1365        });
1366        assert_eq!(to_markdown_table_cell(&doc, false), "A  text\n\nbody");
1367        assert_eq!(doc.export_to_markdown(), "## A  text\n\nbody\n");
1368
1369        let mut doc = DoclingDocument::new("f");
1370        doc.push(Node::FieldRegion {
1371            items: vec![crate::FieldItem {
1372                marker: None,
1373                key: Some("Name:".into()),
1374                value: Some("John Doe".into()),
1375                value_kind: None,
1376            }],
1377        });
1378        assert_eq!(doc.export_to_markdown(), "Name:\n\nJohn Doe\n");
1379    }
1380
1381    #[test]
1382    fn strict_renders_recovered_links_legacy_does_not() {
1383        let mut doc = DoclingDocument::new("cv");
1384        doc.add_paragraph("Find me on LinkedIn or GitHub.");
1385        doc.links = vec![
1386            ("LinkedIn".into(), "https://www.linkedin.com/in/x/".into()),
1387            ("GitHub".into(), "https://github.com/x/".into()),
1388        ];
1389        // Legacy/docling mode: links are left untouched (conformance preserved).
1390        assert_eq!(doc.export_to_markdown(), "Find me on LinkedIn or GitHub.\n");
1391        // Strict mode: anchors become Markdown links.
1392        assert_eq!(
1393            doc.export_to_markdown_with(true),
1394            "Find me on [LinkedIn](https://www.linkedin.com/in/x/) or [GitHub](https://github.com/x/).\n"
1395        );
1396    }
1397
1398    #[test]
1399    fn strict_links_match_escaped_anchor_and_consume_in_order() {
1400        let mut doc = DoclingDocument::new("d");
1401        // The PDF assembler HTML-escapes prose, so by serialization time the body
1402        // already carries `&amp;`; the anchor is stored un-escaped. The matcher must
1403        // escape the anchor to find it. Two identical anchors link in document order.
1404        doc.add_paragraph("AI &amp; ML here, and issues here, then issues there.");
1405        doc.links = vec![
1406            ("AI & ML".into(), "https://a/".into()),
1407            ("issues".into(), "https://first/".into()),
1408            ("issues".into(), "https://second/".into()),
1409        ];
1410        assert_eq!(
1411            doc.export_to_markdown_with(true),
1412            "[AI &amp; ML](https://a/) here, and [issues](https://first/) here, then [issues](https://second/) there.\n"
1413        );
1414    }
1415
1416    /// docling-core#698: the referenced-image destination is percent-encoded —
1417    /// upstream's own case table (paths, Windows flavours, UNC, URLs) plus
1418    /// idempotency on the encoded result.
1419    #[test]
1420    fn referenced_image_destinations_are_escaped() {
1421        let cases = [
1422            (
1423                "doc_artifacts/image_000001_ab12.png",
1424                "doc_artifacts/image_000001_ab12.png",
1425            ),
1426            (
1427                "My Report_artifacts/img.png",
1428                "My%20Report_artifacts/img.png",
1429            ),
1430            ("artifacts/img (1).png", "artifacts/img%20%281%29.png"),
1431            ("100%_scale/a#b?c.png", "100%_scale/a%23b%3Fc.png"),
1432            ("/home/a b/img.png", "/home/a%20b/img.png"),
1433            (
1434                "My Report_artifacts\\img.png",
1435                "My%20Report_artifacts/img.png",
1436            ),
1437            (
1438                "C:/Users/me/My Docs/img.png",
1439                "file:///C:/Users/me/My%20Docs/img.png",
1440            ),
1441            ("C:\\Users\\me\\img.png", "file:///C:/Users/me/img.png"),
1442            (
1443                "//server/share/My Docs/img.png",
1444                "file://server/share/My%20Docs/img.png",
1445            ),
1446            ("\\\\server\\share\\img.png", "file://server/share/img.png"),
1447            ("file:///home/a b/img.png", "file:///home/a%20b/img.png"),
1448            (
1449                "s3://bucket/My Report_artifacts/img.png",
1450                "s3://bucket/My%20Report_artifacts/img.png",
1451            ),
1452            (
1453                "https://example.com:8080/a b.png?w=1&h=2#frag",
1454                "https://example.com:8080/a%20b.png?w=1&h=2#frag",
1455            ),
1456            (
1457                "https://example.com/img (1).png",
1458                "https://example.com/img%20%281%29.png",
1459            ),
1460            ("caf\u{e9}/im\u{e4}ge.png", "caf%C3%A9/im%C3%A4ge.png"),
1461        ];
1462        for (input, expected) in cases {
1463            assert_eq!(escape_uri_path(input), expected, "input {input:?}");
1464            assert_eq!(
1465                escape_uri_path(expected),
1466                expected,
1467                "idempotent {expected:?}"
1468            );
1469        }
1470        // The whole marker, through the referenced-image export.
1471        let mut doc = DoclingDocument::new("t");
1472        doc.push(Node::Picture {
1473            caption: None,
1474            caption_href: None,
1475            image: Some(PictureImage {
1476                dpi: PictureImage::DEFAULT_DPI,
1477                mimetype: "image/png".into(),
1478                width: 1,
1479                height: 1,
1480                data: b"x".to_vec(),
1481            }),
1482            classification: None,
1483            caption_parent: Default::default(),
1484        });
1485        let (md, files) = doc
1486            .export_to_markdown_with_images(ImageMode::Referenced, "My Report (final)_artifacts");
1487        assert!(
1488            md.contains("![Image](My%20Report%20%28final%29_artifacts/image_000000.png)"),
1489            "got:\n{md}"
1490        );
1491        // The file path handed back for writing stays unescaped.
1492        assert_eq!(files[0].0, "My Report (final)_artifacts/image_000000.png");
1493    }
1494
1495    /// Pictures the HTML backend folds into a list item print after the item
1496    /// line with plain newlines; a `<br>` newline in the item's own text is
1497    /// still a GFM hard line break.
1498    #[test]
1499    fn folded_list_item_pictures_keep_plain_newlines() {
1500        let doc = DoclingDocument::new("t");
1501        let ctx = Ctx::new(&doc, false, &MarkdownExportOptions::default());
1502        assert_eq!(
1503            list_item_text("Step\n<!-- image -->", &ctx),
1504            "Step\n<!-- image -->"
1505        );
1506        assert_eq!(
1507            list_item_text("Step\nAlt text\n<!-- image -->\n<!-- image -->", &ctx),
1508            "Step\nAlt text\n<!-- image -->\n<!-- image -->"
1509        );
1510        assert_eq!(
1511            list_item_text("line one\nline two", &ctx),
1512            "line one  \nline two"
1513        );
1514    }
1515
1516    /// docling-core#723: the header block is the leading run of rows on which a
1517    /// `column_header` cell starts, flattened per column with " - ".
1518    #[test]
1519    fn stacked_header_rows_flatten_into_one() {
1520        let mut t = Table {
1521            rows: vec![
1522                vec!["".into(), "% of Total".into(), "% of Total".into()],
1523                vec!["class".into(), "Train".into(), "Test".into()],
1524                vec!["Caption".into(), "2.04".into(), "1.77".into()],
1525            ],
1526            ..Default::default()
1527        };
1528        t.structure = Some(TableStructure {
1529            header_row: vec![true, true, false],
1530            col_continuation: vec![
1531                vec![false, false, true],
1532                vec![false, false, false],
1533                vec![false, false, false],
1534            ],
1535            ..Default::default()
1536        });
1537        assert_eq!(t.header_row_count(), 2);
1538        assert_eq!(
1539            render_table(&t, true),
1540            "| class | % of Total - Train | % of Total - Test |\n| - | - | - |\n| Caption | 2.04 | 1.77 |"
1541        );
1542        // padded: widths from the flattened header, alignment from body rows
1543        assert_eq!(
1544            render_table(&t, false),
1545            "| class   |   % of Total - Train |   % of Total - Test |\n\
1546             |---------|----------------------|---------------------|\n\
1547             | Caption |                 2.04 |                1.77 |"
1548        );
1549    }
1550
1551    /// A header spanning two rows is repeated into the second row by the grid;
1552    /// that row is not a header row unless another header cell starts there.
1553    #[test]
1554    fn vertically_spanning_header_does_not_extend_the_block() {
1555        let mut t = Table {
1556            rows: vec![
1557                vec!["Name".into(), "Value".into()],
1558                vec!["Name".into(), "1".into()],
1559                vec!["x".into(), "2".into()],
1560            ],
1561            ..Default::default()
1562        };
1563        t.structure = Some(TableStructure {
1564            col_header: vec![vec![true, true], vec![true, false], vec![false, false]],
1565            row_continuation: vec![vec![false, false], vec![true, false], vec![false, false]],
1566            ..Default::default()
1567        });
1568        assert_eq!(t.header_row_count(), 1);
1569        assert_eq!(
1570            render_table(&t, true),
1571            "| Name | Value |\n| - | - |\n| Name | 1 |\n| x | 2 |"
1572        );
1573    }
1574
1575    /// Flags that begin on a later row promote nothing: every row stays in the
1576    /// body under an empty header row (tabulate's `headers=["", ""]`).
1577    #[test]
1578    fn header_flags_not_on_row_zero_keep_all_rows_in_the_body() {
1579        let mut t = Table {
1580            rows: vec![
1581                vec!["1".into(), "2".into()],
1582                vec!["a".into(), "b".into()],
1583                vec!["333".into(), "4".into()],
1584            ],
1585            ..Default::default()
1586        };
1587        t.structure = Some(TableStructure {
1588            header_row: vec![false, true, false],
1589            ..Default::default()
1590        });
1591        assert_eq!(t.header_row_count(), 0);
1592        assert_eq!(
1593            render_table(&t, false),
1594            "|     |    |\n|-----|----|\n| 1   | 2  |\n| a   | b  |\n| 333 | 4  |"
1595        );
1596    }
1597
1598    /// A pivot table's row headers (`<th rowspan>`) carry `row_header`, not
1599    /// `column_header` (docling#4216), so the data row beside them is not
1600    /// pulled into the header block — what this port used to reach with a
1601    /// deviation now falls out of the flags themselves.
1602    #[test]
1603    fn pivot_row_headers_do_not_extend_the_header() {
1604        let mut t = Table {
1605            rows: vec![
1606                vec!["Year".into(), "Month".into()],
1607                vec!["2025".into(), "January".into()],
1608                vec!["2025".into(), "February".into()],
1609            ],
1610            ..Default::default()
1611        };
1612        t.structure = Some(TableStructure {
1613            col_header: vec![vec![true, true], vec![false, false], vec![false, false]],
1614            row_header: vec![vec![false, false], vec![true, false], vec![true, false]],
1615            row_continuation: vec![vec![false, false], vec![false, false], vec![true, false]],
1616            ..Default::default()
1617        });
1618        assert_eq!(t.header_row_count(), 1);
1619        assert_eq!(
1620            render_table(&t, true),
1621            "| Year | Month |\n| - | - |\n| 2025 | January |\n| 2025 | February |"
1622        );
1623    }
1624
1625    /// No `column_header` anywhere (first-class cells without flags) → row 0
1626    /// stays the header, as before.
1627    #[test]
1628    fn unflagged_cells_keep_row_zero_as_header() {
1629        let mut t = Table {
1630            rows: vec![vec!["h".into()], vec!["d".into()]],
1631            ..Default::default()
1632        };
1633        t.cells = Some(
1634            [(0usize, "h"), (1, "d")]
1635                .into_iter()
1636                .map(|(r, text)| TableCell {
1637                    text: text.into(),
1638                    bbox: None,
1639                    start_row: r,
1640                    start_col: 0,
1641                    row_span: 1,
1642                    col_span: 1,
1643                    column_header: false,
1644                    row_header: false,
1645                    row_section: false,
1646                })
1647                .collect(),
1648        );
1649        assert_eq!(t.header_row_count(), 1);
1650        assert_eq!(render_table(&t, true), "| h |\n| - |\n| d |");
1651    }
1652
1653    #[test]
1654    fn renders_compact_table() {
1655        let mut doc = DoclingDocument::new("t");
1656        // The compact form is opt-in (the PDF backend sets it); default output uses
1657        // the padded GitHub serializer (covered by the regression fixtures).
1658        doc.compact_tables = true;
1659        doc.push(Node::Table(Table {
1660            rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
1661            location: None,
1662            structure: None,
1663            cell_blocks: None,
1664            cells: None,
1665            caption: None,
1666            caption_parent: Default::default(),
1667        }));
1668        let md = doc.export_to_markdown();
1669        assert_eq!(md, "| a | b |\n| - | - |\n| 1 | 2 |\n");
1670    }
1671
1672    #[test]
1673    fn renders_padded_github_table_by_default() {
1674        let mut doc = DoclingDocument::new("t");
1675        doc.push(Node::Table(Table {
1676            rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
1677            location: None,
1678            structure: None,
1679            cell_blocks: None,
1680            cells: None,
1681            caption: None,
1682            caption_parent: Default::default(),
1683        }));
1684        let md = doc.export_to_markdown();
1685        // Numeric data columns are right-aligned; columns padded to header+2.
1686        assert_eq!(md, "|   a |   b |\n|-----|-----|\n|   1 |   2 |\n");
1687    }
1688
1689    #[test]
1690    fn strict_unescapes_inline_underscores_legacy_keeps_them() {
1691        let mut doc = DoclingDocument::new("t");
1692        doc.add_heading(1, "a\\_b");
1693        doc.add_paragraph("x\\_y");
1694        doc.push(Node::ListItem {
1695            ordered: false,
1696            number: 1,
1697            first_in_list: true,
1698            text: "i\\_j".into(),
1699            level: 0,
1700            marker: None,
1701            location: None,
1702            dclx: None,
1703            href: None,
1704            layer: None,
1705        });
1706        // Legacy reproduces docling's `\_` escaping byte-for-byte.
1707        assert_eq!(doc.export_to_markdown(), "# a\\_b\n\nx\\_y\n\n- i\\_j\n");
1708        // Strict prefers literal underscores (Rust-only readability mode).
1709        assert_eq!(doc.export_to_markdown_with(true), "# a_b\n\nx_y\n\n- i_j\n");
1710    }
1711
1712    /// Drive a document's nodes through [`MarkdownStreamer`] in the given page
1713    /// splits and assert the concatenated chunks equal the buffered serializer.
1714    fn assert_stream_matches(
1715        doc: &DoclingDocument,
1716        strict: bool,
1717        images: ImageMode,
1718        splits: &[usize],
1719    ) {
1720        let (want, want_artifacts) = to_markdown_images(doc, strict, images, "artifacts");
1721        let mut streamer =
1722            MarkdownStreamer::with_artifacts(strict, images, doc.compact_tables, "artifacts")
1723                .with_page_break_placeholder(doc.page_break_placeholder.clone());
1724        let mut got = String::new();
1725        let mut got_artifacts = Vec::new();
1726        let mut start = 0;
1727        for &end in splits {
1728            // Links only matter in strict mode; feed them all with the first batch
1729            // that has content (document order is preserved by the queue).
1730            let links = if start == 0 {
1731                doc.links.as_slice()
1732            } else {
1733                &[]
1734            };
1735            got.push_str(&streamer.push(&doc.nodes[start..end], links));
1736            // Referenced mode: drain per push, as a real caller writing files
1737            // page by page would — numbering must continue across drains.
1738            got_artifacts.extend(streamer.take_artifacts());
1739            start = end;
1740        }
1741        got.push_str(&streamer.push(
1742            &doc.nodes[start..],
1743            if start == 0 {
1744                doc.links.as_slice()
1745            } else {
1746                &[]
1747            },
1748        ));
1749        got_artifacts.extend(streamer.take_artifacts());
1750        got.push_str(&streamer.finish());
1751        assert_eq!(
1752            got, want,
1753            "streamed output diverged (splits={splits:?}, strict={strict})"
1754        );
1755        assert_eq!(
1756            got_artifacts, want_artifacts,
1757            "streamed artifacts diverged (splits={splits:?}, strict={strict})"
1758        );
1759    }
1760
1761    #[test]
1762    fn streaming_is_byte_identical_to_buffered() {
1763        let mut doc = DoclingDocument::new("d");
1764        doc.add_heading(1, "Title");
1765        doc.add_paragraph("First paragraph.");
1766        doc.push(Node::ListItem {
1767            ordered: false,
1768            number: 1,
1769            first_in_list: true,
1770            text: "a".into(),
1771            level: 0,
1772            marker: None,
1773            location: None,
1774            dclx: None,
1775            href: None,
1776            layer: None,
1777        });
1778        doc.push(Node::ListItem {
1779            ordered: false,
1780            number: 2,
1781            first_in_list: false,
1782            text: "b".into(),
1783            level: 0,
1784            marker: None,
1785            location: None,
1786            dclx: None,
1787            href: None,
1788            layer: None,
1789        });
1790        doc.push(Node::Code {
1791            language: Some("rust".into()),
1792            text: "let x = 1;".into(),
1793            orig: None,
1794            pretty: None,
1795        });
1796        doc.push(Node::Table(Table {
1797            rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
1798            location: None,
1799            structure: None,
1800            cell_blocks: None,
1801            cells: None,
1802            caption: None,
1803            caption_parent: Default::default(),
1804        }));
1805        doc.push(Node::Picture {
1806            caption: Some("Fig 1".into()),
1807            caption_href: None,
1808            image: Some(PictureImage {
1809                dpi: PictureImage::DEFAULT_DPI,
1810                mimetype: "image/png".into(),
1811                width: 2,
1812                height: 2,
1813                data: b"png-one".to_vec(),
1814            }),
1815            classification: None,
1816            caption_parent: Default::default(),
1817        });
1818        doc.add_paragraph("Last paragraph.");
1819        // A second embedded picture, so referenced mode must keep numbering
1820        // (`image_000001`) across chunk boundaries.
1821        doc.push(Node::Picture {
1822            caption: None,
1823            caption_href: None,
1824            image: Some(PictureImage {
1825                dpi: PictureImage::DEFAULT_DPI,
1826                mimetype: "image/png".into(),
1827                width: 2,
1828                height: 2,
1829                data: b"png-two".to_vec(),
1830            }),
1831            classification: None,
1832            caption_parent: Default::default(),
1833        });
1834
1835        // A run of list items must never straddle a split, so try splits that fall
1836        // on safe block boundaries (the streaming PDF assembler guarantees this).
1837        for &strict in &[false, true] {
1838            for &images in &[
1839                ImageMode::Placeholder,
1840                ImageMode::Embedded,
1841                ImageMode::Referenced,
1842            ] {
1843                for splits in [&[][..], &[1][..], &[2][..], &[4][..], &[1, 4, 6, 7][..]] {
1844                    assert_stream_matches(&doc, strict, images, splits);
1845                }
1846            }
1847        }
1848    }
1849
1850    #[test]
1851    fn streaming_applies_recovered_links_in_strict_mode() {
1852        let mut doc = DoclingDocument::new("d");
1853        doc.add_paragraph("See LinkedIn for details.");
1854        doc.add_paragraph("And GitHub too.");
1855        doc.links = vec![
1856            ("LinkedIn".into(), "https://lnkd/".into()),
1857            ("GitHub".into(), "https://gh/".into()),
1858        ];
1859        // The second anchor lives in the second block, so it must be carried across
1860        // the page boundary and placed when that block streams out.
1861        assert_stream_matches(&doc, true, ImageMode::Placeholder, &[1]);
1862    }
1863
1864    /// A three-page document with one empty page in the middle and page
1865    /// markers of both kinds, as the backends emit them.
1866    fn paged_doc() -> DoclingDocument {
1867        let mut doc = DoclingDocument::new("p");
1868        doc.push(Node::PageInfo {
1869            page_no: 1,
1870            width: 100.0,
1871            height: 100.0,
1872        });
1873        doc.add_heading(1, "Title");
1874        doc.add_paragraph("Page one.");
1875        // Page two: a marker plus furniture only — renders nothing.
1876        doc.push(Node::PageBreak);
1877        doc.push(Node::PageInfo {
1878            page_no: 2,
1879            width: 100.0,
1880            height: 100.0,
1881        });
1882        doc.push(Node::PageFurniture {
1883            footer: true,
1884            location: [0, 500, 511, 511],
1885            text: "2".into(),
1886        });
1887        doc.push(Node::PageBreak);
1888        doc.push(Node::PageInfo {
1889            page_no: 3,
1890            width: 100.0,
1891            height: 100.0,
1892        });
1893        doc.add_paragraph("Page three.");
1894        // A trailing boundary with nothing after it.
1895        doc.push(Node::PageBreak);
1896        doc
1897    }
1898
1899    #[test]
1900    fn page_break_placeholder_lands_between_pages_only() {
1901        let mut doc = paged_doc();
1902        // Off by default: docling's Markdown carries no page breaks.
1903        assert_eq!(
1904            doc.export_to_markdown(),
1905            "# Title\n\nPage one.\n\nPage three.\n"
1906        );
1907        doc.page_break_placeholder = Some("<!-- page break -->".into());
1908        // One break for the 1→3 transition (the empty page 2 and the doubled
1909        // PageBreak+PageInfo markers collapse), none before the first block,
1910        // none for the trailing boundary.
1911        assert_eq!(
1912            doc.export_to_markdown(),
1913            "# Title\n\nPage one.\n\n<!-- page break -->\n\nPage three.\n"
1914        );
1915        // An empty placeholder is still a (blank) part, as upstream's
1916        // `str.replace(marker, "")` leaves the delimiters around it.
1917        doc.page_break_placeholder = Some(String::new());
1918        assert_eq!(
1919            doc.export_to_markdown(),
1920            "# Title\n\nPage one.\n\n\n\nPage three.\n"
1921        );
1922    }
1923
1924    #[test]
1925    fn page_break_placeholder_never_leads_a_single_page() {
1926        let mut doc = DoclingDocument::new("one");
1927        doc.page_break_placeholder = Some("---".into());
1928        doc.push(Node::PageBreak);
1929        doc.push(Node::PageInfo {
1930            page_no: 1,
1931            width: 10.0,
1932            height: 10.0,
1933        });
1934        doc.add_paragraph("Only page.");
1935        assert_eq!(doc.export_to_markdown(), "Only page.\n");
1936        // Two boundaries with no content between them: still one break.
1937        doc.push(Node::PageBreak);
1938        doc.push(Node::PageBreak);
1939        doc.add_paragraph("Next.");
1940        assert_eq!(doc.export_to_markdown(), "Only page.\n\n---\n\nNext.\n");
1941    }
1942
1943    #[test]
1944    fn page_break_placeholder_streams_byte_identical() {
1945        let mut doc = paged_doc();
1946        doc.page_break_placeholder = Some("<!-- page break -->".into());
1947        // Split at every page marker (how the PDF pipeline pushes page batches)
1948        // and at odd places inside a page: the pending break must survive a
1949        // push that renders nothing (page two) and land on page three's block.
1950        for splits in [
1951            &[3usize][..],
1952            &[3, 6],
1953            &[3, 6, 8],
1954            &[1, 2, 3, 4, 5, 6, 7, 8, 9],
1955            &[8],
1956        ] {
1957            assert_stream_matches(&doc, false, ImageMode::Placeholder, splits);
1958            assert_stream_matches(&doc, true, ImageMode::Placeholder, splits);
1959        }
1960    }
1961
1962    #[test]
1963    fn strict_tightens_punctuation_spacing_legacy_keeps_it() {
1964        let mut doc = DoclingDocument::new("t");
1965        doc.add_paragraph("see [ 37 , 36 ] and ( x ) .");
1966        // Legacy keeps docling's spacing byte-for-byte.
1967        assert_eq!(doc.export_to_markdown(), "see [ 37 , 36 ] and ( x ) .\n");
1968        // Strict tightens punctuation for readable Markdown.
1969        assert_eq!(doc.export_to_markdown_with(true), "see [37, 36] and (x).\n");
1970    }
1971
1972    // ----- MarkdownExportOptions (#599) ------------------------------------
1973
1974    /// A document touching every option: furniture, a comment, a hidden
1975    /// sheet group, a picture with nested text, escaped text.
1976    fn options_doc() -> DoclingDocument {
1977        use crate::document::ContentLayer;
1978        let mut doc = DoclingDocument::new("opts");
1979        doc.push(Node::PageFurniture {
1980            footer: false,
1981            location: [0, 0, 0, 0],
1982            text: "FAX COVER \\_ R&amp;D".into(),
1983        });
1984        doc.add_heading(1, "R&amp;D \\_ report");
1985        doc.add_paragraph("Body a\\_b &lt;tag&gt;");
1986        doc.push(Node::Picture {
1987            caption: Some("Figure 1".into()),
1988            caption_href: None,
1989            image: None,
1990            classification: None,
1991            caption_parent: Default::default(),
1992        });
1993        doc.push(Node::PictureChildren(vec![
1994            Node::Paragraph {
1995                text: "Field: Name".into(),
1996            },
1997            Node::Paragraph {
1998                text: "Field: Date".into(),
1999            },
2000        ]));
2001        doc.push(Node::CommentSection {
2002            name: "comment-1".into(),
2003            text: "[author: A]: note".into(),
2004            refs_note_text: false,
2005            grouped: true,
2006        });
2007        doc.push(Node::Group {
2008            label: "section".into(),
2009            name: Some("sheet: Hidden".into()),
2010            layer: Some(ContentLayer::Invisible),
2011            children: vec![Node::Paragraph {
2012                text: "hidden cell".into(),
2013            }],
2014        });
2015        doc.push(Node::Furniture {
2016            layer: ContentLayer::Furniture,
2017            inner: Box::new(Node::Paragraph {
2018                text: "Page 1 of 2".into(),
2019            }),
2020        });
2021        doc.push(Node::FurnitureText {
2022            label: "page_footer".into(),
2023            text: "footer line".into(),
2024        });
2025        doc
2026    }
2027
2028    /// The defaults are docling's: the options export is the plain export,
2029    /// byte for byte — and the plain export is unchanged (furniture, notes,
2030    /// hidden sheets and picture children out; escaping kept).
2031    #[test]
2032    fn default_options_are_the_default_export() {
2033        let doc = options_doc();
2034        let plain = doc.export_to_markdown();
2035        assert_eq!(
2036            plain,
2037            "# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n"
2038        );
2039        let (with_default, artifacts) =
2040            doc.export_to_markdown_with_options(&MarkdownExportOptions::default());
2041        assert_eq!(with_default, plain);
2042        assert!(artifacts.is_empty());
2043    }
2044
2045    #[test]
2046    fn layers_render_furniture_notes_and_hidden_sheets() {
2047        use crate::document::ContentLayer;
2048        let doc = options_doc();
2049        let md = |layers: ContentLayers| {
2050            doc.export_to_markdown_with_options(&MarkdownExportOptions {
2051                layers,
2052                ..MarkdownExportOptions::default()
2053            })
2054            .0
2055        };
2056        assert_eq!(
2057            md(ContentLayers::BODY.with(ContentLayer::Furniture)),
2058            "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",
2059            "a page header, a furniture-wrapped paragraph and a furniture text item render as paragraphs"
2060        );
2061        assert_eq!(
2062            md(ContentLayers::BODY.with(ContentLayer::Notes)),
2063            "# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n\n[author: A]: note\n"
2064        );
2065        assert_eq!(
2066            md(ContentLayers::BODY.with(ContentLayer::Invisible)),
2067            "# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n\nhidden cell\n",
2068            "a hidden sheet's items sit on the sheet's layer"
2069        );
2070        assert_eq!(
2071            md(ContentLayers::NONE.with(ContentLayer::Furniture)),
2072            "FAX COVER \\_ R&amp;D\n\nPage 1 of 2\n\nfooter line\n",
2073            "without the body layer only the furniture prints"
2074        );
2075        assert_eq!(md(ContentLayers::NONE), "");
2076        assert_eq!(
2077            md(ContentLayers::ALL),
2078            "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"
2079        );
2080    }
2081
2082    /// A list item carries its own layer: a furniture list renders with the
2083    /// furniture layer and stays out of the body export.
2084    #[test]
2085    fn layers_apply_to_list_items() {
2086        use crate::document::ContentLayer;
2087        let mut doc = DoclingDocument::new("lists");
2088        doc.push(Node::ListItem {
2089            ordered: false,
2090            number: 1,
2091            first_in_list: true,
2092            text: "nav link".into(),
2093            level: 0,
2094            marker: None,
2095            location: None,
2096            dclx: None,
2097            href: None,
2098            layer: Some(ContentLayer::Furniture),
2099        });
2100        doc.add_paragraph("body");
2101        assert_eq!(doc.export_to_markdown(), "body\n");
2102        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2103            layers: ContentLayers::BODY.with(ContentLayer::Furniture),
2104            ..MarkdownExportOptions::default()
2105        });
2106        assert_eq!(md, "- nav link\n\nbody\n");
2107    }
2108
2109    #[test]
2110    fn traverse_pictures_prints_the_nested_text_after_the_picture() {
2111        let doc = options_doc();
2112        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2113            traverse_pictures: true,
2114            ..MarkdownExportOptions::default()
2115        });
2116        assert_eq!(
2117            md,
2118            "# R&amp;D \\_ report\n\nBody a\\_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n\nField: Name\n\nField: Date\n"
2119        );
2120    }
2121
2122    #[test]
2123    fn escaping_can_be_turned_off_per_kind() {
2124        let doc = options_doc();
2125        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2126            escape_html: false,
2127            ..MarkdownExportOptions::default()
2128        });
2129        assert_eq!(
2130            md,
2131            "# R&D \\_ report\n\nBody a\\_b <tag>\n\nFigure 1\n\n<!-- image -->\n"
2132        );
2133        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2134            escape_underscores: false,
2135            ..MarkdownExportOptions::default()
2136        });
2137        assert_eq!(
2138            md,
2139            "# R&amp;D _ report\n\nBody a_b &lt;tag&gt;\n\nFigure 1\n\n<!-- image -->\n"
2140        );
2141        // Code and table cells are never touched (upstream leaves them
2142        // unescaped too, so nothing was escaped there to undo).
2143        let mut doc = DoclingDocument::new("code");
2144        doc.push(Node::Code {
2145            language: None,
2146            text: "a &amp; b \\_ c".into(),
2147            orig: None,
2148            pretty: None,
2149        });
2150        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2151            escape_html: false,
2152            escape_underscores: false,
2153            ..MarkdownExportOptions::default()
2154        });
2155        assert_eq!(md, "```\na &amp; b \\_ c\n```\n");
2156    }
2157
2158    #[test]
2159    fn image_placeholder_is_configurable() {
2160        let doc = options_doc();
2161        let (md, _) = doc.export_to_markdown_with_options(&MarkdownExportOptions {
2162            image_placeholder: "[figure]".into(),
2163            ..MarkdownExportOptions::default()
2164        });
2165        assert!(md.contains("Figure 1\n\n[figure]\n"), "{md}");
2166        assert!(!md.contains("<!-- image -->"));
2167    }
2168
2169    /// The streamer takes the same options and its chunks concatenate to the
2170    /// buffered export.
2171    #[test]
2172    fn streamer_matches_buffered_export_with_options() {
2173        use crate::document::ContentLayer;
2174        let doc = options_doc();
2175        let options = MarkdownExportOptions {
2176            layers: ContentLayers::ALL,
2177            traverse_pictures: true,
2178            escape_html: false,
2179            escape_underscores: false,
2180            image_placeholder: "(img)".into(),
2181            ..MarkdownExportOptions::default()
2182        };
2183        let (buffered, _) = doc.export_to_markdown_with_options(&options);
2184        let mut streamer = MarkdownStreamer::new(false, ImageMode::Placeholder, false)
2185            .with_export_options(&options);
2186        let mut out = String::new();
2187        for chunk in doc.nodes.chunks(2) {
2188            out.push_str(&streamer.push(chunk, &[]));
2189        }
2190        out.push_str(&streamer.finish());
2191        assert_eq!(out, buffered);
2192        assert!(
2193            buffered.contains("(img)") && buffered.contains("R&D _ report"),
2194            "{buffered}"
2195        );
2196        let _ = ContentLayer::Notes;
2197    }
2198}