Jump To
Core Linkify
Installation
Node.js module
Install from the command line with NPM
npm install linkifyjs
Import into your JavaScript with require
const linkify = require("linkifyjs");
or with ES modules
import * as linkify from "linkifyjs";
Browser globals
Download linkify and extract the contents into your website’s assets directory. Include the following script in your HTML:
<script src="linkify.js"></script>
Methods
linkify.find (str [, type = null] [, opts = null])
Finds all links in the given string
Params
string
str
Search stringstring
[type
] only find links of the given typeObject
[opts
] Link formatting options
Returns Array
List of links where each element is an object with the
following properties:
- type is the type of entity found. Possible values are
'url'
'email'
'hashtag'
(with Hashtag plugin)'mention'
(with Mention plugin)'ticket'
(with Ticket plugin)'ipv4'
(with IP plugin)'keyword'
(with Keyword plugin)
- value is the original entity substring.
- href is the value of this link’s
href
attribute. - start is the index of the link’s first character in the given string
- end is the index after link’s last character in the given string
linkify.find("For help with GitHub.com, please email support@github.com");
Returns the following array
[
{
type: "url",
value: "GitHub.com",
isLink: true,
href: "http://GitHub.com",
start: 14,
end: 24,
},
{
type: "email",
value: "support@github.com",
isLink: true,
href: "mailto:support@github.com",
start: 39,
end: 57,
},
];
If the second argument is an object, it will be interpreted as opts
with
options, not as type
.
linkify.init()
Avoid calling this manually. init()
runs automatically before invoking
linkify for the first time. Must be manually called after any plugins or custom
plugins get registered after the first invokation.
To avoid calling manually, register any plugins or protocols before finding links:
import * as linkify from "linkifyjs";
import linkifyStr from "linkify-string";
import "linkify-plugin-hashtag";
linkify.registerCustomProtocol("fb");
linkify.registerPlugin("my-custom-plugin", () => {});
linkifyStr("Hello World.com!"); // init() called automatically here on first invocation
// If registering new protocols or plugins *here*, call linkify.init() immediately after
linkify.registerCustomProtocol (scheme [, optionalSlashSlash = false])
Call this before invoking linkify for the first time. Linkify will consider any
string that begins with the given scheme followed by a :
as a URL link.
Params
string
scheme
The scheme. May only contain charactersa-z
and-
(hyphens)boolean
[optionalSlashSlash
] If true, allows links that begin withscheme:
, not justscheme://
linkify.registerCustomProtocol("fb"); // now recognizes links such as fb://feed
linkify.registerCustomProtocol("instagram", true); // now recognizes links such as instagram:account
linkify.registerPlugin (name, plugin)
Register a custom plugin for detecting one or more new kinds of links. Call this before invoking linkify for the first time.
Params
string
name
unique name of the plugin to register •Function
plugin
plugin implementation function
Caution: The plugin development API is in its very early stages and only supports very basic plugins. Updated features, APIs, and docs are in progress.
Check out the sample Hashtag plugin for an example. Check out the Keyword plugin for more advanced usage.
linkify.test (str [, type])
Is the given string a link? Note that linkify is not 100% spec compliant, so this function may return some false positives or false negatives. If this method does not return the expected result for a specific input, please report an issue.
Params
string
str
Test stringstring
[type
] returnstrue
only if the link is of the given type (seelinkify.find
),
Returns Boolean
linkify.test("google.com"); // true
linkify.test("google.com", "email"); // false
linkify.tokenize (str)
Internal method that parses the given string into a generic token entity array. Used by linkify’s interfaces.
Params
string
str
Returns Array