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
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
def avatar_template

View File

@ -229,7 +229,7 @@ class UserSerializer < BasicUserSerializer
end
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
def include_suspend_reason?

View File

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

View File

@ -63,7 +63,7 @@ module Onebox
topic = post.topic
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"," ")
# hack to make it render for now
excerpt.gsub!("[/quote]", "[quote]")

View File

@ -243,9 +243,14 @@ HTML
expect(PrettyText.excerpt("&#39;", 500, text_entities: true)).to eq("'")
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'>"
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