Skip to content

Incorrect code block rendering when preceded by text in markdown #658

@iYnOtUo

Description

@iYnOtUo

Steps to reproduce

Using flexmark to parse the following markdown:

This is a sentence.
```python
def C(m, n):
    k = 1
    l = 1

    for i in range(n):
        k *= m - i
        l *= i + 1
    return round(k / l)

The markdown should render as:

<p>This is a sentence.</p>
<pre><code class="language-python">def C(m, n):
    k = 1
    l = 1

    for i in range(n):
        k *= m - i
        l *= i + 1
    return round(k / l)
</code></pre>

Actual behavior

The code block is rendered incorrectly. The output contains unexpected nested <pre><code> tags and the formatting is broken:

<p>This is a sentence.</p>
<pre><code class="language-python">def C(m, n):
    k = 1
    l = 1
<pre><code>for i in range(n):
    k *= m - i
    l *= i + 1
return round(k / l)
</code></pre>
<p></code></pre></p>

Additional notes

  • When there is no text before the code block, rendering is correct.
  • The issue only appears when a line of text precedes the code block.
  • Version: 0.64.8
  • Code used to render:
private static String markToHTML(String markdown) {
    Parser parser = Parser.builder().build();
    HtmlRenderer renderer = HtmlRenderer.builder()
            .escapeHtml(false)
            .build();

    Node document = parser.parse(markdown);
    return renderer.render(document);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions