Skip to content

Commit

Permalink
Truncate long title in social image card (forem#21371)
Browse files Browse the repository at this point in the history
  • Loading branch information
benhalpern authored Nov 13, 2024
1 parent efd41d0 commit 0eb1197
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/services/images/generate_social_image_magickally.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions spec/services/images/generate_social_image_magickally_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0eb1197

Please sign in to comment.