FEATURE: Remove full quote only if first paragraph. (#6793)

approved
#1
FEATURE: Remove full quote only if first paragraph. (#6793)
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb
index 910f59e..a8ad6ad 100644
--- a/lib/cooked_post_processor.rb
+++ b/lib/cooked_post_processor.rb
@@ -95,7 +95,7 @@ class CookedPostProcessor
     prev = Post.where('post_number < ? AND topic_id = ? AND post_type = ? AND not hidden', @post.post_number, @post.topic_id, Post.types[:regular]).order('post_number desc').limit(1).pluck(:raw).first
     return if !prev
 
-    new_raw = @post.raw.gsub(/\[quote[^\]]*\]\s*#{Regexp.quote(prev.strip)}\s*\[\/quote\]/, '')
+    new_raw = @post.raw.gsub(/\A\s*\[quote[^\]]*\]\s*#{Regexp.quote(prev.strip)}\s*\[\/quote\]/, '')
     return if @post.raw == new_raw
 
     PostRevisor.new(@post).revise!(
diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb
index 8a84857..0c5d05c 100644
--- a/spec/components/cooked_post_processor_spec.rb
+++ b/spec/components/cooked_post_processor_spec.rb
@@ -1153,6 +1153,7 @@ describe CookedPostProcessor do
   context "remove direct reply full quote" do
     let(:topic) { Fabricate(:topic) }
     let!(:post) { Fabricate(:post, topic: topic, raw: "this is the first post") }
+
     let(:raw) do
       <<~RAW
       [quote="#{post.user.username}, post:#{post.post_number}, topic:#{topic.id}"]
@@ -1163,6 +1164,16 @@ describe CookedPostProcessor do
       RAW
     end
 
+    let(:raw2) do
+      <<~RAW
+      and this is the third reply
+
+      [quote="#{post.user.username}, post:#{post.post_number}, topic:#{topic.id}"]
+      this is the first post
+      [/quote]
+      RAW
+    end
+
     it 'works' do
       SiteSetting.remove_full_quote = true
 
@@ -1183,6 +1194,15 @@ describe CookedPostProcessor do
       end
     end
 
+    it 'does not delete quote if not first paragraph' do
+      SiteSetting.remove_full_quote = true
+
+      reply = Fabricate(:post, topic: topic, raw: raw2)
+      CookedPostProcessor.new(reply).removed_direct_reply_full_quotes
+      expect(topic.posts).to eq([post, reply])
+      expect(reply.raw).to eq(raw2)
+    end
+
     it "does nothing when 'remove_full_quote' is disabled" do
       SiteSetting.remove_full_quote = false

GitHub
sha: 825ae868

1 Like
Approved #2
#3

Hmm we added \s* into the regular expression but I don’t think any of the existing test cases covers it.

2 Likes
Follow Up #4
Approved #6
#7

This has been fixed in FIX: remove full quote on direct replies when "typographed".