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])

Finds all links in the given string

Params

  • String str Search string
  • String [type] only find links of the given type

Returns Array List of links where each element is a hash with properties type, value, and href:

  • type is the type of entity found. Possible values are
    • 'url'
    • 'email'
    • 'hashtag' (with Hashtag plugin)
    • 'mention' (with Mention plugin)
    • 'ticket' (with Ticket plugin)
  • value is the original entity substring.
  • href should be the value of this link’s href attribute.
linkify.find('For help with GitHub.com, please email support@github.com');

Returns the array

[
  {
    type: 'url',
    value: 'GitHub.com',
    href: 'http://github.com',
  },
  {
    type: 'email',
    value: 'support@github.com',
    href: 'mailto:support@github.com'
  }
]

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 (str)

Call this before invoking linkify for the first time. Linkify will consider any string that begins with the given protocol followed by a : as a URL link.

Params

  • String str The protocol. May only contain characters a-z and - (hyphens)
linkify.registerCustomProtocol('fb'); // now recognizes links such as fb://feed
linkify.registerCustomProtocol('instagram'); // 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

See example plugin function implementations.

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 string
  • String [type] returns true only if the link is of the given type (see linkify.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