Skip to content

Commit

Permalink
DEV: Add option to keep onebox body content in post excerpt.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Nov 2, 2019
1 parent af65809 commit 2cb805a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/excerpt_parser.rb
Expand Up @@ -18,6 +18,7 @@ def initialize(length, options = nil)
@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
Expand All @@ -34,7 +35,7 @@ def self.get_excerpt(html, length, options)
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
Expand Down Expand Up @@ -95,13 +96,22 @@ def start_element(name, attributes = [])

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"
Expand Down
47 changes: 47 additions & 0 deletions spec/components/excerpt_parser_spec.rb
Expand Up @@ -34,4 +34,51 @@
expect(ExcerptParser.get_excerpt(html, 3, {})).to match_html('<details class="disabled"><summary>foo</summary></details>')
expect(ExcerptParser.get_excerpt(html, 2, {})).to match_html('<details class="disabled"><summary>fo&hellip;</summary></details>')
end

describe "keep_onebox_body parameter" do
it "keeps the body content for external oneboxes" do
html = <<~HTML.strip
<aside class="onebox">
<header class="source">
<img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="32" height="32">
<a href="https://github.com/discourse/discourse" target="_blank">GitHub</a>
</header>
<article class="onebox-body">
<img src="/uploads/default/original/1X/10c0f1565ee5b6ca3fe43f3183529bc0afd26003.jpeg" class="thumbnail">
<h3>
<a href="https://github.com/discourse/discourse" target="_blank">discourse/discourse</a>
</h3>
<p>A platform for community discussion. Free, open, simple. - discourse/discourse</p>
</article>
</aside>
HTML
expect(ExcerptParser.get_excerpt(html, 100, keep_onebox_body: true)).to eq(<<~HTML.strip)
[image]
<a href="https://github.com/discourse/discourse" target="_blank">discourse/discourse</a>
A platform for community discussion. Free, o&hellip;
HTML
end

it "keeps the content for internal oneboxes" do
html = <<~HTML.strip
<aside class="quote" data-post="1" data-topic="8">
<div class="title">
<div class="quote-controls"></div>
<img width="20" height="20" src="/user_avatar/localhost/system/40/2_2.png" class="avatar">
<a href="/t/welcome-to-discourse/8/1">Welcome to Discourse</a>
</div>
<blockquote>The first paragraph of this pinned topic will be visible as a welcome message to all new visitors on your homepage.</blockquote>
</aside>
HTML
expect(ExcerptParser.get_excerpt(html, 100, keep_onebox_body: true)).to eq(<<~HTML.strip)
[image]
<a href="/t/welcome-to-discourse/8/1">Welcome to Discourse</a>
The first paragraph of this pinned topic will be &hellip;
HTML
end
end
end

0 comments on commit 2cb805a

Please sign in to comment.