docling_core/markdown.rs
1//! Markdown serializer for [`DoclingDocument`].
2
3use crate::document::{DoclingDocument, Node, Table};
4
5/// How pictures are rendered (mirrors docling-core's `ImageRefMode`).
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum ImageMode {
8 /// `<!-- image -->` (docling's default, and the only mode without image data).
9 #[default]
10 Placeholder,
11 /// `` — self-contained.
12 Embedded,
13 /// ``; the bytes are returned for the
14 /// caller to write.
15 Referenced,
16}
17
18/// Serializer state threaded through the render walk.
19struct Ctx {
20 strict: bool,
21 /// Emit compact `| a | b |` tables instead of the padded GitHub serializer.
22 compact_tables: bool,
23 images: ImageMode,
24 artifacts_dir: String,
25 /// (relative path, bytes) for each referenced image — written by the caller.
26 artifacts: Vec<(String, Vec<u8>)>,
27 pic_index: usize,
28 /// Rendering the block content of a rich table cell (docling-core 2.94's
29 /// `in_table_cell`, docling-core#540): a heading has no valid Markdown
30 /// form inside a table, so it renders as plain text without `#` markers.
31 in_table_cell: bool,
32}
33
34/// Render a document to a Markdown string (pictures as placeholders).
35///
36/// `strict` selects the serializer-level behaviours that differ between
37/// docling-legacy output and cleaner Markdown — currently the code-fence
38/// language (legacy drops it, strict keeps it).
39pub fn to_markdown(doc: &DoclingDocument, strict: bool) -> String {
40 to_markdown_images(doc, strict, ImageMode::Placeholder, "artifacts").0
41}
42
43/// Render to Markdown with an explicit picture [`ImageMode`]. Returns the
44/// Markdown and, for [`ImageMode::Referenced`], the `(path, bytes)` of each image
45/// the caller should write (relative to the Markdown file).
46pub fn to_markdown_images(
47 doc: &DoclingDocument,
48 strict: bool,
49 images: ImageMode,
50 artifacts_dir: &str,
51) -> (String, Vec<(String, Vec<u8>)>) {
52 let mut ctx = Ctx {
53 strict,
54 compact_tables: doc.compact_tables,
55 images,
56 artifacts_dir: artifacts_dir.to_string(),
57 artifacts: Vec::new(),
58 pic_index: 0,
59 in_table_cell: false,
60 };
61 let mut blocks: Vec<String> = Vec::new();
62 render(&doc.nodes, &mut blocks, &mut ctx);
63 let mut body = blocks.join("\n\n");
64 // Strict mode only: turn recovered source hyperlinks into Markdown links.
65 // docling's standard pipeline drops them, so doing this in legacy mode would
66 // diverge from docling — hence strict-only, leaving conformance output intact.
67 if strict && !doc.links.is_empty() {
68 body = apply_links(&body, &doc.links);
69 }
70 let md = if body.is_empty() {
71 String::new()
72 } else {
73 format!("{body}\n")
74 };
75 (md, ctx.artifacts)
76}
77
78/// Render the block content of a *rich table cell* to Markdown — what
79/// docling-core's table serializer does for a `RichTableCell`
80/// (`doc_serializer.serialize(item, in_table_cell=True)`): the cell's
81/// paragraphs, lists and flattened nested tables render as in a document, but a
82/// heading loses its `#` markers (docling-core#540 — the Markdown spec has no
83/// headings inside tables). Pictures stay placeholders. The caller flattens the
84/// result into its cell text; the table serializer later turns the newlines
85/// into spaces.
86pub fn to_markdown_table_cell(doc: &DoclingDocument, strict: bool) -> String {
87 let mut ctx = Ctx {
88 strict,
89 compact_tables: doc.compact_tables,
90 images: ImageMode::Placeholder,
91 artifacts_dir: String::new(),
92 artifacts: Vec::new(),
93 pic_index: 0,
94 in_table_cell: true,
95 };
96 let mut blocks: Vec<String> = Vec::new();
97 render(&doc.nodes, &mut blocks, &mut ctx);
98 blocks.join("\n\n")
99}
100
101/// Wrap each recovered link's anchor text in Markdown `[anchor](href)`. Anchors
102/// arrive cleaned (curly quotes/dashes already normalized) but un-escaped, so we
103/// match against the body's HTML-escaped (`&`/`<`/`>`) form, the way prose nodes
104/// were serialized. Links are consumed in document order from a moving cursor, so
105/// a repeated anchor (e.g. two "issues") links its successive occurrences rather
106/// than all pointing at the first. An anchor that can't be located is skipped
107/// (its text may have been split across a line wrap or table cell).
108fn apply_links(body: &str, links: &[(String, String)]) -> String {
109 let mut out = body.to_string();
110 let mut cursor = 0usize;
111 for (anchor, href) in links {
112 let anchor = anchor
113 .replace('&', "&")
114 .replace('<', "<")
115 .replace('>', ">");
116 if anchor.is_empty() {
117 continue;
118 }
119 if let Some(rel) = out[cursor..].find(&anchor) {
120 let at = cursor + rel;
121 // Don't relink inside an already-emitted `](` Markdown link target.
122 let replacement = format!("[{anchor}]({href})");
123 out.replace_range(at..at + anchor.len(), &replacement);
124 cursor = at + replacement.len();
125 }
126 }
127 out
128}
129
130/// Like [`apply_links`] but over a single chunk, consuming from a shared queue so
131/// the same `[anchor](href)` rewriting can be applied incrementally as Markdown is
132/// streamed out. Each queued link is matched (in document order) against `chunk`
133/// and rewritten in place; a link whose anchor is not in this chunk is carried
134/// forward in the queue for a later chunk. Anchors are recovered in document
135/// order and a chunk is always a contiguous run of whole blocks, so this
136/// reproduces [`apply_links`]' single moving cursor: the link lands in whichever
137/// chunk contains its anchor, identically to the buffered path. (A link whose
138/// anchor never appears is carried to the end and dropped — the same no-op
139/// `apply_links` performs for an unlocatable anchor.)
140fn apply_links_chunk(chunk: &str, queue: &mut Vec<(String, String)>) -> String {
141 let mut out = chunk.to_string();
142 let mut cursor = 0usize;
143 let mut carried: Vec<(String, String)> = Vec::new();
144 for (anchor_raw, href) in std::mem::take(queue) {
145 let anchor = anchor_raw
146 .replace('&', "&")
147 .replace('<', "<")
148 .replace('>', ">");
149 if anchor.is_empty() {
150 continue;
151 }
152 if let Some(rel) = out[cursor..].find(&anchor) {
153 let at = cursor + rel;
154 let replacement = format!("[{anchor}]({href})");
155 out.replace_range(at..at + anchor.len(), &replacement);
156 cursor = at + replacement.len();
157 } else {
158 // Not in this chunk; try again when its block is flushed.
159 carried.push((anchor_raw, href));
160 }
161 }
162 *queue = carried;
163 out
164}
165
166/// Incremental Markdown serializer: feed finalized, in-document-order batches of
167/// [`Node`]s and receive Markdown chunks whose concatenation is **byte-identical**
168/// to [`to_markdown_images`] over the same nodes. This is the streaming
169/// counterpart of the buffered serializer — used to emit a document's Markdown in
170/// chunks (e.g. page by page, as the parallel PDF pipeline finishes pages) instead
171/// of building the whole string up front.
172///
173/// [`ImageMode::Placeholder`] and [`ImageMode::Embedded`] render inline.
174/// [`ImageMode::Referenced`] additionally hands each picture's bytes out through
175/// [`take_artifacts`](Self::take_artifacts) — construct with
176/// [`with_artifacts`](Self::with_artifacts) and drain after every push so the
177/// bytes can be written to disk as pages finish instead of accumulating for the
178/// whole document (issue #80's memory-bounded image handling).
179///
180/// Each [`push`](Self::push) must contain whole blocks in reading order: a caller
181/// must not split a run of list items across two pushes (the run would render as
182/// two separate lists). Finalized PDF page batches already satisfy this.
183pub struct MarkdownStreamer {
184 strict: bool,
185 images: ImageMode,
186 compact_tables: bool,
187 /// Whether any non-empty chunk has been emitted yet (drives `\n\n` joins and
188 /// the trailing newline).
189 emitted_any: bool,
190 /// Recovered links not yet placed (strict mode), consumed in document order.
191 links: Vec<(String, String)>,
192 /// Referenced mode: the link prefix, the not-yet-drained `(path, bytes)`
193 /// artifacts, and the running image number (continues across pushes so the
194 /// stream matches the buffered serializer's `image_000000…` numbering).
195 artifacts_dir: String,
196 artifacts: Vec<(String, Vec<u8>)>,
197 pic_index: usize,
198}
199
200impl MarkdownStreamer {
201 /// Create a streamer. `compact_tables` mirrors [`DoclingDocument::compact_tables`].
202 /// For [`ImageMode::Referenced`] use [`with_artifacts`](Self::with_artifacts).
203 pub fn new(strict: bool, images: ImageMode, compact_tables: bool) -> Self {
204 debug_assert!(
205 images != ImageMode::Referenced,
206 "referenced image mode needs an artifacts dir; use with_artifacts"
207 );
208 Self::with_artifacts(strict, images, compact_tables, "artifacts")
209 }
210
211 /// Like [`new`](Self::new) but with the artifacts link prefix, allowing
212 /// [`ImageMode::Referenced`]: pictures render as
213 /// `` and each push's image
214 /// bytes wait in [`take_artifacts`](Self::take_artifacts) for the caller to
215 /// write. The concatenated chunks and the artifact list match the buffered
216 /// [`to_markdown_images`] byte-for-byte.
217 pub fn with_artifacts(
218 strict: bool,
219 images: ImageMode,
220 compact_tables: bool,
221 artifacts_dir: &str,
222 ) -> Self {
223 Self {
224 strict,
225 images,
226 compact_tables,
227 emitted_any: false,
228 links: Vec::new(),
229 artifacts_dir: artifacts_dir.to_string(),
230 artifacts: Vec::new(),
231 pic_index: 0,
232 }
233 }
234
235 /// The `(relative path, bytes)` of images rendered by pushes since the last
236 /// drain ([`ImageMode::Referenced`] only — empty otherwise). Paths are
237 /// relative to the Markdown file, i.e. they start with the configured
238 /// artifacts dir.
239 pub fn take_artifacts(&mut self) -> Vec<(String, Vec<u8>)> {
240 std::mem::take(&mut self.artifacts)
241 }
242
243 /// Render one finalized batch of nodes (plus any links recovered from the same
244 /// span, in document order) into the next Markdown chunk. Returns an empty
245 /// string when the batch produces no output (e.g. empty tables/pictures), in
246 /// which case nothing should be written.
247 pub fn push(&mut self, nodes: &[Node], links: &[(String, String)]) -> String {
248 self.links.extend(links.iter().cloned());
249 let mut ctx = Ctx {
250 strict: self.strict,
251 compact_tables: self.compact_tables,
252 images: self.images,
253 artifacts_dir: std::mem::take(&mut self.artifacts_dir),
254 artifacts: std::mem::take(&mut self.artifacts),
255 pic_index: self.pic_index,
256 in_table_cell: false,
257 };
258 let mut blocks: Vec<String> = Vec::new();
259 render(nodes, &mut blocks, &mut ctx);
260 self.artifacts_dir = std::mem::take(&mut ctx.artifacts_dir);
261 self.artifacts = std::mem::take(&mut ctx.artifacts);
262 self.pic_index = ctx.pic_index;
263 if blocks.is_empty() {
264 return String::new();
265 }
266 let mut body = blocks.join("\n\n");
267 if self.strict && !self.links.is_empty() {
268 body = apply_links_chunk(&body, &mut self.links);
269 }
270 let chunk = if self.emitted_any {
271 format!("\n\n{body}")
272 } else {
273 body
274 };
275 self.emitted_any = true;
276 chunk
277 }
278
279 /// Emit the trailing newline that finishes the document (empty if no content
280 /// was produced). Call exactly once, after the final [`push`](Self::push).
281 pub fn finish(self) -> String {
282 if self.emitted_any {
283 "\n".to_string()
284 } else {
285 String::new()
286 }
287 }
288}
289
290/// In `strict` mode, rewrite inline text for readability rather than byte-for-byte
291/// docling fidelity: undo the legacy `\_` underscore escaping, and tighten stray
292/// spaces around punctuation (`[ 37 , 36 ]` → `[37, 36]`, `( x )` → `(x)`). This
293/// cleans up both the PDF backend's glyph-split spacing and the space the legacy
294/// emphasis serialization leaves before punctuation (`*a* ,` → `*a*,`).
295/// Legacy/default output keeps docling's spacing untouched. Only inline text
296/// nodes pass through here — code blocks and table cells are left alone.
297fn strict_text(text: &str, strict: bool) -> String {
298 if !strict {
299 return text.to_string();
300 }
301 text.replace("\\_", "_")
302 .replace(" ,", ",")
303 .replace(" .", ".")
304 .replace(" ;", ";")
305 .replace(" )", ")")
306 .replace("( ", "(")
307 .replace(" ]", "]")
308 .replace("[ ", "[")
309}
310
311/// docling-core 2.92's `_md_line_breaks` (docling-core#721): a single `\n`
312/// inside an item's text becomes a GFM hard line break (`" \n"`, two trailing
313/// spaces) so renderers honour it; a blank line (`\n\n`) is a paragraph break
314/// and stays as is — the document scope already joins blocks with `\n\n`.
315/// Applied to body text, list items and captions, never to code/formulas.
316fn md_line_breaks(text: &str) -> String {
317 if !text.contains('\n') {
318 return text.to_string();
319 }
320 text.split("\n\n")
321 .map(|para| para.replace('\n', " \n"))
322 .collect::<Vec<_>>()
323 .join("\n\n")
324}
325
326/// Undo [`md_line_breaks`] on a rich table cell's flattened Markdown so the
327/// non-Markdown exports (JSON `text`, LaTeX cells) see the cell's raw line
328/// breaks, as docling's do — a rich cell's text is its Markdown serialization
329/// in our model, and the two trailing spaces are a Markdown-only marker.
330pub(crate) fn strip_hard_breaks(text: &str) -> String {
331 if text.contains(" \n") {
332 text.replace(" \n", "\n")
333 } else {
334 text.to_string()
335 }
336}
337
338/// docling-core's `_heading_line_breaks`: a GFM heading cannot span lines, so a
339/// newline inside heading text collapses to a space (`# Hello World`, not a
340/// broken `# Hello\nWorld`).
341fn heading_line_breaks(text: &str) -> String {
342 text.replace('\n', " ")
343}
344
345fn render(nodes: &[Node], blocks: &mut Vec<String>, ctx: &mut Ctx) {
346 let mut i = 0;
347 while i < nodes.len() {
348 match &nodes[i] {
349 Node::ListItem { .. } => {
350 let start = i;
351 i += 1;
352 loop {
353 match nodes.get(i) {
354 Some(Node::ListItem { .. }) => i += 1,
355 // An empty paragraph between two list items is absorbed
356 // into the run — docling keeps such a ListGroup
357 // contiguous rather than splitting it.
358 Some(Node::Paragraph { text })
359 if text.is_empty()
360 && matches!(nodes.get(i + 1), Some(Node::ListItem { .. })) =>
361 {
362 i += 1
363 }
364 _ => break,
365 }
366 }
367 render_list_run(&nodes[start..i], blocks, ctx.strict);
368 }
369 other => {
370 render_one(other, blocks, ctx);
371 i += 1;
372 }
373 }
374 }
375}
376
377/// Render a contiguous run of list items.
378///
379/// Ordered items use their explicit `number`. A new sibling list (marked by
380/// `first_in_list`) at the same depth is separated by a blank line, matching
381/// docling-core's serializer.
382fn render_list_run(items: &[Node], blocks: &mut Vec<String>, strict: bool) {
383 let mut lines: Vec<String> = Vec::new();
384 // Per level, the previous item's (ordered, number) so we can detect a new
385 // sibling list.
386 let mut prev: Vec<Option<(bool, u64)>> = Vec::new();
387 // Whether the previous top-level item was a multilevel projection — an
388 // ordered `1.2.`-style item rendered as a Markdown bullet (docx's DocLang
389 // overlay says ordered, the flat field says bullet). Word numbers such an
390 // item and its parent-level successor within one list (same `numId`), and
391 // docling keeps them in one group — so the kind-flip / number-continuity
392 // breaks below must not fire across it (docling#3902's
393 // docx_list_blank_spacer: `- 1.2. Sub two` directly followed by
394 // `2. Second section`, no blank line).
395 let mut prev_projected = false;
396
397 for item in items {
398 let Node::ListItem {
399 ordered,
400 number,
401 first_in_list,
402 text,
403 level,
404 marker: _,
405 location: _,
406 dclx,
407 href: _,
408 layer,
409 } = item
410 else {
411 continue;
412 };
413 // A non-body (furniture) list item is omitted from Markdown, matching
414 // docling's content-layer filtering.
415 if layer.is_some() {
416 continue;
417 }
418 let level = *level as usize;
419
420 // Returning to a shallower level ends the deeper sibling lists.
421 prev.truncate(level + 1);
422 while prev.len() <= level {
423 prev.push(None);
424 }
425
426 // A new sibling list at the same depth gets a blank line: the kind flips
427 // (`<ul>`↔`<ol>`), an ordered run breaks (`1, 2` then `42`), or the
428 // backend flagged a fresh list (e.g. Markdown's bullet changing `-`→`*`).
429 // Only at the top level: nested sibling groups are children of a list
430 // item, and docling joins an item's children without blank lines.
431 let eff_ordered = dclx.as_ref().map_or(*ordered, |d| d.ordered);
432 if level == 0 {
433 if let Some((prev_ordered, prev_number)) = prev[level] {
434 // A projected predecessor suppresses both heuristics for an
435 // ordered successor: the flat kind flip is an artifact of the
436 // bullet projection, and the numbering continues the deeper
437 // sequence (`1.2.` → `2.`), not this level's.
438 let same_word_list = prev_projected && eff_ordered;
439 let new_list = *first_in_list
440 || (!same_word_list
441 && (prev_ordered != *ordered || (*ordered && *number != prev_number + 1)));
442 if new_list {
443 lines.push(String::new());
444 }
445 }
446 prev_projected = eff_ordered && !*ordered;
447 }
448
449 let indent = " ".repeat(level);
450 let marker = if *ordered {
451 format!("{number}.")
452 } else {
453 "-".to_string()
454 };
455 lines.push(format!(
456 "{indent}{marker} {}",
457 md_line_breaks(&strict_text(text, strict))
458 ));
459 prev[level] = Some((*ordered, *number));
460 }
461
462 // A run consisting only of furniture (content-layer-filtered) items yields no
463 // lines; pushing an empty block here would surface as a stray blank line.
464 if !lines.is_empty() {
465 blocks.push(lines.join("\n"));
466 }
467}
468
469fn render_one(node: &Node, blocks: &mut Vec<String>, ctx: &mut Ctx) {
470 match node {
471 Node::Heading { level, text } => {
472 let text = heading_line_breaks(&strict_text(text, ctx.strict));
473 if ctx.in_table_cell {
474 // docling-core#540: no `#` markers inside a table cell.
475 blocks.push(text);
476 } else {
477 let hashes = "#".repeat((*level).clamp(1, 6) as usize);
478 blocks.push(format!("{hashes} {text}"));
479 }
480 }
481 // An empty body paragraph (docling's blank-line text item) contributes
482 // nothing to Markdown — only DocLang/JSON keep it.
483 Node::Paragraph { text } if text.is_empty() => {}
484 Node::Paragraph { text } => blocks.push(md_line_breaks(&strict_text(text, ctx.strict))),
485 Node::CheckboxItem { checked, text } => {
486 let mark = if *checked { "- [x] " } else { "- [ ] " };
487 blocks.push(md_line_breaks(&strict_text(
488 &format!("{mark}{text}"),
489 ctx.strict,
490 )));
491 }
492 Node::Code {
493 language,
494 text,
495 pretty,
496 ..
497 } => {
498 // Legacy docling never emits a language on the fence; strict keeps it.
499 let lang = match language {
500 Some(l) if ctx.strict => l.as_str(),
501 _ => "",
502 };
503 // Strict prefers the line-preserving rendering when the backend
504 // supplied one (PDF); legacy stays on docling's flat `text`.
505 let body = match pretty {
506 Some(p) if ctx.strict => p.as_str(),
507 _ => text.as_str(),
508 };
509 blocks.push(format!("```{lang}\n{body}\n```"));
510 }
511 // A CodeFormula-decoded display formula renders as docling's `$$…$$`
512 // (the un-enriched pipeline emits a placeholder paragraph instead).
513 Node::Formula { latex, .. } => blocks.push(format!("$${latex}$$")),
514 Node::Table(table) => {
515 // docling renders a table's caption as a text line before the grid.
516 // `caption` is already escaped (backend convention), like a paragraph.
517 if let Some(cap) = &table.caption {
518 if !cap.is_empty() {
519 blocks.push(md_line_breaks(&strict_text(cap, ctx.strict)));
520 }
521 }
522 let rendered = render_table(table, ctx.compact_tables);
523 if !rendered.is_empty() {
524 blocks.push(rendered);
525 }
526 }
527 // Classification predictions don't affect docling's Markdown output.
528 Node::Picture { caption, image, .. } => {
529 if let Some(cap) = caption {
530 if !cap.is_empty() {
531 blocks.push(md_line_breaks(cap));
532 }
533 }
534 blocks.push(picture_marker(image.as_ref(), ctx));
535 }
536 // A chart renders as docling's picture-with-meta markdown: the caption,
537 // the placeholder, the humanized classification ("line_chart" ->
538 // "Line chart"), then the chart's data grid as a regular table.
539 Node::Chart {
540 kind,
541 table,
542 caption,
543 ..
544 } => {
545 if let Some(cap) = caption {
546 if !cap.is_empty() {
547 blocks.push(md_line_breaks(cap));
548 }
549 }
550 blocks.push(picture_marker(None, ctx));
551 blocks.push(humanize_label(kind));
552 let rendered = render_table(table, false);
553 if !rendered.is_empty() {
554 blocks.push(rendered);
555 }
556 }
557 // A DocLang-only node is omitted from Markdown.
558 Node::DoclangOnly(_) => {}
559 // A group on a non-body layer (a hidden spreadsheet sheet) renders
560 // nothing, like every other non-body item.
561 Node::Group { layer: Some(_), .. } => {}
562 Node::Group { children, .. } => render(children, blocks, ctx),
563 Node::FieldRegion { items } => {
564 // The region container and each field item carry no text of their
565 // own; docling-core 2.93 (#724) serializes them to nothing (older
566 // releases emitted a `<!-- missing-text -->` marker for each), so
567 // only an item's marker/key/value appear, as separate paragraphs.
568 for item in items {
569 for part in [&item.marker, &item.key, &item.value].into_iter().flatten() {
570 blocks.push(md_line_breaks(&strict_text(part, ctx.strict)));
571 }
572 }
573 }
574 // A rich inline group renders exactly like a paragraph of its Markdown
575 // text — the structured runs are DocLang-only.
576 Node::InlineGroup { md_text, .. } => {
577 blocks.push(md_line_breaks(&strict_text(md_text, ctx.strict)))
578 }
579 // A plain-text backend dump renders verbatim as a single block.
580 Node::TextDump(text) => {
581 if !text.is_empty() {
582 blocks.push(text.clone());
583 }
584 }
585 // Furniture (page headers/footers, HTML `<title>`) is excluded from
586 // Markdown by default, mirroring docling.
587 Node::Furniture { .. } => {}
588 Node::PageFurniture { .. } => {}
589 // A comment lives in the notes layer — omitted like other furniture;
590 // the annotation on a body item is JSON-only, so render the item.
591 Node::CommentSection { .. } => {}
592 Node::Commented { inner, .. } => render_one(inner, blocks, ctx),
593 // Layout provenance is DocLang-only; render the wrapped node.
594 Node::Located { inner, .. } => render_one(inner, blocks, ctx),
595 // Page breaks are DocLang-only; docling omits them from Markdown.
596 Node::PageBreak => {}
597 // Page markers feed the JSON export only.
598 Node::PageInfo { .. } => {}
599 // Runs of adjacent list items are merged by `render`; a stray single
600 // item (a hand-built document, or a `Located` wrapper around one)
601 // still renders as its own one-item list instead of panicking —
602 // `nodes` is public API, so every representable tree must serialize.
603 Node::ListItem { .. } => render_list_run(std::slice::from_ref(node), blocks, ctx.strict),
604 }
605}
606
607/// The Markdown for a picture under the active [`ImageMode`]; Referenced mode also
608/// records the bytes in `ctx.artifacts` for the caller to write.
609/// docling-core's `_humanize_text`: underscores to spaces, first letter
610/// capitalized ("line_chart" -> "Line chart").
611fn humanize_label(label: &str) -> String {
612 let text = label.replace('_', " ");
613 let mut chars = text.chars();
614 match chars.next() {
615 Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
616 None => text,
617 }
618}
619
620fn picture_marker(image: Option<&crate::PictureImage>, ctx: &mut Ctx) -> String {
621 match (ctx.images, image) {
622 (ImageMode::Embedded, Some(img)) => format!("", img.data_uri()),
623 (ImageMode::Referenced, Some(img)) => {
624 let path = format!(
625 "{}/image_{:06}.{}",
626 ctx.artifacts_dir,
627 ctx.pic_index,
628 ext_for(&img.mimetype)
629 );
630 ctx.pic_index += 1;
631 ctx.artifacts.push((path.clone(), img.data.clone()));
632 format!("")
633 }
634 // Placeholder, or any mode with no extracted image.
635 _ => "<!-- image -->".to_string(),
636 }
637}
638
639fn ext_for(mimetype: &str) -> &str {
640 match mimetype {
641 "image/jpeg" => "jpg",
642 "image/gif" => "gif",
643 "image/webp" => "webp",
644 "image/bmp" => "bmp",
645 "image/tiff" => "tif",
646 _ => "png",
647 }
648}
649
650/// Render a table. `compact` selects between two serializers:
651///
652/// - **padded** (default) — docling-core's `tabulate(tablefmt="github")`: columns
653/// are padded to a fixed width (header width + a minimum padding of 2, or the
654/// widest data cell); numeric columns (every data cell parses as a number) are
655/// right-aligned, others left-aligned; separators are plain dashes of
656/// `width + 2`. Matches current published docling (DOCX/HTML conformance).
657/// - **compact** — `| a | b |` cells with single-dash `| - | - |` separators, no
658/// width padding. Matches the committed PDF groundtruth corpus, which predates
659/// the padded serializer.
660///
661/// Each cell is first escaped (`\n` → space, `|` → `|`) so it can't break the
662/// table. Row 0 is the header.
663/// Whether a table cell counts as a number for column alignment, matching
664/// `tabulate`'s detection: an ordinary float/int (`f64`-parseable, covering
665/// `1e2`/`inf`/`+1.5`) **or** a thousands-separated number like `7,015`.
666fn is_number_cell(t: &str) -> bool {
667 t.parse::<f64>().is_ok() || is_thousands_number(t)
668}
669
670/// A number with comma thousands-separators, per `tabulate`'s
671/// `_float_with_thousands_separators` regex
672/// (`^(([+-]?[0-9]{1,3})(?:,([0-9]{3}))*)?(?(1)\.[0-9]*|\.[0-9]+)?$`): the
673/// integer part is 1–3 digits then any number of `,ddd` groups; the fraction is
674/// optional (and, without an integer part, must have at least one digit).
675fn is_thousands_number(t: &str) -> bool {
676 let b = t.as_bytes();
677 let mut i = 0;
678 let start = i;
679 if i < b.len() && (b[i] == b'+' || b[i] == b'-') {
680 i += 1;
681 }
682 // First digit chunk: 1–3 digits.
683 let d0 = i;
684 while i < b.len() && b[i].is_ascii_digit() && i - d0 < 3 {
685 i += 1;
686 }
687 let has_int = i > d0;
688 if has_int {
689 // Subsequent `,ddd` groups (exactly three digits each).
690 while i + 3 < b.len() + 1
691 && b.get(i) == Some(&b',')
692 && b.get(i + 1).is_some_and(u8::is_ascii_digit)
693 && b.get(i + 2).is_some_and(u8::is_ascii_digit)
694 && b.get(i + 3).is_some_and(u8::is_ascii_digit)
695 {
696 i += 4;
697 }
698 } else {
699 // A sign only counts with an integer part.
700 i = start;
701 }
702 // Optional fraction.
703 if i < b.len() && b[i] == b'.' {
704 i += 1;
705 let f0 = i;
706 while i < b.len() && b[i].is_ascii_digit() {
707 i += 1;
708 }
709 if !has_int && i == f0 {
710 return false; // `.` with no digits and no integer part
711 }
712 } else if !has_int {
713 return false; // neither integer nor fractional part
714 }
715 i == b.len()
716}
717
718pub(crate) fn render_table(table: &Table, compact: bool) -> String {
719 if table.rows.is_empty() {
720 return String::new();
721 }
722 let num_cols = table.rows.iter().map(Vec::len).max().unwrap_or(0);
723 if num_cols == 0 {
724 return String::new();
725 }
726
727 // Escaped, rectangular grid (ragged rows padded with empty cells). `tabulate`
728 // strips data cells of surrounding whitespace but leaves the header row as-is.
729 let grid: Vec<Vec<String>> = table
730 .rows
731 .iter()
732 .enumerate()
733 .map(|(r, row)| {
734 (0..num_cols)
735 .map(|c| {
736 let cell = escape_cell(row.get(c).map(String::as_str).unwrap_or(""));
737 if r == 0 {
738 cell
739 } else {
740 cell.trim().to_string()
741 }
742 })
743 .collect()
744 })
745 .collect();
746
747 if compact {
748 // Compact: cells joined by " | ", no padding, single-dash separators.
749 let render_row = |r: usize| -> String { format!("| {} |", grid[r].join(" | ")) };
750 let mut lines = Vec::with_capacity(grid.len() + 1);
751 lines.push(render_row(0));
752 let sep: Vec<&str> = (0..num_cols).map(|_| "-").collect();
753 lines.push(format!("| {} |", sep.join(" | ")));
754 for r in 1..grid.len() {
755 lines.push(render_row(r));
756 }
757 return lines.join("\n");
758 }
759
760 // Display width (Unicode scalar count — good enough for now).
761 let dw = |s: &str| s.chars().count();
762 let data_rows = 1..grid.len();
763
764 // A column is right-aligned when at least one data cell is numeric and every
765 // non-empty data cell is numeric — matching `tabulate`'s column typing, where
766 // empty cells are "missing" (ignored) and a number may carry thousands
767 // separators (`7,015`), which a plain `f64` parse rejects.
768 let right: Vec<bool> = (0..num_cols)
769 .map(|c| {
770 let mut any = false;
771 for r in data_rows.clone() {
772 let t = grid[r][c].trim();
773 if t.is_empty() {
774 continue;
775 }
776 if !is_number_cell(t) {
777 return false;
778 }
779 any = true;
780 }
781 any
782 })
783 .collect();
784
785 // Column width = max(header_width + MIN_PADDING(2), max data-cell width).
786 let width: Vec<usize> = (0..num_cols)
787 .map(|c| {
788 let mut w = dw(&grid[0][c]) + 2;
789 for r in data_rows.clone() {
790 w = w.max(dw(&grid[r][c]));
791 }
792 w
793 })
794 .collect();
795
796 let fmt_cell = |s: &str, c: usize| -> String {
797 let pad = " ".repeat(width[c].saturating_sub(dw(s)));
798 let body = if right[c] {
799 format!("{pad}{s}")
800 } else {
801 format!("{s}{pad}")
802 };
803 format!(" {body} ")
804 };
805 let render_row = |r: usize| -> String {
806 let cells: Vec<String> = (0..num_cols).map(|c| fmt_cell(&grid[r][c], c)).collect();
807 format!("|{}|", cells.join("|"))
808 };
809
810 let mut lines = Vec::with_capacity(grid.len() + 1);
811 lines.push(render_row(0));
812 let sep: Vec<String> = (0..num_cols).map(|c| "-".repeat(width[c] + 2)).collect();
813 lines.push(format!("|{}|", sep.join("|")));
814 for r in data_rows {
815 lines.push(render_row(r));
816 }
817 lines.join("\n")
818}
819
820/// Escape a table cell so it can't break the markdown table: newlines become
821/// spaces and pipes become the `|` HTML entity (matches docling-core).
822fn escape_cell(s: &str) -> String {
823 s.replace('\n', " ").replace('|', "|")
824}
825
826#[cfg(test)]
827mod tests {
828 use super::*;
829 use crate::PictureImage;
830
831 #[test]
832 fn renders_headings_paragraphs_and_lists() {
833 let mut doc = DoclingDocument::new("demo");
834 doc.add_heading(1, "Title");
835 doc.add_paragraph("Hello world.");
836 doc.push(Node::ListItem {
837 ordered: false,
838 number: 1,
839 first_in_list: true,
840 text: "first".into(),
841 level: 0,
842 marker: None,
843 location: None,
844 dclx: None,
845 href: None,
846 layer: None,
847 });
848 doc.push(Node::ListItem {
849 ordered: false,
850 number: 2,
851 first_in_list: false,
852 text: "second".into(),
853 level: 0,
854 marker: None,
855 location: None,
856 dclx: None,
857 href: None,
858 layer: None,
859 });
860 let md = doc.export_to_markdown();
861 assert_eq!(md, "# Title\n\nHello world.\n\n- first\n- second\n");
862 }
863
864 /// docling-core 2.92 (#721): a single newline inside an item's text is a
865 /// GFM hard line break, a blank line stays a paragraph break, and a heading
866 /// collapses its newline to a space. Nested-table dumps stay verbatim.
867 #[test]
868 fn single_newlines_become_gfm_hard_line_breaks() {
869 let mut doc = DoclingDocument::new("t");
870 doc.push(Node::Heading {
871 level: 1,
872 text: "Hello\nWorld".into(),
873 });
874 doc.push(Node::Paragraph {
875 text: "line one\nline two\n\npara two".into(),
876 });
877 doc.push(Node::ListItem {
878 ordered: false,
879 number: 1,
880 first_in_list: true,
881 text: "item\ncontinued".into(),
882 level: 0,
883 marker: None,
884 location: None,
885 dclx: None,
886 href: None,
887 layer: None,
888 });
889 doc.push(Node::TextDump("A1 B1 \n\n\nC1".into()));
890 assert_eq!(
891 doc.export_to_markdown(),
892 "# Hello World\n\nline one \nline two\n\npara two\n\n- item \ncontinued\n\nA1 B1 \n\n\nC1\n"
893 );
894 }
895
896 /// docling-core#540: inside a rich table cell a heading is plain text;
897 /// docling-core#724: a field region renders only its items' key/value text.
898 #[test]
899 fn table_cell_mode_and_field_regions() {
900 let mut doc = DoclingDocument::new("t");
901 doc.push(Node::Heading {
902 level: 2,
903 text: "A text".into(),
904 });
905 doc.push(Node::Paragraph {
906 text: "body".into(),
907 });
908 assert_eq!(to_markdown_table_cell(&doc, false), "A text\n\nbody");
909 assert_eq!(doc.export_to_markdown(), "## A text\n\nbody\n");
910
911 let mut doc = DoclingDocument::new("f");
912 doc.push(Node::FieldRegion {
913 items: vec![crate::FieldItem {
914 marker: None,
915 key: Some("Name:".into()),
916 value: Some("John Doe".into()),
917 }],
918 });
919 assert_eq!(doc.export_to_markdown(), "Name:\n\nJohn Doe\n");
920 }
921
922 #[test]
923 fn strict_renders_recovered_links_legacy_does_not() {
924 let mut doc = DoclingDocument::new("cv");
925 doc.add_paragraph("Find me on LinkedIn or GitHub.");
926 doc.links = vec![
927 ("LinkedIn".into(), "https://www.linkedin.com/in/x/".into()),
928 ("GitHub".into(), "https://github.com/x/".into()),
929 ];
930 // Legacy/docling mode: links are left untouched (conformance preserved).
931 assert_eq!(doc.export_to_markdown(), "Find me on LinkedIn or GitHub.\n");
932 // Strict mode: anchors become Markdown links.
933 assert_eq!(
934 doc.export_to_markdown_with(true),
935 "Find me on [LinkedIn](https://www.linkedin.com/in/x/) or [GitHub](https://github.com/x/).\n"
936 );
937 }
938
939 #[test]
940 fn strict_links_match_escaped_anchor_and_consume_in_order() {
941 let mut doc = DoclingDocument::new("d");
942 // The PDF assembler HTML-escapes prose, so by serialization time the body
943 // already carries `&`; the anchor is stored un-escaped. The matcher must
944 // escape the anchor to find it. Two identical anchors link in document order.
945 doc.add_paragraph("AI & ML here, and issues here, then issues there.");
946 doc.links = vec![
947 ("AI & ML".into(), "https://a/".into()),
948 ("issues".into(), "https://first/".into()),
949 ("issues".into(), "https://second/".into()),
950 ];
951 assert_eq!(
952 doc.export_to_markdown_with(true),
953 "[AI & ML](https://a/) here, and [issues](https://first/) here, then [issues](https://second/) there.\n"
954 );
955 }
956
957 #[test]
958 fn renders_compact_table() {
959 let mut doc = DoclingDocument::new("t");
960 // The compact form is opt-in (the PDF backend sets it); default output uses
961 // the padded GitHub serializer (covered by the regression fixtures).
962 doc.compact_tables = true;
963 doc.push(Node::Table(Table {
964 rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
965 location: None,
966 structure: None,
967 cell_blocks: None,
968 cells: None,
969 caption: None,
970 }));
971 let md = doc.export_to_markdown();
972 assert_eq!(md, "| a | b |\n| - | - |\n| 1 | 2 |\n");
973 }
974
975 #[test]
976 fn renders_padded_github_table_by_default() {
977 let mut doc = DoclingDocument::new("t");
978 doc.push(Node::Table(Table {
979 rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
980 location: None,
981 structure: None,
982 cell_blocks: None,
983 cells: None,
984 caption: None,
985 }));
986 let md = doc.export_to_markdown();
987 // Numeric data columns are right-aligned; columns padded to header+2.
988 assert_eq!(md, "| a | b |\n|-----|-----|\n| 1 | 2 |\n");
989 }
990
991 #[test]
992 fn strict_unescapes_inline_underscores_legacy_keeps_them() {
993 let mut doc = DoclingDocument::new("t");
994 doc.add_heading(1, "a\\_b");
995 doc.add_paragraph("x\\_y");
996 doc.push(Node::ListItem {
997 ordered: false,
998 number: 1,
999 first_in_list: true,
1000 text: "i\\_j".into(),
1001 level: 0,
1002 marker: None,
1003 location: None,
1004 dclx: None,
1005 href: None,
1006 layer: None,
1007 });
1008 // Legacy reproduces docling's `\_` escaping byte-for-byte.
1009 assert_eq!(doc.export_to_markdown(), "# a\\_b\n\nx\\_y\n\n- i\\_j\n");
1010 // Strict prefers literal underscores (Rust-only readability mode).
1011 assert_eq!(doc.export_to_markdown_with(true), "# a_b\n\nx_y\n\n- i_j\n");
1012 }
1013
1014 /// Drive a document's nodes through [`MarkdownStreamer`] in the given page
1015 /// splits and assert the concatenated chunks equal the buffered serializer.
1016 fn assert_stream_matches(
1017 doc: &DoclingDocument,
1018 strict: bool,
1019 images: ImageMode,
1020 splits: &[usize],
1021 ) {
1022 let (want, want_artifacts) = to_markdown_images(doc, strict, images, "artifacts");
1023 let mut streamer =
1024 MarkdownStreamer::with_artifacts(strict, images, doc.compact_tables, "artifacts");
1025 let mut got = String::new();
1026 let mut got_artifacts = Vec::new();
1027 let mut start = 0;
1028 for &end in splits {
1029 // Links only matter in strict mode; feed them all with the first batch
1030 // that has content (document order is preserved by the queue).
1031 let links = if start == 0 {
1032 doc.links.as_slice()
1033 } else {
1034 &[]
1035 };
1036 got.push_str(&streamer.push(&doc.nodes[start..end], links));
1037 // Referenced mode: drain per push, as a real caller writing files
1038 // page by page would — numbering must continue across drains.
1039 got_artifacts.extend(streamer.take_artifacts());
1040 start = end;
1041 }
1042 got.push_str(&streamer.push(
1043 &doc.nodes[start..],
1044 if start == 0 {
1045 doc.links.as_slice()
1046 } else {
1047 &[]
1048 },
1049 ));
1050 got_artifacts.extend(streamer.take_artifacts());
1051 got.push_str(&streamer.finish());
1052 assert_eq!(
1053 got, want,
1054 "streamed output diverged (splits={splits:?}, strict={strict})"
1055 );
1056 assert_eq!(
1057 got_artifacts, want_artifacts,
1058 "streamed artifacts diverged (splits={splits:?}, strict={strict})"
1059 );
1060 }
1061
1062 #[test]
1063 fn streaming_is_byte_identical_to_buffered() {
1064 let mut doc = DoclingDocument::new("d");
1065 doc.add_heading(1, "Title");
1066 doc.add_paragraph("First paragraph.");
1067 doc.push(Node::ListItem {
1068 ordered: false,
1069 number: 1,
1070 first_in_list: true,
1071 text: "a".into(),
1072 level: 0,
1073 marker: None,
1074 location: None,
1075 dclx: None,
1076 href: None,
1077 layer: None,
1078 });
1079 doc.push(Node::ListItem {
1080 ordered: false,
1081 number: 2,
1082 first_in_list: false,
1083 text: "b".into(),
1084 level: 0,
1085 marker: None,
1086 location: None,
1087 dclx: None,
1088 href: None,
1089 layer: None,
1090 });
1091 doc.push(Node::Code {
1092 language: Some("rust".into()),
1093 text: "let x = 1;".into(),
1094 orig: None,
1095 pretty: None,
1096 });
1097 doc.push(Node::Table(Table {
1098 rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
1099 location: None,
1100 structure: None,
1101 cell_blocks: None,
1102 cells: None,
1103 caption: None,
1104 }));
1105 doc.push(Node::Picture {
1106 caption: Some("Fig 1".into()),
1107 caption_href: None,
1108 image: Some(PictureImage {
1109 mimetype: "image/png".into(),
1110 width: 2,
1111 height: 2,
1112 data: b"png-one".to_vec(),
1113 }),
1114 classification: None,
1115 });
1116 doc.add_paragraph("Last paragraph.");
1117 // A second embedded picture, so referenced mode must keep numbering
1118 // (`image_000001`) across chunk boundaries.
1119 doc.push(Node::Picture {
1120 caption: None,
1121 caption_href: None,
1122 image: Some(PictureImage {
1123 mimetype: "image/png".into(),
1124 width: 2,
1125 height: 2,
1126 data: b"png-two".to_vec(),
1127 }),
1128 classification: None,
1129 });
1130
1131 // A run of list items must never straddle a split, so try splits that fall
1132 // on safe block boundaries (the streaming PDF assembler guarantees this).
1133 for &strict in &[false, true] {
1134 for &images in &[
1135 ImageMode::Placeholder,
1136 ImageMode::Embedded,
1137 ImageMode::Referenced,
1138 ] {
1139 for splits in [&[][..], &[1][..], &[2][..], &[4][..], &[1, 4, 6, 7][..]] {
1140 assert_stream_matches(&doc, strict, images, splits);
1141 }
1142 }
1143 }
1144 }
1145
1146 #[test]
1147 fn streaming_applies_recovered_links_in_strict_mode() {
1148 let mut doc = DoclingDocument::new("d");
1149 doc.add_paragraph("See LinkedIn for details.");
1150 doc.add_paragraph("And GitHub too.");
1151 doc.links = vec![
1152 ("LinkedIn".into(), "https://lnkd/".into()),
1153 ("GitHub".into(), "https://gh/".into()),
1154 ];
1155 // The second anchor lives in the second block, so it must be carried across
1156 // the page boundary and placed when that block streams out.
1157 assert_stream_matches(&doc, true, ImageMode::Placeholder, &[1]);
1158 }
1159
1160 #[test]
1161 fn strict_tightens_punctuation_spacing_legacy_keeps_it() {
1162 let mut doc = DoclingDocument::new("t");
1163 doc.add_paragraph("see [ 37 , 36 ] and ( x ) .");
1164 // Legacy keeps docling's spacing byte-for-byte.
1165 assert_eq!(doc.export_to_markdown(), "see [ 37 , 36 ] and ( x ) .\n");
1166 // Strict tightens punctuation for readable Markdown.
1167 assert_eq!(doc.export_to_markdown_with(true), "see [37, 36] and (x).\n");
1168 }
1169}