Search code examples
rubysinatramarkdowntiltrdiscount

RDiscount :generate_toc with 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

but that doesn't seem to work.

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

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

which also doesn't work.

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


Solution

  • 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'
    

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

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