Browse Source

Skip link checking for URL with prefix in config (#846)

index-subcmd
Tjeu Kayim Vincent Prouillet 5 years ago
parent
commit
75570d041a
7 changed files with 55 additions and 11 deletions
  1. +24
    -3
      components/config/src/config.rs
  2. +1
    -0
      components/link_checker/src/lib.rs
  3. +9
    -0
      components/site/src/lib.rs
  4. +10
    -8
      components/site/tests/site.rs
  5. +5
    -0
      docs/content/documentation/getting-started/configuration.md
  6. +4
    -0
      test_site/config.toml
  7. +2
    -0
      test_site/content/posts/tutorials/programming/rust.md

+ 24
- 3
components/config/src/config.rs View File

@@ -91,13 +91,15 @@ type TranslateTerm = HashMap<String, String>;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)] #[serde(default)]
pub struct LinkChecker { pub struct LinkChecker {
/// Skip link checking for these URL prefixes
pub skip_prefixes: Vec<String>,
/// Skip anchor checking for these URL prefixes /// Skip anchor checking for these URL prefixes
pub skip_anchor_prefixes: Vec<String>, pub skip_anchor_prefixes: Vec<String>,
} }


impl Default for LinkChecker { impl Default for LinkChecker {
fn default() -> LinkChecker { fn default() -> LinkChecker {
LinkChecker { skip_anchor_prefixes: Vec::new() }
LinkChecker { skip_prefixes: Vec::new(), skip_anchor_prefixes: Vec::new() }
} }
} }


@@ -589,10 +591,29 @@ skip_anchor_prefixes = [
"#; "#;


let config = Config::parse(config_str).unwrap(); let config = Config::parse(config_str).unwrap();
let v = config.link_checker.skip_anchor_prefixes;
assert_eq!( assert_eq!(
v,
config.link_checker.skip_anchor_prefixes,
vec!["https://caniuse.com/#feat=", "https://github.com/rust-lang/rust/blob/"] vec!["https://caniuse.com/#feat=", "https://github.com/rust-lang/rust/blob/"]
); );
} }

#[test]
fn link_checker_skip_prefixes() {
let config_str = r#"
title = "My site"
base_url = "example.com"

[link_checker]
skip_prefixes = [
"http://[2001:db8::]/",
"https://www.example.com/path",
]
"#;

let config = Config::parse(config_str).unwrap();
assert_eq!(
config.link_checker.skip_prefixes,
vec!["http://[2001:db8::]/", "https://www.example.com/path",]
);
}
} }

+ 1
- 0
components/link_checker/src/lib.rs View File

@@ -198,6 +198,7 @@ mod tests {
#[test] #[test]
fn skip_anchor_prefixes() { fn skip_anchor_prefixes() {
let config = LinkChecker { let config = LinkChecker {
skip_prefixes: vec![],
skip_anchor_prefixes: vec!["https://github.com/rust-lang/rust/blob/".to_owned()], skip_anchor_prefixes: vec!["https://github.com/rust-lang/rust/blob/".to_owned()],
}; };




+ 9
- 0
components/site/src/lib.rs View File

@@ -399,6 +399,15 @@ impl Site {
all_links all_links
.par_iter() .par_iter()
.filter_map(|(page_path, link)| { .filter_map(|(page_path, link)| {
if self
.config
.link_checker
.skip_prefixes
.iter()
.any(|prefix| link.starts_with(prefix))
{
return None;
}
let res = check_url(&link, &self.config.link_checker); let res = check_url(&link, &self.config.link_checker);
if res.is_valid() { if res.is_valid() {
None None


+ 10
- 8
components/site/tests/site.rs View File

@@ -161,7 +161,10 @@ fn can_build_site_without_live_reload() {
assert!(file_exists!(public, "nested_sass/scss.css")); assert!(file_exists!(public, "nested_sass/scss.css"));


// no live reload code // no live reload code
assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&amp;mindelay=10"), false);
assert_eq!(
file_contains!(public, "index.html", "/livereload.js?port=1112&amp;mindelay=10"),
false
);


// Both pages and sections are in the sitemap // Both pages and sections are in the sitemap
assert!(file_contains!( assert!(file_contains!(
@@ -470,11 +473,7 @@ fn can_build_site_with_pagination_for_index() {
"page/1/index.html", "page/1/index.html",
"http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\"" "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
)); ));
assert!(file_contains!(
public,
"page/1/index.html",
"<title>Redirect</title>"
));
assert!(file_contains!(public, "page/1/index.html", "<title>Redirect</title>"));
assert!(file_contains!( assert!(file_contains!(
public, public,
"page/1/index.html", "page/1/index.html",
@@ -677,8 +676,11 @@ fn can_ignore_markdown_content() {
fn check_site() { fn check_site() {
let (mut site, _tmp_dir, _public) = build_site("test_site"); let (mut site, _tmp_dir, _public) = build_site("test_site");


let prefixes = &site.config.link_checker.skip_anchor_prefixes;
assert_eq!(prefixes, &vec!["https://github.com/rust-lang/rust/blob/"]);
assert_eq!(
site.config.link_checker.skip_anchor_prefixes,
vec!["https://github.com/rust-lang/rust/blob/"]
);
assert_eq!(site.config.link_checker.skip_prefixes, vec!["http://[2001:db8::]/"]);


site.config.enable_check_mode(); site.config.enable_check_mode();
site.load().expect("link check test_site"); site.load().expect("link check test_site");


+ 5
- 0
docs/content/documentation/getting-started/configuration.md View File

@@ -99,6 +99,11 @@ extra_syntaxes = []


# Configure the link checker # Configure the link checker
[link_checker] [link_checker]
# Skip link checking for external URLs that start with these prefixes
skip_prefixes = [
"http://[2001:db8::]/",
]

# Skip anchor checking for external URLs that start with these prefixes # Skip anchor checking for external URLs that start with these prefixes
skip_anchor_prefixes = [ skip_anchor_prefixes = [
"https://caniuse.com/", "https://caniuse.com/",


+ 4
- 0
test_site/config.toml View File

@@ -14,6 +14,10 @@ extra_syntaxes = ["syntaxes"]
ignored_content = ["*/ignored.md"] ignored_content = ["*/ignored.md"]


[link_checker] [link_checker]
skip_prefixes = [
"http://[2001:db8::]/",
]

skip_anchor_prefixes = [ skip_anchor_prefixes = [
"https://github.com/rust-lang/rust/blob/", "https://github.com/rust-lang/rust/blob/",
] ]


+ 2
- 0
test_site/content/posts/tutorials/programming/rust.md View File

@@ -10,4 +10,6 @@ A simple page


Link to some rust-lang [source code][permalink]. Link to some rust-lang [source code][permalink].


Internal web server <http://[2001:db8::]/path>.

[permalink]: https://github.com/rust-lang/rust/blob/c772948b687488a087356cb91432425662e034b9/src/librustc_back/target/mod.rs#L194-L214 [permalink]: https://github.com/rust-lang/rust/blob/c772948b687488a087356cb91432425662e034b9/src/librustc_back/target/mod.rs#L194-L214

Loading…
Cancel
Save