diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rb index 2c62b937198..c162bd89be3 100644 --- a/lib/excerpt_parser.rb +++ b/lib/excerpt_parser.rb @@ -14,6 +14,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document @markdown_images = options[:markdown_images] == true @keep_newlines = options[:keep_newlines] == true @keep_emoji_images = options[:keep_emoji_images] == true + @keep_onebox_source = options[:keep_onebox_source] == true @remap_emoji = options[:remap_emoji] == true @start_excerpt = false end @@ -27,6 +28,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document parser.parse(html) end excerpt = me.excerpt.strip + excerpt = excerpt.gsub(/\s*\n+\s*/, "\n\n") if options[:keep_onebox_source] excerpt = CGI.unescapeHTML(excerpt) if options[:text_entities] == true excerpt end @@ -83,8 +85,11 @@ class ExcerptParser < Nokogiri::XML::SAX::Document end when "aside" - @in_quote = true - + @in_quote = true unless @keep_onebox_source + when 'article' + if @keep_onebox_source && attributes.include?(['class', 'onebox-body']) + @in_quote = true + end when "div", "span" if attributes.include?(["class", "excerpt"]) @excerpt = "" diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 45655f92fe7..8554b82c335 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -273,6 +273,17 @@ HTML expect(PrettyText.excerpt(emoji_code, 100)).to eq(":heart:") end + it "should have an option to preserver onebox source" do + onebox = "\n\n\n" + expected = "meta.discourse.org" + + expect(PrettyText.excerpt(onebox, 100, keep_onebox_source: true)) + .to eq(expected) + + expect(PrettyText.excerpt("#{onebox}\n \n \n \n\n\n #{onebox}", 100, keep_onebox_source: true)) + .to eq("#{expected}\n\n#{expected}") + end + end describe "strip links" do @@ -433,7 +444,7 @@ HTML it "replaces the custom emoji" do CustomEmoji.create!(name: 'trout', upload: Fabricate(:upload)) Emoji.clear_cache - + expect(PrettyText.cook("hello :trout:")).to match(/]+trout[^>]+>/) end end