Giter Club home page Giter Club logo

html-linkify's Introduction

html-linkify

Replace any links and emails found in your text with HTML anchor elements and return properly escaped and safe HTML.

Example

var linkify = require("html-linkify");
var text = linkify(
	"Visit http://foo.com!\n" +
	"Email [email protected]\n" +
	"<script>alert('xss!')</script>");
console.log(text);
// -> Visit <a href="http://foo.com">http://foo.com</a>!
//    Email <a href="mailto:[email protected]">[email protected]</a>
//    &lt;script&gt;alert(&#39;xss!&#39;)&lt;/script&gt;

API

linkify(text, [options])

Replace any links found in the input text with anchor elements.

Options:

  • options.attributes: A mapping of attributes to be added to the generated anchor elements (in addition to href).
  • options.escape: Whether surrounding HTML tags should be escaped or not (default: true).

Todo

  • Allow arbitrary user-definable replacing

Installation

npm install html-linkify

html-linkify's People

Contributors

parshap avatar pstadler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

html-linkify's Issues

Infinite loop bug

The regex gets stuck in an infinite loop (JavaScript) when you have many trailing dots:

var text = 'http://www.google.com............................................';
linkify(text); // stuck in infinite loop

It is stuck here:

var rLink = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;

...

while (match = rLink.exec(text)) { // stuck here

Consider links that don't begin with "www" or "http://"

Currently the link regex only matches links that begin with "www" or "http://" ("or https://"). It may be warranted to consider other links. Possible approaches would be to look for other subdomains, look for other parts of a URI (e.g., a port), or whitelisting gTLDs. It would be useful to see what expectations are set by Twitter's, Facebook's and others' behaviors.

HTML input does not work

When I run this over a text like <iframe src="https://player.vimeo.com/video/123123" ></iframe> it'll kill the iframe.

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.