Skip to content

Commit

Permalink
Add --skip-external-links CLI flag to check subcommand (#2756)
Browse files Browse the repository at this point in the history
* Add `--skip-external-links` CLI flag to `check` subcommand

* Add a test for checks with external links skipped

* Add a description of `--skip-external-links` flag
  • Loading branch information
lo48576 authored Jan 2, 2025
1 parent ed2e577 commit d8ac819
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
10 changes: 9 additions & 1 deletion components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub struct Site {
include_drafts: bool,
build_mode: BuildMode,
shortcode_definitions: HashMap<String, ShortcodeDefinition>,
/// Whether to check external links
check_external_links: bool,
}

impl Site {
Expand Down Expand Up @@ -108,6 +110,7 @@ impl Site {
library: Arc::new(RwLock::new(Library::default())),
build_mode: BuildMode::Disk,
shortcode_definitions,
check_external_links: true,
};

Ok(site)
Expand All @@ -126,6 +129,11 @@ impl Site {
self.include_drafts = true;
}

/// Set the site checker to skip external links check.
pub fn skip_external_links_check(&mut self) {
self.check_external_links = false;
}

/// The index sections are ALWAYS at those paths
/// There are one index section for the default language + 1 per language
fn index_section_paths(&self) -> Vec<(PathBuf, Option<&str>)> {
Expand Down Expand Up @@ -344,7 +352,7 @@ impl Site {
}

// check external links, log the results, and error out if needed
if self.config.is_in_check_mode() {
if self.config.is_in_check_mode() && self.check_external_links {
let external_link_messages = link_checking::check_external_links(self);
if !external_link_messages.is_empty() {
let messages: Vec<String> = external_link_messages
Expand Down
26 changes: 26 additions & 0 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,32 @@ fn panics_on_invalid_external_domain() {
site.load().expect("link check test_site");
}

#[test]
fn external_links_ignored_on_check() {
let (mut site, _tmp_dir, _public) = build_site("test_site");

// remove the invalid domain skip prefix
let i = site
.config
.link_checker
.skip_prefixes
.iter()
.position(|prefix| prefix == "http://invaliddomain")
.unwrap();
site.config.link_checker.skip_prefixes.remove(i);

// confirm the invalid domain skip prefix was removed
assert_eq!(site.config.link_checker.skip_prefixes, vec!["http://[2001:db8::]/"]);

// set a flag to skip external links check
site.skip_external_links_check();

// check the test site with all external links (including invalid domain) skipped, which should
// not cause a panic
site.config.enable_check_mode();
site.load().expect("link check test_site");
}

#[test]
fn can_find_site_and_page_authors() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
Expand Down
2 changes: 2 additions & 0 deletions docs/content/documentation/getting-started/cli-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ The check subcommand will try to build all pages just like the build command wou
results to disk. Additionally, it will also check all external links in Markdown files by trying to fetch
them (links in the template files are not checked).

You can skip link checking for all the external links by `--skip-external-links` flag.

By default, drafts are not loaded. If you wish to include them, pass the `--drafts` flag.

## Colored output
Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ pub enum Command {
/// Include drafts when loading the site
#[clap(long)]
drafts: bool,
/// Skip external links
#[clap(long)]
skip_external_links: bool,
},

/// Generate shell completion
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn check(
base_path: Option<&str>,
base_url: Option<&str>,
include_drafts: bool,
skip_external_links: bool,
) -> Result<()> {
let bp = base_path.map(PathBuf::from).unwrap_or_else(|| PathBuf::from(root_dir));
let mut site = Site::new(bp, config_file)?;
Expand All @@ -22,6 +23,9 @@ pub fn check(
if include_drafts {
site.include_drafts();
}
if skip_external_links {
site.skip_external_links_check();
}
site.load()?;
messages::check_site_summary(&site);
messages::warn_about_ignored_pages(&site);
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ fn main() {
std::process::exit(1);
}
}
Command::Check { drafts } => {
Command::Check { drafts, skip_external_links } => {
console::info("Checking site...");
let start = Instant::now();
let (root_dir, config_file) = get_config_file_path(&cli_dir, &cli.config);
match cmd::check(&root_dir, &config_file, None, None, drafts) {
match cmd::check(&root_dir, &config_file, None, None, drafts, skip_external_links) {
Ok(()) => messages::report_elapsed_time(start),
Err(e) => {
messages::unravel_errors("Failed to check the site", &e);
Expand Down

0 comments on commit d8ac819

Please sign in to comment.