From 2cb805a6832e2d442f729fea80c76442375c6be6 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Sat, 2 Nov 2019 17:14:04 +0530 Subject: [PATCH] DEV: Add option to keep onebox body content in post excerpt. --- lib/excerpt_parser.rb | 18 +++++++--- spec/components/excerpt_parser_spec.rb | 47 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rb index 9b17240d9f9..479c46ef999 100644 --- a/lib/excerpt_parser.rb +++ b/lib/excerpt_parser.rb @@ -18,6 +18,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document @keep_newlines = options[:keep_newlines] == true @keep_emoji_images = options[:keep_emoji_images] == true @keep_onebox_source = options[:keep_onebox_source] == true + @keep_onebox_body = options[:keep_onebox_body] == true @remap_emoji = options[:remap_emoji] == true @start_excerpt = false @in_details_depth = 0 @@ -34,7 +35,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 = excerpt.gsub(/\s*\n+\s*/, "\n\n") if options[:keep_onebox_source] || options[:keep_onebox_body] excerpt = CGI.unescapeHTML(excerpt) if options[:text_entities] == true excerpt end @@ -95,13 +96,22 @@ class ExcerptParser < Nokogiri::XML::SAX::Document when "aside" attributes = Hash[*attributes.flatten] - unless @keep_onebox_source && attributes['class'].include?('onebox') + unless (@keep_onebox_source || @keep_onebox_body) && attributes['class'].include?('onebox') @in_quote = true end + if @keep_onebox_body && attributes['class'].include?('quote') && attributes['data-topic'].present? + @in_quote = false + end + when 'article' - if @keep_onebox_source && attributes.include?(['class', 'onebox-body']) - @in_quote = true + if attributes.include?(['class', 'onebox-body']) + @in_quote = !@keep_onebox_body + end + + when 'header' + if attributes.include?(['class', 'source']) + @in_quote = !@keep_onebox_source end when "div", "span" diff --git a/spec/components/excerpt_parser_spec.rb b/spec/components/excerpt_parser_spec.rb index f3735a907fa..ff11ec55783 100644 --- a/spec/components/excerpt_parser_spec.rb +++ b/spec/components/excerpt_parser_spec.rb @@ -34,4 +34,51 @@ describe ExcerptParser do expect(ExcerptParser.get_excerpt(html, 3, {})).to match_html('
foo
') expect(ExcerptParser.get_excerpt(html, 2, {})).to match_html('
fo…
') end + + describe "keep_onebox_body parameter" do + it "keeps the body content for external oneboxes" do + html = <<~HTML.strip + + HTML + expect(ExcerptParser.get_excerpt(html, 100, keep_onebox_body: true)).to eq(<<~HTML.strip) + [image] + + discourse/discourse + + A platform for community discussion. Free, o… + HTML + end + + it "keeps the content for internal oneboxes" do + html = <<~HTML.strip + + HTML + expect(ExcerptParser.get_excerpt(html, 100, keep_onebox_body: true)).to eq(<<~HTML.strip) + [image] + + Welcome to Discourse + + The first paragraph of this pinned topic will be … + HTML + end + end end