Skip to content

Instantly share code, notes, and snippets.

@jalcine
Last active December 11, 2015 05:48
Show Gist options
  • Save jalcine/4554192 to your computer and use it in GitHub Desktop.
Save jalcine/4554192 to your computer and use it in GitHub Desktop.
An Octopress / Jekyll plugin to provide a tag that automatically generates a bit.ly short-link for the current page in reference.
##
## @author Jacky Alcine <[email protected]>
## @see [TODO: Add URL to post about bit.ly URLs.]
##
## Add in Bit.ly URL support to pages.
require "bitly"
# Ensure use of new API.
Bitly.use_api_version_3
module Jekyll
module Bitly
module Methods
private
def _config (option)
Jekyll.configuration({})["bitly_#{option}"]
end
public
def api_obj
login = _config("login")
api_key = _config("api_key")
::Bitly.new login, api_key
end
end
class ShortenTag < Liquid::Tag
include Jekyll::Bitly::Methods
def initialize (tag_name, text, tokens)
@bitly = api_obj
@link = text
super
end
def render (context)
@bitly.shorten(@link).short_url
end
end
end
end
Liquid::Template.register_tag('bitly_shorten', Jekyll::Bitly::ShortenTag)
bitly_api_key: FOOO
bitly_login: BARRR
<a href="{% bitly_shorten page.url %}">short link</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment