Skip to content

monrus/linkify-urls

 
 

Repository files navigation

linkify-urls Build Status

Linkify URLs in text

Install

$ npm install linkify-urls

Usage

const linkifyUrls = require('linkify-urls');

linkifyUrls('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 = linkifyUrls('See https://sindresorhus.com', {
	type: 'dom',
	attributes: {
		class: 'unicorn',
	}
});
document.body.appendChild(fragment);

API

linkifyUrls(input, [options])

input

Type: string

Text with URLs to linkify.

options

Type: Object

attributes

Type: Object

HTML attributes to add to the link.

type

Type: string
Values: string dom
Default: string

Format of the generated content.

string will return it as a flat string like 'Visit <a href="https://example.com">https://example.com</a>'.

dom will return it as 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.

value

Type: string
Default: The URL

Set a custom HTML value for the link.

Related

License

MIT © Sindre Sorhus

About

Linkify URLs in text

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%