From 6d83b07aaa9db110b5e1ff610e3cfa1d0e2d257e Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Tue, 9 Apr 2024 18:33:13 -0400 Subject: [PATCH] Update embedding Reddit preview links to include captions where applicable --- src/utils.rs | 26 +++++++++++++++++++++----- static/style.css | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 63e679d..e0ce24c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -901,14 +901,30 @@ pub fn rewrite_urls(input_text: &str) -> String { 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 mut image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); + if !image_text.is_empty() { + image_text.remove(0); + image_text.pop(); + image_text.pop(); + image_text.pop(); + image_text.pop(); + } - let image_to_replace = format!(">", ">"); - let image_replacement = format!(""); + let image_to_replace = format!("

"); + + image_text = image_text.replace("\\"", "\""); + + let mut _image_replacement = String::new(); + + if REDDIT_PREVIEW_REGEX.find(&image_text).is_none() { + _image_replacement = format!("
{image_text}
"); + } else { + _image_replacement = format!("
"); + } text1 = REDDIT_PREVIEW_REGEX .replace(&text1, formatted_url) - .replace(&image_to_replace, &image_replacement) + .replace(&image_to_replace, &_image_replacement) .to_string() } } @@ -1169,6 +1185,6 @@ fn test_rewriting_image_links() {

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

caption 1

caption 2

"#; - let output = r#"

"#; + let output = r#"
caption 1
caption 2
"#; assert_eq!(rewrite_urls(input), output); } diff --git a/static/style.css b/static/style.css index a85ab75..6c6a546 100644 --- a/static/style.css +++ b/static/style.css @@ -187,6 +187,15 @@ nav #redlib { vertical-align: -2px; } +figure { + margin: 0; +} + +figcaption { + margin-top: 5px; + text-align: center; +} + #settings_link { opacity: 0.8; margin-left: 10px; @@ -979,10 +988,6 @@ a.search_subreddit:hover { vertical-align: bottom; } -.gallery figcaption { - margin-top: 5px; -} - .gallery .outbound_url { color: var(--accent); text-overflow: ellipsis; @@ -1010,6 +1015,9 @@ a.search_subreddit:hover { .post_body img { max-width: 100%; + display: block; + margin-left: auto; + margin-right: auto; } .post_poll {