Skip to content
Closed
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
13 changes: 13 additions & 0 deletions docling/backend/md_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class MarkdownDocumentBackend(DeclarativeDocumentBackend):
_ENTITY_RE = re.compile(r"&(#\d+|#x[0-9a-fA-F]+|\w+);")
_DELIMITER_CELL_RE = re.compile(r":?-+:?")
_PIPE_ENTITY = "|"
_BR_TAG_RE = re.compile(r"<br[\s/>]", re.IGNORECASE)

@staticmethod
def _split_table_row(row: str) -> list[str]:
Expand Down Expand Up @@ -810,6 +811,18 @@ def _iterate_elements( # noqa: C901
formatting=formatting,
hyperlink=hyperlink,
)

elif self.in_table and isinstance(
element, marko.inline.InlineHTML | marko.inline.AutoLink
):
_log.debug(" - Inline HTML or autolink in a table: %s", element.children)
# The text inside a tag pair and the URL of an autolink arrive as
# RawText children, so only a <br> adds to the cell: a line break,
# as the HTML backend gives for <br> in a cell.
if isinstance(element, marko.inline.InlineHTML) and self._BR_TAG_RE.match(
str(element.children)
):
self._append_table_text("\n")
else:
if not isinstance(element, str):
self._close_table(doc)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_backend_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,35 @@ def test_convert_table_keeps_inline_code_spans():
]


def test_convert_table_keeps_inline_html_and_autolinks():
"""Inline HTML and an autolink inside a GFM table cell are part of that cell.

A <br> is a line break in the cell, the text inside other tags stays
without the tags, and an autolink keeps its URL.
"""
markdown = """| Name<br>(unit) | Value |
| --- | --- |
| CO<sub>2</sub> | <kbd>Ctrl</kbd>+<kbd>C</kbd> |
| a<br>b<br/>c<br />d | see <https://x.org> now |
"""
conv_result = get_converter().convert_string(markdown, format=InputFormat.MD)
assert conv_result.status == ConversionStatus.SUCCESS

assert conv_result.document.texts == []
assert len(conv_result.document.tables) == 1
table_data = conv_result.document.tables[0].data
assert table_data.num_rows == 3
assert table_data.num_cols == 2
assert [cell.text for cell in table_data.table_cells] == [
"Name\n(unit)",
"Value",
"CO2",
"Ctrl+C",
"a\nb\nc\nd",
"see https://x.org now",
]


def test_convert_table_has_no_duplicate_cells():
"""
Regression test:
Expand Down
Loading