Skip to content

Commit

Permalink
Merge pull request redlib-org#88 from ButteredCats/better_preview_han…
Browse files Browse the repository at this point in the history
…dling

Handle preview embedding better
  • Loading branch information
sigaloid authored Apr 8, 2024
2 parents 7c87d63 + 89140c8 commit 27f25e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,8 @@ pub fn format_url(url: &str) -> String {
static REDDIT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|new\.|)(reddit\.com|redd\.it)/"#).unwrap());
static REDDIT_PREVIEW_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"https?://(external-preview|preview)\.redd\.it(.*)[^?]").unwrap());
static REDDIT_EMOJI_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"https?://(www|).redditstatic\.com/(.*)").unwrap());
static REDLIB_PREVIEW_LINK_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#"/preview/(pre|external-pre)/(.*)">"#).unwrap());
static REDLIB_PREVIEW_TEXT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r">(.*)</a>").unwrap());
static REDLIB_PREVIEW_LINK_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#"/preview/(pre|external-pre)/(.*?)>"#).unwrap());
static REDLIB_PREVIEW_TEXT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r">(.*?)</a>").unwrap());

// Rewrite Reddit links to Redlib in body of text
pub fn rewrite_urls(input_text: &str) -> String {
Expand All @@ -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();

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!("><img src=\"{image_url}</a>"))
.to_string()
}
let image_to_replace = format!("<a href=\"{image_url}{image_text}").replace(">>", ">");
let image_replacement = format!("<a href=\"{image_url}<img src=\"{image_url}</a>");

text1 = REDDIT_PREVIEW_REGEX
.replace(&text1, formatted_url)
.replace(&image_to_replace, &image_replacement)
.to_string()
}
} else {
text1
}
}

Expand Down
2 changes: 2 additions & 0 deletions static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 27f25e0

Please sign in to comment.