我正在使用红毯gem进行标记,我想为h2标题生成自动锚点,以允许链接到每个部分。
show.html.erb
<div class = "content"><%= markdown(@post.body) %></div>application_helper.rb
class HTMLwithPygments < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, :lexer => language)
end
end
def markdown(text)
renderer = HTMLwithPygments.new(:hard_wrap => true, :with_toc_data => true)
options = {
:fenced_code_blocks => true,
:no_intra_emphasis => true,
:autolink => true,
:strikethrough => true,
:lax_html_blocks => true,
:superscript => true,
}
Redcarpet::Markdown.new(renderer, options).render(text).html_safe
end我读到过:with_toc_data => true,但它对我不起作用。我在选项区域添加了它。
发布于 2017-04-22 07:24:40
我做的事情和你描述的一样,基本上就是
Redcarpet::Markdown.new(
Redcarpet::Render::HTML.new(with_toc_data: true), {}
).render(text)我的视图包含以下代码
- @article.description.scan(/(#+)(.*)/).each do |menu_item|
= content_tag(:a, menu_item[1], :href => "##{menu_item[1].downcase.strip.gsub(" ","-")}")也许这对你有帮助。
https://stackoverflow.com/questions/14673807
复制相似问题