diff --git a/src/utils.rs b/src/utils.rs index 6f70fa5..d65478a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -903,12 +903,16 @@ pub fn rewrite_urls(input_text: &str) -> String { let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); let mut image_caption = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - /* Remove first and last four characters of image_text to leave us with just the text in the caption without any HTML. + /* As long as image_caption isn't empty remove first and last four characters of image_text to leave us with just the text in the caption without any HTML. This makes it possible to enclose it in a
later on without having stray HTML breaking it */ - image_caption = image_caption[1..image_caption.len() - 4].to_string(); + if !image_caption.is_empty() { + image_caption = image_caption[1..image_caption.len() - 4].to_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!(""); + + //println!("{}", image_to_replace); // _image_replacement needs to be in scope for the replacement at the bottom of the loop let mut _image_replacement = String::new(); @@ -928,6 +932,7 @@ pub fn rewrite_urls(input_text: &str) -> String { text1 = REDDIT_PREVIEW_REGEX .replace(&text1, formatted_url) .replace(&image_to_replace, &_image_replacement) + .replace("

", "") .to_string() } } @@ -1183,11 +1188,7 @@ async fn test_fetching_ws() { #[test] fn test_rewriting_image_links() { - let input = r#"

https://preview.redd.it/zq21ggkj2xo31.png?width=2560&format=png&auto=webp&s=539d8050628ec1190cac26468fe99cc66b6071ab

-

https://preview.redd.it/vty9ocij2xo31.png?width=2560&format=png&auto=webp&s=fc7c7ef993a5e9ef656d5f5d9cf8290a0a1df877

-

https://preview.redd.it/bdfdxkjj2xo31.png?width=2560&format=png&auto=webp&s=d0fa420ece27605e882e89cb4711d75d774322ac

-

caption 1

-

caption 2

"#; - let output = r#"
caption 1
caption 2
"#; + let input = r#"

caption 1

"#; + let output = r#"

caption 1

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