Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/sindresorhus/linkify-urls

Linkify URLs in a string
https://github.com/sindresorhus/linkify-urls

Last synced: 3 months ago
JSON representation

Linkify URLs in a string

Lists

README

        

# linkify-urls

> Linkify URLs in a string

## Install

```sh
npm install linkify-urls
```

## Usage

```js
import linkifyUrls from 'linkify-urls';

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

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

## API

### linkifyUrls(string, options?)

#### string

Type: `string`

A string 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'`

The format of the generated content.

`'string'` will return it as a flat string like `'Visit https://example.com'`.

`'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 | 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:

```js
linkifyUrls('See https://sindresorhus.com/foo', {
value: url => new URL(url).pathname
});
//=> 'See /foo'
```

## Browser compatibility

Version 3 of this package uses [negative lookbehind regex syntax](https://kangax.github.io/compat-table/es2016plus/#test-RegExp_Lookbehind_Assertions). Stay on version 2 if you need to support browsers that doesn't support this feature.

## Related

- [linkify-issues](https://github.com/sindresorhus/linkify-issues) - Linkify GitHub issue references
- [get-urls](https://github.com/sindresorhus/get-urls) - Get all URLs in a string