【发布时间】:2014-12-20 20:31:56
【问题描述】:
我刚刚安装了 redcarpet gem markdown,一切正常,除了代码块,它不能正常工作。
```@font-face {</div><div> font-family: 'Glyphicons Halflings';</div><div> src: font- url('glyphicons-halflings-regular.eot');</div><div> 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 .<br>git commit -m "fix fonts"<br>git push origin master<br>git push heroku master它应该是:``` git add 。 git commit -"fix font" git pu..etc``` 它添加的是纯文本而不是html! -
当然会将其解释为普通文本。这就是为什么它是一个代码块。这样人们就可以阅读它,并且浏览器将其呈现为文本,而不是代码。你到底想做什么?代码块内的任何内容都会自动转义。它放在
pre或code标签中。事实上,在您的问题中,您在 StackoverFlow 上使用了降价。 -
是的,我知道,但是它在里面添加了markdown代码,我希望我自己解释一下。让我们看看,我输入:
def test (enter) @test = "code"(enter) end它转换为:def code <br> @test = "code"<br> end所以它添加“当输入其他任何内容时在里面添加 html 代码并将其呈现为纯文本”它应该添加新行而不显示“br”标签,当我按“空格”时也是如此,它会添加一个:“nbsp;” html 文本并将其呈现为计划文本.. -
你可以在这里看到一张图片:oi57.tinypic.com/snyrzc.jpg 你可以看到它会自动添加“
标签: ruby-on-rails markdown redcarpet