RDiscount:generate_toc与Sinatra [英] RDiscount :generate_toc with Sinatra

查看:181
本文介绍了RDiscount:generate_toc与Sinatra的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我使用Rdiscount进行了sinatra设置,以使用HAML布局呈现Markdown文件.所有这些都有效,但是我希望RDiscount根据haml文件中的标头生成目录.我尝试过在sinatra配置中进行设置.

So I have sinatra setup with Rdiscount to render a markdown file with a HAML layout. This all works but I want RDiscount to generate a table of contents based on the headers in my haml file. I've tried setting it in the sinatra configuration.

set :markdown, :generate_toc => true

但是似乎不起作用.

当我像这样渲染markdown时,我也尝试过这样做:

I've also tried doing it when I render the markdown like so:

markdown :PAGENAMEHERE, :layout => :'layouts/PAGENAMEHERE', :generate_toc => true

这也不起作用.

这甚至有可能吗?如果是,那我在做什么错了?

Is this even possible? If it is, what am I doing wrong?

推荐答案

虽然@three的回答对我有很大帮助,但我想展示一个也许更通用的解决方案:

While @three's answer helped me a lot, I would like to show a perhaps more general solution:

class MDWithTOC < ::Tilt::RDiscountTemplate
  def flags
    [:generate_toc]
  end
end

Tilt.register MDWithTOC, 'md'

在这里,我们重写RDiscount Tilt模板处理程序的flags方法,并将其重新混合为md格式的处理程序.

Here we override the flags method of the RDiscount Tilt template handler and regiter it as a handler for the md format.

现在,您可以像往常一样使用md帮助器,它将生成目录.

Now you can use the md helper as always and it will generate the TOC.

这篇关于RDiscount:generate_toc与Sinatra的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