46

I've got a Markdown-formatted sidebar that I'd like to show up in my Jekyll blog. I'd previously tried to include it like {% include sidebar.markdown %} but it wouldn't actually render the Markdown. I can successfully include it like:

{% capture sidebar %}{% include sidebar.markdown %}{% endcapture %}
{{ sidebar | markdownify }}

and although this is a manageable solution, I'd prefer a more elegant way of accomplishing this. Any ideas? Thanks in advance!

S M
  • 8,155
  • 3
  • 35
  • 36
  • 3
    This link is dead. This is one reason it's better to answer the question instead of linking to a solution. – askewchan May 10 '13 at 18:14
  • I like the original solution, but instead of putting the include between the capture, I just put my content, and moved the capture + render inside my _includes/sidebar.html – stackdump Jul 23 '14 at 03:16

2 Answers2

21

I was looking for this too, it was a PITA discovering how to do it, not much Google content, the most exact finding was a gist that wouldn't work here... dead simple solution:

./_plugins/markdown_tag.rb:

module Jekyll
  class MarkdownTag < Liquid::Tag
    def initialize(tag_name, text, tokens)
      super
      @text = text.strip
    end
    require "kramdown"
    def render(context)
      tmpl = File.read File.join Dir.pwd, "_includes", @text
      Jekyll::Converters::Markdown::KramdownParser.new(Jekyll.configuration()).convert(tmpl)
    end
  end
end
Liquid::Template.register_tag('markdown', Jekyll::MarkdownTag)

UPDATE: blog with usage example: https://web.archive.org/web/20161207125751/http://wolfslittlestore.be/2013/10/rendering-markdown-in-jekyll/

Breno Salgado
  • 1,922
  • 1
  • 20
  • 26
  • 1
    Something weird seems to happen to the markdown with this plugin. For example, when using fenced code blocks, the newlines seem to disappear. I've also added support for MathJax to my installation, and the backslashes get removed. The explicit inclusion given by the OP works just fine in both cases. Any ideas how to work around these problems? – Mike Nov 07 '14 at 18:09
  • 1
    Thanks, your blog post is very helpful. – apotonick Jul 09 '15 at 01:49
5

Jekyll now supports writing simple plugins to add tags, converters, or generators. Take a look at http://jekyllrb.com/docs/plugins/ for details.

shauvik
  • 3,912
  • 2
  • 23
  • 18
Jeffrey Hulten
  • 773
  • 5
  • 11
  • 1
    Ooh, I hadn't thought of creating a new tag. I will give that a whirl, thanks! – S M Sep 17 '11 at 18:56
  • Okay, so I've tried my hand at creating the correct tag but I'm still not quite able to figure out what code is needed. It looks like I'll have to reimplement the include tag (https://github.com/mojombo/jekyll/blob/master/lib/jekyll/tags/include.rb) but I am not sure how to markdownify its output - presumably that would involve a change at line 26 but I am not really a Rubyist and don't know what to do... – S M Sep 22 '11 at 05:54
  • I think you can get the converter for your type with: converter = self.site.converters.find { |c| c.matches(self.ext) } Then use the convert method. See https://github.com/mojombo/jekyll/blob/master/lib/jekyll/converters/markdown.rb for how that is implemented. – Jeffrey Hulten Sep 23 '11 at 00:25
  • 9
    Even though the OP didn't mention **GitHub Pages**, it's important to note that they do not support Jekyll plugins, so if one uses GHP for publishing, the solution won't be valid anymore. – Janusz Lenar Dec 23 '12 at 22:45