Skip to content

Commit 8937c83

Browse files
committed
feat: resolve Filenames through wikilink checker
1 parent b0b289f commit 8937c83

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[[#LocalHeader]]
2+
3+
# LocalHeader
4+
5+
[[Usage#Header|HeaderRenaming]]
6+
[[Space Usage#Header|HeaderRenaming]]
7+
[[Space Usage DifferentDirectory#Header|HeaderRenaming]]
8+
[[DifferentDirectory#Header|HeaderRenaming]]

fixtures/wiki/obsidian-style.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
[[#LocalHeader]]
2-
31
[[Usage]]
42
[[Space Usage]]
53
[[Space Usage DifferentDirectory]]
64
[[DifferentDirectory]]
7-
8-
[[Usage#Header|HeaderRenaming]]
9-
[[Space Usage#Header|HeaderRenaming]]
10-
[[Space Usage DifferentDirectory#Header|HeaderRenaming]]
11-
[[DifferentDirectory#Header|HeaderRenaming]]
12-
13-
# LocalHeader

lychee-bin/tests/cli.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2950,13 +2950,17 @@ mod cli {
29502950
.arg("--include-wikilinks")
29512951
.arg("--fallback-extensions")
29522952
.arg("md")
2953+
.arg("--base-url")
2954+
.arg(fixtures_path())
2955+
.arg("--root-dir")
2956+
.arg(fixtures_path())
29532957
.assert()
29542958
.success();
29552959
}
29562960

29572961
#[test]
29582962
fn test_wikilink_fixture_with_fragments_obsidian_style() {
2959-
let input = fixtures_path().join("wiki/obsidian-style.md");
2963+
let input = fixtures_path().join("wiki/obsidian-style-plus-headers.md");
29602964

29612965
//fragments should resolve all headers
29622966
let dir_links_with_fragment = 2;

lychee-lib/src/checker/file.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl FileChecker {
8181
///
8282
/// Returns a `Status` indicating the result of the check.
8383
pub(crate) async fn check(&self, uri: &Uri) -> Status {
84+
//only populate the wikilink filenames if it is enabled
8485
if self.include_wikilinks {
8586
self.setup_wikilinks().await;
8687
}
@@ -145,8 +146,12 @@ impl FileChecker {
145146
) -> Result<Cow<'a, Path>, ErrorKind> {
146147
let path = match path.metadata() {
147148
// for non-existing paths, attempt fallback extensions
149+
// if fallback extensions don't help, try wikilinks
148150
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
149-
self.apply_fallback_extensions(path, uri).map(Cow::Owned)
151+
match self.apply_fallback_extensions(path, uri).map(Cow::Owned) {
152+
Ok(val) => Ok(val),
153+
Err(_) => self.apply_wikilink_check(path, uri).await.map(Cow::Owned),
154+
}
150155
}
151156

152157
// other IO errors are unexpected and should fail the check
@@ -257,7 +262,7 @@ impl FileChecker {
257262
}
258263

259264
/// Checks a resolved file, optionally verifying fragments for HTML files.
260-
///
265+
///u
261266
/// # Arguments
262267
///
263268
/// * `path` - The resolved path to check.
@@ -323,7 +328,16 @@ impl FileChecker {
323328
// Tries to resolve a link by looking up the filename in the wikilink index
324329
// The
325330
async fn apply_wikilink_check(&self, path: &Path, uri: &Uri) -> Result<PathBuf, ErrorKind> {
326-
self.wikilink_checker.check(path, uri).await
331+
let mut path_buf = path.to_path_buf();
332+
for ext in &self.fallback_extensions {
333+
path_buf.set_extension(ext);
334+
match self.wikilink_checker.check(&path_buf, uri).await {
335+
Err(_) => {}
336+
Ok(resolved_path) => return Ok(resolved_path),
337+
}
338+
}
339+
340+
Err(ErrorKind::InvalidFilePath(uri.clone()))
327341
}
328342
}
329343

lychee-lib/src/utils/wikilink_checker.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ impl WikilinkChecker {
4444
//actively ignore symlinks
4545
.follow_links(false)
4646
.into_iter()
47-
.filter_map(|e| e.ok())
47+
.filter_map(std::result::Result::ok)
4848
{
49-
match entry.path().file_name() {
50-
Some(filename) => {
51-
filenameslock.insert(filename.into(), entry.path().to_path_buf());
52-
}
53-
None => {}
49+
if let Some(filename) = entry.path().file_name() {
50+
filenameslock
51+
.insert(filename.to_ascii_lowercase(), entry.path().to_path_buf());
5452
}
5553
}
5654
}
@@ -65,9 +63,9 @@ impl WikilinkChecker {
6563
None => Err(ErrorKind::InvalidFilePath(uri.clone())),
6664
Some(filename) => {
6765
let filenamelock = self.filenames.lock().await;
68-
if filenamelock.contains_key(filename.into()) {
66+
if filenamelock.contains_key(&filename.to_ascii_lowercase()) {
6967
Ok(filenamelock
70-
.get(filename.into())
68+
.get(&filename.to_ascii_lowercase())
7169
.expect("Could not retrieve inserted Path for discovered Wikilink-Path"))
7270
.cloned()
7371
} else {

0 commit comments

Comments
 (0)