-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Links to markdown files with fragments now have the fragments checked.
Fragments/anchors are generated from markdown files using *unique kebab case* and the *heading attributes* commonmark extension.
- Loading branch information
Showing
10 changed files
with
187 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,58 @@ | ||
use std::{ | ||
collections::{hash_map::Entry, HashMap, HashSet}, | ||
path::Path, | ||
sync::Arc, | ||
}; | ||
|
||
use crate::{extract::markdown::extract_markdown_fragments, types::FileType, Uri}; | ||
use tokio::{fs::File, io::AsyncReadExt, sync::Mutex}; | ||
use url::Url; | ||
|
||
#[derive(Default, Clone, Debug)] | ||
pub(crate) struct FragmentChecker { | ||
cache: Arc<Mutex<HashMap<String, HashSet<String>>>>, | ||
} | ||
|
||
impl FragmentChecker { | ||
pub(crate) async fn check(&self, path: &Path, uri: &Uri) -> Result<bool, std::io::Error> { | ||
let (FileType::Markdown, Some(fragment)) = (FileType::from(path), uri.url.fragment()) else { | ||
// If it is not a markdown file or if there is no fragment, return early. | ||
return Ok(true) | ||
}; | ||
let url_without_frag = Self::remove_fragment(uri.url.clone()); | ||
|
||
let frag_exists = self | ||
.check_cache_if_vacant_populate(url_without_frag, path, fragment) | ||
.await?; | ||
Ok(frag_exists) | ||
} | ||
|
||
fn remove_fragment(url: Url) -> String { | ||
let mut url = url; | ||
url.set_fragment(None); | ||
url.into() | ||
} | ||
|
||
async fn check_cache_if_vacant_populate( | ||
&self, | ||
url_without_frag: String, | ||
path: &Path, | ||
fragment: &str, | ||
) -> Result<bool, std::io::Error> { | ||
let mut fragment_cache = self.cache.lock().await; | ||
match fragment_cache.entry(url_without_frag.clone()) { | ||
Entry::Vacant(entry) => { | ||
let content = Self::read_file_content(path).await?; | ||
let file_frags = extract_markdown_fragments(&content); | ||
Ok(entry.insert(file_frags).contains(fragment)) | ||
} | ||
Entry::Occupied(entry) => Ok(entry.get().contains(fragment)), | ||
} | ||
} | ||
|
||
async fn read_file_content(path: &Path) -> Result<String, std::io::Error> { | ||
let mut content = String::new(); | ||
File::open(path).await?.read_to_string(&mut content).await?; | ||
Ok(content) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod fragment_checker; | ||
pub(crate) mod path; | ||
pub(crate) mod request; | ||
pub(crate) mod reqwest; | ||
|