Skip to content

Whitespace in code blocks is not properly preserved #508

@SteffenDE

Description

@SteffenDE

Hi there,

I'm parsing markdown files with earkmark and hooking into the postprocess to highlight the code with makeup. I noticed that the code earmark parsed does not properly preserve whitespace and therefore also the highlighted code doesn't render correctly:

"""
```xml
<!-- Basic element with attributes -->
<book id="b1" category="fiction" inStock="yes">
  <title>The Great Gatsby</title>
  <!-- Multiple elements of same type -->
  <author>F. Scott Fitzgerald</author>
  <published>1925</published>
  <!-- CDATA section for text with special characters -->
  <description><![CDATA[A story about the American Dream & society in the 1920s]]></description>
</book>
```
"""
|> Earmark.as_html!(
  escape: false,
  postprocessor: fn
    {"code", _attrs, [body], _meta} = node ->
      IO.puts(body)
      node    

    node ->
      node
  end
)

This outputs

<!-- Basic element with attributes -->

<book id="b1" category="fiction" inStock="yes">
  <title>The Great Gatsby</title>
<!-- Multiple elements of same type -->

  <author>F. Scott Fitzgerald</author>
  <published>1925</published>
<!-- CDATA section for text with special characters -->

  <description><![CDATA[A story about the American Dream & society in the 1920s]]></description>
</book>

while it should output

<!-- Basic element with attributes -->
<book id="b1" category="fiction" inStock="yes">
  <title>The Great Gatsby</title>
  <!-- Multiple elements of same type -->
  <author>F. Scott Fitzgerald</author>
  <published>1925</published>
  <!-- CDATA section for text with special characters -->
  <description><![CDATA[A story about the American Dream & society in the 1920s]]></description>
</book>

Using earmark 1.4.47.

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