Last active
January 2, 2016 11:19
-
-
Save kwent/8295854 to your computer and use it in GitHub Desktop.
Twitter mention to url convertor plugin for Octopress
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
# Title: Twitter mention to url convertor plugin for Octopress | |
# Author: Quentin Rousseau http://quentinrousseau.com | |
# Description: Convert all twitter mentions with an url. | |
# | |
# Syntax {% twitter_mention_convertor "You string with @mention1 @mention2" %} | |
# | |
# Example: | |
# {% twitter_mention_convertor "Hello world @quentinrousseau" %} | |
# | |
# Output: | |
# <a href="https://twitter.com/quentinrousseau" alt="@quentinrousseau">@quentinrousseau</a> | |
# | |
module Jekyll | |
class TwitterMentionConvertor < Liquid::Tag | |
@twitter_base_uri = nil | |
def initialize(tag_name, markup, tokens) | |
super | |
@twitter_base_uri = "https://twitter.com/" | |
end | |
def render(context) | |
"#{context[@markup.strip]}".gsub(/@([a-z0-9_]+)/i) do |mention| | |
"<a href=\"#{@twitter_base_uri}#{mention[1..-1]}\" alt=\"#{mention}\">#{mention}</a>" | |
end | |
end | |
end | |
end | |
Liquid::Template.register_tag('twitter_mention_convertor', Jekyll::TwitterMentionConvertor) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment