Created
March 9, 2016 18:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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