mirror of
https://github.com/discourse/discourse.git
synced 2025-03-20 06:05:34 +08:00
PERF: Use a regexp for unicode replacements
This commit is contained in:
parent
e45caa7e2e
commit
b17e5c99cc
@ -118,4 +118,16 @@ class Emoji
|
||||
"#{Discourse.base_uri}/uploads/#{db}/_emoji"
|
||||
end
|
||||
|
||||
def self.unicode_replacements
|
||||
@unicode_replacements ||= Hash[db.map {|e| [e['emoji'], e['aliases'][0]] }]
|
||||
end
|
||||
|
||||
def self.unicode_regexp
|
||||
@unicode_regexp ||= Regexp.union(unicode_replacements.keys)
|
||||
end
|
||||
|
||||
def self.sub_unicode!(text)
|
||||
text.gsub!(unicode_regexp) {|m| ":#{unicode_replacements[m]}:"}
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -246,23 +246,14 @@ module PrettyText
|
||||
end
|
||||
end
|
||||
|
||||
def self.emoji_unicode(text)
|
||||
return text unless SiteSetting.enable_emoji?
|
||||
|
||||
Emoji.db.each do |e|
|
||||
text.gsub!(e['emoji'], ":#{e['aliases'][0]}:")
|
||||
end
|
||||
|
||||
text
|
||||
end
|
||||
|
||||
def self.cook(text, opts={})
|
||||
options = opts.dup
|
||||
|
||||
# we have a minor inconsistency
|
||||
options[:topicId] = opts[:topic_id]
|
||||
|
||||
working_text = emoji_unicode(text.dup)
|
||||
working_text = text.dup
|
||||
Emoji.sub_unicode!(working_text) if SiteSetting.enable_emoji?
|
||||
sanitized = markdown(working_text, options)
|
||||
|
||||
doc = Nokogiri::HTML.fragment(sanitized)
|
||||
|
Loading…
x
Reference in New Issue
Block a user