Skip to content

Instantly share code, notes, and snippets.

@tbjers
Created August 15, 2012 17:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbjers/3361948 to your computer and use it in GitHub Desktop.
Save tbjers/3361948 to your computer and use it in GitHub Desktop.
Liquid Filter to convert URLs to short URLs automatically
require 'bitly'
module Jekyll
class BitlyFilterCache
def initialize
@result_cache = {}
config = Jekyll.configuration({})
@username = config['bitly']['username']
@key = config['bitly']['api_key']
Bitly.use_api_version_3
end
@@instance = BitlyFilterCache.new
def self.instance
@@instance
end
def shorten(input)
input.strip!
return @result_cache[input] if @result_cache.has_key?(input)
puts "Shortening #{input}..."
bitly = Bitly.new(@username, @key)
u = bitly.shorten(input, :history => 1)
@result_cache[input] = u.short_url
puts "New url: #{u.short_url}"
return u.short_url
end
end
module Filters
def bitly(input)
BitlyFilterCache.instance.shorten(input)
end
end
end
This is a link to <a href="http://xorcode.net/PZFMOb">Xorcode</a>.
You could also use a variable, http://xorcode.net/QCpD1G.
This is a link to [Xorcode]({{ 'http://xorcode.com/' | bitly }}).
You could also use a variable, {{ site.production_url + '/' + page.url | bitly }}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment