From 171e98f103a38b395bb9ec26f796258171cf9198 Mon Sep 17 00:00:00 2001 From: YOSHIOKA Takuma Date: Sat, 28 Dec 2024 22:33:36 +0900 Subject: [PATCH 1/3] Add `--skip-external-links` CLI flag to `check` subcommand --- components/site/src/lib.rs | 10 +++++++++- src/cli.rs | 3 +++ src/cmd/check.rs | 4 ++++ src/main.rs | 4 ++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index cd48c219e4..a5b26ce169 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -65,6 +65,8 @@ pub struct Site { include_drafts: bool, build_mode: BuildMode, shortcode_definitions: HashMap, + /// Whether to check external links + check_external_links: bool, } impl Site { @@ -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) @@ -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>)> { @@ -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 = external_link_messages diff --git a/src/cli.rs b/src/cli.rs index c3f68a493c..4a911336d0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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 diff --git a/src/cmd/check.rs b/src/cmd/check.rs index 46522deab9..2a021ce46a 100644 --- a/src/cmd/check.rs +++ b/src/cmd/check.rs @@ -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)?; @@ -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); diff --git a/src/main.rs b/src/main.rs index 88b3e0e617..a2ae16437e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); From c2da1c6276ae30b45acba3114dea1596b7f235cb Mon Sep 17 00:00:00 2001 From: YOSHIOKA Takuma Date: Tue, 31 Dec 2024 22:00:44 +0900 Subject: [PATCH 2/3] Add a test for checks with external links skipped --- components/site/tests/site.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index bf0811e15a..f141583c05 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -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(); From 3cbb69312f6dcb0d9b0be91037a479dc6985a101 Mon Sep 17 00:00:00 2001 From: YOSHIOKA Takuma Date: Tue, 31 Dec 2024 22:11:31 +0900 Subject: [PATCH 3/3] Add a description of `--skip-external-links` flag --- docs/content/documentation/getting-started/cli-usage.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/content/documentation/getting-started/cli-usage.md b/docs/content/documentation/getting-started/cli-usage.md index b8d98637ae..9795b7f2fb 100644 --- a/docs/content/documentation/getting-started/cli-usage.md +++ b/docs/content/documentation/getting-started/cli-usage.md @@ -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