Repository: redlib-org/redlib (language: rust)

Issue:
🐛 Bug Report: Code blocks don't render inside post
## Describe the bug

Code blocks don't render inside post but work inside comments

## Steps to reproduce the bug

Steps to reproduce the behavior:
1. Go to https://www.reddit.com/r/commandline/comments/1fe8uwc/want_to_share_how_i_refer_previous_command_i_use/
4. See code blocks not redering inside post but comments code block work


## Additional context / screenshot

![2024-09-14_15-11-46](https://github.com/user-attachments/assets/744d8bc2-8847-41ae-8c4f-f225529cc12f)



Discussion / hints:
it is not just in posts, all code blocks using ` ``` ` do not work.

https://support.reddithelp.com/hc/en-us/articles/360043033952-Formatting-Guide#wiki_unfortunate_compatibility_recommendations
> Old Reddit only supports indented code blocks

https://www.reddit.com/r/commandline/comments/1fe8uwc/want_to_share_how_i_refer_previous_command_i_use/.json?&raw_json=1

See `selftext_html` field
Maybe we should render the markdown ourselves. I’m not sure whether we should render everything ourselves or just render the text that contains ``` blocks. What do you think? @sigaloid 

selftext_html

```html
<!-- SC_OFF --><div class="md"><p>This was a fun one that bit our open source project today...a log::error! macro in an enum that recursively printed itself.</p>

<p>Be careful out there println!ers 😂</p>

<p><a href="https://preview.redd.it/f7ikci93sx0e1.png?width=2016&amp;format=png&amp;auto=webp&amp;s=9b7ca26ae2f9f71b8b1960423caac2489c5f38ec">https://preview.redd.it/f7ikci93sx0e1.png?width=2016&amp;format=png&amp;auto=webp&amp;s=9b7ca26ae2f9f71b8b1960423caac2489c5f38ec</a></p>
</div><!-- SC_ON -->
text: This was a fun one that bit our open source project today...a log::error! macro in an enum that recursively printed itself.

Be careful out there println!ers 😂

https://preview.redd.it/f7ikci93sx0e1.png?width=2016&format=png&auto=webp&s=9b7ca26ae2f9f71b8b1960423caac2489c5f38ec

  

html: <!-- SC_OFF --><div class="md"><p>Despite the use of caching, my compile/test steps takes several minutes to execute, even if zero changes exist between runs.</p>

<p>```yaml
version: 2.1</p>

<p>commands:
  restore_cache_cmd:
    steps:
      - restore_cache:
          key: cache_cmd-v1-{{ arch }}-{{ checksum &quot;Cargo.lock&quot; }}</p>

<p>save_cache_cmd:
    steps:
      - save_cache:
          key: cache_cmd-v1-{{ arch }}-{{ checksum &quot;Cargo.lock&quot; }}
          paths:
            - &quot;~/.cargo&quot;
            - &quot;./target&quot;</p>

<p>jobs:
  # Precompile all the rust dependencies
  compile:
    executor: nix-executor
    steps:
      - restore_cache_cmd
      - run:
          name: Compile App
          command: |
            cargo zigbuild --target x86_64-unknown-linux-musl --bin server --release
      - save_cache_cmd
```</p>

<p>Critically, I see that the deps are not compiled but the binaries takes in this step like 6 min.</p>
</div><!-- SC_ON -->
```

selftext: 

````
Despite the use of caching, my compile/test steps takes several minutes to execute, even if zero changes exist between runs.


```yaml
version: 2.1

commands:
  restore_cache_cmd:
    steps:
      - restore_cache:
          key: cache_cmd-v1-{{ arch }}-{{ checksum "Cargo.lock" }}

  save_cache_cmd:
    steps:
      - save_cache:
          key: cache_cmd-v1-{{ arch }}-{{ checksum "Cargo.lock" }}
          paths:
            - "~/.cargo"
            - "./target"

jobs:
  # Precompile all the rust dependencies
  compile:
    executor: nix-executor
    steps:
      - restore_cache_cmd
      - run:
          name: Compile App
          command: |
            cargo zigbuild --target x86_64-unknown-linux-musl --bin server --release
      - save_cache_cmd
```

Critically, I see that the deps are not compiled but the binaries takes in this step like 6 min.
````

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index 24791b44..6578d178 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1302,6 +1302,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"
@@ -1383,6 +1401,7 @@ dependencies = [
  "once_cell",
  "percent-encoding",
  "pretty_env_logger",
+ "pulldown-cmark",
  "regex",
  "rinja",
  "route-recognizer",
diff --git a/Cargo.toml b/Cargo.toml
index 843b9c9b..fc9074b5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -47,6 +47,7 @@ rss = "2.0.7"
 arc-swap = "1.7.1"
 serde_json_path = "0.7.1"
 async-recursion = "1.1.1"
+pulldown-cmark = { version = "0.12.0", features = ["simd", "html"], default-features = false }
 common-words-all = { version = "0.0.2", default-features = false, features = ["english", "one"] }
 hyper-rustls = { version = "0.24.2", features = [ "http2" ] }
 tegen = "0.1.4"
diff --git a/src/utils.rs b/src/utils.rs
index 23db3d0f..0aa3159a 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -743,7 +743,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
diff --git a/static/style.css b/static/style.css
index a9d893a3..76b55ddb 100644
--- a/static/style.css
+++ b/static/style.css
@@ -1199,6 +1199,13 @@ a.search_subreddit:hover {
     overflow-wrap: anywhere;
 }
 
+.post_body pre {
+    background: var(--background);
+    overflow-x: auto;
+    margin: 10px 0;
+    padding: 10px;
+}
+
 .post_body img {
     max-width: 100%;
     display: block;


Test patch: