From a39a86607770bc5ed971c81fd70c595c6dc5586b Mon Sep 17 00:00:00 2001 From: trichoplax Date: Thu, 30 Jul 2026 15:22:21 +0100 Subject: [PATCH 1/4] Normalize post body_markdown newlines --- app/models/post.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index 6b8f12b94..a94527d7c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -31,6 +31,10 @@ class Post < ApplicationRecord serialize :tags_cache, coder: YAML, type: Array + normalizes :body_markdown, with: -> body_markdown { + body_markdown.encode(body_markdown.encoding, universal_newline: true) + } + validates :body, presence: true, length: { maximum: 65_535 } validates :body_markdown, presence: true, length: { maximum: 30_000 } validates :doc_slug, uniqueness: { scope: [:community_id], case_sensitive: false }, if: -> { doc_slug.present? } From 940583daa20f253fd98eee7b1e3f92ba46da8f23 Mon Sep 17 00:00:00 2001 From: trichoplax Date: Thu, 30 Jul 2026 15:33:54 +0100 Subject: [PATCH 2/4] Rubocop improvements to lambda --- app/models/post.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index a94527d7c..60a4505d7 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -31,7 +31,7 @@ class Post < ApplicationRecord serialize :tags_cache, coder: YAML, type: Array - normalizes :body_markdown, with: -> body_markdown { + normalizes :body_markdown, with: lambda { |body_markdown| body_markdown.encode(body_markdown.encoding, universal_newline: true) } From 2f4b674e5f590e1d534608b5dba71e717129acbf Mon Sep 17 00:00:00 2001 From: trichoplax Date: Thu, 30 Jul 2026 16:13:51 +0100 Subject: [PATCH 3/4] Add tests for body_markdown after normalizing newlines --- test/models/post_test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/models/post_test.rb b/test/models/post_test.rb index f332c1cfb..5d9006bb5 100644 --- a/test/models/post_test.rb +++ b/test/models/post_test.rb @@ -100,7 +100,9 @@ class PostTest < ActiveSupport::TestCase ['# Heading', '

Heading

', true], ['a' * 30_000, 'a' * 65_535, true], ['a' * 30_001, '

body_markdown is too long

', false], - ['body is too long', 'a' * 65_536, false] + ['body is too long', 'a' * 65_536, false], + ["a\r\n" * 15_000, '

body_markdown is too long before converting newlines to \n

', true], + ["a\r\n" * 15_000 + 'a', '

body_markdown is too long even after converting newlines to \n

', false] ].each do |test_case| post = Post.new(shared_params.merge({ body_markdown: test_case.first, body: test_case.second })) From 2e290c300fadd1f9a2e8ba6e1f7fa98a451bd74e Mon Sep 17 00:00:00 2001 From: trichoplax Date: Thu, 30 Jul 2026 16:19:43 +0100 Subject: [PATCH 4/4] Rubocop improvements to string construction --- test/models/post_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/models/post_test.rb b/test/models/post_test.rb index 5d9006bb5..dc3148b54 100644 --- a/test/models/post_test.rb +++ b/test/models/post_test.rb @@ -102,7 +102,7 @@ class PostTest < ActiveSupport::TestCase ['a' * 30_001, '

body_markdown is too long

', false], ['body is too long', 'a' * 65_536, false], ["a\r\n" * 15_000, '

body_markdown is too long before converting newlines to \n

', true], - ["a\r\n" * 15_000 + 'a', '

body_markdown is too long even after converting newlines to \n

', false] + ["#{"a\r\n" * 15_000}a", '

body_markdown is too long even after converting newlines to \n

', false] ].each do |test_case| post = Post.new(shared_params.merge({ body_markdown: test_case.first, body: test_case.second }))