Skip to content

Commit 7770c57

Browse files
fix Code blocks err #227 (#323)
* fix Code blocks #227 * add pulldown-cmark * add pulldown-cmark * fix Code blocks err #227 * add pre style for post codeblock * Update style.css (fix Code blocks err #227 ) --------- Co-authored-by: Matthew Esposito <matt@matthew.science>
1 parent c7f55c1 commit 7770c57

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Cargo.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ rss = "2.0.7"
4747
arc-swap = "1.7.1"
4848
serde_json_path = "0.7.1"
4949
async-recursion = "1.1.1"
50+
pulldown-cmark = { version = "0.12.0", features = ["simd", "html"], default-features = false }
5051
common-words-all = { version = "0.0.2", default-features = false, features = ["english", "one"] }
5152
hyper-rustls = { version = "0.24.2", features = [ "http2" ] }
5253
tegen = "0.1.4"

src/utils.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,15 @@ pub async fn parse_post(post: &Value) -> Post {
743743
get_setting("REDLIB_PUSHSHIFT_FRONTEND").unwrap_or_else(|| String::from(crate::config::DEFAULT_PUSHSHIFT_FRONTEND)),
744744
)
745745
} else {
746-
rewrite_urls(&val(post, "selftext_html"))
746+
let selftext = val(post, "selftext");
747+
if selftext.contains("```") {
748+
let mut html_output = String::new();
749+
let parser = pulldown_cmark::Parser::new(&selftext);
750+
pulldown_cmark::html::push_html(&mut html_output, parser);
751+
rewrite_urls(&html_output)
752+
} else {
753+
rewrite_urls(&val(post, "selftext_html"))
754+
}
747755
};
748756

749757
// Build a post using data parsed from Reddit post API

static/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,13 @@ a.search_subreddit:hover {
11991199
overflow-wrap: anywhere;
12001200
}
12011201

1202+
.post_body pre {
1203+
background: var(--background);
1204+
overflow-x: auto;
1205+
margin: 10px 0;
1206+
padding: 10px;
1207+
}
1208+
12021209
.post_body img {
12031210
max-width: 100%;
12041211
display: block;

0 commit comments

Comments
 (0)