Skip to content

Instantly share code, notes, and snippets.

@DakuTree
Last active June 23, 2018 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DakuTree/af46d0629a1a3d9181514c3f2398666d to your computer and use it in GitHub Desktop.
Save DakuTree/af46d0629a1a3d9181514c3f2398666d to your computer and use it in GitHub Desktop.
Jekyll - Custom post_file_link tag plugin
# Jekyll - Custom post_file_link tag plugin. Points to /files/:title/FILE.ext
#
# How to install: Drop this file in `_plugins`.
# How to use: `{% post_file_link FILENAME.EXT %}`
module Jekyll
class RenderPostFileLinkTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text
end
def lookup(context, name)
lookup = context
name.split(".").each { |value| lookup = lookup[value] }
lookup
end
def render(context)
path = "#{lookup(context, 'site.baseurl')}/files/"
path += "#{lookup(context, 'page.path')}".split('/').last.chomp('.md')
"#{path}/#{@text}"
end
end
end
Liquid::Template.register_tag('post_file_link', Jekyll::RenderPostFileLinkTag)
# Alternate tag name.
Liquid::Template.register_tag('post_file_url', Jekyll::RenderPostFileLinkTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment