Skip to content
Merged
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
7 changes: 6 additions & 1 deletion docling/backend/md_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_backend_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
]


Expand Down
Loading