2016-11-17 17:28:27 -05:00
|
|
|
import { buildEmojiUrl, isCustomEmoji } from "pretty-text/emoji";
|
2016-06-14 14:31:51 -04:00
|
|
|
import { translations } from "pretty-text/emoji/data";
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
const MAX_NAME_LENGTH = 60;
|
2017-07-12 08:13:33 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
let translationTree = null;
|
|
|
|
|
2020-05-27 14:11:52 -04:00
|
|
|
export function resetTranslationTree() {
|
|
|
|
translationTree = null;
|
|
|
|
}
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
// This allows us to efficiently search for aliases
|
|
|
|
// We build a data structure that allows us to quickly
|
|
|
|
// search through our N next chars to see if any match
|
|
|
|
// one of our alias emojis.
|
2020-05-27 14:11:52 -04:00
|
|
|
function buildTranslationTree(customEmojiTranslation) {
|
2017-07-14 08:27:28 -04:00
|
|
|
let tree = [];
|
|
|
|
let lastNode;
|
|
|
|
|
2020-05-27 14:11:52 -04:00
|
|
|
const allTranslations = Object.assign(
|
|
|
|
{},
|
|
|
|
translations,
|
|
|
|
customEmojiTranslation || {}
|
|
|
|
);
|
|
|
|
|
|
|
|
Object.keys(allTranslations).forEach((key) => {
|
2017-07-14 08:27:28 -04:00
|
|
|
let node = tree;
|
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
for (let i = 0; i < key.length; i++) {
|
2017-07-14 08:27:28 -04:00
|
|
|
let code = key.charCodeAt(i);
|
|
|
|
let found = false;
|
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
for (let j = 0; j < node.length; j++) {
|
2017-07-14 08:27:28 -04:00
|
|
|
if (node[j][0] === code) {
|
|
|
|
node = node[j][1];
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2019-01-04 09:14:16 -05:00
|
|
|
// code, children, value
|
2017-07-14 08:27:28 -04:00
|
|
|
let tmp = [code, []];
|
|
|
|
node.push(tmp);
|
|
|
|
lastNode = tmp;
|
|
|
|
node = tmp[1];
|
|
|
|
}
|
2017-07-12 18:10:51 -04:00
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2020-05-27 14:11:52 -04:00
|
|
|
lastNode[2] = allTranslations[key];
|
2017-07-14 08:27:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return tree;
|
2017-07-12 08:13:33 -04:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
function imageFor(code, opts) {
|
|
|
|
code = code.toLowerCase();
|
|
|
|
const url = buildEmojiUrl(code, opts);
|
|
|
|
if (url) {
|
|
|
|
const title = `:${code}:`;
|
|
|
|
const classes = isCustomEmoji(code, opts) ? "emoji emoji-custom" : "emoji";
|
|
|
|
return { url, title, classes };
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 08:13:33 -04:00
|
|
|
|
2018-11-26 09:26:59 -05:00
|
|
|
function getEmojiName(content, pos, state, inlineEmoji) {
|
2017-07-14 08:27:28 -04:00
|
|
|
if (content.charCodeAt(pos) !== 58) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-12 08:13:33 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
if (pos > 0) {
|
|
|
|
let prev = content.charCodeAt(pos - 1);
|
|
|
|
if (
|
2018-11-26 09:26:59 -05:00
|
|
|
!inlineEmoji &&
|
2017-07-14 08:27:28 -04:00
|
|
|
!state.md.utils.isSpace(prev) &&
|
|
|
|
!state.md.utils.isPunctChar(String.fromCharCode(prev))
|
|
|
|
) {
|
|
|
|
return;
|
2017-07-12 08:13:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
pos++;
|
|
|
|
if (content.charCodeAt(pos) === 58) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let length = 0;
|
|
|
|
while (length < MAX_NAME_LENGTH) {
|
|
|
|
length++;
|
|
|
|
|
|
|
|
if (content.charCodeAt(pos + length) === 58) {
|
|
|
|
// check for t2-t6
|
2022-04-01 11:35:17 -04:00
|
|
|
if (content.slice(pos + length + 1, pos + length + 4).match(/t[2-6]:/)) {
|
2017-07-14 08:27:28 -04:00
|
|
|
length += 3;
|
|
|
|
}
|
|
|
|
break;
|
2017-07-12 18:10:51 -04:00
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
|
|
|
|
if (pos + length > content.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length === MAX_NAME_LENGTH) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:35:17 -04:00
|
|
|
return content.slice(pos, pos + length);
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// straight forward :smile: to emoji image
|
|
|
|
function getEmojiTokenByName(name, state) {
|
|
|
|
let info;
|
|
|
|
if ((info = imageFor(name, state.md.options.discourse))) {
|
|
|
|
let token = new state.Token("emoji", "img", 0);
|
|
|
|
token.attrs = [
|
|
|
|
["src", info.url],
|
|
|
|
["title", info.title],
|
|
|
|
["class", info.classes],
|
|
|
|
["alt", info.title],
|
2022-02-09 06:18:59 -05:00
|
|
|
["loading", "lazy"],
|
|
|
|
["width", "20"],
|
|
|
|
["height", "20"],
|
2017-07-14 08:27:28 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:11:52 -04:00
|
|
|
function getEmojiTokenByTranslation(
|
|
|
|
content,
|
|
|
|
pos,
|
|
|
|
state,
|
|
|
|
customEmojiTranslation
|
|
|
|
) {
|
|
|
|
translationTree =
|
|
|
|
translationTree || buildTranslationTree(customEmojiTranslation);
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
let t = translationTree;
|
2017-07-14 08:27:28 -04:00
|
|
|
let start = pos;
|
2019-01-04 09:14:16 -05:00
|
|
|
let found = null;
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
while (t.length > 0 && pos < content.length) {
|
2019-01-04 11:19:44 -05:00
|
|
|
let matched = false;
|
2017-07-14 08:27:28 -04:00
|
|
|
let code = content.charCodeAt(pos);
|
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
for (let i = 0; i < t.length; i++) {
|
|
|
|
if (t[i][0] === code) {
|
2019-01-04 11:19:44 -05:00
|
|
|
matched = true;
|
2019-01-04 09:14:16 -05:00
|
|
|
found = t[i][2];
|
|
|
|
t = t[i][1];
|
2017-07-14 08:27:28 -04:00
|
|
|
break;
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
2019-01-04 09:14:16 -05:00
|
|
|
|
2019-01-04 11:19:44 -05:00
|
|
|
if (!matched) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
pos++;
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// quick boundary check
|
|
|
|
if (start > 0) {
|
|
|
|
let leading = content.charAt(start - 1);
|
|
|
|
if (
|
|
|
|
!state.md.utils.isSpace(leading.charCodeAt(0)) &&
|
|
|
|
!state.md.utils.isPunctChar(leading)
|
|
|
|
) {
|
2016-06-14 14:31:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
// check trailing for punct or space
|
|
|
|
if (pos < content.length) {
|
|
|
|
let trailing = content.charCodeAt(pos);
|
|
|
|
if (!state.md.utils.isSpace(trailing)) {
|
|
|
|
return;
|
2017-06-05 08:06:23 -04:00
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let token = getEmojiTokenByName(found, state);
|
|
|
|
if (token) {
|
|
|
|
return { pos, token };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 11:19:44 -05:00
|
|
|
function applyEmoji(
|
|
|
|
content,
|
|
|
|
state,
|
|
|
|
emojiUnicodeReplacer,
|
|
|
|
enableShortcuts,
|
2020-05-27 14:11:52 -04:00
|
|
|
inlineEmoji,
|
2022-12-01 16:38:16 -05:00
|
|
|
customEmojiTranslation,
|
2023-04-13 03:38:54 -04:00
|
|
|
watchedWordsReplacer,
|
|
|
|
emojiDenyList
|
2019-01-04 11:19:44 -05:00
|
|
|
) {
|
2017-07-14 08:27:28 -04:00
|
|
|
let result = null;
|
|
|
|
let start = 0;
|
|
|
|
|
|
|
|
if (emojiUnicodeReplacer) {
|
|
|
|
content = emojiUnicodeReplacer(content);
|
|
|
|
}
|
|
|
|
|
2022-12-01 16:38:16 -05:00
|
|
|
if (watchedWordsReplacer) {
|
|
|
|
const watchedWordRegex = Object.keys(watchedWordsReplacer);
|
|
|
|
|
|
|
|
watchedWordRegex.forEach((watchedWord) => {
|
|
|
|
if (content?.match(watchedWord)) {
|
|
|
|
const regex = new RegExp(watchedWord, "g");
|
|
|
|
const matches = content.match(regex);
|
|
|
|
const replacement = watchedWordsReplacer[watchedWord].replacement;
|
|
|
|
|
|
|
|
matches.forEach(() => {
|
|
|
|
const matchingRegex = regex.exec(content);
|
|
|
|
if (matchingRegex) {
|
|
|
|
content = content.replace(matchingRegex[1], replacement);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-13 03:38:54 -04:00
|
|
|
// prevent denied emoji and aliases from being rendered
|
|
|
|
if (emojiDenyList?.length > 0) {
|
|
|
|
emojiDenyList.forEach((emoji) => {
|
|
|
|
if (content?.match(emoji)) {
|
|
|
|
const regex = new RegExp(`:${emoji}:`, "g");
|
|
|
|
content = content.replace(regex, "");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-04 11:19:44 -05:00
|
|
|
let end = content.length;
|
2017-06-05 08:06:23 -04:00
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
for (let i = 0; i < content.length - 1; i++) {
|
2017-07-14 08:27:28 -04:00
|
|
|
let offset = 0;
|
|
|
|
let token = null;
|
|
|
|
|
2019-01-04 11:19:44 -05:00
|
|
|
const name = getEmojiName(content, i, state, inlineEmoji);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
token = getEmojiTokenByName(name, state);
|
2017-07-14 08:27:28 -04:00
|
|
|
if (token) {
|
2019-01-04 11:19:44 -05:00
|
|
|
offset = name.length + 2;
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:21:44 -05:00
|
|
|
if (enableShortcuts && !token) {
|
2017-07-14 08:27:28 -04:00
|
|
|
// handle aliases (note: we can't do this in inline cause ; is not a split point)
|
2020-05-27 14:11:52 -04:00
|
|
|
const info = getEmojiTokenByTranslation(
|
|
|
|
content,
|
|
|
|
i,
|
|
|
|
state,
|
|
|
|
customEmojiTranslation
|
|
|
|
);
|
2017-07-12 08:13:33 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
if (info) {
|
|
|
|
offset = info.pos - i;
|
|
|
|
token = info.token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
result = result || [];
|
2019-01-04 11:19:44 -05:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
if (i - start > 0) {
|
2019-01-04 11:19:44 -05:00
|
|
|
let text = new state.Token("text", "", 0);
|
|
|
|
text.content = content.slice(start, i);
|
|
|
|
result.push(text);
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
|
|
|
|
result.push(token);
|
2019-01-04 11:19:44 -05:00
|
|
|
|
|
|
|
end = start = i + offset;
|
|
|
|
i += offset - 1;
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:19:44 -05:00
|
|
|
if (end < content.length) {
|
|
|
|
let text = new state.Token("text", "", 0);
|
|
|
|
text.content = content.slice(end);
|
|
|
|
result.push(text);
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 03:54:26 -05:00
|
|
|
// we check for a result <= 5 because we support maximum 3 large emojis
|
|
|
|
// EMOJI SPACE EMOJI SPACE EMOJI => 5 tokens
|
|
|
|
if (result && result.length > 0 && result.length <= 5) {
|
|
|
|
// we ensure line starts and ends with an emoji
|
|
|
|
// and has no more than 3 emojis
|
|
|
|
if (
|
|
|
|
result[0].type === "emoji" &&
|
|
|
|
result[result.length - 1].type === "emoji" &&
|
|
|
|
result.filter((r) => r.type === "emoji").length <= 3
|
|
|
|
) {
|
2020-01-17 11:21:08 -05:00
|
|
|
let onlyEmojiLine = true;
|
2020-01-16 03:54:26 -05:00
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
const checkNextToken = (t) => {
|
|
|
|
if (!t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!["emoji", "text"].includes(t.type)) {
|
2020-01-17 11:21:08 -05:00
|
|
|
onlyEmojiLine = false;
|
2020-01-16 03:54:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// a text token should always have an emoji before
|
|
|
|
// and be a space
|
|
|
|
if (
|
|
|
|
t.type === "text" &&
|
|
|
|
((result[index - 1] && result[index - 1].type !== "emoji") ||
|
|
|
|
t.content !== " ")
|
|
|
|
) {
|
2020-01-17 11:21:08 -05:00
|
|
|
onlyEmojiLine = false;
|
2020-01-16 03:54:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// exit as soon as possible
|
2020-01-17 11:21:08 -05:00
|
|
|
if (onlyEmojiLine) {
|
2020-01-16 03:54:26 -05:00
|
|
|
index += 1;
|
|
|
|
checkNextToken(result[index]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
checkNextToken(result[index]);
|
|
|
|
|
2020-01-17 11:21:08 -05:00
|
|
|
if (onlyEmojiLine) {
|
2020-01-16 03:54:26 -05:00
|
|
|
result.forEach((r) => {
|
|
|
|
if (r.type === "emoji") {
|
2020-01-17 11:21:08 -05:00
|
|
|
applyOnlyEmojiClass(r);
|
2020-01-16 03:54:26 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:21:08 -05:00
|
|
|
function applyOnlyEmojiClass(token) {
|
2020-01-16 03:54:26 -05:00
|
|
|
token.attrs.forEach((attr) => {
|
|
|
|
if (attr[0] === "class") {
|
|
|
|
attr[1] = `${attr[1]} only-emoji`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
export function setup(helper) {
|
|
|
|
helper.registerOptions((opts, siteSettings, state) => {
|
2019-06-03 03:41:26 -04:00
|
|
|
opts.features.emoji = !state.disableEmojis && !!siteSettings.enable_emoji;
|
2018-01-23 20:21:44 -05:00
|
|
|
opts.features.emojiShortcuts = !!siteSettings.enable_emoji_shortcuts;
|
2018-11-26 09:26:59 -05:00
|
|
|
opts.features.inlineEmoji = !!siteSettings.enable_inline_emoji_translation;
|
2017-07-14 08:27:28 -04:00
|
|
|
opts.emojiSet = siteSettings.emoji_set || "";
|
|
|
|
opts.customEmoji = state.customEmoji;
|
2021-03-02 14:04:16 -05:00
|
|
|
opts.emojiCDNUrl = siteSettings.external_emoji_url;
|
2023-04-13 03:38:54 -04:00
|
|
|
opts.emojiDenyList = state.emojiDenyList;
|
2017-07-14 08:27:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
helper.registerPlugin((md) => {
|
|
|
|
md.core.ruler.push("emoji", (state) =>
|
|
|
|
md.options.discourse.helpers.textReplace(state, (c, s) =>
|
2018-01-23 20:21:44 -05:00
|
|
|
applyEmoji(
|
|
|
|
c,
|
|
|
|
s,
|
|
|
|
md.options.discourse.emojiUnicodeReplacer,
|
2018-11-26 09:26:59 -05:00
|
|
|
md.options.discourse.features.emojiShortcuts,
|
2020-05-27 14:11:52 -04:00
|
|
|
md.options.discourse.features.inlineEmoji,
|
2022-12-01 16:38:16 -05:00
|
|
|
md.options.discourse.customEmojiTranslation,
|
2023-04-13 03:38:54 -04:00
|
|
|
md.options.discourse.watchedWordsReplace,
|
|
|
|
md.options.discourse.emojiDenyList
|
2018-06-15 11:03:24 -04:00
|
|
|
)
|
2018-01-23 20:21:44 -05:00
|
|
|
)
|
2017-07-14 08:27:28 -04:00
|
|
|
);
|
2016-06-14 14:31:51 -04:00
|
|
|
});
|
2017-10-15 18:34:30 -04:00
|
|
|
|
2020-10-27 22:22:06 -04:00
|
|
|
helper.allowList([
|
2020-01-16 03:54:26 -05:00
|
|
|
"img[class=emoji]",
|
|
|
|
"img[class=emoji emoji-custom]",
|
|
|
|
"img[class=emoji emoji-custom only-emoji]",
|
|
|
|
"img[class=emoji only-emoji]",
|
2022-02-09 06:18:59 -05:00
|
|
|
"img[loading=lazy]",
|
|
|
|
"img[width=20]",
|
|
|
|
"img[height=20]",
|
2020-01-16 03:54:26 -05:00
|
|
|
]);
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|