From 3492a9105671c6795f4b92f54d9b88f93def4105 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 24 Jan 2018 12:21:44 +1100 Subject: [PATCH] FEATURE: allow site operators to disable emoji shortcuts --- .../engines/discourse-markdown/emoji.js.es6 | 12 +++++++++--- config/locales/server.en.yml | 1 + config/site_settings.yml | 3 +++ spec/components/pretty_text_spec.rb | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js.es6 index 522edbe7669..dd45e3f54c7 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js.es6 @@ -170,7 +170,7 @@ function getEmojiTokenByTranslation(content, pos, state) { } } -function applyEmoji(content, state, emojiUnicodeReplacer) { +function applyEmoji(content, state, emojiUnicodeReplacer, enableShortcuts) { let i; let result = null; let contentToken = null; @@ -195,7 +195,7 @@ function applyEmoji(content, state, emojiUnicodeReplacer) { } } - if (!token) { + if (enableShortcuts && !token) { // handle aliases (note: we can't do this in inline cause ; is not a split point) // let info = getEmojiTokenByTranslation(content, i, state); @@ -231,13 +231,19 @@ function applyEmoji(content, state, emojiUnicodeReplacer) { export function setup(helper) { helper.registerOptions((opts, siteSettings, state)=>{ opts.features.emoji = !!siteSettings.enable_emoji; + opts.features.emojiShortcuts = !!siteSettings.enable_emoji_shortcuts; opts.emojiSet = siteSettings.emoji_set || ""; opts.customEmoji = state.customEmoji; }); helper.registerPlugin((md)=>{ md.core.ruler.push('emoji', state => md.options.discourse.helpers.textReplace( - state, (c,s)=>applyEmoji(c,s,md.options.discourse.emojiUnicodeReplacer)) + state, (c,s)=>applyEmoji( + c, + s, + md.options.discourse.emojiUnicodeReplacer, + md.options.discourse.features.emojiShortcuts + )) ); }); diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 84d12f05b5d..33021688e57 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1524,6 +1524,7 @@ en: slug_generation_method: "Choose a slug generation method. 'encoded' will generate percent encoding string. 'none' will disable slug at all." enable_emoji: "Enable emoji" + enable_emoji_shortcuts: "Common smiley text such as :) :p :( will be converted to emojis" emoji_set: "How would you like your emoji?" enforce_square_emoji: "Force a square aspect ratio to all emojis." diff --git a/config/site_settings.yml b/config/site_settings.yml index dcdc6caa5f6..fe2c4415dff 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -601,6 +601,9 @@ posting: enable_emoji: default: true client: true + enable_emoji_shortcuts: + default: true + client: true emoji_set: default: 'twitter' client: true diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index d02683bee8c..918056cc42f 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -746,6 +746,20 @@ describe PrettyText do expect(PrettyText.cook("💣")).not_to match(/\:bomb\:/) end + it "doesn't replace emoji if emoji is disabled" do + SiteSetting.enable_emoji = false + expect(PrettyText.cook(":bomb:")).to eq("

:bomb:

") + end + + it "doesn't replace shortcuts if disabled" do + SiteSetting.enable_emoji_shortcuts = false + expect(PrettyText.cook(":)")).to eq("

:)

") + end + + it "does replace shortcuts if enabled" do + expect(PrettyText.cook(":)")).to match("smile") + end + it "replaces skin toned emoji" do expect(PrettyText.cook("hello 👱🏿‍♀️")).to eq("

hello \":blonde_woman:t6:\"

") expect(PrettyText.cook("hello 👩‍🎤")).to eq("

hello \":woman_singer:\"

")