A simple jQuery Plugin to automagically create anchors from recognised HTTP links and convert hash tags into links on Twitter

Linkify (and Hashify)

A simple jQuery Plugin to automagically create anchors from recognised HTTP links and convert hash tags into links to Twitter.

Get the code on github.

I created linkify after using the @anywhere Twitter library and found that it didn’t automatically link urls in tweets or hash tags.

Usage

$([selector]).linkify();  / $([selector]).hashify();

Examples

$(‘p’).linkify(); – convert all recognised urls in all matched paragraph elements to hyperlinks.

$(‘p’).hashify(); – convert all recognised hashtags in all matched paragraph elements to links to hash tags on Twitter.

$(‘.toLink’).linkify(); convert all recognised urls in all matched elements with the class .toLink to hyperlinks.

$(‘.toLink’).hashify(); convert all recognised hashtags in all matched all matched elements with the class .toLink tags to links to hash tags on Twitter.

Simple not much to it.

Advertisements

One thought on “A simple jQuery Plugin to automagically create anchors from recognised HTTP links and convert hash tags into links on Twitter

  1. Any chance you might be able to add an atify function to this plugin as well for creating links from @username links? I gave it a try and wasn’t able to get it functioning properly. I’m no javascript developer so I was just trying to fudge my way though it. This is what I came up with so far, basically modifying the structure of your other functions to suit my needs.

    $.fn.atify = function() {

    function _atify(s) {

    var re = /\B@([\w-]+)/gm;
    var match, replacements = [],i = 0;
    while (match = re.exec(s)) {
    replacements.push(‘‘ + match[0] + ‘‘);

    s = s.replace(re, ‘{‘ + i + ‘}’);
    i++;
    }

    return i > 0 ? replaceSubstitutions(s, replacements) : s;

    }

    $(this).html(_atify(this.html()));

    return this;
    };

    The issue I ran into was that when I applied it to a tweet container it would just repeat the first tweet and overwrite the other tweets with the first. The regex does seem to work though.

    Keep up the good work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s