npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-linkifier

v4.0.0

Published

Finds links in text and converts them to <a> elements

Downloads

3,488

Readme

Features

  • Very small (~2KB minified and gzipped)
  • Zero external dependencies
  • Use it as function or component
  • Works great with complex URLs and handles many corner cases
  • Allows custom props to be applied to <a> elements
  • Automatically prepends http:// to the href or mailto: for emails

Live demo

Demo

Install

npm install --save react-linkifier

Basic usage

As component

import Linkifier from 'react-linkifier';

const MyComponent = () => (
    <Linkifier>
        <div>check this: www.example.org</div>
    </Linkifier>
);

// Render result:
// <div>
//     <span>check this: </span><a href="http://www.example.org">www.example.org</a>
// </div>

As function

import {linkifier} from 'react-linkifier';

const MyComponent = () => (
    <div>
        {linkifier('www.example.org')}
    </div>
);

// Render result:
// <div>
//     <a href=\"http://www.example.org\">www.example.org</a>
// </div>

Advanced usage

As component

className and other props are assigned to the link elements.

import Linkifier from 'react-linkifier';

const MyComponent = () => (
    <Linkifier target="_blank" className="my-class">
        <div>www.example.org</div>
    </Linkifier>
);

// Render result:
// <div>
//     <a target=\"_blank\" class=\"my-class\" href=\"http://www.example.org\">www.example.org</a>
// </div>

With custom renderer

If you want to change the way <Linkifier /> renders links, for example when you want to use a custom component instead of <a>, you can use the renderer prop:

import Linkifier from 'react-linkifier';

const RedLink = ({href, children}) => (
    <a href={href} style={{color: 'red'}}>
        {children}
    </a>
);

const MyComponent = () => (
    <Linkifier renderer={RedLink}>
        <div>www.example.org</div>
    </Linkifier>
);

// Render result:
// <div>
//     <a href=\"http://www.example.org\" style=\"color:red;\">www.example.org</a>
// </div>

Ignore elements

Use the ignore prop to skip some children. By default ignores a and button

const ignore = [...Linkifier.DEFAULT_IGNORED, 'pre'];

const MyComponent = () => (
    <Linkifier ignore={ignore}>
        <pre>
            http://example.org
        </pre>
        <a href="http://example.org">example</a>
        <button>http://example.org</button>
    </Linkifier>
);

// None of these urls will be linkified

As function

import {linkifier} from 'react-linkifier';

const text = 'check this: www.example.org';

const MyComponent = () => (
    <div>
        {linkifier(text, {target: '_blank', className: 'link'})}
    </div>
);

// Render result:
// <div>
//     <span>check this: </span>
//     <a target="_blank" class="link" href="http://www.example.org">www.example.org</a>
// </div>

With custom renderer

When using linkifier as a function you can also pass a custom renderer:

import {linkifier} from 'react-linkifier';

const RedLink = ({href, children}) => (
    <a href={href} style={{color: 'red'}}>
        {children}
    </a>
);

const text = 'check this: www.example.org';

const MyComponent = () => (
    <div>
        {linkifier(text, {renderer: RedLink})}
    </div>
);

// Render result:
// <div>
//     <span>check this: </span>
//     <a href="http://www.example.org" style="color:red;">www.example.org</a>
// </div>

Options

  • protocol: this protocol will be used if the protocol part is missing
  • renderer: pass a component to use a custom link renderer, defaults to a.
  • ignore: list of elements to ignore (defaults to ['a', 'button'])
  • Rest of properties of the options object (eg: style, className) or props of the Linkifier component are passed as props to the link element

License

MIT

Credits

Artwork by emojione.com