FEATURE: omit images from og and twitter description tags

This commit is contained in:
Gerhard Schlager 2017-11-28 12:27:43 +01:00
parent 04e8b7dffb
commit 44ee388070
6 changed files with 32 additions and 17 deletions

View File

@ -216,10 +216,9 @@ module ApplicationHelper
[:url, :title, :description].each do |property|
if opts[property].present?
escape = (property != :image)
content = (property == :url ? opts[property] : gsub_emoji_to_unicode(opts[property]))
result << tag(:meta, { property: "og:#{property}", content: content }, nil, escape)
result << tag(:meta, { name: "twitter:#{property}", content: content }, nil, escape)
result << tag(:meta, { property: "og:#{property}", content: content }, nil, true)
result << tag(:meta, { name: "twitter:#{property}", content: content }, nil, true)
end
end

View File

@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><%= @topic_view.topic.title %></title>
<%= raw crawlable_meta_data(title: @topic_view.title, description: @topic_view.summary, image: @topic_view.image_url, read_time: @topic_view.read_time, like_count: @topic_view.like_count) %>
<%= raw crawlable_meta_data(title: @topic_view.title, description: @topic_view.summary(strip_images: true), image: @topic_view.image_url, read_time: @topic_view.read_time, like_count: @topic_view.like_count) %>
<% if @topic_view.prev_page %>
<link rel="prev" href="<%= @topic_view.prev_page_path -%>">
<% end %>

View File

@ -88,7 +88,7 @@
<% content_for :head do %>
<%= auto_discovery_link_tag(@topic_view, {action: :feed, slug: @topic_view.topic.slug, topic_id: @topic_view.topic.id}, title: t('rss_posts_in_topic', topic: @topic_view.title), type: 'application/rss+xml') %>
<%= raw crawlable_meta_data(title: @topic_view.title, description: @topic_view.summary, image: @topic_view.image_url, read_time: @topic_view.read_time, like_count: @topic_view.like_count, ignore_canonical: true) %>
<%= raw crawlable_meta_data(title: @topic_view.title, description: @topic_view.summary(strip_images: true), image: @topic_view.image_url, read_time: @topic_view.read_time, like_count: @topic_view.like_count, ignore_canonical: true) %>
<% if @topic_view.prev_page || @topic_view.next_page %>
<% if @topic_view.prev_page %>

View File

@ -10,6 +10,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
@current_length = 0
options || {}
@strip_links = options[:strip_links] == true
@strip_images = options[:strip_images] == true
@text_entities = options[:text_entities] == true
@markdown_images = options[:markdown_images] == true
@keep_newlines = options[:keep_newlines] == true
@ -53,18 +54,19 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
when "img"
attributes = Hash[*attributes.flatten]
if attributes["class"]&.include?('emoji')
if @remap_emoji
title = (attributes["alt"] || "").gsub(":", "")
title = Emoji.lookup_unicode(title) || attributes["alt"]
return characters(title)
elsif @keep_emoji_images
return include_tag(name, attributes)
else
return characters(attributes["alt"])
end
if attributes["class"]&.include?('emoji')
if @remap_emoji
title = (attributes["alt"] || "").gsub(":", "")
title = Emoji.lookup_unicode(title) || attributes["alt"]
return characters(title)
elsif @keep_emoji_images
return include_tag(name, attributes)
else
return characters(attributes["alt"])
end
end
unless @strip_images
# If include_images is set, include the image in markdown
characters("!") if @markdown_images
@ -77,6 +79,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
end
characters("(#{attributes['src']})") if @markdown_images
end
when "a"
unless @strip_links

View File

@ -169,10 +169,10 @@ class TopicView
@desired_post
end
def summary
def summary(opts = {})
return nil if desired_post.blank?
# TODO, this is actually quite slow, should be cached in the post table
excerpt = desired_post.excerpt(500, strip_links: true, text_entities: true)
excerpt = desired_post.excerpt(500, opts.merge(strip_links: true, text_entities: true))
(excerpt || "").gsub(/\n/, ' ').strip
end

View File

@ -388,6 +388,19 @@ describe PrettyText do
it "should remove meta informations" do
expect(PrettyText.excerpt(wrapped_image, 100)).to match_html "<a href='//localhost:3000/uploads/default/4399/33691397e78b4d75.png' class='lightbox' title='Screen Shot 2014-04-14 at 9.47.10 PM.png'>[image]</a>"
end
it "should strip images when option is set" do
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif'>", 100, strip_images: true)).to be_blank
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif'> Hello world!", 100, strip_images: true)).to eq("Hello world!")
end
it "should strip images, but keep emojis when option is set" do
emoji_image = "<img src='/images/emoji/twitter/heart.png?v=1' title=':heart:' class='emoji' alt='heart'>"
html = "<img src='http://cnn.com/a.gif'> Hello world #{emoji_image}"
expect(PrettyText.excerpt(html, 100, strip_images: true)).to eq("Hello world heart")
expect(PrettyText.excerpt(html, 100, strip_images: true, keep_emoji_images: true)).to match_html("Hello world #{emoji_image}")
end
end
it "should have an option to strip links" do