【问题标题】:Markdown Line Breaks in Code Blocks代码块中的 Markdown 换行符
【发布时间】:2012-05-03 19:26:38
【问题描述】:

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

【问题讨论】:

  • 这就是您在网络浏览器中看到的内容,但是您得到的 HTML 是什么?
  • 如果你通过 github 的 gh-pages 渲染它,问题可能只是 github 不支持 redcarpet2,它允许使用围栏代码块。见this question
  • @cboettig 不错的猜测,但 Github 风格的 Markdown does support this.
  • @Phrogz Github-flavored markdown 支持,但具有讽刺意味的是 Github 的 Jekyll 驱动的 gh-pages 不支持 Github-Flavored-Markdown。 (请参阅我上面评论中的链接问题)。因此,我可以通过将这样的降价文件放入 github 上的 gh-pages 分支来重现所描述的错误。

标签: ruby haml markdown redcarpet


【解决方案1】:

尝试将降价结果包装在 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 输出中 &lt;pre&gt; 标记中的所有换行符都将使用等效的 HTML 实体进行转义,然后 Haml 自动缩进将忽略它们。

【讨论】:

  • 任何从谷歌找到这个的人,find_and_preserve 终于解决了我一直在努力解决几个小时的 Markdown 渲染中出现奇怪缩进的问题。
【解决方案2】:

解析的结果在我的 &lt;pre&gt; 块内有换行符:

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. 将您的 CSS 设置为在 &lt;pre&gt; 块中使用预格式化:

    pre { white-space:pre }
    

【讨论】:

  • 好多了!但它仍然在填补空白。在 OP 中看到,我添加了 html 源代码。第一行左对齐,但后面的行有太多的前导空白。想法?
  • 想通了...是 HAML 导致了问题。降价源是从 HAML 布局调用的模板。在开发中,HAML 以漂亮的格式输出 html,带有缩进,从而改变代码块的显示。在使用 :ugly 输出的生产中,它看起来是正确的。此外,@Phrogz 的 pre { white-space:pre } 也是让它看起来正确的必要条件。
  • 这适用于 haml 布局的开发= preserve(yield)
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    尝试使用此脚本来确定它是在您的应用程序中还是在红地毯上。

    我无法重现您遇到的问题(使用 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>
    

    【讨论】:

    • 在上面的测试中我得到了和你一样的结果。但是在 Rails 视图中,它不会转义代码块中的任何内容。我应该补充一点,我正在使用带有自定义 Redcarpet 渲染器的 markdown-rails gem。你可以在这里看到我的叉子:github.com/threadhead/markdown-rails
    猜你喜欢
    • 2012-07-03
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多