2018-06-15 23:03:24 +08:00
|
|
|
import { buildEmojiUrl, isCustomEmoji } from "pretty-text/emoji";
|
|
|
|
import { translations } from "pretty-text/emoji/data";
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
const MAX_NAME_LENGTH = 60;
|
2017-07-12 20:13:33 +08:00
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
let translationTree = null;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
function buildTranslationTree() {
|
|
|
|
let tree = [];
|
|
|
|
let lastNode;
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
Object.keys(translations).forEach(key => {
|
2017-07-14 20:27:28 +08:00
|
|
|
let node = tree;
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
for (let i = 0; i < key.length; i++) {
|
2017-07-14 20:27:28 +08:00
|
|
|
let code = key.charCodeAt(i);
|
|
|
|
let found = false;
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
for (let j = 0; j < node.length; j++) {
|
2017-07-14 20:27:28 +08:00
|
|
|
if (node[j][0] === code) {
|
|
|
|
node = node[j][1];
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2019-01-04 22:14:16 +08:00
|
|
|
// code, children, value
|
2017-07-14 20:27:28 +08:00
|
|
|
let tmp = [code, []];
|
|
|
|
node.push(tmp);
|
|
|
|
lastNode = tmp;
|
|
|
|
node = tmp[1];
|
|
|
|
}
|
2017-07-13 06:10:51 +08:00
|
|
|
}
|
2017-07-14 20:27:28 +08:00
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
lastNode[2] = translations[key];
|
2017-07-14 20:27:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return tree;
|
2017-07-12 20:13:33 +08:00
|
|
|
}
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2017-07-14 20:27:28 +08: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";
|
2018-06-15 23:03:24 +08:00
|
|
|
return { url, title, classes };
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-12 20:13:33 +08:00
|
|
|
|
2018-11-26 22:26:59 +08:00
|
|
|
function getEmojiName(content, pos, state, inlineEmoji) {
|
2017-07-14 20:27:28 +08:00
|
|
|
if (content.charCodeAt(pos) !== 58) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-12 20:13:33 +08:00
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
if (pos > 0) {
|
2018-06-15 23:03:24 +08:00
|
|
|
let prev = content.charCodeAt(pos - 1);
|
|
|
|
if (
|
2018-11-26 22:26:59 +08:00
|
|
|
!inlineEmoji &&
|
2018-06-15 23:03:24 +08:00
|
|
|
!state.md.utils.isSpace(prev) &&
|
|
|
|
!state.md.utils.isPunctChar(String.fromCharCode(prev))
|
|
|
|
) {
|
2017-07-14 20:27:28 +08:00
|
|
|
return;
|
2017-07-12 20:13:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
pos++;
|
|
|
|
if (content.charCodeAt(pos) === 58) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let length = 0;
|
2018-06-15 23:03:24 +08:00
|
|
|
while (length < MAX_NAME_LENGTH) {
|
2017-07-14 20:27:28 +08:00
|
|
|
length++;
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (content.charCodeAt(pos + length) === 58) {
|
2017-07-14 20:27:28 +08:00
|
|
|
// check for t2-t6
|
2018-06-15 23:03:24 +08:00
|
|
|
if (content.substr(pos + length + 1, 3).match(/t[2-6]:/)) {
|
2017-07-14 20:27:28 +08:00
|
|
|
length += 3;
|
|
|
|
}
|
|
|
|
break;
|
2017-07-13 06:10:51 +08:00
|
|
|
}
|
2017-07-14 20:27:28 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (pos + length > content.length) {
|
2017-07-14 20:27:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length === MAX_NAME_LENGTH) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return content.substr(pos, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// straight forward :smile: to emoji image
|
|
|
|
function getEmojiTokenByName(name, state) {
|
|
|
|
let info;
|
2018-06-15 23:03:24 +08:00
|
|
|
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]
|
|
|
|
];
|
2017-07-14 20:27:28 +08:00
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEmojiTokenByTranslation(content, pos, state) {
|
|
|
|
translationTree = translationTree || buildTranslationTree();
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
let t = translationTree;
|
2017-07-14 20:27:28 +08:00
|
|
|
let start = pos;
|
2019-01-04 22:14:16 +08:00
|
|
|
let found = null;
|
2017-07-14 20:27:28 +08:00
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
while (t.length > 0 && pos < content.length) {
|
2019-01-05 00:19:44 +08:00
|
|
|
let matched = false;
|
2017-07-14 20:27:28 +08:00
|
|
|
let code = content.charCodeAt(pos);
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
for (let i = 0; i < t.length; i++) {
|
|
|
|
if (t[i][0] === code) {
|
2019-01-05 00:19:44 +08:00
|
|
|
matched = true;
|
2019-01-04 22:14:16 +08:00
|
|
|
found = t[i][2];
|
|
|
|
t = t[i][1];
|
2017-07-14 20:27:28 +08:00
|
|
|
break;
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
2019-01-04 22:14:16 +08:00
|
|
|
|
2019-01-05 00:19:44 +08:00
|
|
|
if (!matched) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
pos++;
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// quick boundary check
|
|
|
|
if (start > 0) {
|
2018-06-15 23:03:24 +08:00
|
|
|
let leading = content.charAt(start - 1);
|
|
|
|
if (
|
|
|
|
!state.md.utils.isSpace(leading.charCodeAt(0)) &&
|
|
|
|
!state.md.utils.isPunctChar(leading)
|
|
|
|
) {
|
2016-06-15 02:31:51 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
// check trailing for punct or space
|
|
|
|
if (pos < content.length) {
|
|
|
|
let trailing = content.charCodeAt(pos);
|
2018-06-15 23:03:24 +08:00
|
|
|
if (!state.md.utils.isSpace(trailing)) {
|
2017-07-14 20:27:28 +08:00
|
|
|
return;
|
2017-06-05 20:06:23 +08:00
|
|
|
}
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let token = getEmojiTokenByName(found, state);
|
|
|
|
if (token) {
|
|
|
|
return { pos, token };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 00:19:44 +08:00
|
|
|
function applyEmoji(
|
|
|
|
content,
|
|
|
|
state,
|
|
|
|
emojiUnicodeReplacer,
|
|
|
|
enableShortcuts,
|
|
|
|
inlineEmoji
|
|
|
|
) {
|
2017-07-14 20:27:28 +08:00
|
|
|
let result = null;
|
|
|
|
let start = 0;
|
|
|
|
|
|
|
|
if (emojiUnicodeReplacer) {
|
|
|
|
content = emojiUnicodeReplacer(content);
|
|
|
|
}
|
|
|
|
|
2019-01-05 00:19:44 +08:00
|
|
|
let end = content.length;
|
2017-06-05 20:06:23 +08:00
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
for (let i = 0; i < content.length - 1; i++) {
|
2017-07-14 20:27:28 +08:00
|
|
|
let offset = 0;
|
|
|
|
let token = null;
|
|
|
|
|
2019-01-05 00:19:44 +08:00
|
|
|
const name = getEmojiName(content, i, state, inlineEmoji);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
token = getEmojiTokenByName(name, state);
|
2017-07-14 20:27:28 +08:00
|
|
|
if (token) {
|
2019-01-05 00:19:44 +08:00
|
|
|
offset = name.length + 2;
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
|
|
|
|
2018-01-24 09:21:44 +08:00
|
|
|
if (enableShortcuts && !token) {
|
2017-07-14 20:27:28 +08:00
|
|
|
// handle aliases (note: we can't do this in inline cause ; is not a split point)
|
2019-01-04 22:14:16 +08:00
|
|
|
const info = getEmojiTokenByTranslation(content, i, state);
|
2017-07-12 20:13:33 +08:00
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
if (info) {
|
|
|
|
offset = info.pos - i;
|
|
|
|
token = info.token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
result = result || [];
|
2019-01-05 00:19:44 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (i - start > 0) {
|
2019-01-05 00:19:44 +08:00
|
|
|
let text = new state.Token("text", "", 0);
|
|
|
|
text.content = content.slice(start, i);
|
|
|
|
result.push(text);
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
2017-07-14 20:27:28 +08:00
|
|
|
|
|
|
|
result.push(token);
|
2019-01-05 00:19:44 +08:00
|
|
|
|
|
|
|
end = start = i + offset;
|
|
|
|
i += offset - 1;
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
|
|
|
|
2019-01-05 00:19:44 +08:00
|
|
|
if (end < content.length) {
|
|
|
|
let text = new state.Token("text", "", 0);
|
|
|
|
text.content = content.slice(end);
|
|
|
|
result.push(text);
|
2017-07-14 20:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
2018-06-15 23:03:24 +08:00
|
|
|
helper.registerOptions((opts, siteSettings, state) => {
|
2017-07-14 20:27:28 +08:00
|
|
|
opts.features.emoji = !!siteSettings.enable_emoji;
|
2018-01-24 09:21:44 +08:00
|
|
|
opts.features.emojiShortcuts = !!siteSettings.enable_emoji_shortcuts;
|
2018-11-26 22:26:59 +08:00
|
|
|
opts.features.inlineEmoji = !!siteSettings.enable_inline_emoji_translation;
|
2017-07-14 20:27:28 +08:00
|
|
|
opts.emojiSet = siteSettings.emoji_set || "";
|
|
|
|
opts.customEmoji = state.customEmoji;
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
helper.registerPlugin(md => {
|
|
|
|
md.core.ruler.push("emoji", state =>
|
|
|
|
md.options.discourse.helpers.textReplace(state, (c, s) =>
|
|
|
|
applyEmoji(
|
|
|
|
c,
|
|
|
|
s,
|
|
|
|
md.options.discourse.emojiUnicodeReplacer,
|
2018-11-26 22:26:59 +08:00
|
|
|
md.options.discourse.features.emojiShortcuts,
|
|
|
|
md.options.discourse.features.inlineEmoji
|
2018-06-15 23:03:24 +08:00
|
|
|
)
|
|
|
|
)
|
2017-07-14 20:27:28 +08:00
|
|
|
);
|
2016-06-15 02:31:51 +08:00
|
|
|
});
|
2017-10-16 06:34:30 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
helper.whiteList(["img[class=emoji]", "img[class=emoji emoji-custom]"]);
|
2016-06-15 02:31:51 +08:00
|
|
|
}
|