5.0.2 • Published 7 months ago

linkify-urls v5.0.2

Weekly downloads
2,351
License
MIT
Repository
github
Last release
7 months ago

linkify-urls

Linkify URLs in a string

Install

npm install linkify-urls

Usage

import {linkifyUrlsToHtml, linkifyUrlsToDom} from 'linkify-urls';

linkifyUrlsToHtml('See https://sindresorhus.com', {
	attributes: {
		class: 'unicorn',
		one: 1,
		foo: true,
		multiple: [
			'a',
			'b'
		]
	}
});
//=> 'See <a href="https://sindresorhus.com" class="unicorn" one="1" foo multiple="a b">https://sindresorhus.com</a>'


// In the browser
const fragment = linkifyUrlsToDom('See https://sindresorhus.com', {
	attributes: {
		class: 'unicorn',
	}
});
document.body.appendChild(fragment);

API

linkifyUrlsToHtml(string, options?)

Returns an HTML string like 'Visit <a href="https://example.com">https://example.com</a>'.

string

Type: string

A string with URLs to linkify.

options

Type: object

attributes

Type: object

HTML attributes to add to the link.

value

Type: string | Function\ Default: The URL

Set a custom HTML value for the link.

If it's a function, it will receive the URL as a string:

linkifyUrlsToHtml('See https://sindresorhus.com/foo', {
	value: url => new URL(url).pathname
});
//=> 'See <a href="https://sindresorhus.com/foo">/foo</a>'

linkifyUrlsToDom(string, options?)

Returns a DocumentFragment ready to be appended in a DOM safely, like DocumentFragment(TextNode('Visit '), HTMLAnchorElement('https://example.com')).

This type only works in the browser.

options

See options above.

Related

5.0.2

7 months ago

5.0.1

7 months ago

5.0.0

9 months ago

4.1.0

2 years ago

4.0.0

4 years ago

3.1.1

5 years ago

3.1.0

6 years ago

3.0.0

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.0

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago