Not able to get line breaks using redcarpet

2k views Asked by At

I am not able to get a line break <br>, even if there is a line with spaces and newline in the content. Is there any way this can enabled in redcarpet.

http://daringfireball.net/projects/markdown/syntax#p

"When you do want to insert a
break tag using Markdown, you end a line with two or more spaces, then type return."

Why is redcarpet not detecting the empty space line ?

Loading development environment (Rails 3.2.3)
irb(main):001:0> rendere = Redcarpet::Render::HTML.new(:hard_wrap => true)
=> #<Redcarpet::Render::HTML:0xa2cc414>
irb(main):002:0> markdown = Redcarpet::Markdown.new(rendere, :autolink => true, :space_after_headers => true)
=> #<Redcarpet::Markdown:0x98ab0c0>
irb(main):003:0> markdown.render("hi how are you doing\n      \nFirst line in the next paragraph\n            \nFirst line in the third paragraph\n")
=> "<p>hi how are you doing</p>\n\n<p>First line in the next paragraph</p>\n\n<p>First line in the third paragraph</p>\n"
irb(main):004:0> markdown.render("hi how are you doing\r\n      \r\nFirst line in the next paragraph\r\n            \r\nFirst line in the third paragraph\r\n")
=> "<p>hi how are you doing</p>\n\n<p>First line in the next paragraph</p>\n\n<p>First line in the third paragraph</p>\n"
irb(main):005:0>
1

There are 1 answers

0
matt On BEST ANSWER

The <br> elements are not added if the line is the last in a paragraph – only if there is something on the next line. For example this markdown (where all the lines have two trailing spaces):

Has trailing spaces, but no br.  

Several lines, all with trailing spaces.  
All but the last have br elements at the end.  
This line has spaces, but no br.  

produces this HTML with Redcarpet:

<p>Has trailing spaces, but no br.  </p>

<p>Several lines, all with trailing spaces.<br>
All but the last have br elements at the end.<br>
This line has spaces, but no br.  </p>

Your example consists of three single line paragraphs, so each line is the last in the paragraph and doesn’t have the <br> added.

This behaviour is the same as the original Markdown.pl (basically – the output isn’t identical but <br>s are added to the same lines).