Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
#What is link.js?
# Link.js v2

It takes links link http://cosmicsearch.org and turns it to a link.
Turns text links into clickable HTML links. It even ignores normal text and works for most link formats.

#New Site
For example:

http://getlink.js.org/
`console.log(Link("Test https://google.com").out());`

<a href="https://js.org" target="_blank" title="JS.ORG | JavaScript Community">
<img src="https://logo.js.org/dark_horz.png" width="102" alt="JS.ORG Logo"/></a>
<!-- alternatives [bright|dark]_[horz|vert|tiny].png (width[horz:102,vert:50,tiny:77]) -->
# Usage

#Docs
`Link("Input Text")`

```
linkjs.do(string)
linkjs.Version
linkjs.Author
```
`.out()`

# Demos
`.setTo("Element")`

##Simple
# Usage Examples

```
var linkjsdemo = linkjs.do("this is some text a link is here http://cosmicsearch.org I want to email so email@cosmicsearch.org lets trick it up and add a period http://cosmicsearch.org.");
```
### Turning a text link into a link tag.

##The DOM
`Link("https://google.com").out();`

```
document.write(linkjs.do("this is some text a link is here http://cosmicsearch.org I want to email so email@cosmicsearch.org lets trick it up and add a period http://cosmicsearch.org."));
```
Output: `<a href="https://google.com" target="_blank">https://google.com</a>`

##Alert
```
var linkjsdemo = linkjs.do("this is some text a link is here http://cosmicsearch.org I want to email so email@cosmicsearch.org lets trick it up and add a period http://cosmicsearch.org.");
alert(linkjsdemo)
```
### Turning text links from the body tag into link tags then replacing them.

`Link(document.getElementsByTagName("body")[0].innerHTML).setTo("body");`
45 changes: 32 additions & 13 deletions link.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
var linkjs = {
Version: 1.0,
Author: "Cosmic Web Services",
do : function (inputText) {
var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

return replacedText
var link = function() {
this.in = "";

this.setInput = function(input) {
this.in = input;
return this;
}

this.setTo = function(selector) {
var sel = document.querySelectorAll(selector);

for (var i = sel.length - 1; i >= 0; i--) {
sel[i].innerHTML = this.out();
}
};

return this;
}

this.out = function() {
var input = this.in;
var output;

output = input.replace(/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim, '<a href="$1" target="_blank">$1</a>');
output = output.replace(/(^|[^\/])(www\.[\S]+(\b|$))/gim, '$1<a href="http://$2" target="_blank">$2</a>');
output = output.replace(/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim, '<a href="mailto:$1">$1</a>');

return output;
}
}

function Link(input) {
return new link().setInput(input);
}
34 changes: 33 additions & 1 deletion link.min.js
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
var linkjs={Version:1,Author:"Cosmic Web Services","do":function(a){var e=/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,r=a.replace(e,'<a href="$1" target="_blank">$1</a>'),t=/(^|[^\/])(www\.[\S]+(\b|$))/gim,r=r.replace(t,'$1<a href="http://$2" target="_blank">$2</a>'),i=/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim,r=r.replace(i,'<a href="mailto:$1">$1</a>');return r}};
var link = function() {
this.in = "";

this.setInput = function(input) {
this.in = input;
return this;
}

this.setTo = function(selector) {
var sel = document.querySelectorAll(selector);

for (var i = sel.length - 1; i >= 0; i--) {
sel[i].innerHTML = this.out();
}

return this;
}

this.out = function() {
var input = this.in;
var output;

output = input.replace(/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim, '<a href="$1" target="_blank">$1</a>');
output = output.replace(/(^|[^\/])(www\.[\S]+(\b|$))/gim, '$1<a href="http://$2" target="_blank">$2</a>');
output = output.replace(/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim, '<a href="mailto:$1">$1</a>');

return output;
}
}

function Link(input) {
return new link().setInput(input);
}