Skip to content

Should not treat newline at end of pre/code block as blank line #218

Closed

Description

@stevecj

When round-tripping between Markdown and HTML, I am seeing an extra blank line appended to every code block on every round trip because the HTML generated by markdown2 has a newline at the end of the content within the pre/code tags.

I have found that typical browsers don't display that as a blank line, so markdownify should not treat it as one either.

I think that the correct behavior should be to ignore the final character within the elements when it is a newline character.

Activity

  1. stevecj commented on May 12, 2025

    @stevecj
    Author

    I have actually now found the same issue with leading newlines. I think that the first leading newline should also be ignored if present.

    The case for this is even stronger because, from https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/pre

    Whitespace inside this element is displayed as written, with one exception. If one or more leading newline characters are included immediately following the opening <pre> tag, the first newline character is stripped.

  2. chrispy-snps commented on May 16, 2025

    @chrispy-snps
    Collaborator

    @stevecj - I created a quick testcase and this is indeed an unfortunate round-trip behavior:

    import markdownify
    import markdown2
    
    html = """
    <p>LINE</p>
    <pre>
    PRE
    </pre>
    <p>LINE</p>
    """
    
    md = markdownify.MarkdownConverter().convert(html)
    print(md)
    
    print("="*40)
    
    print(markdown2.markdown(md, extras=["fenced-code-blocks"]))
    LINE
    
    ```
    
    PRE
    
    ```
    
    LINE
    
    <p>LINE</p>
    
    <pre><code>
    PRE
    
    </code></pre>
    
    <p>LINE</p>
    

    If we chomp a single newline from the start and end, is it going to nibble away valid/meaningful newlines during repeated round-trips?

    How would you feel about an option that strips all leading/trailing newlines from <pre> elements?

  3. stevecj commented on May 17, 2025

    @stevecj
    Author

    I can do more testing, but it looks to me like stripping the first at the head and the last at the tail will usually do the right thing. If there are intentional newlines, there will still be 1 added at the head/tail during conversion to HTML besides the intentional ones.

  4. chrispy-snps commented on May 24, 2025

    @chrispy-snps
    Collaborator

    @stevecj - what if we implemented a new strip_pre configuration option that behaves as follows?

    strip_pre

    Controls whether leading and trailing newlines are removed from content in <pre> elements. Supported values are STRIP (strip all leading/trailing newlines), STRIP_ONE (strip a single leading/trailing newline), STRIP (strip all leading/trailing newlines), and None (leave as-is). Newlines within the content are unaffected. Defaults to None.

    My company could actually use the STRIP behavior, as our content pipeline has no need to preserve leading/trailing newlines in <pre> blocks.

  5. stevecj commented on May 27, 2025

    @stevecj
    Author

    That seems fine to me. I think that stripping 1 or stripping all should probably become the default though. From what I can see, stripping none is the least likely to give expected/desired results.

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