FEATURE: line with only 1 to 3 emojis will now display as large emojis

This commit is contained in:
Joffrey JAFFEUX
2020-01-16 09:54:26 +01:00
committed by GitHub
parent 91f8ada8e0
commit 948bd00340
5 changed files with 124 additions and 12 deletions

View File

@@ -231,9 +231,68 @@ function applyEmoji(
result.push(text);
}
// 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
) {
let allEmojiLine = true;
let index = 0;
const checkNextToken = t => {
if (!t) {
return;
}
if (!["emoji", "text"].includes(t.type)) {
allEmojiLine = false;
}
// 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 !== " ")
) {
allEmojiLine = false;
}
// exit as soon as possible
if (allEmojiLine) {
index += 1;
checkNextToken(result[index]);
}
};
checkNextToken(result[index]);
if (allEmojiLine) {
result.forEach(r => {
if (r.type === "emoji") {
applyAllEmojiClass(r);
}
});
}
}
}
return result;
}
function applyAllEmojiClass(token) {
token.attrs.forEach(attr => {
if (attr[0] === "class") {
attr[1] = `${attr[1]} only-emoji`;
}
});
}
export function setup(helper) {
helper.registerOptions((opts, siteSettings, state) => {
opts.features.emoji = !state.disableEmojis && !!siteSettings.enable_emoji;
@@ -257,5 +316,10 @@ export function setup(helper) {
);
});
helper.whiteList(["img[class=emoji]", "img[class=emoji emoji-custom]"]);
helper.whiteList([
"img[class=emoji]",
"img[class=emoji emoji-custom]",
"img[class=emoji emoji-custom only-emoji]",
"img[class=emoji only-emoji]"
]);
}