Last Updated: February 25, 2016
·
5.416K
· keyboardcowboy

Use Markdown inside HTML blocks in Jekyll

I can't take credit for the code, but it was so helpful to me as a new Jekyll developer that I need to share it.

Jekyll can be easily extended using plugins. Simply create a directory at your site root called _plugins and add your plugins there. See the jekyll docs for more info.

I found the markdown plugin in this StackOverflow Post.

module Jekyll
  class MarkdownBlock < Liquid::Block
    def initialize(tag_name, text, tokens)
      super
    end
    require "kramdown"
    def render(context)
      content = super
      "#{Kramdown::Document.new(content).to_html}"
    end
  end
end
Liquid::Template.register_tag('markdown', Jekyll::MarkdownBlock)

Then you can wrap anything, including an included .md file, in this block to have it render as markdown, even inside of HTML.

{% markdown %}
[Stack Overflow](http://www.stackoverflow.com)
{% endmarkdown %}

Credit to MisterMetaphor for this.

This plugin utilizes the Ruby kramdown markdown parser.