Crates.io | linkify |
lib.rs | linkify |
version | 0.10.0 |
source | src |
created_at | 2017-05-13 06:41:06.643425 |
updated_at | 2023-06-24 06:50:57.495596 |
description | Finds URLs and email addresses in plain text. Takes care to get the boundaries right with surrounding punctuation like parentheses. |
homepage | https://github.com/robinst/linkify |
repository | https://github.com/robinst/linkify |
max_upload_size | |
id | 14370 |
size | 90,084 |
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.
Your reaction might be: "Do I need a library for this? Why not a regex?". Let's look at a few cases:
http://example.com/.
the link should not include the trailing dothttp://example.com/,
should not include the trailing comma(http://example.com/)
should not include the parensSeems simple enough. But then we also have these cases:
https://en.wikipedia.org/wiki/Link_(The_Legend_of_Zelda)
should include the trailing parenhttp://üñîçøðé.com/ä
should also work for Unicode (including Emoji and Punycode)<http://example.com/>
should not include angle bracketsThis 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.
Try it out online on the demo playground (Rust compiled to WebAssembly): https://robinst.github.io/linkify/
If you want to use it on the command line, try lychee. It uses linkify to extract all links and checks if they're valid, but it can also just print them like this:
$ echo 'Test https://example.org (and https://example.com)' | lychee --dump -
https://example.org/
https://example.com/
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());
Option to allow URLs without schemes:
use linkify::LinkFinder;
let input = "Look, no scheme: example.org/foo";
let mut finder = LinkFinder::new();
// true by default
finder.url_must_have_scheme(false);
let links: Vec<_> = finder.links(input).collect();
assert_eq!(links[0].as_str(), "example.org/foo");
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.
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.
Pull requests, issues and comments welcome! Make sure to add tests for new features and bug fixes.
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.