Skip to content

Commit 589e1f3

Browse files
committed
feat: WikilinkChecker as optional
include span in tests fix: allow too many lines
1 parent 840499d commit 589e1f3

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

lychee-lib/src/checker/file.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) struct FileChecker {
3838
/// Utility for performing fragment checks in HTML files.
3939
fragment_checker: FragmentChecker,
4040
/// Utility for checking wikilinks, indexes files in a given directory
41-
wikilink_checker: WikilinkChecker,
41+
wikilink_checker: Option<WikilinkChecker>,
4242
}
4343

4444
impl FileChecker {
@@ -335,19 +335,26 @@ impl FileChecker {
335335

336336
// Initializes the index of the wikilink checker
337337
fn setup_wikilinks(&self) -> Result<(), ErrorKind> {
338-
self.wikilink_checker.setup_wikilinks_index()
338+
match &self.wikilink_checker {
339+
Some(checker) => checker.setup_wikilinks_index(),
340+
None => Err(ErrorKind::WikilinkCheckerInit(
341+
"Initialization failed, no checker instantiated".to_string(),
342+
)),
343+
}
339344
}
340345

341346
// Tries to resolve a link by looking up the filename in the wikilink index
342347
fn apply_wikilink_check(&self, path: &Path, uri: &Uri) -> Result<PathBuf, ErrorKind> {
343348
let mut path_buf = path.to_path_buf();
344349
for ext in &self.fallback_extensions {
345350
path_buf.set_extension(ext);
346-
match self.wikilink_checker.contains_path(&path_buf) {
347-
None => {
348-
trace!("Tried to find wikilink {} at {}", uri, path_buf.display());
351+
if let Some(checker) = &self.wikilink_checker {
352+
match checker.contains_path(&path_buf) {
353+
None => {
354+
trace!("Tried to find wikilink {} at {}", uri, path_buf.display());
355+
}
356+
Some(resolved_path) => return Ok(resolved_path),
349357
}
350-
Some(resolved_path) => return Ok(resolved_path),
351358
}
352359
}
353360

lychee-lib/src/extract/markdown.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn md_extensions() -> Options {
2424
}
2525

2626
/// Extract unparsed URL strings from a Markdown string.
27+
#[allow(clippy::too_many_lines)]
2728
pub(crate) fn extract_markdown(
2829
input: &str,
2930
include_verbatim: bool,
@@ -97,8 +98,8 @@ pub(crate) fn extract_markdown(
9798
text: wikilink.to_string(),
9899
element: Some("a".to_string()),
99100
attribute: Some("wikilink".to_string()),
100-
// wiki links start with `[[`, so offset the span by `2`
101-
span: span.start + 2
101+
// wiki links start with `[[`, so offset the span by `2`
102+
span: span_provider.span(span.start + 2)
102103
}])
103104
} else {
104105
warn!("WARNING: The wikilink destination url {dest_url} could not be cleaned by removing potholes and fragments");
@@ -679,16 +680,19 @@ Shortcut link: [link4]
679680
text: "foo".to_string(),
680681
element: Some("a".to_string()),
681682
attribute: Some("wikilink".to_string()),
683+
span: span(2, 3),
682684
},
683685
RawUri {
684686
text: "foo".to_string(),
685687
element: Some("a".to_string()),
686688
attribute: Some("wikilink".to_string()),
689+
span: span(3, 3),
687690
},
688691
RawUri {
689692
text: "foo".to_string(),
690693
element: Some("a".to_string()),
691694
attribute: Some("wikilink".to_string()),
695+
span: span(4, 3),
692696
},
693697
];
694698
assert_eq!(uris, expected);

lychee-lib/src/types/error.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ pub enum ErrorKind {
174174
#[error("Failed to lock a Mutex")]
175175
MutexPoisoned,
176176

177+
/// Error when initializing the Wikilink Checker
178+
#[error("Failed to initialize Wikilink Checker")]
179+
WikilinkCheckerInit(String),
180+
177181
/// Test-only error variant for formatter tests
178182
/// Available in both test and debug builds to support cross-crate testing
179183
#[cfg(any(test, debug_assertions))]
@@ -341,7 +345,10 @@ impl ErrorKind {
341345
}.into(),
342346
ErrorKind::MutexPoisoned => Some (
343347
"One or more threads failed and poisoned a Mutex".to_string()
344-
)
348+
),
349+
ErrorKind::WikilinkCheckerInit(reason) => Some(format!(
350+
"Error initialzing the Wikilink Checker: {reason} ",
351+
)),
345352
}
346353
}
347354

@@ -478,6 +485,7 @@ impl Hash for ErrorKind {
478485
Self::Cookies(e) => e.to_string().hash(state),
479486
Self::StatusCodeSelectorError(e) => e.to_string().hash(state),
480487
Self::MutexPoisoned => "Mutex Poisoned".to_string().hash(state),
488+
Self::WikilinkCheckerInit(e) => e.to_string().hash(state),
481489
}
482490
}
483491
}

lychee-lib/src/utils/wikilink_checker.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ pub(crate) struct WikilinkChecker {
1919
}
2020

2121
impl WikilinkChecker {
22-
pub(crate) fn new(base: Option<Base>) -> Self {
23-
Self {
24-
basedir: base,
25-
..Default::default()
22+
pub(crate) fn new(base: Option<Base>) -> Option<Self> {
23+
if base.is_none() {
24+
None
25+
} else {
26+
warn!(
27+
"The Wikilink Checker could not be initialized because the base directory is missing"
28+
);
29+
Some(Self {
30+
basedir: base,
31+
..Default::default()
32+
})
2633
}
2734
}
2835

@@ -68,7 +75,10 @@ impl WikilinkChecker {
6875
// A remote base is of no use for the wikilink checker, silently skip over it
6976
Base::Remote(remote_base_name) => {
7077
warn!("Error using remote base url for checking wililinks: {remote_base_name}");
71-
Ok(())
78+
Err(ErrorKind::WikilinkCheckerInit(
79+
"Remote Base Directory found, only local directories are allowed"
80+
.to_string(),
81+
))
7282
}
7383
},
7484
}

0 commit comments

Comments
 (0)