Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pull/4882'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Jun 6, 2024
2 parents 0030c76 + 24c138a commit 67be661
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/controllers/diary_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def show
@entry = entries.find_by(:id => params[:id])
if @entry
@title = t ".title", :user => params[:display_name], :title => @entry.title
@og_image = @entry.body.image
@comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
else
@title = t "diary_entries.no_such_entry.title", :id => params[:id]
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/open_graph_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module OpenGraphHelper
def opengraph_tags(title = nil)
def opengraph_tags(title = nil, og_image = nil)
tags = {
"og:site_name" => t("layouts.project_name.title"),
"og:title" => [title, t("layouts.project_name.title")].compact.join(" | "),
"og:type" => "website",
"og:image" => image_url("osm_logo_256.png"),
"og:image" => og_image ? URI.join(root_url, og_image) : image_url("osm_logo_256.png"),
"og:url" => url_for(:only_path => false),
"og:description" => t("layouts.intro_text")
}
Expand Down
2 changes: 1 addition & 1 deletion app/models/diary_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DiaryEntry < ApplicationRecord
after_save :spam_check

def body
RichText.new(self[:body_format], self[:body])
@body ||= RichText.new(self[:body_format], self[:body])
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_meta.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>
<%= opengraph_tags(@title, @og_image) %>
<% if flash[:matomo_goal] -%>
<%= tag.meta :name => "matomo-goal", :content => flash[:matomo_goal] %>
<% end -%>
27 changes: 26 additions & 1 deletion lib/rich_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def spam_score
(spammy_phrases * 40)
end

def image
nil
end

protected

def simple_format(text)
Expand Down Expand Up @@ -80,12 +84,33 @@ def to_text

class Markdown < Base
def to_html
linkify(sanitize(Kramdown::Document.new(self).to_html), :all)
linkify(sanitize(document.to_html), :all)
end

def to_text
to_s
end

def image
return @image if defined? @image

@image = first_image_element(document.root)&.attr&.[]("src")
end

private

def document
@document ||= Kramdown::Document.new(self)
end

def first_image_element(element)
return element if element.type == :img

element.children.find do |child|
nested_image = first_image_element(child)
break nested_image if nested_image
end
end
end

class Text < Base
Expand Down
22 changes: 22 additions & 0 deletions test/controllers/diary_entries_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,28 @@ def test_show_hidden_comments
end
end

def test_show_og_image
user = create(:user)
diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)")

get diary_entry_path(user, diary_entry)
assert_response :success
assert_dom "head meta[property='og:image']" do
assert_dom "> @content", "https://example.com/picture.jpg"
end
end

def test_show_og_image_with_relative_uri
user = create(:user)
diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/picture.jpg)")

get diary_entry_path(user, diary_entry)
assert_response :success
assert_dom "head meta[property='og:image']" do
assert_dom "> @content", "#{root_url}picture.jpg"
end
end

def test_hide
user = create(:user)
diary_entry = create(:diary_entry, :user => user)
Expand Down
25 changes: 25 additions & 0 deletions test/lib/rich_text_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,31 @@ def test_text_spam_score
assert_equal 141, r.spam_score.round
end

def test_text_no_image
r = RichText.new("text", "foo https://example.com/ bar")
assert_nil r.image
end

def test_html_no_image
r = RichText.new("html", "foo <a href='https://example.com/'>bar</a> baz")
assert_nil r.image
end

def test_markdown_no_image
r = RichText.new("markdown", "foo [bar](https://example.com/) baz")
assert_nil r.image
end

def test_markdown_image
r = RichText.new("markdown", "foo ![bar](https://example.com/image.jpg) baz")
assert_equal "https://example.com/image.jpg", r.image
end

def test_markdown_first_image
r = RichText.new("markdown", "foo ![bar1](https://example.com/image1.jpg) baz\nfoo ![bar2](https://example.com/image2.jpg) baz")
assert_equal "https://example.com/image1.jpg", r.image
end

private

def assert_html(richtext, &block)
Expand Down

0 comments on commit 67be661

Please sign in to comment.