-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce --root-dir #1576
Merged
Merged
Introduce --root-dir #1576
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
452c550
windows
trask 866ba38
Introduce --root-path
trask 250f572
lint
trask cd074f9
lint
trask 2e9c8fd
Simplification
trask 4de28cd
Add unit tests
trask 2489d56
Add integration test
trask be3a062
Sync docs
trask bdb5ec6
Add missing comment to make CI happy
trask 144e1e5
Revert one of the Windows-specific changes because causing a test fai…
trask d2283d4
Support both options at the same time
trask 24c2e12
Revert a comment change that is no longer applicable
trask 4d3ef2b
Remove unused code
trask 196a441
Fix and simplification
trask 1ce48cf
Integration test both at the same time
trask 393919e
Unit tests both at the same time
trask 55a8198
Remove now redundant comment
trask 70fa35b
Revert windows-specific change, seems not needed after recent changes
trask ae0ed42
Use Collector::default()
trask 3331cda
extract method and unit tests
trask fb5aff5
clippy
trask bf8507b
clippy: &Option<A> -> Option<&A>
trask 0bb19b9
Remove outdated comment
trask b767b36
Rename --root-path to --root-dir
trask 83b28c4
Restrict --root-dir to absolute paths for now
trask 909e8c3
Move root dir check
trask File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<title>About</title> | ||
</head> | ||
<body> | ||
<h1 id="fragment">About</h1> | ||
</body> | ||
</html> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<html> | ||
<head> | ||
<title>Index</title> | ||
</head> | ||
<body> | ||
<h1>Index Title</h1> | ||
<a id="good"></a> | ||
<p> | ||
<ul> | ||
<li> | ||
<a href="/nested">home</a> | ||
</li> | ||
<li> | ||
<a href="/nested/about">About</a> | ||
</li> | ||
<li> | ||
<a href="/nested/another page">About</a> | ||
</li> | ||
<li> | ||
<a href="/nested/about/index.html#fragment">Fragment</a> | ||
</li> | ||
<li> | ||
<a href="/nested/about/index.html#missing">Missing</a> | ||
</li> | ||
<li> | ||
<a href="#good">Good</a> | ||
</li> | ||
<li> | ||
<a href="#bad">Bad</a> | ||
</li> | ||
</ul> | ||
</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ use futures::{ | |
StreamExt, | ||
}; | ||
use par_stream::ParStreamExt; | ||
use std::path::PathBuf; | ||
|
||
/// Collector keeps the state of link collection | ||
/// It drives the link extraction from inputs | ||
|
@@ -21,20 +22,37 @@ pub struct Collector { | |
skip_hidden: bool, | ||
include_verbatim: bool, | ||
use_html5ever: bool, | ||
root_dir: Option<PathBuf>, | ||
base: Option<Base>, | ||
} | ||
|
||
impl Default for Collector { | ||
fn default() -> Self { | ||
Collector { | ||
basic_auth_extractor: None, | ||
skip_missing_inputs: false, | ||
include_verbatim: false, | ||
use_html5ever: false, | ||
skip_hidden: true, | ||
skip_ignored: true, | ||
root_dir: None, | ||
base: None, | ||
} | ||
} | ||
} | ||
|
||
impl Collector { | ||
/// Create a new collector with an empty cache | ||
#[must_use] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clippy told me to remove this:
|
||
pub const fn new(base: Option<Base>) -> Self { | ||
pub const fn new(root_dir: Option<PathBuf>, base: Option<Base>) -> Self { | ||
Collector { | ||
basic_auth_extractor: None, | ||
skip_missing_inputs: false, | ||
include_verbatim: false, | ||
use_html5ever: false, | ||
skip_hidden: true, | ||
skip_ignored: true, | ||
root_dir, | ||
base, | ||
} | ||
} | ||
|
@@ -119,12 +137,19 @@ impl Collector { | |
}) | ||
.flatten() | ||
.par_then_unordered(None, move |(content, base)| { | ||
let root_dir = self.root_dir.clone(); | ||
let basic_auth_extractor = self.basic_auth_extractor.clone(); | ||
async move { | ||
let content = content?; | ||
let extractor = Extractor::new(self.use_html5ever, self.include_verbatim); | ||
let uris: Vec<RawUri> = extractor.extract(&content); | ||
let requests = request::create(uris, &content, &base, &basic_auth_extractor); | ||
let requests = request::create( | ||
uris, | ||
&content.source, | ||
root_dir.as_ref(), | ||
base.as_ref(), | ||
basic_auth_extractor.as_ref(), | ||
); | ||
Result::Ok(stream::iter(requests.into_iter().map(Ok))) | ||
} | ||
}) | ||
|
@@ -148,14 +173,22 @@ mod tests { | |
}; | ||
|
||
// Helper function to run the collector on the given inputs | ||
async fn collect(inputs: Vec<Input>, base: Option<Base>) -> HashSet<Uri> { | ||
let responses = Collector::new(base).collect_links(inputs); | ||
async fn collect( | ||
inputs: Vec<Input>, | ||
root_dir: Option<PathBuf>, | ||
base: Option<Base>, | ||
) -> HashSet<Uri> { | ||
let responses = Collector::new(root_dir, base).collect_links(inputs); | ||
responses.map(|r| r.unwrap().uri).collect().await | ||
} | ||
|
||
// Helper function for collecting verbatim links | ||
async fn collect_verbatim(inputs: Vec<Input>, base: Option<Base>) -> HashSet<Uri> { | ||
let responses = Collector::new(base) | ||
async fn collect_verbatim( | ||
inputs: Vec<Input>, | ||
root_dir: Option<PathBuf>, | ||
base: Option<Base>, | ||
) -> HashSet<Uri> { | ||
let responses = Collector::new(root_dir, base) | ||
.include_verbatim(true) | ||
.collect_links(inputs); | ||
responses.map(|r| r.unwrap().uri).collect().await | ||
|
@@ -246,7 +279,7 @@ mod tests { | |
}, | ||
]; | ||
|
||
let links = collect_verbatim(inputs, None).await; | ||
let links = collect_verbatim(inputs, None, None).await; | ||
|
||
let expected_links = HashSet::from_iter([ | ||
website(TEST_STRING), | ||
|
@@ -269,7 +302,7 @@ mod tests { | |
file_type_hint: Some(FileType::Markdown), | ||
excluded_paths: None, | ||
}; | ||
let links = collect(vec![input], Some(base)).await; | ||
let links = collect(vec![input], None, Some(base)).await; | ||
|
||
let expected_links = HashSet::from_iter([ | ||
website("https://endler.dev"), | ||
|
@@ -295,7 +328,7 @@ mod tests { | |
file_type_hint: Some(FileType::Html), | ||
excluded_paths: None, | ||
}; | ||
let links = collect(vec![input], Some(base)).await; | ||
let links = collect(vec![input], None, Some(base)).await; | ||
|
||
let expected_links = HashSet::from_iter([ | ||
website("https://github.com/lycheeverse/lychee/"), | ||
|
@@ -324,7 +357,7 @@ mod tests { | |
file_type_hint: Some(FileType::Html), | ||
excluded_paths: None, | ||
}; | ||
let links = collect(vec![input], Some(base)).await; | ||
let links = collect(vec![input], None, Some(base)).await; | ||
|
||
let expected_links = HashSet::from_iter([ | ||
website("https://example.com/static/image.png"), | ||
|
@@ -351,7 +384,7 @@ mod tests { | |
excluded_paths: None, | ||
}; | ||
|
||
let links = collect(vec![input], Some(base)).await; | ||
let links = collect(vec![input], None, Some(base)).await; | ||
|
||
let expected = HashSet::from_iter([ | ||
website("https://localhost.com/@/internal.md"), | ||
|
@@ -373,7 +406,7 @@ mod tests { | |
file_type_hint: Some(FileType::Html), | ||
excluded_paths: None, | ||
}; | ||
let links = collect(vec![input], Some(base)).await; | ||
let links = collect(vec![input], None, Some(base)).await; | ||
|
||
let expected_links = HashSet::from_iter([ | ||
// the body links wouldn't be present if the file was parsed strictly as XML | ||
|
@@ -406,7 +439,7 @@ mod tests { | |
excluded_paths: None, | ||
}; | ||
|
||
let links = collect(vec![input], None).await; | ||
let links = collect(vec![input], None, None).await; | ||
|
||
let expected_urls = HashSet::from_iter([ | ||
website("https://github.com/lycheeverse/lychee/"), | ||
|
@@ -425,7 +458,7 @@ mod tests { | |
file_type_hint: None, | ||
excluded_paths: None, | ||
}; | ||
let links = collect(vec![input], None).await; | ||
let links = collect(vec![input], None, None).await; | ||
|
||
let expected_links = HashSet::from_iter([mail("user@example.com")]); | ||
|
||
|
@@ -468,7 +501,7 @@ mod tests { | |
}, | ||
]; | ||
|
||
let links = collect(inputs, None).await; | ||
let links = collect(inputs, None, None).await; | ||
|
||
let expected_links = HashSet::from_iter([ | ||
website(&format!( | ||
|
@@ -502,7 +535,7 @@ mod tests { | |
excluded_paths: None, | ||
}; | ||
|
||
let links = collect(vec![input], Some(base)).await; | ||
let links = collect(vec![input], None, Some(base)).await; | ||
|
||
let expected_links = HashSet::from_iter([ | ||
path("/path/to/root/index.html"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,15 +30,6 @@ impl Base { | |
} | ||
} | ||
|
||
/// Return the directory if the base is local | ||
#[must_use] | ||
pub(crate) fn dir(&self) -> Option<PathBuf> { | ||
match self { | ||
Self::Remote(_) => None, | ||
Self::Local(d) => Some(d.clone()), | ||
} | ||
} | ||
|
||
Comment on lines
-33
to
-41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
pub(crate) fn from_source(source: &InputSource) -> Option<Base> { | ||
match &source { | ||
InputSource::RemoteUrl(url) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing; I think we should move this to
Collector::new
. lychee can also be used as library through lychee-lib so that's another entrypoint where this invariant should be enforcedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done