Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docling_core/transforms/serializer/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,16 @@ def serialize_strikethrough(self, text: str, **kwargs: Any):
"""Apply Markdown-specific strikethrough serialization."""
return f"~~{text}~~"

@override
def serialize_subscript(self, text: str, **kwargs: Any):
"""Apply Markdown-specific subscript serialization."""
return f"<sub>{text}</sub>"

@override
def serialize_superscript(self, text: str, **kwargs: Any):
"""Apply Markdown-specific superscript serialization."""
return f"<sup>{text}</sup>"

@override
def serialize_hyperlink(
self,
Expand Down
15 changes: 13 additions & 2 deletions docling_core/transforms/serializer/plain_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class PlainTextDocSerializer(MarkdownDocSerializer):
"""Document serializer that produces clean plain text.

Strips all Markdown decoration — heading markers, bold/italic/strikethrough
markers, and hyperlink syntax — while keeping list bullets (``-``), ordered
list numbers, and table-cell separators (``|``) intact.
markers, sub/superscript tags, and hyperlink syntax — while keeping list
bullets (``-``), ordered list numbers, and table-cell separators (``|``)
intact.
"""

text_serializer: BaseTextSerializer = PlainTextTextSerializer()
Expand All @@ -65,6 +66,16 @@ def serialize_strikethrough(self, text: str, **kwargs: Any) -> str:
"""Apply plain-text strikethrough serialization."""
return text

@override
def serialize_subscript(self, text: str, **kwargs: Any) -> str:
"""Apply plain-text subscript serialization."""
return text

@override
def serialize_superscript(self, text: str, **kwargs: Any) -> str:
"""Apply plain-text superscript serialization."""
return text

@override
def serialize_hyperlink(
self,
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_doc.embedded.md.gt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

- (i) Item 1 in A
- (ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_doc.placeholder.md.gt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

- (i) Item 1 in A
- (ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_doc.referenced.md.gt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

- (i) Item 1 in A
- (ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_document.yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

- (i) Item 1 in A
- (ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_legacy_annot_mark_false.gt.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

- (i) Item 1 in A
- (ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_legacy_annot_mark_true.gt.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

- (i) Item 1 in A
- (ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_mode_always_valid_false.gt.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

(i) Item 1 in A
(ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_mode_always_valid_true.gt.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

1. (i) Item 1 in A
2. (ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_mode_auto_valid_false.gt.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

(i) Item 1 in A
(ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_mode_auto_valid_true.gt.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

- (i) Item 1 in A
- (ii) Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_mode_never_valid_false.gt.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

Item 1 in A
Item 2 in A
Expand Down
2 changes: 1 addition & 1 deletion test/data/doc/constructed_mode_never_valid_true.gt.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $$E=mc^2$$

<!-- missing-form-item -->

Some formatting chops: **bold** *italic* underline ~~strikethrough~~ subscript superscript [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)
Some formatting chops: **bold** *italic* underline ~~strikethrough~~ <sub>subscript</sub> <sup>superscript</sup> [hyperlink](.) &amp; [~~***everything at the same time.***~~](https://github.com/DS4SD/docling)

1. Item 1 in A
2. Item 2 in A
Expand Down
28 changes: 27 additions & 1 deletion test/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from docling_core.transforms.serializer.webvtt import WebVTTDocSerializer, WebVTTParams
from docling_core.transforms.visualizer.layout_visualizer import LayoutVisualizer
from docling_core.types.doc import DoclingDocument
from docling_core.types.doc import DoclingDocument, Formatting, Script
from docling_core.types.doc.base import ImageRefMode
from docling_core.types.doc.document import (
BaseMeta,
Expand Down Expand Up @@ -204,6 +204,32 @@ def test_md_inline_and_formatting():
verify(exp_file=src.with_suffix(".gt.md"), actual=actual)


def test_md_subscript_formatting():
"""Subscript spans are exported as inline <sub> HTML in Markdown."""
doc = DoclingDocument(name="test")
doc.add_text(
label=DocItemLabel.TEXT,
text="H2O",
formatting=Formatting(script=Script.SUB),
)

actual = MarkdownDocSerializer(doc=doc).serialize().text
assert "<sub>H2O</sub>" in actual


def test_md_superscript_formatting():
"""Superscript spans are exported as inline <sup> HTML in Markdown."""
doc = DoclingDocument(name="test")
doc.add_text(
label=DocItemLabel.TEXT,
text="2",
formatting=Formatting(script=Script.SUPER),
)

actual = MarkdownDocSerializer(doc=doc).serialize().text
assert "<sup>2</sup>" in actual


def test_md_pb_placeholder_and_page_filter():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
Expand Down
Loading