[][src]Crate linkcheck

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.

Examples

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](https://example.com) and
[a file](./README.md).
"#;
let file_id = files.add("blah.md", 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 = links.map(|(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!(result.invalid.is_empty());
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

Re-exports

pub use validation::validate;

Modules

scanners

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

validation

Code for validating the various types of Link.

Structs

BasicContext

A basic Context implementation which uses all the defaults.

Link

A link to some other resource.