Merge pull request #5056 from AntonKhorev/diary-og-description
Set og:description meta tag to diary entry description
This commit is contained in:
commit
735a5cf9e2
6 changed files with 134 additions and 14 deletions
|
@ -68,8 +68,11 @@ 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,
|
||||
"og:description" => @entry.body.description
|
||||
}
|
||||
@comments = can?(:unhide, DiaryComment) ? @entry.comments : @entry.visible_comments
|
||||
else
|
||||
@title = t "diary_entries.no_such_entry.title", :id => params[:id]
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
module OpenGraphHelper
|
||||
require "addressable/uri"
|
||||
|
||||
def opengraph_tags(title = nil, og_image = nil, og_image_alt = nil)
|
||||
def opengraph_tags(title, properties)
|
||||
tags = {
|
||||
"og:site_name" => t("layouts.project_name.title"),
|
||||
"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(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
|
||||
|
|
|
@ -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, @opengraph_properties || {}) %>
|
||||
<% if flash[:matomo_goal] -%>
|
||||
<%= tag.meta :name => "matomo-goal", :content => flash[:matomo_goal] %>
|
||||
<% end -%>
|
||||
|
|
|
@ -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 || "")
|
||||
|
@ -57,6 +59,10 @@ module RichText
|
|||
nil
|
||||
end
|
||||
|
||||
def description
|
||||
nil
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def simple_format(text)
|
||||
|
@ -105,6 +111,12 @@ module RichText
|
|||
@image_element.attr["alt"] if @image_element
|
||||
end
|
||||
|
||||
def description
|
||||
return @description if defined? @description
|
||||
|
||||
@description = first_truncated_text_content(document.root)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def document
|
||||
|
@ -120,9 +132,44 @@ module RichText
|
|||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def truncated_text_content(element)
|
||||
text = ""
|
||||
|
||||
append_text = lambda do |child|
|
||||
if child.type == :text
|
||||
text << child.value
|
||||
else
|
||||
child.children.each do |c|
|
||||
append_text.call(c)
|
||||
break if text.length > MAX_DESCRIPTION_LENGTH
|
||||
end
|
||||
end
|
||||
end
|
||||
append_text.call(element)
|
||||
|
||||
return nil if text.blank?
|
||||
|
||||
text.truncate(MAX_DESCRIPTION_LENGTH)
|
||||
end
|
||||
|
||||
def image?(element)
|
||||
element.type == :img || (element.type == :html_element && element.value == "img")
|
||||
end
|
||||
|
||||
def paragraph?(element)
|
||||
element.type == :p || (element.type == :html_element && element.value == "p")
|
||||
end
|
||||
end
|
||||
|
||||
class Text < Base
|
||||
|
|
|
@ -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 => "")
|
||||
|
||||
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\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)
|
||||
|
|
|
@ -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 <a href='https://example.com/'>bar</a> baz")
|
||||
assert_nil r.image
|
||||
assert_nil r.image_alt
|
||||
assert_nil r.description
|
||||
end
|
||||
|
||||
def test_markdown_no_image
|
||||
|
@ -328,6 +330,52 @@ 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
|
||||
|
||||
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_after_image
|
||||
r = RichText.new("markdown", "\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
|
||||
end
|
||||
|
||||
def test_markdown_html_description
|
||||
r = RichText.new("markdown", "<p>Can use HTML tags.</p>")
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue