]> gitweb.mndrdr.org Git - arelpe.git/commitdiff
Fixed the post function for dispatch summaries (up to 950 words)
authorAidan Cornelius-Bell <[email protected]>
Sat, 18 Jan 2025 02:09:29 +0000 (12:39 +1030)
committerAidan Cornelius-Bell <[email protected]>
Sat, 18 Jan 2025 02:09:29 +0000 (12:39 +1030)
app/models/post.rb

index 6a54d0c12fb64f21638793ad2d20cd7993099abe..e771914adafef3d5c23e1e95a806324ddc74c401 100644 (file)
@@ -66,12 +66,25 @@ class Post < ApplicationRecord
     tags.split(/\s*(?:,|\s+and)\s*/).map { |tag| "<code>#{tag.strip}</code>" }.join(', ') + '.'
   end
 
-  def generate_excerpt(max_length = 210)
+  def generate_excerpt(max_length = 950)
     return "" if content.blank?
-
     stripped_content = ActionController::Base.helpers.strip_tags(content)
-    excerpt = stripped_content.split('.').first || stripped_content[0...max_length]
-    excerpt = excerpt[0...max_length] if excerpt.length > max_length
+
+    # Split by double line breaks and get first substantive paragraph
+    paragraphs = stripped_content.split(/\r\n\r\n/)
+
+    # Skip the "Dear friends" paragraph and get the next one
+    excerpt = paragraphs.drop(1).first || stripped_content[0...max_length]
+
+    # If we need to trim, find the last complete sentence within max_length
+    if excerpt.length > max_length
+      excerpt = excerpt[0...max_length]
+      # Find the last period that's followed by a space or end of string
+      last_sentence = excerpt.rindex(/\.\s|\.$/)
+      excerpt = excerpt[0..last_sentence] if last_sentence
+    end
+
+    # Clean up the text
     excerpt.gsub!("Dear friends,", "")
     excerpt.gsub!(/\s+/, ' ')
     excerpt.strip