#web #text #url #links

linkify

Finds URLs and email addresses in plain text. Takes care to get the boundaries right with surrounding punctuation like parentheses.

10 releases (6 breaking)

0.7.0 May 18, 2021
0.5.0 Feb 13, 2021
0.4.0 Aug 5, 2019
0.3.1 Feb 4, 2018
0.1.2 Jun 9, 2017

#33 in Text processing

Download history 1443/week @ 2021-05-30 1350/week @ 2021-06-06 1460/week @ 2021-06-13 1525/week @ 2021-06-20 1400/week @ 2021-06-27 1523/week @ 2021-07-04 1504/week @ 2021-07-11 2012/week @ 2021-07-18 1805/week @ 2021-07-25 1556/week @ 2021-08-01 1747/week @ 2021-08-08 1402/week @ 2021-08-15 1936/week @ 2021-08-22 2283/week @ 2021-08-29 3021/week @ 2021-09-05 3772/week @ 2021-09-12

5,918 downloads per month
Used in 11 crates (9 directly)

MIT/Apache

27KB
471 lines

Linkify

Linkify is a Rust library to find links such as URLs and email addresses in plain text. It's smart about where a link ends, such as with trailing punctuation.

Documentation Crate ci codecov

Introduction

Your reaction might be: "Do I need a library for this? Why not a regex?". Let's look at a few cases:

  • In http://example.com/. the link should not include the trailing dot
  • http://example.com/, should not include the trailing comma
  • (http://example.com/) should not include the parens

Seems simple enough. But then we also have these cases:

  • https://en.wikipedia.org/wiki/Link_(The_Legend_of_Zelda) should include the trailing paren
  • http://üñîçøðé.com/ä should also work for Unicode (including Emoji and Punycode)
  • <http://example.com/> should not include angle brackets

This library behaves as you'd expect in the above cases and many more. It uses a simple scan with linear runtime.

In addition to URLs, it can also find email addresses.

Demo 🧑‍🔬

Try it out on the demo playground (Rust compiled to WebAssembly): https://robinst.github.io/linkify/

Usage

Basic usage:

extern crate linkify;

use linkify::{LinkFinder, LinkKind};

let input = "Have you seen http://example.com?";
let finder = LinkFinder::new();
let links: Vec<_> = finder.links(input).collect();

assert_eq!(1, links.len());
let link = &links[0];

assert_eq!("http://example.com", link.as_str());
assert_eq!(14, link.start());
assert_eq!(32, link.end());
assert_eq!(&LinkKind::Url, link.kind());

Restrict the kinds of links:

use linkify::{LinkFinder, LinkKind};

let input = "http://example.com and foo@example.com";
let mut finder = LinkFinder::new();
finder.kinds(&[LinkKind::Email]);
let links: Vec<_> = finder.links(input).collect();

assert_eq!(1, links.len());
let link = &links[0];
assert_eq!("foo@example.com", link.as_str());
assert_eq!(&LinkKind::Email, link.kind());

See full documentation on docs.rs.

Conformance

This crates makes an effort to respect the various standards, namely:

At the same time, it does not guarantee that the returned links are valid. If in doubt, it rather returns a link than skipping it.

If you need to validate URLs, e.g. for checking TLDs, use another library on the returned links.

Contributing

Pull requests, issues and comments welcome! Make sure to add tests for new features and bug fixes.

License

Linkify is distributed under the terms of both the MIT license and the Apache License (Version 2.0). See LICENSE-APACHE and LICENSE-MIT for details. Opening a pull requests is assumed to signal agreement with these licensing terms.

Dependencies

~205KB