FIX: Emoji in Discourse onebox is wrapped in square brackets.

This commit is contained in:
Guo Xiang Tan 2015-12-14 21:46:15 +08:00
parent 365301fb23
commit a362ad9407
5 changed files with 19 additions and 10 deletions

View File

@ -31,7 +31,7 @@ class UserActionSerializer < ApplicationSerializer
def excerpt def excerpt
cooked = object.cooked || PrettyText.cook(object.raw) cooked = object.cooked || PrettyText.cook(object.raw)
PrettyText.excerpt(cooked, 300, keep_emojis: true) if cooked PrettyText.excerpt(cooked, 300, keep_emoji_images: true) if cooked
end end
def avatar_template def avatar_template

View File

@ -229,7 +229,7 @@ class UserSerializer < BasicUserSerializer
end end
def bio_excerpt def bio_excerpt
object.user_profile.bio_excerpt(350 , { keep_newlines: true, keep_emojis: true }) object.user_profile.bio_excerpt(350 , { keep_newlines: true, keep_emoji_images: true })
end end
def include_suspend_reason? def include_suspend_reason?

View File

@ -13,7 +13,8 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
@text_entities = options[:text_entities] == true @text_entities = options[:text_entities] == true
@markdown_images = options[:markdown_images] == true @markdown_images = options[:markdown_images] == true
@keep_newlines = options[:keep_newlines] == true @keep_newlines = options[:keep_newlines] == true
@keep_emojis = options[:keep_emojis] == true @keep_emoji_images = options[:keep_emoji_images] == true
@keep_emoji_codes = options[:keep_emoji_codes] == true
@start_excerpt = false @start_excerpt = false
end end
@ -48,11 +49,14 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
def start_element(name, attributes=[]) def start_element(name, attributes=[])
case name case name
when "img" when "img"
attributes = Hash[*attributes.flatten] attributes = Hash[*attributes.flatten]
if @keep_emojis && attributes["class"] == 'emoji' if attributes["class"] == 'emoji'
return include_tag(name, attributes) if @keep_emoji_images
return include_tag(name, attributes)
elsif @keep_emoji_codes
return characters(attributes["alt"])
end
end end
# If include_images is set, include the image in markdown # If include_images is set, include the image in markdown

View File

@ -63,7 +63,7 @@ module Onebox
topic = post.topic topic = post.topic
slug = Slug.for(topic.title) slug = Slug.for(topic.title)
excerpt = post.excerpt(SiteSetting.post_onebox_maxlength) excerpt = post.excerpt(SiteSetting.post_onebox_maxlength, { keep_emoji_codes: true })
excerpt.gsub!("\n"," ") excerpt.gsub!("\n"," ")
# hack to make it render for now # hack to make it render for now
excerpt.gsub!("[/quote]", "[quote]") excerpt.gsub!("[/quote]", "[quote]")

View File

@ -243,9 +243,14 @@ HTML
expect(PrettyText.excerpt("&#39;", 500, text_entities: true)).to eq("'") expect(PrettyText.excerpt("&#39;", 500, text_entities: true)).to eq("'")
end end
it "should have an option to preserve emojis" do it "should have an option to preserve emoji images" do
emoji_image = "<img src='/images/emoji/emoji_one/heart.png?v=1' title=':heart:' class='emoji' alt='heart'>" emoji_image = "<img src='/images/emoji/emoji_one/heart.png?v=1' title=':heart:' class='emoji' alt='heart'>"
expect(PrettyText.excerpt(emoji_image, 100, { keep_emojis: true })).to match_html(emoji_image) expect(PrettyText.excerpt(emoji_image, 100, { keep_emoji_images: true })).to match_html(emoji_image)
end
it "should have an option to preserve emoji codes" do
emoji_code = "<img src='/images/emoji/emoji_one/heart.png?v=1' title=':heart:' class='emoji' alt=':heart:'>"
expect(PrettyText.excerpt(emoji_code, 100, { keep_emoji_codes: true })).to eq(":heart:")
end end
end end
@ -390,7 +395,7 @@ HTML
end end
it "doesn't replace unicode emoji if emoji is disabled" do it "doesn't replace unicode emoji if emoji is disabled" do
SiteSetting.enable_emoji = false SiteSetting.enable_emoji = false
expect(PrettyText.cook("💣")).not_to match(/\:bomb\:/) expect(PrettyText.cook("💣")).not_to match(/\:bomb\:/)
end end
end end