From 097a414b22a9068cd9af6ec758eb2876da3dd654 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 05:27:28 +0300 Subject: [PATCH 1/9] Add a blank richtext description method --- lib/rich_text.rb | 4 ++++ test/lib/rich_text_test.rb | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index a439342f7..f554e5ab5 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -57,6 +57,10 @@ module RichText nil end + def description + nil + end + protected def simple_format(text) diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index e0b315276..3e17cac7b 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -250,16 +250,18 @@ class RichTextTest < ActiveSupport::TestCase assert_equal 141, r.spam_score.round end - def test_text_no_image + def test_text_no_opengraph_properties r = RichText.new("text", "foo https://example.com/ bar") assert_nil r.image assert_nil r.image_alt + assert_nil r.description end - def test_html_no_image + def test_html_no_opengraph_properties r = RichText.new("html", "foo bar baz") assert_nil r.image assert_nil r.image_alt + assert_nil r.description end def test_markdown_no_image @@ -328,6 +330,11 @@ class RichTextTest < ActiveSupport::TestCase assert_equal "have src", r.image_alt end + def test_markdown_no_description + r = RichText.new("markdown", "#Nope") + assert_nil r.description + end + private def assert_html(richtext, &block) From aa8dd75e5ec959e377f469a83bf67898472a67e5 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 06:06:10 +0300 Subject: [PATCH 2/9] Use first paragraph as richtext description --- lib/rich_text.rb | 33 +++++++++++++++++++++++++++++++++ test/lib/rich_text_test.rb | 15 +++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index f554e5ab5..b720f33e7 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -109,6 +109,11 @@ module RichText @image_element.attr["alt"] if @image_element end + def description + @paragraph_element = first_paragraph_element(document.root) unless defined? @paragraph_element + text_content(@paragraph_element) if @paragraph_element + end + private def document @@ -124,9 +129,37 @@ module RichText end end + def first_paragraph_element(element) + return element if paragraph?(element) + + element.children.find do |child| + nested_paragraph = first_paragraph_element(child) + break nested_paragraph if nested_paragraph + end + end + + def text_content(element) + text = "" + + append_text = lambda do |child| + if child.type == :text + text << child.value + else + child.children.each { |c| append_text.call(c) } + end + end + append_text.call(element) + + text + end + def image?(element) element.type == :img || (element.type == :html_element && element.value == "img") end + + def paragraph?(element) + element.type == :p + end end class Text < Base diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index 3e17cac7b..3536db398 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -335,6 +335,21 @@ class RichTextTest < ActiveSupport::TestCase assert_nil r.description end + def test_markdown_description + r = RichText.new("markdown", "This is an article about something.") + assert_equal "This is an article about something.", r.description + end + + def test_markdown_description_after_heading + r = RichText.new("markdown", "#Heading\n\nHere starts the text.") + assert_equal "Here starts the text.", r.description + end + + def test_markdown_description_elements + r = RichText.new("markdown", "*Something* **important** [here](https://example.com/).") + assert_equal "Something important here.", r.description + end + private def assert_html(richtext, &block) From e5279dacde1f53db0d94a1bca2ab4ff45ed5ceec Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 06:09:38 +0300 Subject: [PATCH 3/9] Detect

