Skip to content

Instantly share code, notes, and snippets.

@CoffeeAndCode
Created January 5, 2013 03:24
Show Gist options
  • Save CoffeeAndCode/4459531 to your computer and use it in GitHub Desktop.
Save CoffeeAndCode/4459531 to your computer and use it in GitHub Desktop.
Jekyll plugin to return <img> markup by only passing in the image filename and optional alt text. Image path is "/images/:year/:slug/:filename".
module Jekyll
class ImagePathTag < Liquid::Tag
@alt = nil
@url = nil
IMAGE_URL = /((https?:\/\/|\/)?(\S+))/i
IMAGE_URL_WITH_ALT = /((https?:\/\/|\/)?(\S+))(\s+)"(.*?)"/i
def initialize(tag_name, markup, tokens)
super
if markup =~ IMAGE_URL_WITH_ALT
@alt = $5
@url = $1
elsif markup =~ IMAGE_URL
@url = $1
end
end
def render(context)
current_page = context.environments.first['page']
post = context.registers[:site].posts.find { |x| x.url == current_page['url'] }
"<img alt=\"#{@alt}\" src=\"/images/#{current_page['date'].year}/#{post.slug}/#{@url}\" />"
end
end
end
Liquid::Template.register_tag('image_path', Jekyll::ImagePathTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment