From eb6c5e5e1ef2843b09269e23e2f0118813994a02 Mon Sep 17 00:00:00 2001
From: mikupls <93015331+mikupls@users.noreply.github.com>
Date: Sat, 26 Mar 2022 21:26:30 +0100
Subject: [PATCH] Fix backslash url rewriting and add tests for rewrite_urls.
(#461)
* Fix backslash url rewriting.
Add test for rewrite_urls.
Fixes #281.
* Update to v0.22.5
Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com>
---
Cargo.lock | 2 +-
Cargo.toml | 2 +-
src/post.rs | 4 ++--
src/subreddit.rs | 6 +++---
src/utils.rs | 16 +++++++++++++++-
5 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index f7368d4..5e14354 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -540,7 +540,7 @@ checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f"
[[package]]
name = "libreddit"
-version = "0.22.4"
+version = "0.22.5"
dependencies = [
"askama",
"async-recursion",
diff --git a/Cargo.toml b/Cargo.toml
index d51e7a0..ef18b8e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,7 +3,7 @@ name = "libreddit"
description = " Alternative private front-end to Reddit"
license = "AGPL-3.0"
repository = "https://github.com/spikecodes/libreddit"
-version = "0.22.4"
+version = "0.22.5"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2021"
diff --git a/src/post.rs b/src/post.rs
index 857b0b4..2ebc313 100644
--- a/src/post.rs
+++ b/src/post.rs
@@ -102,7 +102,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
let body = if val(post, "removed_by_category") == "moderator" {
format!("
", permalink)
} else {
- rewrite_urls(&val(post, "selftext_html")).replace("\\", "")
+ rewrite_urls(&val(post, "selftext_html"))
};
// Build a post using data parsed from Reddit post API
@@ -201,7 +201,7 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str,
let body = if val(&comment, "author") == "[deleted]" && val(&comment, "body") == "[removed]" {
format!("", post_link, id)
} else {
- rewrite_urls(&val(&comment, "body_html")).to_string()
+ rewrite_urls(&val(&comment, "body_html"))
};
let author = Author {
diff --git a/src/subreddit.rs b/src/subreddit.rs
index dde8fb8..141c011 100644
--- a/src/subreddit.rs
+++ b/src/subreddit.rs
@@ -336,10 +336,10 @@ pub async fn sidebar(req: Request) -> Result, String> {
match json(path, quarantined).await {
// If success, receive JSON in response
Ok(response) => template(WikiTemplate {
- wiki: rewrite_urls(&val(&response, "description_html").replace("\\", "")),
+ wiki: rewrite_urls(&val(&response, "description_html")),
// wiki: format!(
// "{}
Moderators
",
- // rewrite_urls(&val(&response, "description_html").replace("\\", "")),
+ // rewrite_urls(&val(&response, "description_html"),
// moderators(&sub, quarantined).await.unwrap_or(vec!["Could not fetch moderators".to_string()]).join(""),
// ),
sub,
@@ -411,7 +411,7 @@ async fn subreddit(sub: &str, quarantined: bool) -> Result {
name: esc!(&res, "display_name"),
title: esc!(&res, "title"),
description: esc!(&res, "public_description"),
- info: rewrite_urls(&val(&res, "description_html").replace("\\", "")),
+ info: rewrite_urls(&val(&res, "description_html")),
// moderators: moderators_list(sub, quarantined).await.unwrap_or_default(),
icon: format_url(&icon),
members: format_num(members),
diff --git a/src/utils.rs b/src/utils.rs
index 8558fd7..56a7de7 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -607,8 +607,12 @@ pub fn format_url(url: &str) -> String {
// Rewrite Reddit links to Libreddit in body of text
pub fn rewrite_urls(input_text: &str) -> String {
+
let text1 =
- Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|)(reddit\.com|redd\.it)/"#).map_or(String::new(), |re| re.replace_all(input_text, r#"href="/"#).to_string());
+ Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|)(reddit\.com|redd\.it)/"#)
+ .map_or(String::new(), |re| re.replace_all(input_text, r#"href="/"#).to_string())
+ // Remove (html-encoded) "\" from URLs.
+ .replace("%5C", "").replace(r"\", "");
// Rewrite external media previews to Libreddit
Regex::new(r"https://external-preview\.redd\.it(.*)[^?]").map_or(String::new(), |re| {
@@ -710,6 +714,7 @@ pub async fn error(req: Request, msg: String) -> Result, St
#[cfg(test)]
mod tests {
use super::format_num;
+ use super::rewrite_urls;
#[test]
fn format_num_works() {
@@ -719,4 +724,13 @@ mod tests {
assert_eq!(format_num(1001), ("1.0k".to_string(), "1001".to_string()));
assert_eq!(format_num(1_999_999), ("2.0m".to_string(), "1999999".to_string()));
}
+
+ #[test]
+ fn rewrite_urls_removes_backslashes() {
+ let comment_body_html = r#"https://www.reddit.com/r/linux\\_gaming/comments/x/just\\_a\\_test/"#;
+ assert_eq!(
+ rewrite_urls(comment_body_html),
+ r#"https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/"#
+ )
+ }
}