Skip to content

Commit

Permalink
various link checker level changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcz committed May 10, 2022
1 parent 8b56180 commit 12dc16e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 43 deletions.
9 changes: 5 additions & 4 deletions components/markdown/src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::fmt::Write;

use errors::bail;
use libs::gh_emoji::Replacer as EmojiReplacer;
use libs::once_cell::sync::Lazy;
use libs::pulldown_cmark as cmark;
use libs::tera;

use crate::context::RenderContext;
use errors::{anyhow, Context, Error, Result};
use errors::{Context, Error, Result};
use libs::pulldown_cmark::escape::escape_html;
use utils::site::resolve_internal_link;
use utils::slugs::slugify_anchors;
Expand Down Expand Up @@ -140,14 +141,14 @@ fn fix_link(
}
Err(_) => {
let msg = format!(
"Dead relative link `{}` in {}",
"Broken relative link `{}` in {}",
link,
context.current_page_path.unwrap_or("unknown"),
);
match context.config.link_checker.internal_level {
config::LinkCheckerLevel::Error => return Err(anyhow!(msg)),
config::LinkCheckerLevel::Error => bail!(msg),
config::LinkCheckerLevel::Warn => {
console::error(&msg);
console::warn(&msg);
link.to_string() // TODO document that broken internal relative links will show up in the HTML href as "@/path"
}
}
Expand Down
5 changes: 2 additions & 3 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod sitemap;
pub mod tpls;

use std::collections::HashMap;
use std::fmt::format;
use std::fs::remove_dir_all;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, RwLock};
Expand Down Expand Up @@ -306,7 +305,7 @@ impl Site {
.map(|(i, msg)| format!(" {}. {}", i + 1, msg))
.collect();
let msg = format!(
"Found {} broken internal anchor links\n{}",
"Found {} broken internal anchor link(s)\n{}",
messages.len(),
messages.join("\n")
);
Expand All @@ -326,7 +325,7 @@ impl Site {
.map(|(i, msg)| format!(" {}. {}", i + 1, msg))
.collect();
let msg = format!(
"Found {} broken external links\n{}",
"Found {} broken external link(s)\n{}",
messages.len(),
messages.join("\n")
);
Expand Down
26 changes: 11 additions & 15 deletions components/site/src/link_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{collections::HashMap, path::PathBuf, thread};
use config::LinkCheckerLevel;
use libs::rayon::prelude::*;

use crate::{anyhow, Site};
use crate::Site;
use errors::{bail, Result};
use libs::rayon;
use libs::url::Url;
Expand Down Expand Up @@ -173,14 +173,15 @@ pub fn check_external_links(site: &Site) -> Result<(), Vec<String>> {
}
);

if !invalid_url_links.is_empty() {
for err in invalid_url_links.into_iter() {
let msg = err.domain.as_ref().unwrap_err();
match site.config.link_checker.internal_level {
LinkCheckerLevel::Error => console::error(&msg.to_string()),
LinkCheckerLevel::Warn => console::warn(&msg.to_string()),
}
}
for err in invalid_url_links.into_iter() {
let msg = err.domain.as_ref().unwrap_err().to_string();
messages.push(msg);
}

// error out if we're in error mode and any external URLs couldn't be parsed
match site.config.link_checker.external_level {
LinkCheckerLevel::Error if messages.len() > 0 => return Err(messages),
_ => (),
}

let mut links_by_domain: HashMap<String, Vec<&LinkDef>> = HashMap::new();
Expand Down Expand Up @@ -248,7 +249,7 @@ pub fn check_external_links(site: &Site) -> Result<(), Vec<String>> {

for (page_path, link, check_res) in errors.iter() {
messages.push(format!(
"Dead link in {} to {}: {}",
"Broken link in {} to {}: {}",
page_path.to_string_lossy(),
link,
link_checker::message(check_res)
Expand All @@ -258,10 +259,5 @@ pub fn check_external_links(site: &Site) -> Result<(), Vec<String>> {
Err(pool_err) => messages.push(pool_err.to_string()),
}

// match site.config.link_checker.external_level {
// LinkCheckerLevel::Error => Err(anyhow!("Dead links found")),
// LinkCheckerLevel::Warn => Ok(()),
// }

Err(messages)
}
4 changes: 2 additions & 2 deletions link-checker-level-test/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ highlight_code = false
# Put all your custom variables here

[link_checker]
internal_level = "error"
external_level = "error"
internal_level = "warn"
external_level = "warn"
15 changes: 7 additions & 8 deletions link-checker-level-test/content/post.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ template = "single.html"
+++

[good internal link](./)
[no DNS external link](https://sddfsdjf.com)
[404 external link](https://clayto.com/1234)
[invalid domain external link](https://cla<yto.com/)
[invalid domain external link](https://cla<yto.com/)
[invalid domain external link](https://cla<yto.com/)
[invalid domain external link](https://cla<yto.com/)
[invalid domain external link](https://cla<yto.com/)
[invalid domain external link](https://cla<yto.com/)
[bad anchor link 1](#foo)
[bad anchor link 2](#bar)
[bad internal link 1](@/foo)
[bad internal link 2](@/bar)
[good external link 1](https://getzola.org)
[bad external link 2](https://cla<yto.com/)
[external 404](https://clayto.com/foo)
18 changes: 7 additions & 11 deletions link-checker-level-test/public/post/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<p><a href="https://example.com/post/#foo">bad anchor link 1</a>
<p><a href="./">good internal link</a>
<a href="https://example.com/post/#foo">bad anchor link 1</a>
<a href="https://example.com/post/#bar">bad anchor link 2</a>
<a href="https://example.com/post/#baz">bad anchor link 3</a>
<a href="./">good internal link</a>
<a href="https://sddfsdjf.com">no DNS external link</a>
<a href="https://clayto.com/1234">404 external link</a>
<a href="https://cla%3Cyto.com/">invalid domain external link</a>
<a href="https://cla%3Cyto.com/">invalid domain external link</a>
<a href="https://cla%3Cyto.com/">invalid domain external link</a>
<a href="https://cla%3Cyto.com/">invalid domain external link</a>
<a href="https://cla%3Cyto.com/">invalid domain external link</a>
<a href="https://cla%3Cyto.com/">invalid domain external link</a></p>
<a href="@/foo">bad internal link 1</a>
<a href="@/bar">bad internal link 2</a>
<a href="https://g%7Betzola.org">invalid domain external link 1</a>
<a href="https://cla%3Cyto.com/">invalid domain external link 2</a></p>
<!-- [external 404](https://clayto.com/foo) -->

0 comments on commit 12dc16e

Please sign in to comment.