1use crate::document::{DoclingDocument, Node, Table};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum ImageMode {
8 #[default]
10 Placeholder,
11 Embedded,
13 Referenced,
16}
17
18struct Ctx {
20 strict: bool,
21 compact_tables: bool,
23 images: ImageMode,
24 artifacts_dir: String,
25 artifacts: Vec<(String, Vec<u8>)>,
27 pic_index: usize,
28}
29
30pub fn to_markdown(doc: &DoclingDocument, strict: bool) -> String {
36 to_markdown_images(doc, strict, ImageMode::Placeholder, "artifacts").0
37}
38
39pub fn to_markdown_images(
43 doc: &DoclingDocument,
44 strict: bool,
45 images: ImageMode,
46 artifacts_dir: &str,
47) -> (String, Vec<(String, Vec<u8>)>) {
48 let mut ctx = Ctx {
49 strict,
50 compact_tables: doc.compact_tables,
51 images,
52 artifacts_dir: artifacts_dir.to_string(),
53 artifacts: Vec::new(),
54 pic_index: 0,
55 };
56 let mut blocks: Vec<String> = Vec::new();
57 render(&doc.nodes, &mut blocks, &mut ctx);
58 let mut body = blocks.join("\n\n");
59 if strict && !doc.links.is_empty() {
63 body = apply_links(&body, &doc.links);
64 }
65 let md = if body.is_empty() {
66 String::new()
67 } else {
68 format!("{body}\n")
69 };
70 (md, ctx.artifacts)
71}
72
73fn apply_links(body: &str, links: &[(String, String)]) -> String {
81 let mut out = body.to_string();
82 let mut cursor = 0usize;
83 for (anchor, href) in links {
84 let anchor = anchor
85 .replace('&', "&")
86 .replace('<', "<")
87 .replace('>', ">");
88 if anchor.is_empty() {
89 continue;
90 }
91 if let Some(rel) = out[cursor..].find(&anchor) {
92 let at = cursor + rel;
93 let replacement = format!("[{anchor}]({href})");
95 out.replace_range(at..at + anchor.len(), &replacement);
96 cursor = at + replacement.len();
97 }
98 }
99 out
100}
101
102fn apply_links_chunk(chunk: &str, queue: &mut Vec<(String, String)>) -> String {
113 let mut out = chunk.to_string();
114 let mut cursor = 0usize;
115 let mut carried: Vec<(String, String)> = Vec::new();
116 for (anchor_raw, href) in std::mem::take(queue) {
117 let anchor = anchor_raw
118 .replace('&', "&")
119 .replace('<', "<")
120 .replace('>', ">");
121 if anchor.is_empty() {
122 continue;
123 }
124 if let Some(rel) = out[cursor..].find(&anchor) {
125 let at = cursor + rel;
126 let replacement = format!("[{anchor}]({href})");
127 out.replace_range(at..at + anchor.len(), &replacement);
128 cursor = at + replacement.len();
129 } else {
130 carried.push((anchor_raw, href));
132 }
133 }
134 *queue = carried;
135 out
136}
137
138pub struct MarkdownStreamer {
156 strict: bool,
157 images: ImageMode,
158 compact_tables: bool,
159 emitted_any: bool,
162 links: Vec<(String, String)>,
164 artifacts_dir: String,
168 artifacts: Vec<(String, Vec<u8>)>,
169 pic_index: usize,
170}
171
172impl MarkdownStreamer {
173 pub fn new(strict: bool, images: ImageMode, compact_tables: bool) -> Self {
176 debug_assert!(
177 images != ImageMode::Referenced,
178 "referenced image mode needs an artifacts dir; use with_artifacts"
179 );
180 Self::with_artifacts(strict, images, compact_tables, "artifacts")
181 }
182
183 pub fn with_artifacts(
190 strict: bool,
191 images: ImageMode,
192 compact_tables: bool,
193 artifacts_dir: &str,
194 ) -> Self {
195 Self {
196 strict,
197 images,
198 compact_tables,
199 emitted_any: false,
200 links: Vec::new(),
201 artifacts_dir: artifacts_dir.to_string(),
202 artifacts: Vec::new(),
203 pic_index: 0,
204 }
205 }
206
207 pub fn take_artifacts(&mut self) -> Vec<(String, Vec<u8>)> {
212 std::mem::take(&mut self.artifacts)
213 }
214
215 pub fn push(&mut self, nodes: &[Node], links: &[(String, String)]) -> String {
220 self.links.extend(links.iter().cloned());
221 let mut ctx = Ctx {
222 strict: self.strict,
223 compact_tables: self.compact_tables,
224 images: self.images,
225 artifacts_dir: std::mem::take(&mut self.artifacts_dir),
226 artifacts: std::mem::take(&mut self.artifacts),
227 pic_index: self.pic_index,
228 };
229 let mut blocks: Vec<String> = Vec::new();
230 render(nodes, &mut blocks, &mut ctx);
231 self.artifacts_dir = std::mem::take(&mut ctx.artifacts_dir);
232 self.artifacts = std::mem::take(&mut ctx.artifacts);
233 self.pic_index = ctx.pic_index;
234 if blocks.is_empty() {
235 return String::new();
236 }
237 let mut body = blocks.join("\n\n");
238 if self.strict && !self.links.is_empty() {
239 body = apply_links_chunk(&body, &mut self.links);
240 }
241 let chunk = if self.emitted_any {
242 format!("\n\n{body}")
243 } else {
244 body
245 };
246 self.emitted_any = true;
247 chunk
248 }
249
250 pub fn finish(self) -> String {
253 if self.emitted_any {
254 "\n".to_string()
255 } else {
256 String::new()
257 }
258 }
259}
260
261fn strict_text(text: &str, strict: bool) -> String {
269 if !strict {
270 return text.to_string();
271 }
272 text.replace("\\_", "_")
273 .replace(" ,", ",")
274 .replace(" .", ".")
275 .replace(" ;", ";")
276 .replace(" )", ")")
277 .replace("( ", "(")
278 .replace(" ]", "]")
279 .replace("[ ", "[")
280}
281
282fn render(nodes: &[Node], blocks: &mut Vec<String>, ctx: &mut Ctx) {
283 let mut i = 0;
284 while i < nodes.len() {
285 match &nodes[i] {
286 Node::ListItem { .. } => {
287 let start = i;
288 i += 1;
289 loop {
290 match nodes.get(i) {
291 Some(Node::ListItem { .. }) => i += 1,
292 Some(Node::Paragraph { text })
296 if text.is_empty()
297 && matches!(nodes.get(i + 1), Some(Node::ListItem { .. })) =>
298 {
299 i += 1
300 }
301 _ => break,
302 }
303 }
304 render_list_run(&nodes[start..i], blocks, ctx.strict);
305 }
306 other => {
307 render_one(other, blocks, ctx);
308 i += 1;
309 }
310 }
311 }
312}
313
314fn render_list_run(items: &[Node], blocks: &mut Vec<String>, strict: bool) {
320 let mut lines: Vec<String> = Vec::new();
321 let mut prev: Vec<Option<(bool, u64)>> = Vec::new();
324
325 for item in items {
326 let Node::ListItem {
327 ordered,
328 number,
329 first_in_list,
330 text,
331 level,
332 marker: _,
333 location: _,
334 dclx: _,
335 href: _,
336 layer,
337 } = item
338 else {
339 continue;
340 };
341 if layer.is_some() {
344 continue;
345 }
346 let level = *level as usize;
347
348 prev.truncate(level + 1);
350 while prev.len() <= level {
351 prev.push(None);
352 }
353
354 if level == 0 {
360 if let Some((prev_ordered, prev_number)) = prev[level] {
361 let new_list = *first_in_list
362 || prev_ordered != *ordered
363 || (*ordered && *number != prev_number + 1);
364 if new_list {
365 lines.push(String::new());
366 }
367 }
368 }
369
370 let indent = " ".repeat(level);
371 let marker = if *ordered {
372 format!("{number}.")
373 } else {
374 "-".to_string()
375 };
376 lines.push(format!("{indent}{marker} {}", strict_text(text, strict)));
377 prev[level] = Some((*ordered, *number));
378 }
379
380 if !lines.is_empty() {
383 blocks.push(lines.join("\n"));
384 }
385}
386
387fn render_one(node: &Node, blocks: &mut Vec<String>, ctx: &mut Ctx) {
388 match node {
389 Node::Heading { level, text } => {
390 let hashes = "#".repeat((*level).clamp(1, 6) as usize);
391 blocks.push(format!("{hashes} {}", strict_text(text, ctx.strict)));
392 }
393 Node::Paragraph { text } if text.is_empty() => {}
396 Node::Paragraph { text } => blocks.push(strict_text(text, ctx.strict)),
397 Node::CheckboxItem { checked, text } => {
398 let mark = if *checked { "- [x] " } else { "- [ ] " };
399 blocks.push(strict_text(&format!("{mark}{text}"), ctx.strict));
400 }
401 Node::Code {
402 language,
403 text,
404 pretty,
405 ..
406 } => {
407 let lang = match language {
409 Some(l) if ctx.strict => l.as_str(),
410 _ => "",
411 };
412 let body = match pretty {
415 Some(p) if ctx.strict => p.as_str(),
416 _ => text.as_str(),
417 };
418 blocks.push(format!("```{lang}\n{body}\n```"));
419 }
420 Node::Formula { latex, .. } => blocks.push(format!("$${latex}$$")),
423 Node::Table(table) => {
424 if let Some(cap) = &table.caption {
427 if !cap.is_empty() {
428 blocks.push(strict_text(cap, ctx.strict));
429 }
430 }
431 let rendered = render_table(table, ctx.compact_tables);
432 if !rendered.is_empty() {
433 blocks.push(rendered);
434 }
435 }
436 Node::Picture { caption, image, .. } => {
438 if let Some(cap) = caption {
439 if !cap.is_empty() {
440 blocks.push(cap.clone());
441 }
442 }
443 blocks.push(picture_marker(image.as_ref(), ctx));
444 }
445 Node::Chart {
449 kind,
450 table,
451 caption,
452 ..
453 } => {
454 if let Some(cap) = caption {
455 if !cap.is_empty() {
456 blocks.push(cap.clone());
457 }
458 }
459 blocks.push(picture_marker(None, ctx));
460 blocks.push(humanize_label(kind));
461 let rendered = render_table(table, false);
462 if !rendered.is_empty() {
463 blocks.push(rendered);
464 }
465 }
466 Node::DoclangOnly(_) => {}
468 Node::Group { children, .. } => render(children, blocks, ctx),
469 Node::FieldRegion { items } => {
470 blocks.push(MISSING_TEXT.to_string());
475 for item in items {
476 blocks.push(MISSING_TEXT.to_string());
477 for part in [&item.marker, &item.key, &item.value].into_iter().flatten() {
478 blocks.push(strict_text(part, ctx.strict));
479 }
480 }
481 }
482 Node::InlineGroup { md_text, .. } => blocks.push(strict_text(md_text, ctx.strict)),
485 Node::TextDump(text) => {
487 if !text.is_empty() {
488 blocks.push(text.clone());
489 }
490 }
491 Node::Furniture { .. } => {}
494 Node::PageFurniture { .. } => {}
495 Node::Located { inner, .. } => render_one(inner, blocks, ctx),
497 Node::PageBreak => {}
499 Node::PageInfo { .. } => {}
501 Node::ListItem { .. } => unreachable!("list items are rendered in runs"),
503 }
504}
505
506const MISSING_TEXT: &str = "<!-- missing-text -->";
509
510fn humanize_label(label: &str) -> String {
515 let text = label.replace('_', " ");
516 let mut chars = text.chars();
517 match chars.next() {
518 Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
519 None => text,
520 }
521}
522
523fn picture_marker(image: Option<&crate::PictureImage>, ctx: &mut Ctx) -> String {
524 match (ctx.images, image) {
525 (ImageMode::Embedded, Some(img)) => format!("", img.data_uri()),
526 (ImageMode::Referenced, Some(img)) => {
527 let path = format!(
528 "{}/image_{:06}.{}",
529 ctx.artifacts_dir,
530 ctx.pic_index,
531 ext_for(&img.mimetype)
532 );
533 ctx.pic_index += 1;
534 ctx.artifacts.push((path.clone(), img.data.clone()));
535 format!("")
536 }
537 _ => "<!-- image -->".to_string(),
539 }
540}
541
542fn ext_for(mimetype: &str) -> &str {
543 match mimetype {
544 "image/jpeg" => "jpg",
545 "image/gif" => "gif",
546 "image/webp" => "webp",
547 "image/bmp" => "bmp",
548 "image/tiff" => "tif",
549 _ => "png",
550 }
551}
552
553fn is_number_cell(t: &str) -> bool {
570 t.parse::<f64>().is_ok() || is_thousands_number(t)
571}
572
573fn is_thousands_number(t: &str) -> bool {
579 let b = t.as_bytes();
580 let mut i = 0;
581 let start = i;
582 if i < b.len() && (b[i] == b'+' || b[i] == b'-') {
583 i += 1;
584 }
585 let d0 = i;
587 while i < b.len() && b[i].is_ascii_digit() && i - d0 < 3 {
588 i += 1;
589 }
590 let has_int = i > d0;
591 if has_int {
592 while i + 3 < b.len() + 1
594 && b.get(i) == Some(&b',')
595 && b.get(i + 1).is_some_and(u8::is_ascii_digit)
596 && b.get(i + 2).is_some_and(u8::is_ascii_digit)
597 && b.get(i + 3).is_some_and(u8::is_ascii_digit)
598 {
599 i += 4;
600 }
601 } else {
602 i = start;
604 }
605 if i < b.len() && b[i] == b'.' {
607 i += 1;
608 let f0 = i;
609 while i < b.len() && b[i].is_ascii_digit() {
610 i += 1;
611 }
612 if !has_int && i == f0 {
613 return false; }
615 } else if !has_int {
616 return false; }
618 i == b.len()
619}
620
621pub(crate) fn render_table(table: &Table, compact: bool) -> String {
622 if table.rows.is_empty() {
623 return String::new();
624 }
625 let num_cols = table.rows.iter().map(Vec::len).max().unwrap_or(0);
626 if num_cols == 0 {
627 return String::new();
628 }
629
630 let grid: Vec<Vec<String>> = table
633 .rows
634 .iter()
635 .enumerate()
636 .map(|(r, row)| {
637 (0..num_cols)
638 .map(|c| {
639 let cell = escape_cell(row.get(c).map(String::as_str).unwrap_or(""));
640 if r == 0 {
641 cell
642 } else {
643 cell.trim().to_string()
644 }
645 })
646 .collect()
647 })
648 .collect();
649
650 if compact {
651 let render_row = |r: usize| -> String { format!("| {} |", grid[r].join(" | ")) };
653 let mut lines = Vec::with_capacity(grid.len() + 1);
654 lines.push(render_row(0));
655 let sep: Vec<&str> = (0..num_cols).map(|_| "-").collect();
656 lines.push(format!("| {} |", sep.join(" | ")));
657 for r in 1..grid.len() {
658 lines.push(render_row(r));
659 }
660 return lines.join("\n");
661 }
662
663 let dw = |s: &str| s.chars().count();
665 let data_rows = 1..grid.len();
666
667 let right: Vec<bool> = (0..num_cols)
672 .map(|c| {
673 let mut any = false;
674 for r in data_rows.clone() {
675 let t = grid[r][c].trim();
676 if t.is_empty() {
677 continue;
678 }
679 if !is_number_cell(t) {
680 return false;
681 }
682 any = true;
683 }
684 any
685 })
686 .collect();
687
688 let width: Vec<usize> = (0..num_cols)
690 .map(|c| {
691 let mut w = dw(&grid[0][c]) + 2;
692 for r in data_rows.clone() {
693 w = w.max(dw(&grid[r][c]));
694 }
695 w
696 })
697 .collect();
698
699 let fmt_cell = |s: &str, c: usize| -> String {
700 let pad = " ".repeat(width[c].saturating_sub(dw(s)));
701 let body = if right[c] {
702 format!("{pad}{s}")
703 } else {
704 format!("{s}{pad}")
705 };
706 format!(" {body} ")
707 };
708 let render_row = |r: usize| -> String {
709 let cells: Vec<String> = (0..num_cols).map(|c| fmt_cell(&grid[r][c], c)).collect();
710 format!("|{}|", cells.join("|"))
711 };
712
713 let mut lines = Vec::with_capacity(grid.len() + 1);
714 lines.push(render_row(0));
715 let sep: Vec<String> = (0..num_cols).map(|c| "-".repeat(width[c] + 2)).collect();
716 lines.push(format!("|{}|", sep.join("|")));
717 for r in data_rows {
718 lines.push(render_row(r));
719 }
720 lines.join("\n")
721}
722
723fn escape_cell(s: &str) -> String {
726 s.replace('\n', " ").replace('|', "|")
727}
728
729#[cfg(test)]
730mod tests {
731 use super::*;
732 use crate::PictureImage;
733
734 #[test]
735 fn renders_headings_paragraphs_and_lists() {
736 let mut doc = DoclingDocument::new("demo");
737 doc.add_heading(1, "Title");
738 doc.add_paragraph("Hello world.");
739 doc.push(Node::ListItem {
740 ordered: false,
741 number: 1,
742 first_in_list: true,
743 text: "first".into(),
744 level: 0,
745 marker: None,
746 location: None,
747 dclx: None,
748 href: None,
749 layer: None,
750 });
751 doc.push(Node::ListItem {
752 ordered: false,
753 number: 2,
754 first_in_list: false,
755 text: "second".into(),
756 level: 0,
757 marker: None,
758 location: None,
759 dclx: None,
760 href: None,
761 layer: None,
762 });
763 let md = doc.export_to_markdown();
764 assert_eq!(md, "# Title\n\nHello world.\n\n- first\n- second\n");
765 }
766
767 #[test]
768 fn strict_renders_recovered_links_legacy_does_not() {
769 let mut doc = DoclingDocument::new("cv");
770 doc.add_paragraph("Find me on LinkedIn or GitHub.");
771 doc.links = vec![
772 ("LinkedIn".into(), "https://www.linkedin.com/in/x/".into()),
773 ("GitHub".into(), "https://github.com/x/".into()),
774 ];
775 assert_eq!(doc.export_to_markdown(), "Find me on LinkedIn or GitHub.\n");
777 assert_eq!(
779 doc.export_to_markdown_with(true),
780 "Find me on [LinkedIn](https://www.linkedin.com/in/x/) or [GitHub](https://github.com/x/).\n"
781 );
782 }
783
784 #[test]
785 fn strict_links_match_escaped_anchor_and_consume_in_order() {
786 let mut doc = DoclingDocument::new("d");
787 doc.add_paragraph("AI & ML here, and issues here, then issues there.");
791 doc.links = vec![
792 ("AI & ML".into(), "https://a/".into()),
793 ("issues".into(), "https://first/".into()),
794 ("issues".into(), "https://second/".into()),
795 ];
796 assert_eq!(
797 doc.export_to_markdown_with(true),
798 "[AI & ML](https://a/) here, and [issues](https://first/) here, then [issues](https://second/) there.\n"
799 );
800 }
801
802 #[test]
803 fn renders_compact_table() {
804 let mut doc = DoclingDocument::new("t");
805 doc.compact_tables = true;
808 doc.push(Node::Table(Table {
809 rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
810 location: None,
811 structure: None,
812 cell_blocks: None,
813 caption: None,
814 }));
815 let md = doc.export_to_markdown();
816 assert_eq!(md, "| a | b |\n| - | - |\n| 1 | 2 |\n");
817 }
818
819 #[test]
820 fn renders_padded_github_table_by_default() {
821 let mut doc = DoclingDocument::new("t");
822 doc.push(Node::Table(Table {
823 rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
824 location: None,
825 structure: None,
826 cell_blocks: None,
827 caption: None,
828 }));
829 let md = doc.export_to_markdown();
830 assert_eq!(md, "| a | b |\n|-----|-----|\n| 1 | 2 |\n");
832 }
833
834 #[test]
835 fn strict_unescapes_inline_underscores_legacy_keeps_them() {
836 let mut doc = DoclingDocument::new("t");
837 doc.add_heading(1, "a\\_b");
838 doc.add_paragraph("x\\_y");
839 doc.push(Node::ListItem {
840 ordered: false,
841 number: 1,
842 first_in_list: true,
843 text: "i\\_j".into(),
844 level: 0,
845 marker: None,
846 location: None,
847 dclx: None,
848 href: None,
849 layer: None,
850 });
851 assert_eq!(doc.export_to_markdown(), "# a\\_b\n\nx\\_y\n\n- i\\_j\n");
853 assert_eq!(doc.export_to_markdown_with(true), "# a_b\n\nx_y\n\n- i_j\n");
855 }
856
857 fn assert_stream_matches(
860 doc: &DoclingDocument,
861 strict: bool,
862 images: ImageMode,
863 splits: &[usize],
864 ) {
865 let (want, want_artifacts) = to_markdown_images(doc, strict, images, "artifacts");
866 let mut streamer =
867 MarkdownStreamer::with_artifacts(strict, images, doc.compact_tables, "artifacts");
868 let mut got = String::new();
869 let mut got_artifacts = Vec::new();
870 let mut start = 0;
871 for &end in splits {
872 let links = if start == 0 {
875 doc.links.as_slice()
876 } else {
877 &[]
878 };
879 got.push_str(&streamer.push(&doc.nodes[start..end], links));
880 got_artifacts.extend(streamer.take_artifacts());
883 start = end;
884 }
885 got.push_str(&streamer.push(
886 &doc.nodes[start..],
887 if start == 0 {
888 doc.links.as_slice()
889 } else {
890 &[]
891 },
892 ));
893 got_artifacts.extend(streamer.take_artifacts());
894 got.push_str(&streamer.finish());
895 assert_eq!(
896 got, want,
897 "streamed output diverged (splits={splits:?}, strict={strict})"
898 );
899 assert_eq!(
900 got_artifacts, want_artifacts,
901 "streamed artifacts diverged (splits={splits:?}, strict={strict})"
902 );
903 }
904
905 #[test]
906 fn streaming_is_byte_identical_to_buffered() {
907 let mut doc = DoclingDocument::new("d");
908 doc.add_heading(1, "Title");
909 doc.add_paragraph("First paragraph.");
910 doc.push(Node::ListItem {
911 ordered: false,
912 number: 1,
913 first_in_list: true,
914 text: "a".into(),
915 level: 0,
916 marker: None,
917 location: None,
918 dclx: None,
919 href: None,
920 layer: None,
921 });
922 doc.push(Node::ListItem {
923 ordered: false,
924 number: 2,
925 first_in_list: false,
926 text: "b".into(),
927 level: 0,
928 marker: None,
929 location: None,
930 dclx: None,
931 href: None,
932 layer: None,
933 });
934 doc.push(Node::Code {
935 language: Some("rust".into()),
936 text: "let x = 1;".into(),
937 orig: None,
938 pretty: None,
939 });
940 doc.push(Node::Table(Table {
941 rows: vec![vec!["a".into(), "b".into()], vec!["1".into(), "2".into()]],
942 location: None,
943 structure: None,
944 cell_blocks: None,
945 caption: None,
946 }));
947 doc.push(Node::Picture {
948 caption: Some("Fig 1".into()),
949 image: Some(PictureImage {
950 mimetype: "image/png".into(),
951 width: 2,
952 height: 2,
953 data: b"png-one".to_vec(),
954 }),
955 classification: None,
956 });
957 doc.add_paragraph("Last paragraph.");
958 doc.push(Node::Picture {
961 caption: None,
962 image: Some(PictureImage {
963 mimetype: "image/png".into(),
964 width: 2,
965 height: 2,
966 data: b"png-two".to_vec(),
967 }),
968 classification: None,
969 });
970
971 for &strict in &[false, true] {
974 for &images in &[
975 ImageMode::Placeholder,
976 ImageMode::Embedded,
977 ImageMode::Referenced,
978 ] {
979 for splits in [&[][..], &[1][..], &[2][..], &[4][..], &[1, 4, 6, 7][..]] {
980 assert_stream_matches(&doc, strict, images, splits);
981 }
982 }
983 }
984 }
985
986 #[test]
987 fn streaming_applies_recovered_links_in_strict_mode() {
988 let mut doc = DoclingDocument::new("d");
989 doc.add_paragraph("See LinkedIn for details.");
990 doc.add_paragraph("And GitHub too.");
991 doc.links = vec![
992 ("LinkedIn".into(), "https://lnkd/".into()),
993 ("GitHub".into(), "https://gh/".into()),
994 ];
995 assert_stream_matches(&doc, true, ImageMode::Placeholder, &[1]);
998 }
999
1000 #[test]
1001 fn strict_tightens_punctuation_spacing_legacy_keeps_it() {
1002 let mut doc = DoclingDocument::new("t");
1003 doc.add_paragraph("see [ 37 , 36 ] and ( x ) .");
1004 assert_eq!(doc.export_to_markdown(), "see [ 37 , 36 ] and ( x ) .\n");
1006 assert_eq!(doc.export_to_markdown_with(true), "see [37, 36] and (x).\n");
1008 }
1009}