Ruby on rails 3 红地毯锚定至收割台

Ruby on rails 3 红地毯锚定至收割台,ruby-on-rails-3,redcarpet,Ruby On Rails 3,Redcarpet,我正在使用红毯宝石进行降价,我想为h2标题生成自动锚,以允许链接到每个部分 show.html.erb <div class = "content"><%= markdown(@post.body) %></div> 应用程序\u helper.rb class HTMLwithPygments < Redcarpet::Render::HTML def block_code(code, language) Pygments.highlig

我正在使用红毯宝石进行降价,我想为h2标题生成自动锚,以允许链接到每个部分

show.html.erb

<div class = "content"><%= markdown(@post.body) %></div>
应用程序\u 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,但它对我不起作用。我在选项区添加了它。

我做了与您描述的相同的事情,基本上是

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(" ","-")}")

也许这会有帮助。

这对我来说很有用。带有_toc_data:true的选项只需在html h1标记中添加一个ID,例如bongos