From 97a0680bd04dc9ae4eaa5d877e3517c442d2678a Mon Sep 17 00:00:00 2001 From: imabritishcow Date: Sun, 9 May 2021 01:22:26 +0000 Subject: [PATCH] Support GIFs in comments (#217) * Support GIFs in comments * Fix removing Giphy links so it only removes Giphy links * Remove removing link to Giphy --- src/utils.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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(), } }