Last active
June 23, 2018 23:06
-
-
Save DakuTree/af46d0629a1a3d9181514c3f2398666d to your computer and use it in GitHub Desktop.
Jekyll - Custom post_file_link tag plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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