【问题标题】:Escape html inside code block markdown redcarpet gem rails 4在代码块markdown redcarpet gem rails 4中转义html
【发布时间】:2014-12-20 20:31:56
【问题描述】:

我刚刚安装了 redcarpet gem markdown,一切正常,除了代码块,它不能正常工作。

```@font-face {</div><div>&nbsp; font-family: 'Glyphicons Halflings';</div><div>&nbsp; src: font-   url('glyphicons-halflings-regular.eot');</div><div>&nbsp; src: font-url('glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), font-url('glyphicons-halflings-regular.woff') format('woff'), font-url('glyphicons-halflings-regular.ttf') format('truetype'), font-url('glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');</div><div>}```

正如你看到它在代码块中添加 html 作为原始文本,所以如果我写:

```my code here
another code```

它会输出:

```my code here<br>another code```

有什么建议可以解决这个问题吗?

在我的 application_helper.rb 我有:

def markdown(text)
  options = {
   filter_html:     false,
   hard_wrap:       true,
   link_attributes: { rel: 'nofollow', target: "_blank" },
   space_after_headers: true,
   fenced_code_blocks: true
  }

  extensions = {
   autolink:           true,
   no_intra_emphasis: true,
   superscript:        true,
   highlight: true,
   strikethrough: true,
   quote: true,
   no_images: true,
   no_styles: true,
   prettify: true,
   superscript: true,
   footnotes: true,
   tables: true
 }

  renderer = Redcarpet::Render::HTML.new(options)
  markdown = Redcarpet::Markdown.new(renderer, extensions)
  markdown.render(text).html_safe

结束

在我看来.html.erb 我有:

<p class="body">
 <%= markdown(@post.body).html_safe %>
</p>

【问题讨论】:

  • 我不明白问题出在哪里。你能澄清一下吗?
  • 当我在 Markdown 语言中编写代码时:git add .&lt;br&gt;git commit -m "fix fonts"&lt;br&gt;git push origin master&lt;br&gt;git push heroku master 它应该是:``` git add 。 git commit -"fix font" git pu..etc``` 它添加的是纯文本而不是html!
  • 当然会将其解释为普通文本。这就是为什么它是一个代码块。这样人们就可以阅读它,并且浏览器将其呈现为文本,而不是代码。你到底想做什么?代码块内的任何内容都会自动转义。它放在precode 标签中。事实上,在您的问题中,您在 StackoverFlow 上使用了降价。
  • 是的,我知道,但是它在里面添加了markdown代码,我希望我自己解释一下。让我们看看,我输入:def test (enter) @test = "code"(enter) end 它转换为:def code &lt;br&gt; @test = "code"&lt;br&gt; end 所以它添加“当输入其他任何内容时在里面添加 html 代码并将其呈现为纯文本”它应该添加新行而不显示“br”标签,当我按“空格”时也是如此,它会添加一个:“nbsp;” html 文本并将其呈现为计划文本..
  • 你可以在这里看到一张图片:oi57.tinypic.com/snyrzc.jpg 你可以看到它会自动添加“

标签: ruby-on-rails markdown redcarpet


【解决方案1】:

在 bootsy gem 和 redcarpet gem 之间是个问题。我最终通过删除 bootsy gem 和所有与之相关的东西来解决它,并坚持使用 redcarpet gem。似乎.html_escape 在使用两个宝石时发生了冲突。还是谢谢!

【讨论】:

    猜你喜欢
    • 2016-04-10
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多