Canticore has Filters! Making Markdown Work for Us

by Ardekantur

I was going to create a new repo on GitHub to show off this awesomeness, but then I figured, why not just show everyone how to use the new filter functionality?

Minus all the cruft of being a gem, this is the only code you’ll write to create a Markdown plugin for your Canticore blog.

require 'canticore/plugin'
require 'rdiscount'
 
Canticore::PluginGem::Create 'markdown' do
  enhances :all
 
  def to_markdown_formatted_html data
    RDiscount.new(data).to_html
  end
 
  filter [:post, :body], :to_markdown_formatted_html
end

That filter command does exactly what you expect it to. Every time it comes across a post body, it sends it as the parameter to the function to_markdown_formatted_html! Simple as that.

Theme designers, take note: Filters are not applied automatically! The HAML line = post.body will still output the plain post body, and not spin up the filter function at all. The line = post.filtered(:body), however, will!

I haven’t yet decided if filters can be arbitrarily ordered. I think that’s needless complexity and I can’t think of a good reason to do so.

Interested in developing Canticore or for Canticore? There hasn’t been a better time to get started! Send a message to my GitHub profile if you’d like to contribute, and I’ll point you towards some thing that needs to be done. I have a fucking laundry list, here.