Skip to content

Markdown backend strips the first-line indentation of code blocks #4485

Closed

Description

@ankit2235

Bug

The Markdown backend calls .strip() on code block content (md_backend.py, child.children.strip()), which removes the leading indentation of the first line while later lines keep theirs. The code's relative indentation changes, so the content is altered rather than just trimmed: a YAML snippet changes structure, and an indented Python snippet becomes an IndentationError.

Code blocks whose first line is not indented are unaffected, which is probably why this has gone unnoticed.

Input (fenced code) Extracted code text
key: 1 / sub: / x: 2 key: 1 / sub: / x: 2
x = 1 / y = 2 x = 1 / y = 2

The same happens for indented (non-fenced) code blocks.

PR #3891 already fixes the equivalent problem for HTML <pre> (trimming only the leading newline and trailing whitespace instead of .strip()), but it does not touch the Markdown backend. LaTeX verbatim shows the same behaviour; I'm leaving that out of scope here.

Steps to reproduce

from io import BytesIO

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

md = "```yaml\n  key: 1\n  sub:\n    x: 2\n```\n"
doc = DocumentConverter().convert(
    DocumentStream(name="a.md", stream=BytesIO(md.encode()))
).document
print([t.text for t in doc.texts if t.label == "code"])

Actual: ['key: 1\n sub:\n x: 2']
Expected: [' key: 1\n sub:\n x: 2']

Docling version

2.131.0 (the line is unchanged on main @ bba2ec5)

Python version

3.12.14

I'd like to send a PR for the Markdown backend.

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