Created
April 10, 2016 21:19
-
-
Save cermakcz/9063b4272478614337feb0330e840b5a to your computer and use it in GitHub Desktop.
Simple Jekyll plugin for creating a CDN url Liquid tag
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
#cdn | |
cdnurl: 'https://s3-eu-west-1.amazonaws.com/outoftime/' |
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
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