mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
FIX: strip spoilers in notification excerpts
This commit is contained in:
parent
76e76140e1
commit
55942224ed
|
@ -429,7 +429,7 @@ class PostAlerter
|
||||||
post_number: post.post_number,
|
post_number: post.post_number,
|
||||||
topic_title: post.topic.title,
|
topic_title: post.topic.title,
|
||||||
topic_id: post.topic.id,
|
topic_id: post.topic.id,
|
||||||
excerpt: excerpt || post.excerpt(400, text_entities: true, strip_links: true, remap_emoji: true),
|
excerpt: excerpt || post.excerpt(400, text_entities: true, strip_links: true, remap_emoji: true, strip_spoilers: true),
|
||||||
username: username || post.username,
|
username: username || post.username,
|
||||||
post_url: post_url
|
post_url: post_url
|
||||||
}
|
}
|
||||||
|
|
|
@ -347,6 +347,7 @@ en:
|
||||||
revert_version_same: "The current version is same as the version you are trying to revert to."
|
revert_version_same: "The current version is same as the version you are trying to revert to."
|
||||||
|
|
||||||
excerpt_image: "image"
|
excerpt_image: "image"
|
||||||
|
excerpt_spoiler: "spoiler"
|
||||||
|
|
||||||
groups:
|
groups:
|
||||||
success:
|
success:
|
||||||
|
|
|
@ -17,6 +17,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
@keep_emoji_images = options[:keep_emoji_images] == true
|
@keep_emoji_images = options[:keep_emoji_images] == true
|
||||||
@keep_onebox_source = options[:keep_onebox_source] == true
|
@keep_onebox_source = options[:keep_onebox_source] == true
|
||||||
@remap_emoji = options[:remap_emoji] == true
|
@remap_emoji = options[:remap_emoji] == true
|
||||||
|
@strip_spoilers = options[:strip_spoilers] == true
|
||||||
@start_excerpt = false
|
@start_excerpt = false
|
||||||
@in_details_depth = 0
|
@in_details_depth = 0
|
||||||
@summary_contents = ""
|
@summary_contents = ""
|
||||||
|
@ -110,7 +111,11 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
end
|
end
|
||||||
# Preserve spoilers
|
# Preserve spoilers
|
||||||
if attributes.include?(["class", "spoiler"])
|
if attributes.include?(["class", "spoiler"])
|
||||||
|
if @strip_spoilers
|
||||||
|
characters("[#{I18n.t 'excerpt_spoiler'}]")
|
||||||
|
else
|
||||||
include_tag("span", attributes)
|
include_tag("span", attributes)
|
||||||
|
end
|
||||||
@in_spoiler = true
|
@in_spoiler = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -194,6 +199,8 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
|
|
||||||
@excerpt << before_string if before_string
|
@excerpt << before_string if before_string
|
||||||
|
|
||||||
|
string = "" if @in_spoiler && @strip_spoilers
|
||||||
|
|
||||||
encode = encode ? lambda { |s| ERB::Util.html_escape(s) } : lambda { |s| s }
|
encode = encode ? lambda { |s| ERB::Util.html_escape(s) } : lambda { |s| s }
|
||||||
if count_it && @current_length + string.length > @length
|
if count_it && @current_length + string.length > @length
|
||||||
length = [0, @length - @current_length - 1].max
|
length = [0, @length - @current_length - 1].max
|
||||||
|
|
|
@ -32,4 +32,14 @@ describe ExcerptParser do
|
||||||
expect(ExcerptParser.get_excerpt(html, 3, {})).to match_html('<details class="disabled"><summary>foo</summary></details>')
|
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…</summary></details>')
|
expect(ExcerptParser.get_excerpt(html, 2, {})).to match_html('<details class="disabled"><summary>fo…</summary></details>')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "preserves spoilers by default" do
|
||||||
|
html = 'Foo <span class="spoiler">bar</span>'
|
||||||
|
expect(ExcerptParser.get_excerpt(html, 100, {})).to match_html('Foo <span class="spoiler">bar</span>')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "strips spoilers when specified" do
|
||||||
|
html = 'Foo <span class="spoiler">bar</span>'
|
||||||
|
expect(ExcerptParser.get_excerpt(html, 100, strip_spoilers: true)).to match_html("Foo [#{I18n.t 'excerpt_spoiler'}]")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user