mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-19 05:32:01 +03:00
d4dca0e81f
Platforms like Misskey have irregularly-shaped custom emojis (emojos?) - So far this handles horizontally-wide emojis, not tall ones (haven't seen any) - text-overflow: ellipsis is not used because it can't ellipsis-fy wide emoji images
18 lines
737 B
JavaScript
18 lines
737 B
JavaScript
function emojifyText(text, emojis = []) {
|
|
if (!text) return '';
|
|
if (!emojis.length) return text;
|
|
if (text.indexOf(':') === -1) return text;
|
|
// Replace shortcodes in text with emoji
|
|
// emojis = [{ shortcode: 'smile', url: 'https://example.com/emoji.png' }]
|
|
emojis.forEach((emoji) => {
|
|
const { shortcode, staticUrl, url } = emoji;
|
|
text = text.replace(
|
|
new RegExp(`:${shortcode}:`, 'g'),
|
|
`<picture><source srcset="${staticUrl}" media="(prefers-reduced-motion: reduce)"></source><img class="shortcode-emoji emoji" src="${url}" alt=":${shortcode}:" width="16" height="16" loading="lazy" decoding="async" /></picture>`,
|
|
);
|
|
});
|
|
// console.log(text, emojis);
|
|
return text;
|
|
}
|
|
|
|
export default emojifyText;
|