Skip to content

Instantly share code, notes, and snippets.

@mignev
Forked from tmtk75/markdown-tag.rb
Last active March 3, 2016 11:34
Show Gist options
  • Save mignev/7759676 to your computer and use it in GitHub Desktop.
Save mignev/7759676 to your computer and use it in GitHub Desktop.
=begin
Jekyll tag to include Markdown text from _includes directory preprocessing with Liquid.
Usage:
{% markdown <filename> %}
=end
module Jekyll
class MarkdownTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text.strip
end
def render(context)
tmpl = File.read File.join Dir.pwd, "_includes", @text
site = context.registers[:site]
converter = site.getConverterImpl(Jekyll::Converters::Markdown)
tmpl = (Liquid::Template.parse tmpl).render site.site_payload
html = converter.convert(tmpl)
end
end
end
Liquid::Template.register_tag('markdown', Jekyll::MarkdownTag)
@mamafanfan
Copy link

was trying to include a markdown file that was not a post. and this does it perfectly.

@moble
Copy link

moble commented Nov 7, 2014

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. Explicit inclusion along the lines of

{% capture my-include %}{% include test.md %}{% endcapture %}
{{ my-include | markdownify }}

Doesn't have these problems. Any ideas how to work around these while still using the plugin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment