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
14 changes: 14 additions & 0 deletions docling/backend/md_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,20 @@ def _append_table_text(self, text: str) -> None:

def _close_table(self, doc: DoclingDocument):
self.in_pipeless_table = False
if self.in_table and not (
len(self.md_table_buffer) > 1
and MarkdownDocumentBackend._is_delimiter_row(self.md_table_buffer[1])
):
# GFM: a table needs a delimiter row right after its header, so
# lines that merely start with a pipe are a paragraph. Building a
# table here would drop the second line, taken for that row.
Comment on lines +350 to +352

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first sentence is useful: it states the GFM rule. The second sentence explains what the old, buggy code did, not what a future maintainer needs to know. It should be removed or rewritten to describe the invariant. For example:

Suggested change
# GFM: a table needs a delimiter row right after its header, so
# lines that merely start with a pipe are a paragraph. Building a
# table here would drop the second line, taken for that row.
# GFM: a table requires a delimiter row immediately after its header.
# Without one, lines that start with a pipe are ordinary paragraph text.

text = " ".join(
unescape(row.strip()) for row in self.md_table_buffer if row.strip()
)
self.in_table = False
self.md_table_buffer = []
if text:
doc.add_text(label=DocItemLabel.TEXT, text=text)
if self.in_table:
_log.debug("=== TABLE START ===")
for md_table_row in self.md_table_buffer:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_backend_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,24 @@ def test_convert_pipes_in_prose_stay_text():
assert conv_result.document.tables == []


def test_convert_leading_pipe_lines_without_delimiter_row_keep_all_text():
"""
GFM: a table needs a delimiter row right after its header. Without one,
lines that start with a pipe are a paragraph, and the second of them used
to be dropped as if it were that delimiter row.
"""
cases = [
("| a | b |\n| 1 | 2 |\n| 3 | 4 |\n", "| a | b | | 1 | 2 | | 3 | 4 |"),
("| quoted note\nsecond line\n", "| quoted note second line"),
]

for markdown, expected in cases:
conv_result = get_converter().convert_string(markdown, format=InputFormat.MD)
assert conv_result.status == ConversionStatus.SUCCESS
assert conv_result.document.tables == []
assert [item.text for item in conv_result.document.texts] == [expected]


def test_convert_pipeless_table_does_not_leak_into_later_text():
"""
Regression guard:
Expand Down
Loading