Skip to content

Markdown backend keeps a whitespace-only first line in code blocks #4541

Closed

Description

@ankit2235

Bug

Follow-up to #4485 / #4487. The Markdown backend trims code block content with lstrip("\n").rstrip(), so a first line that holds only spaces survives: the code item starts with a line of whitespace. #4502 fixed the same case in the LaTeX backend (review suggestion from @DanielNg0729) by dropping leading lines that hold only whitespace.

Steps to reproduce

from docling.datamodel.base_models import InputFormat
from docling.document_converter import DocumentConverter

md = "```\n   \n    x = 1\n```\n"
doc = DocumentConverter().convert_string(md, format=InputFormat.MD).document
print([t.text for t in doc.texts if t.label == "code"])

Actual: [' \n x = 1']
Expected: [' x = 1']

Docling version

main @ 51fe9ff

Python version

3.12.14

I'd like to send a PR using the same trimming as #4502.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions