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: about 1 month ago
JSON representation
Linkify URLs in a string
- Host: GitHub
- URL: https://github.com/sindresorhus/linkify-urls
- Owner: sindresorhus
- License: mit
- Created: 2017-06-02T04:08:21.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-09-09T09:04:37.000Z (about 1 year ago)
- Last Synced: 2024-04-14T09:55:30.405Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 38.1 KB
- Stars: 159
- Watchers: 7
- Forks: 22
- Open Issues: 7
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# linkify-urls
> Linkify URLs in a string
## Install
```sh
npm install linkify-urls
```## Usage
```js
import {linkifyUrlsToHtml, linkifyUrlsToDom} from 'linkify-urls';linkifyUrlsToHtml('See https://sindresorhus.com', {
attributes: {
class: 'unicorn',
one: 1,
foo: true,
multiple: [
'a',
'b'
]
}
});
//=> 'See https://sindresorhus.com'// 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 https://example.com'`.
#### 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 URLSet a custom HTML value for the link.
If it's a function, it will receive the URL as a string:
```js
linkifyUrlsToHtml('See https://sindresorhus.com/foo', {
value: url => new URL(url).pathname
});
//=> 'See /foo'
```### 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](#options) above.
## 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