Gravatar filter for Jekyll

My blog is powered by Jekyll and I wanted to display gravatars on comments and for post author. The simplest way was to write a liquid filter:

require 'digest/md5'

module Jekyll
  module GravatarFilter
    def to_gravatar(input)
      email_address = input.downcase.strip
      hash = Digest::MD5.hexdigest(email_address)
      "http://www.gravatar.com/avatar/#{hash}"
    end
  end
end

Liquid::Template.register_filter(Jekyll::GravatarFilter)

The only thing it does is generate the basic gravatar URL:

  • with this input: {{ 'user@example.com' | to_gravatar }}
  • the filter will output: http://www.gravatar.com/avatar/8ab058066d75ea5b7e613094a7676d82

The gravatar URL accept extra arguments like setting the image size and default image:

  • <img src="{{ comment.email | to_gravatar }}?s=40&d=mm" />

See the gravatar filter on github.

Comments Add one by sending me an email.