Crate linkcheck[][src]

Expand description

A library for extracting and validating links.

The majority of this code has been extracted from the mdbook-linkcheck plugin, so it may have some bias towards the way mdbook works.


If you were validating links in batches, this is one way to go about it:

use linkcheck::{Link, BasicContext};
use std::path::Path;
use codespan::Files;

// first we need somewhere to put the source documents we'll be checking
let mut files = Files::new();

// then we add some items
let src = r#"
This is some markdown linking to [a website]( and
[a file](./
let file_id = files.add("", src);

// we then need to extract all the links and their location in the document
let links = linkcheck::scanners::markdown(src);

// at the moment we just have a stream of (&str, Span)... To give nice
// diagnostics we need to turn this into a stream of Links that know which
// document they came from.
let links =|(url, span)| Link::new(url, span, file_id));

// we've collected all our links, now it's time for validation!

// when validating file links we need to know what the current directory is
let current_dir = Path::new(env!("CARGO_MANIFEST_DIR"));

// the validation process also need some contextual information (e.g. HTTP
// client, file system validation options, and a cache for expensive web
// requests).
// Basic users won't need to tweak this in any way, so a default context
// type has been provided for you.
let ctx = BasicContext::default();

// and now we can run the validation step!
let result = linkcheck::validate(current_dir, links, &ctx).await;

assert_eq!(result.valid.len(), 2);

Cargo Features

Extra functionality is accessible by enabling feature flags. The features currently available are:

  • serde-1 - Adds Serialize and Deserialize implementations for use with serde


pub use validation::validate;


A scanner is just a function that which can extract links from a body of text.

Code for validating the various types of Link.


A basic Context implementation which uses all the defaults.

A link to some other resource.