Skip to content

Instantly share code, notes, and snippets.

@cermakcz
Created April 10, 2016 21:19
Show Gist options
  • Save cermakcz/9063b4272478614337feb0330e840b5a to your computer and use it in GitHub Desktop.
Save cermakcz/9063b4272478614337feb0330e840b5a to your computer and use it in GitHub Desktop.
Simple Jekyll plugin for creating a CDN url Liquid tag
#cdn
cdnurl: 'https://s3-eu-west-1.amazonaws.com/outoftime/'
module Jekyll
class CdnUrlTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@path = text
end
def render(context)
resolvedPath = ''
# Check if @path is actually a name of a variable or just a textual path.
if !context[@path].nil?
resolvedPath = context[@path]
else
resolvedPath = @path
end
# Strip the leading slash if there's any.
if resolvedPath.start_with?('/')
resolvedPath = resolvedPath[1..-1]
end
"#{context.registers[:site].config['cdnurl']}#{resolvedPath}"
end
end
end
Liquid::Template.register_tag('cdn_url', Jekyll::CdnUrlTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment