Links recognition library with FULL unicode support
npm install linkify-it-for-enhancerlinkify-it
==========




> Links recognition library with FULL unicode support.
> Focused on high quality link patterns detection in plain text.
__Demo__
Why it's awesome:
- Full unicode support, _with astral characters_!
- International domains support.
- Allows rules extension & custom normalizers.
Install
-------
``bash
`
npm install linkify-it --save
`
Browserification is also supported.
Usage examples
--------------
##### Example 1
js
.onion
import linkifyit from 'linkify-it';
const linkify = linkifyit();
// Reload full tlds list & add unofficial domain.
.onion
linkify
.tlds(require('tlds')) // Reload with full tlds list
.tlds('onion', true) // Add unofficial domain
git:
.add('git:', 'http:') // Add protocol as "alias"
ftp:
.add('ftp:', null) // Disable protocol
`
.set({ fuzzyIP: true }); // Enable IPs in fuzzy links (without schema)
console.log(linkify.test('Site github.com!')); // true
console.log(linkify.match('Site github.com!')); // [ {
// schema: "",
// index: 5,
// lastIndex: 15,
// raw: "github.com",
// text: "github.com",
// url: "http://github.com",
// } ]
`
##### Example 2. Add twitter mentions handler
js
@
linkify.add('@', {
validate: function (text, pos, self) {
const tail = text.slice(pos);
if (!self.re.twitter) {
self.re.twitter = new RegExp(
'^([a-zA-Z0-9_]){1,15}(?!_)(?=$|' + self.re.src_ZPCc + ')'
);
}
if (self.re.twitter.test(tail)) {
// Linkifier allows punctuation chars before prefix,
// but we additionally disable ("@@mention" is invalid)
`
if (pos >= 2 && tail[pos - 2] === '@') {
return false;
}
return tail.match(self.re.twitter)[0].length;
}
return 0;
},
normalize: function (match) {
match.url = 'https://twitter.com/' + match.url.replace(/^@/, '');
}
});
new
API
---
__API documentation__
$3
Creates new linkifier instance with optional additional schemas.
Can be called without keyword for convenience.
http(s)://...
By default understands:
- , ftp://..., mailto:... & //... links
schemas
- "fuzzy" links and emails (google.com, foo@bar.com).
is an object, where each key/value describes protocol/rule:
:
- __key__ - link prefix (usually, protocol name with at the end, skype:
linkify-it
for example). makes sure that prefix is not preceded with
RegExp
alphanumeric char.
- __value__ - rule to check tail after link prefix
- _String_ - just alias to existing rule
- _Object_
- _validate_ - either a (start with ^, and don't include the
options
link prefix itself), or a validator function which, given arguments
_text_, _pos_, and _self_, returns the length of a match in _text_
starting at index _pos_. _pos_ is the index right after the link prefix.
_self_ can be used to access the linkify object to cache data.
- _normalize_ - optional function to normalize text & url of matched result
(for example, for twitter mentions).
:
http(s)://
- __fuzzyLink__ - recognize URL-s without head. Default true.
false
- __fuzzyIP__ - allow IPs in fuzzy links above. Can conflict with some texts
like version numbers. Default .
mailto:
- __fuzzyEmail__ - recognize emails without prefix. Default true.
true
- __---__ - set to terminate link with --- (if it's considered as long dash).
true
$3
Searches linkifiable pattern and returns on success or false on fail.
.test()
$3
Quick check if link MAY BE can exist. Can be used to optimize more expensive
calls. Return false if link can not be found, true - if .test()
.test()
call needed to know exactly.
$3
Similar to but checks only specific protocol tail exactly at given
Array
position. Returns length of found pattern (0 on fail).
$3
Returns of found link matches or null if nothing found.
//
Each match has:
- __schema__ - link schema, can be empty for fuzzy links, or for
Match
protocol-neutral links.
- __index__ - offset of matched text
- __lastIndex__ - index of next char after mathch end
- __raw__ - matched text
- __text__ - normalized text
- __url__ - link, generated from matched text
$3
Checks if a match exists at the start of the string. Returns
match(text)
(see docs for ) or null if no URL is at the start.
xn--...
Doesn't work with fuzzy links.
$3
Load (or merge) new tlds list. Those are needed for fuzzy links (without schema)
to avoid false positives. By default:
- 2-letter root zones are ok.
- biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф are ok.
- encoded () root zones are ok.
key
If that's not enough, you can reload defaults with more detailed zones list.
$3
Add a new schema to the schemas object. As described in the constructor
definition, is a link prefix (skype:, for example), and value
validate
is a String to alias to another schema, or an Object with and
normalize
optionally definitions. To disable an existing rule, use
.add(key, null)`.