diff --git a/app/models/post.rb b/app/models/post.rb index 6b8f12b94..60a4505d7 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: lambda { |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? } diff --git a/test/models/post_test.rb b/test/models/post_test.rb index f332c1cfb..dc3148b54 100644 --- a/test/models/post_test.rb +++ b/test/models/post_test.rb @@ -100,7 +100,9 @@ class PostTest < ActiveSupport::TestCase ['# Heading', '
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 }))