Giter VIP home page Giter VIP logo

linkify-urls's Introduction

linkify-urls

Linkify URLs in a string

Install

npm install linkify-urls

Usage

import linkifyUrls from '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(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 <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 | 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:

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

Browser compatibility

Version 3 of this package uses negative lookbehind regex syntax. Stay on version 2 if you need to support browsers that doesn't support this feature.

Related

linkify-urls's People

Contributors

bendingbender avatar bfred-it avatar dijs avatar fregante avatar katsute avatar pmpinto avatar richienb avatar rocktimsaikia avatar sindresorhus avatar superhawk610 avatar tikotzky avatar yeskunall avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

linkify-urls's Issues

Skip prefixed URLs

We should not linkify URLs with a + prefix, like git+https://github.com/sindreorhus/ava.

Not works in safari

Error in console: SyntaxError: Invalid regular expression: invalid group specifier name

Parentheses in URLs break linkifying

@fregante edit: Issue transferred from refined-github. This issue still happens in v3.1.0

Screenshot 2019-09-07 at 14 57 46


See this line:

The URL contains parentheses which make the anchor cut off the URL in both the href and the visual link producing a wrong URL.

Original valid URL:

https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md

Allow custom HTML value

This would help solve refined-github/refined-github#703

const linkify = (href, options) => createHtmlElement({
	name: 'a',
	attributes: Object.assign({href: ''}, options.attributes, {href}),
	value: options.value || href
}); 

If the user can pass the value as part of the options, the library can parse the href text and add an anchor element that has whatever the user wants as its text (HTML in the case of refined-github/refined-github#695 to preserve the span tags)

Support localhost URLs

Currently, localhost URLs (e.g. - http://localhost/foo) are not supported since they do not have a top level domain.

This could be rather trivially added by making the TLD capture group optional, but rejecting localhost URLs may be by design, in which case please feel free to close this issue.

Output React component

import {LinkifyUrls} from "linkify-urls/react"

<LinkifyUrls
	string='a string with https://example.com'
	target="_blank"
/>

Does it make sense as part of this module? The current solution:

<div
  dangerouslySetInnerHTML={{
    __html: linkifyUrls('a string with https://example.com', {
      attributes: {
        target: "_blank",
      },
    }),
  }}
></div>

React does not accept DocumentFragment and other raw elements.

URLs should be escaped

URLs may contain HTML-looking parts, such as &amp, which browsers friendly turn to & in code.

Live example: https://runkit.com/embed/jxchw34aqffr (RunKit nicely shows the rendered HTML as well)

runkit screenshot

The value should always be escaped; the href attribute doesn't seem to be as smart with entities (&amp; is converted to & but &amp isn't) so probably it doesn't need to be escaped.

URL Links with Period at End Fail in Markdown

refined-github/refined-github#381 introduced adding links to URLs in code, which is a really cool feature!

However, it doesn't follow the same rules as Markdown in Markdown code, such as the case where a link has a period at the end (the link on https://github.com/palmerhq/tsdx/pull/138/files goes to https://github.com/alexreardon/tiny-invariant., with the period):

Screen Shot 2019-08-15 at 17 12 18

I can think of two ways to deal with this off the top of my head (maybe there's a better / easier alternative):

  • use a different regular expression in Markdown files that more closely matches Markdown semantics
  • don't allow periods at the last character of a URL? I haven't seen many (any?) URLs like this, and I think the number of URLs to be fixed by this is probably a lot higher...

URLs that include @ are broken

For example:
http://address.io/@something
returns
<a href="http://address.io/" target="_blank">http://address.io/</a>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.