as richtext paragraph --- lib/rich_text.rb | 2 +- test/lib/rich_text_test.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index b720f33e7..b0b83cf5a 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -158,7 +158,7 @@ module RichText end def paragraph?(element) - element.type == :p + element.type == :p || (element.type == :html_element && element.value == "p") end end diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index 3536db398..929abf823 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -350,6 +350,11 @@ class RichTextTest < ActiveSupport::TestCase assert_equal "Something important here.", r.description end + def test_markdown_html_description + r = RichText.new("markdown", "

Can use HTML tags.

") + assert_equal "Can use HTML tags.", r.description + end + private def assert_html(richtext, &block) From 88a7ca5625344e31538600d251ab227824582467 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 06:53:43 +0300 Subject: [PATCH 4/9] Truncate long richtext descriptions --- lib/rich_text.rb | 13 +++++++++---- test/lib/rich_text_test.rb | 11 +++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index b0b83cf5a..03800725a 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -3,6 +3,8 @@ module RichText "Business Description:", "Additional Keywords:" ].freeze + MAX_DESCRIPTION_LENGTH = 500 + def self.new(format, text) case format when "html" then HTML.new(text || "") @@ -111,7 +113,7 @@ module RichText def description @paragraph_element = first_paragraph_element(document.root) unless defined? @paragraph_element - text_content(@paragraph_element) if @paragraph_element + truncated_text_content(@paragraph_element) if @paragraph_element end private @@ -138,19 +140,22 @@ module RichText end end - def text_content(element) + def truncated_text_content(element) text = "" append_text = lambda do |child| if child.type == :text text << child.value else - child.children.each { |c| append_text.call(c) } + child.children.each do |c| + append_text.call(c) + break if text.length > MAX_DESCRIPTION_LENGTH + end end end append_text.call(element) - text + text.truncate(MAX_DESCRIPTION_LENGTH) end def image?(element) diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index 929abf823..e2cb5ea26 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -355,6 +355,17 @@ class RichTextTest < ActiveSupport::TestCase assert_equal "Can use HTML tags.", r.description end + def test_markdown_description_max_length + r = RichText.new("markdown", "x" * RichText::MAX_DESCRIPTION_LENGTH) + assert_equal "x" * RichText::MAX_DESCRIPTION_LENGTH, r.description + + r = RichText.new("markdown", "y" * (RichText::MAX_DESCRIPTION_LENGTH + 1)) + assert_equal "#{'y' * (RichText::MAX_DESCRIPTION_LENGTH - 3)}...", r.description + + r = RichText.new("markdown", "*zzzzzzzzz*z" * ((RichText::MAX_DESCRIPTION_LENGTH + 1) / 10.0).ceil) + assert_equal "#{'z' * (RichText::MAX_DESCRIPTION_LENGTH - 3)}...", r.description + end + private def assert_html(richtext, &block) From 6be766d015c692703d053997e5575f13f77d442a Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 07:46:10 +0300 Subject: [PATCH 5/9] Skip paragraphs with no text when looking for richtext description --- lib/rich_text.rb | 21 +++++++++++++-------- test/lib/rich_text_test.rb | 10 ++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index 03800725a..bdf9c37ca 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -112,8 +112,9 @@ module RichText end def description - @paragraph_element = first_paragraph_element(document.root) unless defined? @paragraph_element - truncated_text_content(@paragraph_element) if @paragraph_element + return @description if defined? @description + + @description = first_truncated_text_content(document.root) end private @@ -131,12 +132,14 @@ module RichText end end - def first_paragraph_element(element) - return element if paragraph?(element) - - element.children.find do |child| - nested_paragraph = first_paragraph_element(child) - break nested_paragraph if nested_paragraph + def first_truncated_text_content(element) + if paragraph?(element) + truncated_text_content(element) + else + element.children.find do |child| + text = first_truncated_text_content(child) + break text unless text.nil? + end end end @@ -155,6 +158,8 @@ module RichText end append_text.call(element) + return nil if text.blank? + text.truncate(MAX_DESCRIPTION_LENGTH) end diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index e2cb5ea26..63c70b099 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -345,6 +345,16 @@ class RichTextTest < ActiveSupport::TestCase assert_equal "Here starts the text.", r.description end + def test_markdown_description_after_image + r = RichText.new("markdown", "![bar](https://example.com/image.jpg)\n\nThis is below the image.") + assert_equal "This is below the image.", r.description + end + + def test_markdown_description_only_first_paragraph + r = RichText.new("markdown", "This thing.\n\nMaybe also that thing.") + assert_equal "This thing.", r.description + end + def test_markdown_description_elements r = RichText.new("markdown", "*Something* **important** [here](https://example.com/).") assert_equal "Something important here.", r.description From e0cec1a6e8859c966e3dc22671842c472ba6d3bc Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 18:55:08 +0300 Subject: [PATCH 6/9] Remove default argument values of OpenGraph tag helper --- app/helpers/open_graph_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/open_graph_helper.rb b/app/helpers/open_graph_helper.rb index ad24c73b2..a2c429ed7 100644 --- a/app/helpers/open_graph_helper.rb +++ b/app/helpers/open_graph_helper.rb @@ -1,7 +1,7 @@ module OpenGraphHelper require "addressable/uri" - def opengraph_tags(title = nil, og_image = nil, og_image_alt = nil) + def opengraph_tags(title, og_image, og_image_alt) tags = { "og:site_name" => t("layouts.project_name.title"), "og:title" => title || t("layouts.project_name.title"), From 43f544053738dbf84ca32941b65dc3c30251b22b Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 08:13:29 +0300 Subject: [PATCH 7/9] Pass properties hash to OpenGraph helper --- app/helpers/open_graph_helper.rb | 16 ++++++++-------- app/views/layouts/_meta.html.erb | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/helpers/open_graph_helper.rb b/app/helpers/open_graph_helper.rb index a2c429ed7..dbd204462 100644 --- a/app/helpers/open_graph_helper.rb +++ b/app/helpers/open_graph_helper.rb @@ -1,7 +1,7 @@ module OpenGraphHelper require "addressable/uri" - def opengraph_tags(title, og_image, og_image_alt) + def opengraph_tags(title, properties) tags = { "og:site_name" => t("layouts.project_name.title"), "og:title" => title || t("layouts.project_name.title"), @@ -9,7 +9,7 @@ module OpenGraphHelper "og:url" => url_for(:only_path => false), "og:description" => t("layouts.intro_text") }.merge( - opengraph_image_properties(og_image, og_image_alt) + opengraph_image_properties(properties) ) safe_join(tags.map do |property, content| @@ -19,13 +19,13 @@ module OpenGraphHelper private - def opengraph_image_properties(og_image, og_image_alt) + def opengraph_image_properties(properties) begin - if og_image - properties = {} - properties["og:image"] = Addressable::URI.join(root_url, og_image).normalize - properties["og:image:alt"] = og_image_alt if og_image_alt - return properties + if properties["og:image"] + image_properties = {} + image_properties["og:image"] = Addressable::URI.join(root_url, properties["og:image"]).normalize + image_properties["og:image:alt"] = properties["og:image:alt"] if properties["og:image:alt"] + return image_properties end rescue Addressable::URI::InvalidURIError # return default image diff --git a/app/views/layouts/_meta.html.erb b/app/views/layouts/_meta.html.erb index f09449761..822238894 100644 --- a/app/views/layouts/_meta.html.erb +++ b/app/views/layouts/_meta.html.erb @@ -21,7 +21,7 @@ <% end -%> <%= tag.link :rel => "search", :type => "application/opensearchdescription+xml", :title => "OpenStreetMap Search", :href => asset_path("osm.xml") %> <%= tag.meta :name => "description", :content => "OpenStreetMap is the free wiki world map." %> -<%= opengraph_tags(@title, @og_image, @og_image_alt) %> +<%= opengraph_tags(@title, "og:image" => @og_image, "og:image:alt" => @og_image_alt) %> <% if flash[:matomo_goal] -%> <%= tag.meta :name => "matomo-goal", :content => flash[:matomo_goal] %> <% end -%> From 5573df77417e3cf0199d9275afefca68e396c71f Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 08:18:43 +0300 Subject: [PATCH 8/9] Set OpenGraph properties hash in diary entry show action --- app/controllers/diary_entries_controller.rb | 6 ++++-- app/views/layouts/_meta.html.erb | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index 9962c7efd..1f47e2588 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -68,8 +68,10 @@ class DiaryEntriesController < ApplicationController @entry = entries.find_by(:id => params[:id]) if @entry @title = t ".title", :user => params[:display_name], :title => @entry.title - @og_image = @entry.body.image - @og_image_alt = @entry.body.image_alt + @opengraph_properties = { + "og:image" => @entry.body.image, + "og:image:alt" => @entry.body.image_alt + } @comments = can?(:unhide, DiaryComment) ? @entry.comments : @entry.visible_comments else @title = t "diary_entries.no_such_entry.title", :id => params[:id] diff --git a/app/views/layouts/_meta.html.erb b/app/views/layouts/_meta.html.erb index 822238894..790d13fc3 100644 --- a/app/views/layouts/_meta.html.erb +++ b/app/views/layouts/_meta.html.erb @@ -21,7 +21,7 @@ <% end -%> <%= tag.link :rel => "search", :type => "application/opensearchdescription+xml", :title => "OpenStreetMap Search", :href => asset_path("osm.xml") %> <%= tag.meta :name => "description", :content => "OpenStreetMap is the free wiki world map." %> -<%= opengraph_tags(@title, "og:image" => @og_image, "og:image:alt" => @og_image_alt) %> +<%= opengraph_tags(@title, @opengraph_properties || {}) %> <% if flash[:matomo_goal] -%> <%= tag.meta :name => "matomo-goal", :content => flash[:matomo_goal] %> <% end -%> From 5f4ae358140bbfce2c5648a556aeb7a55f89536e Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 7 Aug 2024 08:33:41 +0300 Subject: [PATCH 9/9] Set og:description meta tag to diary entry description --- app/controllers/diary_entries_controller.rb | 3 ++- app/helpers/open_graph_helper.rb | 2 +- .../diary_entries_controller_test.rb | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) 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)