diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 63e873d380b..30ed1962338 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/views/topics/plain.html.erb b/app/views/topics/plain.html.erb index b6d9a3f865a..7e7e31bd027 100644 --- a/app/views/topics/plain.html.erb +++ b/app/views/topics/plain.html.erb @@ -3,7 +3,7 @@ <%= @topic_view.topic.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 %> <% end %> diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb index 68dad7ec1ca..c67fd23ecf6 100644 --- a/app/views/topics/show.html.erb +++ b/app/views/topics/show.html.erb @@ -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 %> diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rb index c73695a0a93..e56a3c32373 100644 --- a/lib/excerpt_parser.rb +++ b/lib/excerpt_parser.rb @@ -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 diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 9949a3f3226..b45b6cfe213 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -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 diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index fef584a6178..52ad0b61830 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -388,6 +388,19 @@ describe PrettyText do it "should remove meta informations" do expect(PrettyText.excerpt(wrapped_image, 100)).to match_html "[image]" end + + it "should strip images when option is set" do + expect(PrettyText.excerpt("", 100, strip_images: true)).to be_blank + expect(PrettyText.excerpt(" 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 = "heart" + html = " 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