Skip to content

Instantly share code, notes, and snippets.

@wadewest
Created March 9, 2016 18:05
Show Gist options
  • Save wadewest/1d53f2318723d7232258 to your computer and use it in GitHub Desktop.
Save wadewest/1d53f2318723d7232258 to your computer and use it in GitHub Desktop.
Custom HAML filter using Redcarpet to generate HTML with table of contents from Markdown. Copy file to config/initializers/ to install in Rails.
module Haml::Filters
module MarkdownWithTOC
include Haml::Filters::Base
def render(text)
<<~HAML_MD
<nav class="toc">
#{Redcarpet::Markdown.new( Redcarpet::Render::HTML_TOC.new ).render(text)}
</nav>
#{Redcarpet::Markdown.new( Redcarpet::Render::HTML
.new( with_toc_data: true ) )
.render(text)}
HAML_MD
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment