1
0
mirror of https://github.com/funamitech/mastodon synced 2024-12-02 00:38:33 +09:00
YuruToot/app/javascript/mastodon/emoji.js
Sorin Davidoi c1f201c49a 🎄🔨 Force tree shake emojione (#4202)
* chore(yarn): Install babel-plugin-preval as development dependency

* feat(babel): Add preval as a plugin

* feat(emojione_light): Prevaled module what tree-shaked emojione

* refactor(emoji): Use emojione_light

* feat: Preload emojione_picker bundle

* fix(emojione_light): Do not use Object.entries

* fix(emojify): Update tests

* chore(emojione_light): Remove silly ascii art
2017-07-14 20:30:12 +02:00

35 lines
1.3 KiB
JavaScript

import { unicodeToFilename } from './emojione_light';
import Trie from 'substring-trie';
const trie = new Trie(Object.keys(unicodeToFilename));
function emojify(str) {
// This walks through the string from start to end, ignoring any tags (<p>, <br>, etc.)
// and replacing valid unicode strings
// that _aren't_ within tags with an <img> version.
// The goal is to be the same as an emojione.regUnicode replacement, but faster.
let i = -1;
let insideTag = false;
let match;
while (++i < str.length) {
const char = str.charAt(i);
if (insideTag && char === '>') {
insideTag = false;
} else if (char === '<') {
insideTag = true;
} else if (!insideTag && (match = trie.search(str.substring(i)))) {
const unicodeStr = match;
if (unicodeStr in unicodeToFilename) {
const filename = unicodeToFilename[unicodeStr];
const alt = unicodeStr;
const replacement = `<img draggable="false" class="emojione" alt="${alt}" src="/emoji/${filename}.svg" />`;
str = str.substring(0, i) + replacement + str.substring(i + unicodeStr.length);
i += (replacement.length - unicodeStr.length); // jump ahead the length we've added to the string
}
}
}
return str;
}
export default emojify;