Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Jul 17, 2023
1 parent 68d7f9c commit 9497bf0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lychee-lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl ClientBuilder {
require_https: self.require_https,
quirks,
include_fragments: self.include_fragments,
fragment_checker: Default::default(),
fragment_checker: FragmentChecker::new(),
})
}
}
Expand Down Expand Up @@ -687,7 +687,7 @@ impl Client {

/// Checks a `file` URI's fragment.
pub async fn check_fragment(&self, path: &Path, uri: &Uri) -> Status {
match self.fragment_checker.check(path, uri).await {
match self.fragment_checker.check(path, &uri.url).await {
Ok(true) => Status::Ok(StatusCode::OK),
Ok(false) => ErrorKind::InvalidFragment(uri.clone()).into(),
Err(err) => {
Expand Down
8 changes: 5 additions & 3 deletions lychee-lib/src/extract/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ pub(crate) fn extract_markdown_fragments(input: &str) -> HashSet<String> {
out.insert(frag.to_string());
}

let id = id_generator.generate(&mut heading);
out.insert(id);
if !heading.is_empty() {
let id = id_generator.generate(&mut heading);
out.insert(id);
heading.clear();
}

in_heading = false;
heading.clear();
}
Event::Text(text) => {
if in_heading {
Expand Down
29 changes: 23 additions & 6 deletions lychee-lib/src/utils/fragment_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,47 @@ use std::{
sync::Arc,
};

use crate::{extract::markdown::extract_markdown_fragments, types::FileType, Result, Uri};
use crate::{extract::markdown::extract_markdown_fragments, types::FileType, Result};
use tokio::{fs, sync::Mutex};
use url::Url;

/// Holds a cache of fragments for a given URL.
///
/// Fragments, also known as anchors, are used to link to a specific
/// part of a page. For example, the URL `https://example.com#foo`
/// will link to the element with the `id` of `foo`.
///
/// This cache is used to avoid having to re-parse the same file
/// multiple times when checking if a given URL contains a fragment.
///
/// The cache is stored in a `HashMap` with the URL as the key and
/// a `HashSet` of fragments as the value.
#[derive(Default, Clone, Debug)]
pub(crate) struct FragmentChecker {
cache: Arc<Mutex<HashMap<String, HashSet<String>>>>,
}

impl FragmentChecker {
/// Creates a new `FragmentChecker`.
pub(crate) fn new() -> Self {
Self {
cache: Default::default(),
}
}

/// Checks if the given path contains the given fragment.
pub(crate) async fn check(&self, path: &Path, uri: &Uri) -> Result<bool> {
match (FileType::from(path), uri.url.fragment()) {
pub(crate) async fn check(&self, path: &Path, url: &Url) -> Result<bool> {
match (FileType::from(path), url.fragment()) {
(FileType::Markdown, Some(fragment)) => {
let url_without_frag = Self::remove_fragment(uri.url.clone());
let url_without_frag = Self::remove_fragment(url.clone());
self.populate_cache_if_vacant(url_without_frag, path, fragment)
.await
}
_ => Ok(false),
}
}

fn remove_fragment(url: Url) -> String {
let mut url = url;
fn remove_fragment(mut url: Url) -> String {
url.set_fragment(None);
url.into()
}
Expand Down

0 comments on commit 9497bf0

Please sign in to comment.