]> git.openstreetmap.org Git - rails.git/commitdiff
Set og:description meta tag to diary entry description
authorAnton Khorev <tony29@yandex.ru>
Wed, 7 Aug 2024 05:33:41 +0000 (08:33 +0300)
committerAnton Khorev <tony29@yandex.ru>
Wed, 7 Aug 2024 15:59:16 +0000 (18:59 +0300)
app/controllers/diary_entries_controller.rb
app/helpers/open_graph_helper.rb
test/controllers/diary_entries_controller_test.rb

index 1f47e25880e38f74149fa48b3036aa6ae423958e..a8fc4180835d881745108263787d62d12faf7af4 100644 (file)
@@ -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
index dbd20446295d76a556d10a28481e26eb40a7e039..b3f0ab92849e81be230992c0e3ff1f524258cc64 100644 (file)
@@ -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)
     )
index e3bbb490dfdf3c56a4d566647405379dd7db5571..1ea183400c2ea549f5bf629cd1b69c7e596ee21f 100644 (file)
@@ -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)