From b7555441014a7c6469c36c5e4147349a97db02f4 Mon Sep 17 00:00:00 2001 From: Ariel Juodziukynas Date: Fri, 29 May 2026 15:06:00 -0300 Subject: [PATCH 1/2] IIRR-34: Fix newlines around codeblocks for Redcarpet renderer --- app/assets/stylesheets/application.scss | 4 ++++ app/helpers/application_helper.rb | 8 ++++++++ test/helpers/markdown_helper_test.rb | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 19c3649..64f7c57 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -125,6 +125,10 @@ td:last-child { width: 120px; } +td pre { + margin-bottom: 1rem; +} + /* Banner styles - using styleguide colors where possible */ .banner { position: fixed; diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3acfde9..e4608c4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -2,6 +2,14 @@ module ApplicationHelper def markdown(text) renderer = Redcarpet::Render::HTML.new(escape_html: true) markdown = Redcarpet::Markdown.new(renderer, fenced_code_blocks: true) + + # Redcarpet needs the codeblocks to have 2 newlines before them + # fixing this at render because it's not really required by Discord + # it's just a quirk of Redcarpet + if text.match?(/[^\n]\n```(.*)\n```/m) + text = text.gsub(/[^\n]\n```(.*)\n```/m, "\n\n```\\1\n```") + end + markdown.render(text.to_s).html_safe end end diff --git a/test/helpers/markdown_helper_test.rb b/test/helpers/markdown_helper_test.rb index bc0f8cb..878bc8b 100644 --- a/test/helpers/markdown_helper_test.rb +++ b/test/helpers/markdown_helper_test.rb @@ -24,4 +24,18 @@ class ACodeBlock assert_includes as_html, "
class ACodeBlock"
   end
+
+  test "fixes newlines around code blocks" do
+    puzzle_content = <<~CONTENT
+    Some content with
+    ```
+    class ACodeBlock
+    end
+    ```
+    in it
+    CONTENT
+    as_html = markdown(puzzle_content)
+
+    assert_includes as_html, "
class ACodeBlock"
+  end
 end

From 4f9ec6869e7fcd536449183c968f0b1b5729db99 Mon Sep 17 00:00:00 2001
From: Ariel Juodziukynas 
Date: Mon, 1 Jun 2026 09:43:29 -0300
Subject: [PATCH 2/2] IIRR-34: Fix bug with regexp that fixes newlines eating
 last character

---
 app/helpers/application_helper.rb    | 2 +-
 test/helpers/markdown_helper_test.rb | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index e4608c4..8c76b4b 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -7,7 +7,7 @@ def markdown(text)
     # fixing this at render because it's not really required by Discord
     # it's just a quirk of Redcarpet
     if text.match?(/[^\n]\n```(.*)\n```/m)
-      text = text.gsub(/[^\n]\n```(.*)\n```/m, "\n\n```\\1\n```")
+      text = text.gsub(/([^\n])\n```(.*?)\n```/m, "\\1\n\n```\\2\n```")
     end
 
     markdown.render(text.to_s).html_safe
diff --git a/test/helpers/markdown_helper_test.rb b/test/helpers/markdown_helper_test.rb
index 878bc8b..3b4b9d9 100644
--- a/test/helpers/markdown_helper_test.rb
+++ b/test/helpers/markdown_helper_test.rb
@@ -36,6 +36,6 @@ class ACodeBlock
     CONTENT
     as_html = markdown(puzzle_content)
 
-    assert_includes as_html, "
class ACodeBlock"
+    assert_includes as_html, "with

\n\n
class ACodeBlock"
   end
 end