Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
99 lines
2.9 KiB
TypeScript
99 lines
2.9 KiB
TypeScript
import { emoji } from './lib';
|
|
|
|
// Escapes a plain text value into its HTML representation so it can be safely
|
|
// reinserted via innerHTML after emoji rendering (e.g. `<` becomes `<`).
|
|
const escapeHtml = (value: string): string => {
|
|
const escaper = document.createElement('div');
|
|
escaper.textContent = value;
|
|
return escaper.innerHTML;
|
|
};
|
|
|
|
const renderEmojiPackages = (value: string): string =>
|
|
Object.entries(emoji.packages)
|
|
.reverse()
|
|
.reduce((rendered, [, emojiPackage]) => emojiPackage.render(rendered), value);
|
|
|
|
/**
|
|
* emojiParser is a function that will replace emojis
|
|
*/
|
|
export const emojiParser = (html: string) => {
|
|
html = html.trim();
|
|
|
|
// ' to apostrophe (') for emojis such as :')
|
|
html = html.replace(/'/g, "'");
|
|
|
|
// '<br>' to ' <br> ' for emojis such at line breaks
|
|
html = html.replace(/<br>/g, ' <br> ');
|
|
|
|
const checkEmojiOnly = document.createElement('div');
|
|
|
|
checkEmojiOnly.innerHTML = html;
|
|
|
|
// Render emojis only inside text nodes so shortcodes that live in markup
|
|
// (e.g. a URL containing `:smile:` in an anchor's href) stay untouched.
|
|
// Emojis inside code blocks are intentionally left as their literal shortcode.
|
|
const textWalker = document.createTreeWalker(checkEmojiOnly, NodeFilter.SHOW_TEXT, {
|
|
acceptNode: (node) => (node.parentElement?.closest('code') ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT),
|
|
});
|
|
|
|
const textNodes: Text[] = [];
|
|
while (textWalker.nextNode()) {
|
|
textNodes.push(textWalker.currentNode as Text);
|
|
}
|
|
|
|
textNodes.forEach((textNode) => {
|
|
const escaped = escapeHtml(textNode.nodeValue ?? '');
|
|
const rendered = renderEmojiPackages(escaped);
|
|
|
|
if (rendered === escaped) {
|
|
return;
|
|
}
|
|
|
|
const fragmentHolder = document.createElement('span');
|
|
fragmentHolder.innerHTML = rendered;
|
|
textNode.replaceWith(...Array.from(fragmentHolder.childNodes));
|
|
});
|
|
|
|
const emojis = Array.from(checkEmojiOnly.querySelectorAll('.emoji:not(:empty)'));
|
|
|
|
let hasText = false;
|
|
|
|
const isElement = (node: Node): node is Element => node.nodeType === Node.ELEMENT_NODE;
|
|
|
|
const isTextNode = (node: Node): node is Text => node.nodeType === Node.TEXT_NODE;
|
|
|
|
const filter = (node: Node) => {
|
|
if (isElement(node) && node.classList.contains('emoji')) {
|
|
return NodeFilter.FILTER_REJECT;
|
|
}
|
|
return NodeFilter.FILTER_ACCEPT;
|
|
};
|
|
|
|
const walker = document.createTreeWalker(checkEmojiOnly, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, filter);
|
|
|
|
while (walker.nextNode()) {
|
|
if (isTextNode(walker.currentNode) && walker.currentNode.nodeValue.trim() !== '') {
|
|
hasText = true;
|
|
break;
|
|
}
|
|
}
|
|
const emojiOnly = emojis.length && !hasText;
|
|
|
|
if (emojiOnly) {
|
|
for (let i = 0, len = emojis.length; i < len; i++) {
|
|
const { classList } = emojis[i];
|
|
classList.add('big');
|
|
}
|
|
}
|
|
|
|
html = checkEmojiOnly.innerHTML;
|
|
|
|
// apostrophe (') back to '
|
|
html = html.replace(/\'/g, ''');
|
|
|
|
// line breaks ' <br> ' back to '<br>'
|
|
html = html.replace(/ <br> /g, '<br>');
|
|
|
|
return html;
|
|
};
|