From 0eb1197690ca814456a080f97935e698a0195067 Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Wed, 13 Nov 2024 13:18:08 -0500 Subject: [PATCH] Truncate long title in social image card (#21371) --- app/models/article.rb | 2 +- app/services/images/generate_social_image_magickally.rb | 1 + .../images/generate_social_image_magickally_spec.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/article.rb b/app/models/article.rb index 82a95bcfcb6aa..ff90b976aecce 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1060,7 +1060,7 @@ def set_nth_published_at end def title_to_slug - truncated_title = title[0..100].split[0...-1].join(" ") + truncated_title = title.size > 100 ? title[0..100].split[0...-1].join(" ") : title "#{Sterile.sluggerize(truncated_title)}-#{rand(100_000).to_s(26)}" end diff --git a/app/services/images/generate_social_image_magickally.rb b/app/services/images/generate_social_image_magickally.rb index ab1f9ad9f754d..ab4479534441a 100644 --- a/app/services/images/generate_social_image_magickally.rb +++ b/app/services/images/generate_social_image_magickally.rb @@ -91,6 +91,7 @@ def add_logo(result) end def add_text(result, title, date, author_name) + title = title.truncate(128) title = wrap_text(title) font_size = calculate_font_size(title) diff --git a/spec/services/images/generate_social_image_magickally_spec.rb b/spec/services/images/generate_social_image_magickally_spec.rb index 9eda3b6d60b9a..b54b8050c4bdd 100644 --- a/spec/services/images/generate_social_image_magickally_spec.rb +++ b/spec/services/images/generate_social_image_magickally_spec.rb @@ -173,6 +173,14 @@ generator.send(:add_text, result_image, "title", "date", "author_name") expect(result_image).to have_received(:combine_options).exactly(3).times end + + it "truncates text longer than 128" do + allow(generator).to receive(:wrap_text).and_return("whatever") + text = "This is a very long text that is definitely more than 128 characters long and should be wrapped over multiple lines. This is a very long text that is definitely more than 128 characters long and should be wrapped over multiple lines." + truncated_text = "This is a very long text that is definitely more than 128 characters long and should be wrapped over multiple lines. This is ..." + generator.send(:add_text, result_image, text, "date", "author_name") + expect(generator).to have_received(:wrap_text).with(truncated_text) + end end context "add_profile_image" do