parent
20aeb7edac
commit
c85db8ac2a
1 changed files with 21 additions and 8 deletions
|
@ -28,23 +28,36 @@ pub fn read_dir(dir: &PathBuf) -> Vec<String> {
|
||||||
|
|
||||||
/// Rewrite all URLs in `input` to the format `/s/<domain>/<path..>`
|
/// Rewrite all URLs in `input` to the format `/s/<domain>/<path..>`
|
||||||
fn internalize_urls(input: &str, base: &str) -> String {
|
fn internalize_urls(input: &str, base: &str) -> String {
|
||||||
let url_pattern = r#"(?:(https?://([a-zA-Z0-9.-]+))?(/[\w./-]*))"#;
|
let url_pattern = r#"(\s|"|')(?:(<?)(https?:\/\/([a-zA-Z0-9.-]+))?(\/[\w./-]*))"#;
|
||||||
let re = regex::Regex::new(url_pattern).unwrap();
|
let re = regex::Regex::new(url_pattern).unwrap();
|
||||||
|
|
||||||
re.replace_all(input, |caps: ®ex::Captures| {
|
re.replace_all(input, |caps: ®ex::Captures| {
|
||||||
if let Some(domain) = caps.get(2) {
|
if caps.get(1).map(|x| x.as_str()).unwrap_or_default() == "<" {
|
||||||
let domain = domain.as_str().trim_start_matches("www.");
|
return caps.get(0).unwrap().as_str().to_string();
|
||||||
let path = caps.get(3).map_or("", |m| m.as_str());
|
}
|
||||||
|
|
||||||
|
let wrap = caps.get(1).map(|x| x.as_str()).unwrap_or_default();
|
||||||
|
|
||||||
|
if let Some(domain) = caps.get(3) {
|
||||||
|
let domain = domain.as_str();
|
||||||
|
let (protocol, domain) = if domain.starts_with("https://") {
|
||||||
|
("https", domain.trim_start_matches("https://"))
|
||||||
|
} else {
|
||||||
|
("http", domain.trim_start_matches("http://"))
|
||||||
|
};
|
||||||
|
|
||||||
|
let domain = domain.trim_start_matches("www.");
|
||||||
|
let path = caps.get(5).map_or("", |m| m.as_str());
|
||||||
|
|
||||||
// Skip transformation if the domain is in the blacklist
|
// Skip transformation if the domain is in the blacklist
|
||||||
if check_blacklist(domain) {
|
if check_blacklist(domain) {
|
||||||
format!("https://{domain}{path}")
|
format!("{wrap}{protocol}://{domain}{path}")
|
||||||
} else {
|
} else {
|
||||||
format!("/s/{domain}{path}")
|
format!("{wrap}/s/{domain}{path}")
|
||||||
}
|
}
|
||||||
} else if let Some(path) = caps.get(3) {
|
} else if let Some(path) = caps.get(5) {
|
||||||
// Handle relative paths
|
// Handle relative paths
|
||||||
format!("/s/{base}{}", path.as_str())
|
format!("{wrap}/s/{base}{}", path.as_str())
|
||||||
} else {
|
} else {
|
||||||
// Default fallback
|
// Default fallback
|
||||||
caps[0].to_string()
|
caps[0].to_string()
|
||||||
|
|
Loading…
Add table
Reference in a new issue