代码块中的Markdown换行符

时间:2012-05-03 19:26:38

标签: ruby haml markdown redcarpet

使用Redcarpet,当我在降价中包含以下内容时,它不会考虑任何换行符或缩进。我在线的末尾尝试了两个空格。代码之间的额外行。似乎没什么用。

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

```

我明白了:

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

以下是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)

我需要做些什么才能让线条正确断开并保留缩进,就像在这里或GitHub一样?

更新 - 源代码如下:

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

4 个答案:

答案 0 :(得分:5)

尝试将降价结果包装在find_and_preserve Haml助手

# 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助手包装渲染调用时,markdown输出中<pre>标记内的所有换行都会使用等效的HTML实体进行转义,然后Haml自动缩进将忽略它们。

答案 1 :(得分:4)

解析的结果对我来说在<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>
  1. 确认您在输出HTML
  2. 中看到类似的包装器
  3. 设置CSS以在<pre>块中使用预格式化:

    pre { white-space:pre }
    

答案 2 :(得分:2)

在Github上,我需要做的就是用<pre></pre>标签包装缩进/格式化的文本。

答案 3 :(得分:0)

尝试使用此脚本来确定它是应用程序还是redcarpet中的内容。

我无法重现您所拥有的问题(使用redcarpet-2.1.1 gem)。将其放入文件中,然后运行它(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

结果恰如其分:

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