From dc11cc6e18ef8e0a2c961759bc54faa22d8c6fd2 Mon Sep 17 00:00:00 2001 From: Aidan Cornelius-Bell Date: Sat, 18 Jan 2025 12:39:29 +1030 Subject: [PATCH] Fixed the post function for dispatch summaries (up to 950 words) --- app/models/post.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 6a54d0c..e771914 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -66,12 +66,25 @@ class Post < ApplicationRecord tags.split(/\s*(?:,|\s+and)\s*/).map { |tag| "#{tag.strip}" }.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 -- 2.39.5