Skip to content

Commit 341e27f

Browse files
committed
feat: WikilinkChecker as optional
include span in tests fix: allow too many lines fix merge conflicts fix merge conflicts
1 parent 70e3d39 commit 341e27f

File tree

6 files changed

+58
-57
lines changed

6 files changed

+58
-57
lines changed

README.md

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ Options:
540540
Find links in verbatim sections like `pre`- and `code` blocks
541541
542542
--include-wikilinks
543-
Check WikiLinks in Markdown files
543+
Check WikiLinks in Markdown files, this requires specifying --base-url
544544
545545
--index-files <INDEX_FILES>
546546
When checking locally, resolves directory links to a separate index file.
@@ -684,28 +684,6 @@ Options:
684684
User agent
685685
686686
[default: lychee/0.20.1]
687-
[default: color]
688-
[possible values: plain, color, emoji, task]
689-
690-
-f, --format <FORMAT>
691-
Output format of final status report
692-
693-
[default: compact]
694-
[possible values: compact, detailed, json, markdown, raw]
695-
696-
--require-https
697-
When HTTPS is available, treat HTTP links as errors
698-
699-
--cookie-jar <COOKIE_JAR>
700-
Tell lychee to read cookies from the given file. Cookies will be stored in the
701-
cookie jar and sent with requests. New cookies will be stored in the cookie jar
702-
and existing cookies will be updated.
703-
704-
--include-wikilinks
705-
Check WikiLinks in Markdown files, this requires specifying --base-url
706-
707-
-h, --help
708-
Print help (see a summary with '-h')
709687
710688
-v, --verbose...
711689
Set verbosity level; more output per occurrence (e.g. `-v` or `-vv`)
@@ -721,13 +699,13 @@ Options:
721699

722700
### Exit codes
723701

724-
0 Success. The operation was completed successfully as instructed.
702+
0 Success. The operation was completed successfully as instructed.
725703

726-
1 Missing inputs or any unexpected runtime failures or configuration errors
704+
1 Missing inputs or any unexpected runtime failures or configuration errors
727705

728-
2 Link check failures. At least one non-excluded link failed the check.
706+
2 Link check failures. At least one non-excluded link failed the check.
729707

730-
3 Encountered errors in the config file.
708+
3 Encountered errors in the config file.
731709

732710
### Ignoring links
733711

lychee-bin/tests/cli.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,7 +2524,7 @@ The config file should contain every possible key for documentation purposes."
25242524
.arg("--dump")
25252525
.arg("--include-wikilinks")
25262526
.arg("--base-url")
2527-
.arg(fixtures_path())
2527+
.arg(fixtures_path!())
25282528
.arg(test_path)
25292529
.assert()
25302530
.success()
@@ -2987,51 +2987,51 @@ The config file should contain every possible key for documentation purposes."
29872987

29882988
#[test]
29892989
fn test_wikilink_fixture_obsidian_style() {
2990-
let input = fixtures_path().join("wiki/obsidian-style.md");
2990+
let input = fixtures_path!().join("wiki/obsidian-style.md");
29912991

29922992
// testing without fragments should not yield failures
2993-
main_command()
2993+
main_command!()
29942994
.arg(&input)
29952995
.arg("--include-wikilinks")
29962996
.arg("--fallback-extensions")
29972997
.arg("md")
29982998
.arg("--base-url")
2999-
.arg(fixtures_path())
2999+
.arg(fixtures_path!())
30003000
.assert()
30013001
.success()
30023002
.stdout(contains("4 OK"));
30033003
}
30043004

30053005
#[test]
30063006
fn test_wikilink_fixture_with_fragments_obsidian_style_fixtures_excluded() {
3007-
let input = fixtures_path().join("wiki/obsidian-style-plus-headers.md");
3007+
let input = fixtures_path!().join("wiki/obsidian-style-plus-headers.md");
30083008

30093009
// fragments should resolve all headers
3010-
main_command()
3010+
main_command!()
30113011
.arg(&input)
30123012
.arg("--include-wikilinks")
30133013
.arg("--fallback-extensions")
30143014
.arg("md")
30153015
.arg("--base-url")
3016-
.arg(fixtures_path())
3016+
.arg(fixtures_path!())
30173017
.assert()
30183018
.success()
30193019
.stdout(contains("4 OK"));
30203020
}
30213021

30223022
#[test]
30233023
fn test_wikilink_fixture_with_fragments_obsidian_style() {
3024-
let input = fixtures_path().join("wiki/obsidian-style-plus-headers.md");
3024+
let input = fixtures_path!().join("wiki/obsidian-style-plus-headers.md");
30253025

30263026
// fragments should resolve all headers
3027-
main_command()
3027+
main_command!()
30283028
.arg(&input)
30293029
.arg("--include-wikilinks")
30303030
.arg("--include-fragments")
30313031
.arg("--fallback-extensions")
30323032
.arg("md")
30333033
.arg("--base-url")
3034-
.arg(fixtures_path())
3034+
.arg(fixtures_path!())
30353035
.assert()
30363036
.success()
30373037
.stdout(contains("4 OK"));

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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,9 @@ pub enum ErrorKind {
183183
#[error("Failed to lock a Mutex")]
184184
MutexPoisoned,
185185

186-
/// Test-only error variant for formatter tests
187-
/// Available in both test and debug builds to support cross-crate testing
188-
#[cfg(any(test, debug_assertions))]
189-
#[error("Generic test error")]
190-
TestError,
186+
/// Error when initializing the Wikilink Checker
187+
#[error("Failed to initialize Wikilink Checker")]
188+
WikilinkCheckerInit(String),
191189
}
192190

193191
impl ErrorKind {
@@ -349,7 +347,10 @@ impl ErrorKind {
349347
ErrorKind::PreprocessorError{command, reason} => Some(format!("Command '{command}' failed {reason}. Check value of the pre option")),
350348
ErrorKind::MutexPoisoned => Some (
351349
"One or more threads failed and poisoned a Mutex".to_string()
352-
)
350+
),
351+
ErrorKind::WikilinkCheckerInit(reason) => Some(format!(
352+
"Error initializing the Wikilink Checker: {reason} ",
353+
)),
353354
}
354355
}
355356

@@ -480,7 +481,8 @@ impl Hash for ErrorKind {
480481
Self::Cookies(e) => e.hash(state),
481482
Self::StatusCodeSelectorError(e) => e.to_string().hash(state),
482483
Self::PreprocessorError { command, reason } => (command, reason).hash(state),
483-
Self::MutexPoisoned => "Mutex Poisoned".to_string().hash(state),
484+
Self::MutexPoisoned => "Mutex Poisoned".hash(state),
485+
Self::WikilinkCheckerInit(e) => e.hash(state),
484486
}
485487
}
486488
}

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)