1.0.0 • Published 6 years ago

links-finder v1.0.0

Weekly downloads
22
License
MIT
Repository
github
Last release
6 years ago

links-finder

Small lib to find all links in a string and get their positions or wrap them with something(like html <a> tag)

Install library

  npm install --save links-finder

or

  yarn add links-finder

or

Download our minified version

or

Download our gziped version


Live demo

Take a look here


Examples of finding links

Default usage
  import linksFinder from 'links-finder';
  console.log(linksFinder.findLinks('magic http://blah.com another')); // will log [{start: 6, end: 20}]
Several links
  import linksFinder from 'links-finder';
  console.log(linksFinder.findLinks('magic http://blah.com and https://x.com')); // will log [{start: 6, end: 20}, {start: 26, end: 38}]
No links
  import linksFinder from 'links-finder';
  console.log(linksFinder.findLinks('magic')); // will log []
Links without http
  import linksFinder from 'links-finder';
  console.log(linksFinder.findLinks('magic www.blah.com and some')); // will log [{start: 6, end: 17}]
  console.log(linksFinder.findLinks('magic blah.com')); // will log [{start: 6, end: 13}]

Examples of wrapping links

Example of wrapping link with in React way
  import linksFinder from 'links-finder';
  console.log(linksFinder.wrapLinks('magic http://blah.com', {
    onMatch: (link) => <a href={link}>{link}</a>
  })); // will log 'magic <a href="http://blah.com"/>http://blah.com</a>'
Example of removing all links
  import linksFinder from 'links-finder';
  console.log(linksFinder.wrapLinks('magic http://blah.com', {
    onMatch: (link) => ''
  })); // will log 'magic '

Examples with tree shaking

  import { findLinks } from 'links-finder';
  console.log(findLinks('magic http://blah.com')); // will log [{start: 6, end: 20}]
  import { wrapLinks } from 'links-finder';
  console.log(wrapLinks('magic http://blah.com', {
    onMatch: (link) => <a href={link}>{link}</a>
  })); // will log 'magic <a href="http://blah.com"/>http://blah.com</a>'
For more example, you can take a looks to our test spec file

This lib is super fast and super tiny(< 6kb)
We support all modern browsers starting from IE9

Feel free to open ISSUES and create pull requests