From: Anton Khorev Date: Wed, 7 Aug 2024 05:33:41 +0000 (+0300) Subject: Set og:description meta tag to diary entry description X-Git-Tag: live~326^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/5f4ae358140bbfce2c5648a556aeb7a55f89536e Set og:description meta tag to diary entry description --- diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index 1f47e2588..a8fc41808 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -70,7 +70,8 @@ class DiaryEntriesController < ApplicationController @title = t ".title", :user => params[:display_name], :title => @entry.title @opengraph_properties = { "og:image" => @entry.body.image, - "og:image:alt" => @entry.body.image_alt + "og:image:alt" => @entry.body.image_alt, + "og:description" => @entry.body.description } @comments = can?(:unhide, DiaryComment) ? @entry.comments : @entry.visible_comments else diff --git a/app/helpers/open_graph_helper.rb b/app/helpers/open_graph_helper.rb index dbd204462..b3f0ab928 100644 --- a/app/helpers/open_graph_helper.rb +++ b/app/helpers/open_graph_helper.rb @@ -7,7 +7,7 @@ module OpenGraphHelper "og:title" => title || t("layouts.project_name.title"), "og:type" => "website", "og:url" => url_for(:only_path => false), - "og:description" => t("layouts.intro_text") + "og:description" => properties["og:description"] || t("layouts.intro_text") }.merge( opengraph_image_properties(properties) ) diff --git a/test/controllers/diary_entries_controller_test.rb b/test/controllers/diary_entries_controller_test.rb index e3bbb490d..1ea183400 100644 --- a/test/controllers/diary_entries_controller_test.rb +++ b/test/controllers/diary_entries_controller_test.rb @@ -744,6 +744,28 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_dom "head meta[property='og:image:alt']", :count => 0 end + def test_show_no_og_description + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "![nope](https://example.com/nope.jpg)") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:description']" do + assert_dom "> @content", I18n.t("layouts.intro_text") + end + end + + def test_show_og_description + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "# Hello\n\n![hello](https://example.com/hello.jpg)\n\nFirst paragraph.\n\nSecond paragraph.") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:description']" do + assert_dom "> @content", "First paragraph." + end + end + def test_hide user = create(:user) diary_entry = create(:diary_entry, :user => user)