无法使用redcarpet获得换行符

时间:2013-12-11 23:50:59

标签: ruby-on-rails ruby markdown redcarpet

我无法获得换行符< br>,即使内容中有空格和换行符。有没有办法在redcarpet中启用它。

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

“如果您想使用Markdown插入中断标记,则结束包含两个或更多空格的行,然后键入return。”

为什么redcarpet没有检测到空的空行?

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 个答案:

答案 0 :(得分:4)

如果该行是段落中的最后一行,则不会添加<br>元素 - 仅当下一行中有内容时才会添加。例如,这个markdown(所有行都有两个尾随空格):

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.  

使用Redcarpet生成此HTML:

<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>

您的示例包含三个单行段落,因此每行都是段落中的最后一行,并且没有添加<br>

此行为与原始Markdown.pl相同(基本上 - 输出不相同,但<br>被添加到同一行。)