diff --git a/README.md b/README.md index 9320ab3..2ec5495 100644 --- a/README.md +++ b/README.md @@ -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());` - -JS.ORG Logo - +# 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: `https://google.com` -##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");` diff --git a/link.js b/link.js index 31e88c2..25d7a6f 100644 --- a/link.js +++ b/link.js @@ -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, '$1'); - var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim; - var replacedText = replacedText.replace(replacePattern2, '$1$2'); - var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim; - var replacedText = replacedText.replace(replacePattern3, '$1'); - - 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, '$1'); + output = output.replace(/(^|[^\/])(www\.[\S]+(\b|$))/gim, '$1$2'); + output = output.replace(/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim, '$1'); + + return output; + } +} + +function Link(input) { + return new link().setInput(input); +} diff --git a/link.min.js b/link.min.js index 2b80b9f..25d7a6f 100644 --- a/link.min.js +++ b/link.min.js @@ -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,'$1'),t=/(^|[^\/])(www\.[\S]+(\b|$))/gim,r=r.replace(t,'$1$2'),i=/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim,r=r.replace(i,'$1');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, '$1'); + output = output.replace(/(^|[^\/])(www\.[\S]+(\b|$))/gim, '$1$2'); + output = output.replace(/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim, '$1'); + + return output; + } +} + +function Link(input) { + return new link().setInput(input); +}