25 lines
910 B
JavaScript
25 lines
910 B
JavaScript
import * as mfm from "mfm-js";
|
|
import { unique } from "../prelude/array.js";
|
|
const wrappedEmojiRegex = /([:;])([^:;\s]{1,100})\1/g;
|
|
const glyphEmojiRegex = /;([^:;\s]{1,100});/g;
|
|
export function extractCustomEmojisFromMfm(nodes) {
|
|
const emojiNodes = mfm.extract(nodes, (node)=>{
|
|
return node.type === "emojiCode" && node.props.name.length <= 100;
|
|
});
|
|
const glyphNames = mfm.extract(nodes, (node)=>node.type === "text").flatMap((node)=>Array.from(node.props.text.matchAll(glyphEmojiRegex), (match)=>match[1]));
|
|
return unique([
|
|
...emojiNodes.map((x)=>x.props.name),
|
|
...glyphNames
|
|
]);
|
|
}
|
|
export function extractCustomEmojiNamesFromText(texts) {
|
|
const emojis = [];
|
|
for (const text of texts){
|
|
if (!text) continue;
|
|
for (const match of text.matchAll(wrappedEmojiRegex)){
|
|
emojis.push(match[2]);
|
|
}
|
|
}
|
|
return unique(emojis);
|
|
}
|