parent
3696f61b02
commit
20aeb7edac
4 changed files with 37 additions and 30 deletions
|
@ -3,7 +3,7 @@ use std::{collections::HashSet, path::PathBuf};
|
|||
use crate::{
|
||||
blacklist::{check_blacklist, check_blacklist_path},
|
||||
conf::get_config,
|
||||
favicon::download_fav_for
|
||||
favicon::download_fav_for,
|
||||
};
|
||||
|
||||
mod document;
|
||||
|
@ -27,20 +27,28 @@ pub fn read_dir(dir: &PathBuf) -> Vec<String> {
|
|||
}
|
||||
|
||||
/// Rewrite all URLs in `input` to the format `/s/<domain>/<path..>`
|
||||
fn internalize_urls(input: &str) -> String {
|
||||
let url_pattern = r"https?://([a-zA-Z0-9.-]+)(/[\w./-]*)";
|
||||
fn internalize_urls(input: &str, base: &str) -> String {
|
||||
let url_pattern = r#"(?:(https?://([a-zA-Z0-9.-]+))?(/[\w./-]*))"#;
|
||||
let re = regex::Regex::new(url_pattern).unwrap();
|
||||
|
||||
re.replace_all(input, |caps: ®ex::Captures| {
|
||||
let domain = caps[1].trim_start_matches("www.");
|
||||
let path = &caps[2];
|
||||
if let Some(domain) = caps.get(2) {
|
||||
let domain = domain.as_str().trim_start_matches("www.");
|
||||
let path = caps.get(3).map_or("", |m| m.as_str());
|
||||
|
||||
// Dont transform if in blacklist
|
||||
if check_blacklist(domain) {
|
||||
return format!("https://{domain}/{path}");
|
||||
// Skip transformation if the domain is in the blacklist
|
||||
if check_blacklist(domain) {
|
||||
format!("https://{domain}{path}")
|
||||
} else {
|
||||
format!("/s/{domain}{path}")
|
||||
}
|
||||
} else if let Some(path) = caps.get(3) {
|
||||
// Handle relative paths
|
||||
format!("/s/{base}{}", path.as_str())
|
||||
} else {
|
||||
// Default fallback
|
||||
caps[0].to_string()
|
||||
}
|
||||
|
||||
format!("/s/{domain}/{path}")
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue