• Stars
    star
    289
  • Rank 143,419 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tiny little tool to find URLs in a string of text and hyperlink them

autolink-js

autolink-js is a small (about half a kilobyte), simple, and tested JavaScript tool that takes a string of text, finds URLs within it, and hyperlinks them.

Why bother releasing such a tiny little method?

I recently needed to find and hyperlink URLs in user-submitted text and was surprised to find that doing what seemed like such a simple task wasn't already a Solved Problem. Different regex solutions led to different unwanted side effects, and other utilities were far, far more complex and feature rich than I needed.

Basic Usage

autolink-js adds an autoLink() method to JavaScript's String prototype, so you can use it on any JavaScript string. Take a look at the tests, but essentially, after including either autolink.js or autolink-min.js to your page, it works like this:

// Input
"This is a link to Google http://google.com".autoLink()

// Output
"This is a link to Google <a href='http://google.com'>http://google.com</a>"

Additional Options

You can pass any additional HTML attributes to the anchor tag with a JavaScript object, like this:

// Input
"This is a link to Google http://google.com".autoLink({ target: "_blank", rel: "nofollow", id: "1" })

// Output
"This is a link to Google <a href='http://google.com' target='_blank' rel='nofollow' id='1'>http://google.com</a>"

Callback

Callback option can be used to redefine how links will be rendered.

// Input
"This is a link to image http://example.com/logo.png".autoLink({
  callback: function(url) {
    return /\.(gif|png|jpe?g)$/i.test(url) ? '<img src="' + url + '">' : null;
  }
});

// Output
"This is a link to image <img src='http://example.com/logo.png'>"

Example

Open example/example.html in your web browser and view the source for a simple but full-featured example of using with jQuery.

Running the tests

After cloning this repository, simply open test/suite.html in your web browser. The tests will run automatically.

More Repositories

1

kitty

Having a bad day? Gem install kitty and and run `kitty` to print some kittens.
Ruby
27
star
2

railroad

RailRoad is a class diagrams generator for Ruby on Rails applications. (Forked)
Ruby
13
star
3

souleater

Rails starter template: Bort + Paperclip + Shoulda + Web App Theme
9
star
4

corral

Use Corral to hide certain features in your application.
Ruby
6
star
5

obfuscator

Ruby
6
star
6

Improvement

Ruby
4
star
7

prototypish

For rapidly building portable design and interface prototypes DRYly without relying on server technologies
JavaScript
3
star
8

including

Experiment to implement granular loading of module code in Ruby
Ruby
3
star
9

.js

dotjs files
JavaScript
2
star
10

is-he-sworn-in-yet-

Quick and Dirty Rails App to Determine if Barack Obama is the Current President
Ruby
2
star
11

muzak-cli

CoffeeScript
2
star
12

prawn-with-awesome-fonts

Custom installation of the wonderful Prawn Ruby Gem, with additional fonts added.
2
star
13

piechart

JavaScript
2
star
14

degrees

What are the degrees out today?
JavaScript
2
star
15

read_later

Read Later buttons for saving articles to Instapaper or Readability
Ruby
2
star
16

kitty-cljs

Command line kitties! This time in ClojureScript
JavaScript
2
star
17

pusherfun

Tiny little WebSockets chat on Sinatra with MongoDB for persistence
Ruby
2
star
18

dice

If you're playing a game that requires a roll of a die, you've come to the right place.
Ruby
2
star
19

paper-alchemy

Base Rails template for morphing data objects into ink on paper
Ruby
2
star
20

q

2
star
21

FancyPants

A web app theme geared towards RoR, written in Sass.
JavaScript
2
star
22

bryanwoods.github.com

Github page
1
star
23

password

password: brain-dead simple password storage by Giles Bowkett
Ruby
1
star
24

Postbim

Ruby
1
star
25

github-flavored-markdown

JavaScript
1
star
26

fancypants_generators

Ruby
1
star
27

sortablelist

Ruby
1
star
28

dreamcatcher-ruby

Ruby
1
star
29

coding_fun

Fun with code
Ruby
1
star
30

Project-Euler-Fun

Ruby
1
star
31

livestats

JavaScript
1
star
32

muzak-redirect

Ruby
1
star
33

whothefrank

WHOIS, Sinatra style.
Ruby
1
star
34

PassRod

MacRuby app for password management
Ruby
1
star
35

viewtalitarian

Ruby
1
star
36

rmb-simple-demo-ruby

ActionScript
1
star
37

messing-around-with-compojure

Messing around with Compojure
Clojure
1
star
38

pragprog-iphone-sdk-development

Objective-C
1
star
39

cKitty

C Port of the kitty rubygem for faster kittens
C
1
star
40

Learning_ObjectiveC_and_Cocoa

Exercises from Cocoa Programming For Max OS X Third Edition
Objective-C
1
star
41

dramcatcher-js

JavaScript
1
star
42

elm_rpg

Learning Elm by making an RPG
Elm
1
star
43

pollen-report

Little Sinatra app to tell me if I should fear the great outdoors today
Ruby
1
star
44

bacon-express

Come on now ride the bacon express
JavaScript
1
star
45

five-by-five

Fun with the PragProg CoffeeScript book
JavaScript
1
star
46

bryanwoods4e.com

bryanwoods4e.com
1
star
47

bacon-sinatra

Come on now ride the bacon sinatra
Ruby
1
star
48

BryanBurger

Enjoying one burger at a time, PyGame stylee.
Python
1
star
49

todogame

MMOTDL
Ruby
1
star
50

moat

Brain-dead simple password storage. Improved.
Ruby
1
star
51

dave_game

Let's see how many languages in which I can (extremely shittily) write a trivial program
1
star
52

catprint

Ruby wrapper for CatPrint's ActionPrint API
Ruby
1
star
53

pusherfun-node

Simple WebSockets chat on Node.js and Pusher
JavaScript
1
star
54

mean

Monkeypatches Ruby's Array Class to find the mean of an array of integers. As a bonus feature, Mean also finds the sum of an array of integers. Nifty.
Ruby
1
star
55

portfolio

Oh, the memories we'll have.
1
star
56

evergreen

A perennially fresh CSS framework for Ruby on Rails applications.
1
star
57

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
1
star
58

binaryme

You are a goddamned beautiful snowflake. Probably.
Ruby
1
star