FEATURE: allow site operators to disable emoji shortcuts

This commit is contained in:
Sam 2018-01-24 12:21:44 +11:00
parent 7ba06de0d6
commit 3492a91056
4 changed files with 27 additions and 3 deletions

View File

@ -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
))
);
});

View File

@ -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."

View File

@ -601,6 +601,9 @@ posting:
enable_emoji:
default: true
client: true
enable_emoji_shortcuts:
default: true
client: true
emoji_set:
default: 'twitter'
client: true

View File

@ -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("<p>:bomb:</p>")
end
it "doesn't replace shortcuts if disabled" do
SiteSetting.enable_emoji_shortcuts = false
expect(PrettyText.cook(":)")).to eq("<p>:)</p>")
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("<p>hello <img src=\"/images/emoji/twitter/blonde_woman/6.png?v=5\" title=\":blonde_woman:t6:\" class=\"emoji\" alt=\":blonde_woman:t6:\"></p>")
expect(PrettyText.cook("hello 👩‍🎤")).to eq("<p>hello <img src=\"/images/emoji/twitter/woman_singer.png?v=5\" title=\":woman_singer:\" class=\"emoji\" alt=\":woman_singer:\"></p>")