diff --git a/src/utils.rs b/src/utils.rs index ccf062f..dc9555b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -445,9 +445,21 @@ pub fn format_url(url: &str) -> String { } // Rewrite Reddit links to Libreddit in body of text -pub fn rewrite_urls(text: &str) -> String { - match Regex::new(r#"href="(https|http|)://(www.|old.|np.|amp.|)(reddit).(com)/"#) { - Ok(re) => re.replace_all(text, r#"href="/"#).to_string(), +pub fn rewrite_urls(input_text: &str) -> String { + let text1 = match Regex::new(r#"href="(https|http|)://(www.|old.|np.|amp.|)(reddit).(com)/"#) { + Ok(re) => re.replace_all(input_text, r#"href="/"#).to_string(), + Err(_) => String::new(), + }; + + // Rewrite external media previews to Libreddit + match Regex::new(r"https://external-preview\.redd\.it(.*)[^?]") { + Ok(re) => { + if re.is_match(&text1) { + re.replace_all(&text1, format_url(re.find(&text1).unwrap().as_str())).to_string() + } else { + text1 + } + }, Err(_) => String::new(), } }