From f7c182dcd80a371bb75234c0275a020bb3632175 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 7 Apr 2024 19:22:20 -0400 Subject: [PATCH 1/4] Fix a couple of edge cases with image embedding and don't check if REDDIT_PREVIEW_REGEX matches before executing loop --- src/utils.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 9b67e0b..da98706 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -877,8 +877,8 @@ pub fn format_url(url: &str) -> String { static REDDIT_REGEX: Lazy = Lazy::new(|| Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|new\.|)(reddit\.com|redd\.it)/"#).unwrap()); static REDDIT_PREVIEW_REGEX: Lazy = Lazy::new(|| Regex::new(r"https?://(external-preview|preview)\.redd\.it(.*)[^?]").unwrap()); static REDDIT_EMOJI_REGEX: Lazy = Lazy::new(|| Regex::new(r"https?://(www|).redditstatic\.com/(.*)").unwrap()); -static REDLIB_PREVIEW_LINK_REGEX: Lazy = Lazy::new(|| Regex::new(r#"/preview/(pre|external-pre)/(.*)">"#).unwrap()); -static REDLIB_PREVIEW_TEXT_REGEX: Lazy = Lazy::new(|| Regex::new(r">(.*)").unwrap()); +static REDLIB_PREVIEW_LINK_REGEX: Lazy = Lazy::new(|| Regex::new(r#"/preview/(pre|external-pre)/(.*?)>"#).unwrap()); +static REDLIB_PREVIEW_TEXT_REGEX: Lazy = Lazy::new(|| Regex::new(r">(.*?)").unwrap()); // Rewrite Reddit links to Redlib in body of text pub fn rewrite_urls(input_text: &str) -> String { @@ -894,24 +894,23 @@ pub fn rewrite_urls(input_text: &str) -> String { .replace("\\_", "_"); // Rewrite external media previews to Redlib - if REDDIT_PREVIEW_REGEX.is_match(&text1) { - loop { - if REDDIT_PREVIEW_REGEX.find(&text1).is_none() { - return text1; - } else { - let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default()); + loop { + if REDDIT_PREVIEW_REGEX.find(&text1).is_none() { + return text1; + } else { + let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default()); - let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - let image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); + let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); + let image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); + + let image_to_replace = format!(">", ">"); + let image_replacement = format!(""); - text1 = REDDIT_PREVIEW_REGEX - .replace(&text1, format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default())) - .replace(&image_text, &format!(">")) - .to_string() - } + text1 = REDDIT_PREVIEW_REGEX + .replace(&text1, formatted_url) + .replace(&image_to_replace, &image_replacement) + .to_string() } - } else { - text1 } } From ccfe7d0eeb561d177f9c6186fb06153ddf354411 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 7 Apr 2024 19:24:55 -0400 Subject: [PATCH 2/4] Make embedded images keep aspect ration when shrinking --- static/style.css | 2 ++ 1 file changed, 2 insertions(+) diff --git a/static/style.css b/static/style.css index 5e8236d..a85ab75 100644 --- a/static/style.css +++ b/static/style.css @@ -1177,11 +1177,13 @@ a.search_subreddit:hover { .comment img { max-width: 50%; + height: auto; } @media screen and (max-width: 500px) { .comment img { max-width: 80%; + height: auto; } } From 99048c4683c2df20dea2b3bf63a841801631697c Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 7 Apr 2024 19:51:20 -0400 Subject: [PATCH 3/4] Fix failing checks --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index da98706..ddae2c9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -895,7 +895,7 @@ pub fn rewrite_urls(input_text: &str) -> String { // Rewrite external media previews to Redlib loop { - if REDDIT_PREVIEW_REGEX.find(&text1).is_none() { + if REDDIT_PREVIEW_REGEX.find(&text1).is_none() { return text1; } else { let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default()); From 89140c8cf73659de9c43f060d463bfa11c953b6b Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 7 Apr 2024 19:59:54 -0400 Subject: [PATCH 4/4] Actually fix checks this time --- src/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index ddae2c9..63e679d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -895,14 +895,14 @@ pub fn rewrite_urls(input_text: &str) -> String { // Rewrite external media previews to Redlib loop { - if REDDIT_PREVIEW_REGEX.find(&text1).is_none() { + if REDDIT_PREVIEW_REGEX.find(&text1).is_none() { return text1; } else { let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default()); let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); let image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - + let image_to_replace = format!(">", ">"); let image_replacement = format!("");