Anchorme.js
A library to convert URLs to a click-able HTML anchor elements
Features
- Highly sensitive.
- produces the least possible false positives if any.
- Skips HTML, so it doesn't break your HTML if it had a URL as an attribute for an element.
- Links with or without protocols.
- Preserve upper and lower case, although when detecting, it's basically case insensitive.
- Checks against full IANA list of TLDs.
- Works with IPs, FTPs, Emails and files.
- Also works when ports are defined.
- Small in size.
- No RegExp involved, very readable and maintainable.
- Supports setting custom attributes with any values.
- Supports checking IPs only, Emails only, or URLs only.
- Helper methods can be used for other purposes.
Getting Started
Download
File
Download the library from dist
folder (either anchorme.js
or anchorme.min.js
).
NPM
Install via NPM: npm install anchorme
Usage
var anchorme = require("anchorme");
var someText = "this is a text with a link www.github.com ..";
var result = anchorme(someText);
anchorme(someText,{
attributes:[
{
name:"target",
value:"_blank",
},
function(obj){
if(obj.reason === "email") return {name:"class",value:"email"};
else return {name:"class",value:"regular-link"}
}
],
})
Demo
To test how this library would work for you, head over to here to test it.
Available options
Truncation
This will convert a long like like this:
https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/src/tests/hasprotocol.js
to this:
https://raw.githubusercontent.com/alexcorv...
Default Value: 0
(Won't truncate)
Example
anchorme(string,{
truncate:40
})
Excluding
You can exclude IPs/Emails/URLs/Files like this:
anchorme(string,{
emails:false,
urls:false,
ips:false,
files:false
})
Default Value: all are true
Adding attributes
You can add attributes to the links produced by anchorme. using the attributes
prop in the options. this options should be an array of the attributes you'd like to pass.
Values of this array can be:
anchorme(string,{
attributes:[
{
name:"class",
value:"something"
},
{
name:"target",
value:"blank"
}
]
});
- Functions that return an object
anchorme(string,{
attributes:[
{
name:"class",
value:"link"
},
function(data){
if(data.reason === "ip") return {name:"class",value:"ip-link"};
},
function(data){
if(data.protocol !== "mailto:") return {name:"target",value:"blank"};
}
]
});
Where data
is an object containing detailed info about the link in question. The example above will add ip-link
class to all the links that are IPs, and add target='_blank'
to all the links that are not emails.
If you log the data object you'll get something similar to this:
{
"reason": "email",
"protocol": "mailto:",
"raw": "a@b.co",
"encoded": "a@b.co",
}
Setting default protocol
If the link came without protocol, like www.google.com
then anchorme will add the http://
by default. However you can set your own default protocol.
anchorme(string,{
defaultProtocol:"ftp://",
})
In some cases, you might want the protocol to be set conditionally. Anchorme allows you to pass a function as the defaultProtocol
and uses whatever this function returns.
anchorme(string,{
defaultProtocol:function(url){
if(url.indexOf("secure") > 0) return "https://";
else return "http://";
},
})
Additional functionalities
Listing all valid URLs
Although anchorme was authored to transform URLs in text strings to a click-able HTML anchor tags, passing true
to list
property in options will change the library's behavior and instead of returning a text with an HTML tags it will only return an array of valid URLs.
anchorme(myText,{
list:true
})
Validating
it can also be used for validation:
anchorme.validate.ip("1.1.1.1:3000/something");
anchorme.validate.email("alex@array.com");
anchorme.validate.url("google.co.uk");
Contributing
- Clone this repository
cd anchorme.js && npm install
- install mocha globally (for running the tests):
mocha test/run
- ..
- Build
node build/build
- Test
node test/run
License: The MIT License (MIT) - Copyright (c) 2017 Alex Corvi