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>
This commit is contained in:
parent
ed11135af8
commit
eb6c5e5e1e
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -540,7 +540,7 @@ checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libreddit"
|
name = "libreddit"
|
||||||
version = "0.22.4"
|
version = "0.22.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"askama",
|
"askama",
|
||||||
"async-recursion",
|
"async-recursion",
|
||||||
|
@ -3,7 +3,7 @@ name = "libreddit"
|
|||||||
description = " Alternative private front-end to Reddit"
|
description = " Alternative private front-end to Reddit"
|
||||||
license = "AGPL-3.0"
|
license = "AGPL-3.0"
|
||||||
repository = "https://github.com/spikecodes/libreddit"
|
repository = "https://github.com/spikecodes/libreddit"
|
||||||
version = "0.22.4"
|
version = "0.22.5"
|
||||||
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
|
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
|
|||||||
let body = if val(post, "removed_by_category") == "moderator" {
|
let body = if val(post, "removed_by_category") == "moderator" {
|
||||||
format!("<div class=\"md\"><p>[removed] — <a href=\"https://www.reveddit.com{}\">view removed post</a></p></div>", permalink)
|
format!("<div class=\"md\"><p>[removed] — <a href=\"https://www.reveddit.com{}\">view removed post</a></p></div>", permalink)
|
||||||
} else {
|
} else {
|
||||||
rewrite_urls(&val(post, "selftext_html")).replace("\\", "")
|
rewrite_urls(&val(post, "selftext_html"))
|
||||||
};
|
};
|
||||||
|
|
||||||
// Build a post using data parsed from Reddit post API
|
// 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]" {
|
let body = if val(&comment, "author") == "[deleted]" && val(&comment, "body") == "[removed]" {
|
||||||
format!("<div class=\"md\"><p>[removed] — <a href=\"https://www.reveddit.com{}{}\">view removed comment</a></p></div>", post_link, id)
|
format!("<div class=\"md\"><p>[removed] — <a href=\"https://www.reveddit.com{}{}\">view removed comment</a></p></div>", post_link, id)
|
||||||
} else {
|
} else {
|
||||||
rewrite_urls(&val(&comment, "body_html")).to_string()
|
rewrite_urls(&val(&comment, "body_html"))
|
||||||
};
|
};
|
||||||
|
|
||||||
let author = Author {
|
let author = Author {
|
||||||
|
@ -336,10 +336,10 @@ pub async fn sidebar(req: Request<Body>) -> Result<Response<Body>, String> {
|
|||||||
match json(path, quarantined).await {
|
match json(path, quarantined).await {
|
||||||
// If success, receive JSON in response
|
// If success, receive JSON in response
|
||||||
Ok(response) => template(WikiTemplate {
|
Ok(response) => template(WikiTemplate {
|
||||||
wiki: rewrite_urls(&val(&response, "description_html").replace("\\", "")),
|
wiki: rewrite_urls(&val(&response, "description_html")),
|
||||||
// wiki: format!(
|
// wiki: format!(
|
||||||
// "{}<hr><h1>Moderators</h1><br><ul>{}</ul>",
|
// "{}<hr><h1>Moderators</h1><br><ul>{}</ul>",
|
||||||
// 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(""),
|
// moderators(&sub, quarantined).await.unwrap_or(vec!["Could not fetch moderators".to_string()]).join(""),
|
||||||
// ),
|
// ),
|
||||||
sub,
|
sub,
|
||||||
@ -411,7 +411,7 @@ async fn subreddit(sub: &str, quarantined: bool) -> Result<Subreddit, String> {
|
|||||||
name: esc!(&res, "display_name"),
|
name: esc!(&res, "display_name"),
|
||||||
title: esc!(&res, "title"),
|
title: esc!(&res, "title"),
|
||||||
description: esc!(&res, "public_description"),
|
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(),
|
// moderators: moderators_list(sub, quarantined).await.unwrap_or_default(),
|
||||||
icon: format_url(&icon),
|
icon: format_url(&icon),
|
||||||
members: format_num(members),
|
members: format_num(members),
|
||||||
|
16
src/utils.rs
16
src/utils.rs
@ -607,8 +607,12 @@ pub fn format_url(url: &str) -> String {
|
|||||||
|
|
||||||
// Rewrite Reddit links to Libreddit in body of text
|
// Rewrite Reddit links to Libreddit in body of text
|
||||||
pub fn rewrite_urls(input_text: &str) -> String {
|
pub fn rewrite_urls(input_text: &str) -> String {
|
||||||
|
|
||||||
let text1 =
|
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
|
// Rewrite external media previews to Libreddit
|
||||||
Regex::new(r"https://external-preview\.redd\.it(.*)[^?]").map_or(String::new(), |re| {
|
Regex::new(r"https://external-preview\.redd\.it(.*)[^?]").map_or(String::new(), |re| {
|
||||||
@ -710,6 +714,7 @@ pub async fn error(req: Request<Body>, msg: String) -> Result<Response<Body>, St
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::format_num;
|
use super::format_num;
|
||||||
|
use super::rewrite_urls;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn format_num_works() {
|
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(1001), ("1.0k".to_string(), "1001".to_string()));
|
||||||
assert_eq!(format_num(1_999_999), ("2.0m".to_string(), "1999999".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#"<a href=\"https://www.reddit.com/r/linux%5C_gaming/comments/x/just%5C_a%5C_test%5C/\">https://www.reddit.com/r/linux\\_gaming/comments/x/just\\_a\\_test/</a>"#;
|
||||||
|
assert_eq!(
|
||||||
|
rewrite_urls(comment_body_html),
|
||||||
|
r#"<a href="https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/">https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/</a>"#
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user