From 15fcafc99aa23f3890765e2a4f0632bfcee219cc Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Thu, 17 Oct 2024 20:03:51 -0400 Subject: [PATCH 1/2] Fix images embedded by rewrite_urls() having an empty

above and below them that caused weird gaps in some scenarios --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 6f97775..9413085 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -951,7 +951,7 @@ pub fn rewrite_urls(input_text: &str) -> String { } // image_url contains > at the end of it, and right above this we remove image_text's front >, leaving us with just a single > between them - let image_to_replace = format!(""); + let image_to_replace = format!("

"); // _image_replacement needs to be in scope for the replacement at the bottom of the loop let mut _image_replacement = String::new(); From 10b32ed79289032d0617c989a51ca87729a1bd77 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Thu, 17 Oct 2024 21:16:03 -0400 Subject: [PATCH 2/2] Fix test for new embedding behavior --- src/utils.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 9413085..5d060d9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -909,7 +909,7 @@ pub fn format_url(url: &str) -> String { // These are links we want to replace in-body 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|i)\.redd\.it(.*)[^?]").unwrap()); +static REDDIT_PREVIEW_REGEX: Lazy = Lazy::new(|| Regex::new(r"https?://(external-preview|preview|i)\.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#"/(img|preview/)(pre|external-pre)?/(.*?)>"#).unwrap()); static REDLIB_PREVIEW_TEXT_REGEX: Lazy = Lazy::new(|| Regex::new(r">(.*?)").unwrap()); @@ -934,6 +934,8 @@ pub fn rewrite_urls(input_text: &str) -> String { // Remove (html-encoded) "\" from URLs. text1 = text1.replace("%5C", "").replace("\\_", "_"); + println!("text1 before: {text1}"); + // Rewrite external media previews to Redlib loop { if REDDIT_PREVIEW_REGEX.find(&text1).is_none() { @@ -1367,7 +1369,7 @@ async fn test_fetching_ws() { fn test_rewriting_image_links() { let input = r#"

caption 1

"#; - let output = r#"

caption 1
caption 1
"#; assert_eq!(rewrite_urls(input), output); }