Skip to content

Indent before HTML block elements causes indent in Markdown output #98

Closed

Description

@chrispy-snps

In our HTML, block elements are indented:

<html>
  <body>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
      sed do eiusmod tempor incididunt ut labore et dolore magna
      aliqua. Ut enim ad minim veniam, quis nostrud exercitation
      ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </p>
  </body>
</html>

When HTML with indented block elements is converted, the indent causes incorrect formatting in the output.

Converting this indented <p> element:

from markdownify import markdownify as md

print(repr(md("""\
  <p>This is
     some text.</p>
""")))

produces this:

' This is\n some text.\n\n\n'
 ^       ^^^

It happens for non-<p> elements too. Converting these indented <h1> elements with the UNDERLINED and ATX heading formats:

print(repr(md("""\
    <h1>Title</h1>
""")))

print(repr(md("""\
    <h1>Title</h1>
""", heading_style="ATX")))

produces this:

' Title\n=====\n\n\n'
 ^

' # Title\n\n\n'
 ^

As a workaround, we iterate through all text object descendants in all text-containing block elements (<p>, <entry>, <li>, etc.) and convert newlines to spaces, but this is expensive on large document sets.

Possibly related to #31.

Activity

  1. changed the title [-]Indent in `<p>` causes indent in Markdown output[/-] [+]Indent before HTML block elements causes indent in Markdown output[/+] on Nov 26, 2023
  2. chrispy-snps commented on Jan 14, 2024

    @chrispy-snps
    CollaboratorAuthor

    This seems to be a duplicate of issue #96.

  3. mirabilos commented on Jan 31, 2024

    @mirabilos

    or rather #88 perhaps

  4. added a commit that references this issue on Apr 9, 2024
    60d8666
  5. added a commit that references this issue on May 21, 2025
    5b22cd3
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions