Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Jekyll, is there a concise way to render a Markdown partial?

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!

like image 434
S M Avatar asked Aug 29 '11 04:08

S M


2 Answers

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/

like image 161
Breno Salgado Avatar answered Sep 22 '22 09:09

Breno Salgado


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

like image 23
Jeffrey Hulten Avatar answered Sep 22 '22 09:09

Jeffrey Hulten