Line breaks in code blocks

Using Redcarpet, when I include something like the following in my markdown, it does not respect any line breaks or indentation. I tried two spaces at the end of the lines. Extra lines between code. Nothing seems to work.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<hash>
   <money>3</money>
</hash>

```

I see:

<?xml version="1.0" encoding="UTF-8"?> <hash> <money>3</money> </hash>

Here are the settings for Redcarpet:

Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true, :fenced_code_blocks => true, :no_intra_emphasis => true, :lax_html_blocks => true)

What do I need to do so that the lines are broken correctly and keep the indentation, as here, or on GitHub?

Refresh . And the source looks like this:

<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
                &lt;hash&gt;
                &lt;money&gt;3&lt;/money&gt;
                &lt;/hash&gt;  
                </code></pre>
+5
source share
4 answers

Try to wrap markdown results in find_and_preserveHaml Assistant

# Assuming a setup like this:
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
code_snippet = "    <xml>\n      <tag/>\n    </xml>"

# This should prevent undesirable spaces within code blocks:
find_and_preserve(markdown.render(code_snippet)).html_safe

find_and_preserve Haml, <pre> HTML, Haml .

+5

<pre> :

require 'redcarpet'
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, fenced_code_blocks:true)

puts md.render("```xml\n<foo>\n</foo>\n```")
#=> <pre><code class="xml">&lt;foo&gt;
#=> &lt;/foo&gt;
#=> </code></pre>
  • , HTML
  • CSS <pre>:

    pre { white-space:pre }
    
+4

Github , , / <pre> </pre>.

+1

script, - .

I can not reproduce the problem you are facing (with the gem of redcarpet-2.1.1). Put this in a file, then run it ( ruby redcarpet_test.rb):

require 'rubygems'
require 'redcarpet'

md = %Q{...
```xml
<?xml version="1.0" encoding="UTF-8"?>
<hash>
   <money>3</money>
</hash>
```
...}

r = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true, :fenced_code_blocks => true, :no_intra_emphasis => true, :lax_html_blocks => true)

puts r.render md

This corresponds to the following:

<p>...
<code>xml
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;hash&gt;
   &lt;money&gt;3&lt;/money&gt;
&lt;/hash&gt;
</code>
...</p>
0
source

All Articles