From 4e29c6e32b5463bc637862b3ccc11ffd0de35d44 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Thu, 15 Oct 2026 16:59:25 -0700 Subject: [PATCH] Keep a code block's leading newline when linking a message's text A
 whose text starts with a newline needs one more written out to
survive being parsed again, as the html5 serializer does with
preserve_newline. Without it, linking text dropped one more of a code
block's leading blank lines than rendering did before.
---
 app/helpers/messages_helper.rb       | 3 ++-
 test/helpers/messages_helper_test.rb | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb
index 2b3faf6..687d9a4 100644
--- a/app/helpers/messages_helper.rb
+++ b/app/helpers/messages_helper.rb
@@ -130,7 +130,8 @@ module MessagesHelper
         end
       end
 
-      fragment.to_html.html_safe
+      # A 
 starting with a newline needs an extra one written out to survive the next parse
+      fragment.to_html(preserve_newline: true).html_safe
     end
 
     def messages_actions
diff --git a/test/helpers/messages_helper_test.rb b/test/helpers/messages_helper_test.rb
index 65830f1..911317a 100644
--- a/test/helpers/messages_helper_test.rb
+++ b/test/helpers/messages_helper_test.rb
@@ -58,6 +58,12 @@ class MessagesHelperTest < ActionView::TestCase
     end
   end
 
+  test "linking text doesn't eat a code block's leading blank line" do
+    linked = view.send(:auto_link_text, "
\n\n\nfirst https://example.com
".html_safe) + + assert_equal "\nfirst https://example.com", Nokogiri::HTML5.fragment(linked).at_css("pre").text + end + test "message_presentation leaves an existing link's text alone" do message = Message.create! room: rooms(:pets), body: '
https://example.org/
', client_message_id: "0015", creator: users(:jason)