trentm / python-markdown2

markdown2: A fast and complete implementation of Markdown in Python
Other
2.66k stars 433 forks source link

Conversion Issue with Single Line Breaks in Lists and Tables #548

Closed syntaxsurge closed 10 months ago

syntaxsurge commented 10 months ago

Description

When using the markdown2 library to convert Markdown content to HTML, I am encountering an issue where certain Markdown elements, specifically lists (both ordered and unordered) and tables, are not converting correctly unless they are preceded by two line breaks. This issue poses a significant challenge as the content I am working with is dynamic, and preprocessing it to insert additional line breaks disrupts the original formatting and flow of the text.

Steps to Reproduce

The issue can be reproduced with the following Markdown examples:

  1. Ordered List:

    import markdown2
    
    markdown_text = """
    Here is a list of movies:
    1. Movie A
    2. Movie B
    3. Movie C
    """
    
    html = markdown2.markdown(markdown_text)
    print(html)

    Expected Output: An HTML-converted ordered list. Actual Output: The list items are not recognized as part of an ordered list due to the absence of an additional line break before the list starts.

  2. Unordered List:

    import markdown2
    
    markdown_text = """
    Some fruits include:
    - Apple
    - Banana
    - Cherry
    """
    
    html = markdown2.markdown(markdown_text)
    print(html)

    Expected Output: An HTML-converted unordered list. Actual Output: The list items are not recognized as part of an unordered list due to the same issue with line breaks.

  3. Tables:

    import markdown2
    
    markdown_text = """
    | Header 1 | Header 2 |
    |----------|----------|
    | Row 1    | Data     |
    | Row 2    | Data     |
    """
    
    html = markdown2.markdown(markdown_text)
    print(html)

    Expected Output: An HTML-converted table. Actual Output: The table is not properly recognized and converted due to the lack of an additional line break before it begins.

Expected Behavior

The markdown2 library should ideally convert Markdown elements like lists and tables to their corresponding HTML counterparts without requiring two line breaks before them. This behavior is crucial for maintaining the integrity of dynamically generated content where additional line breaks can disrupt the structure and readability.

Additional Context

This issue significantly affects the processing of dynamic content where manual insertion of line breaks is not feasible or disrupts the intended formatting. A solution or workaround that enables the markdown2 library to recognize and convert these elements with single line breaks would be greatly appreciated.

Thank you for your attention to this matter.

Crozzers commented 10 months ago

The lists should be covered by the cuddled-lists extra, but I'm not sure why the table isn't recognised. I'll look into that

syntaxsurge commented 10 months ago

The lists should be covered by the cuddled-lists extra, but I'm not sure why the table isn't recognised. I'll look into that

Thanks, it is working now. The reason why the table did not work because I forgot to add extras=['tables'].