36 lines
989 B
TypeScript
36 lines
989 B
TypeScript
import { cache } from "@solidjs/router";
|
|
import type { mastodon } from "masto";
|
|
import { createRenderEffect, createResource, type Accessor } from "solid-js";
|
|
|
|
const CUSTOM_EMOJI_REGEX = /:(\S+):/g;
|
|
|
|
/**
|
|
* Resolve the custom emojis in string to HTML.
|
|
*/
|
|
export function resolveCustomEmoji(
|
|
content: string,
|
|
emojis: mastodon.v1.CustomEmoji[],
|
|
) {
|
|
return content.replace(CUSTOM_EMOJI_REGEX, (original, shortcode: string) => {
|
|
const emoji = emojis.find((x) => x.shortcode === shortcode);
|
|
if (!emoji) {
|
|
return original;
|
|
}
|
|
return `<img src="${emoji.url}" class="custom-emoji" alt="${shortcode}"/>`;
|
|
});
|
|
}
|
|
|
|
export function appliedCustomEmoji(
|
|
target: { innerHTML: string },
|
|
content: string,
|
|
emojis?: mastodon.v1.CustomEmoji[],
|
|
) {
|
|
createRenderEffect(() => {
|
|
const result = emojis ? resolveCustomEmoji(content, emojis) : content;
|
|
target.innerHTML = result;
|
|
});
|
|
}
|
|
|
|
export function hasCustomEmoji(s: string) {
|
|
return CUSTOM_EMOJI_REGEX.test(s);
|
|
}
|