diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index d1c15d9d761..a09b45fb13f 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -340,8 +340,8 @@ module PrettyText doc = Nokogiri::HTML.fragment(html) DiscourseEvent.trigger(:reduce_excerpt, doc, options) strip_image_wrapping(doc) + strip_oneboxed_media(doc) html = doc.to_html - ExcerptParser.get_excerpt(html, max_length, options) end @@ -374,6 +374,11 @@ module PrettyText doc.css(".lightbox-wrapper .meta").remove end + def self.strip_oneboxed_media(doc) + doc.css("audio").remove + doc.css("video").remove + end + def self.convert_vimeo_iframes(doc) doc.css("iframe[src*='player.vimeo.com']").each do |iframe| if iframe["data-original-href"].present? diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 1a9118d08c0..f3218293f5c 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -771,6 +771,29 @@ describe PrettyText do )).to eq("boom") end end + + it 'should strip audio/video' do + html = <<~HTML + +

Listen to this!

+ HTML + + expect(PrettyText.excerpt(html, 100)).to eq("Listen to this!") + + html = <<~HTML +
+ +
+

Watch this, but not in the excerpt.

+ HTML + + expect(PrettyText.excerpt(html, 100)).to eq("Watch this, but not in the excerpt.") + end end describe "strip links" do