首页 文章

Redcarpet锚定到 Headers

提问于
浏览
1

我正在使用Redcarpet gem进行降价,我想为h2 Headers 生成自动锚点,以便链接到每个部分 .

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但它对我不起作用 . 我在选项区添加了它 .

1 回答

  • 0

    我做的与你描述的基本相同

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

    也许这有帮助 .

相关问题