From d54ec3f801793eb149054d49f237205d419c7fcc Mon Sep 17 00:00:00 2001 From: freedit-dev Date: Fri, 15 Nov 2024 15:57:38 +0800 Subject: [PATCH 1/6] fix Code blocks https://github.com/redlib-org/redlib/issues/227 --- src/utils.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 170f53db..72ef56d6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -376,7 +376,17 @@ impl Post { let awards = Awards::parse(&data["all_awardings"]); // selftext_html is set for text posts when browsing. - let mut body = rewrite_urls(&val(post, "selftext_html")); + let mut body = { + let selftext = val(post, "selftext"); + if selftext.contains("```") { + let mut html_output = String::new(); + let parser = pulldown_cmark::Parser::new(&selftext); + pulldown_cmark::html::push_html(&mut html_output, parser); + rewrite_urls(&html_output) + } else { + rewrite_urls(&val(post, "selftext_html")) + } + }; if body.is_empty() { body = rewrite_urls(&val(post, "body_html")); } From 3d4a32dd82b93c5282fb4e81fd0870f9f0b7ede3 Mon Sep 17 00:00:00 2001 From: freedit-dev Date: Fri, 15 Nov 2024 16:04:09 +0800 Subject: [PATCH 2/6] add pulldown-cmark --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 2fe14563..c161c28d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ rss = "2.0.7" arc-swap = "1.7.1" serde_json_path = "0.6.7" async-recursion = "1.1.1" +pulldown-cmark = { version = "0.12.0", features = ["simd", "html"], default-features = false } [dev-dependencies] From 3f578142425d815171bf83fc546379a975e59e3d Mon Sep 17 00:00:00 2001 From: freedit-dev Date: Fri, 15 Nov 2024 16:04:46 +0800 Subject: [PATCH 3/6] add pulldown-cmark --- Cargo.lock | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index d4a1a057..6de21fe5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1099,6 +1099,24 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pulldown-cmark" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f86ba2052aebccc42cbbb3ed234b8b13ce76f75c3551a303cb2bcffcff12bb14" +dependencies = [ + "bitflags", + "memchr", + "pulldown-cmark-escape", + "unicase", +] + +[[package]] +name = "pulldown-cmark-escape" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae" + [[package]] name = "quick-error" version = "1.2.3" @@ -1172,6 +1190,7 @@ dependencies = [ "once_cell", "percent-encoding", "pretty_env_logger", + "pulldown-cmark", "regex", "rinja", "route-recognizer", From 19914b7f20b889ffacf756aa3f3ab8311ee5fc52 Mon Sep 17 00:00:00 2001 From: freedit-dev Date: Fri, 15 Nov 2024 08:57:06 +0000 Subject: [PATCH 4/6] fix Code blocks err #227 --- src/utils.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 72ef56d6..46095b29 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -376,17 +376,7 @@ impl Post { let awards = Awards::parse(&data["all_awardings"]); // selftext_html is set for text posts when browsing. - let mut body = { - let selftext = val(post, "selftext"); - if selftext.contains("```") { - let mut html_output = String::new(); - let parser = pulldown_cmark::Parser::new(&selftext); - pulldown_cmark::html::push_html(&mut html_output, parser); - rewrite_urls(&html_output) - } else { - rewrite_urls(&val(post, "selftext_html")) - } - }; + let mut body = rewrite_urls(&val(post, "selftext_html")); if body.is_empty() { body = rewrite_urls(&val(post, "body_html")); } @@ -730,7 +720,15 @@ pub async fn parse_post(post: &Value) -> Post { get_setting("REDLIB_PUSHSHIFT_FRONTEND").unwrap_or_else(|| String::from(crate::config::DEFAULT_PUSHSHIFT_FRONTEND)), ) } else { - rewrite_urls(&val(post, "selftext_html")) + let selftext = val(post, "selftext"); + if selftext.contains("```") { + let mut html_output = String::new(); + let parser = pulldown_cmark::Parser::new(&selftext); + pulldown_cmark::html::push_html(&mut html_output, parser); + rewrite_urls(&html_output) + } else { + rewrite_urls(&val(post, "selftext_html")) + } }; // Build a post using data parsed from Reddit post API From 4b1543e98dbf0a6f91a932239ef1cc32cfd5c638 Mon Sep 17 00:00:00 2001 From: freedit-dev Date: Fri, 15 Nov 2024 17:38:02 +0800 Subject: [PATCH 5/6] add pre style for post codeblock --- static/style.css | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/static/style.css b/static/style.css index a9d893a3..d53e5c92 100644 --- a/static/style.css +++ b/static/style.css @@ -1199,6 +1199,12 @@ a.search_subreddit:hover { overflow-wrap: anywhere; } +.post_body pre { + background: var(--background); + overflow-x: auto; + margin: 10px 0; +} + .post_body img { max-width: 100%; display: block; From b88efa105308458ef3ccf9c918082466f73ee8c7 Mon Sep 17 00:00:00 2001 From: freedit-dev Date: Fri, 15 Nov 2024 23:10:00 +0800 Subject: [PATCH 6/6] Update style.css (fix Code blocks err #227 ) --- static/style.css | 1 + 1 file changed, 1 insertion(+) diff --git a/static/style.css b/static/style.css index d53e5c92..76b55ddb 100644 --- a/static/style.css +++ b/static/style.css @@ -1203,6 +1203,7 @@ a.search_subreddit:hover { background: var(--background); overflow-x: auto; margin: 10px 0; + padding: 10px; } .post_body img {