From 3a39792aa907a5a5083b9f39e620cc6ba9ab7810 Mon Sep 17 00:00:00 2001 From: Ankit Kumar Date: Sun, 4 Oct 2026 01:14:16 +0530 Subject: [PATCH] fix(md): drop whitespace-only lines before code block content Signed-off-by: Ankit Kumar --- docling/backend/md_backend.py | 7 ++++++- tests/test_backend_markdown.py | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docling/backend/md_backend.py b/docling/backend/md_backend.py index 06101307dc..969529e9bf 100644 --- a/docling/backend/md_backend.py +++ b/docling/backend/md_backend.py @@ -793,7 +793,12 @@ def _iterate_elements( # noqa: C901 and isinstance((child := element.children[0]), marko.inline.RawText) # Drop blank lines around the code but keep the first line's # indentation, which is part of the code. - and len(snippet_text := child.children.lstrip("\n").rstrip()) > 0 + and len( + snippet_text := re.sub( + r"\A(?:[ \t]*\r?\n)+", "", child.children + ).rstrip() + ) + > 0 ): self._close_table(doc) _log.debug(" - Code Block: %s", element.children) diff --git a/tests/test_backend_markdown.py b/tests/test_backend_markdown.py index 04462f254e..cf92c88be7 100644 --- a/tests/test_backend_markdown.py +++ b/tests/test_backend_markdown.py @@ -257,6 +257,11 @@ def test_code_block_keeps_first_line_indentation(): "\n" " after blank line\n" "```\n" + "\n" + "```\n" + " \n" + " after spaces-only line\n" + "```\n" ) conv_result = get_converter().convert_string(markdown, format=InputFormat.MD) assert conv_result.status == ConversionStatus.SUCCESS @@ -268,6 +273,7 @@ def test_code_block_keeps_first_line_indentation(): " key: 1\n sub:\n x: 2", " deeper\nbase", " after blank line", + " after spaces-only line", ]