tutu/src/masto/toot.ts

26 lines
715 B
TypeScript
Raw Normal View History

2024-08-12 21:55:26 +08:00
import { cache } from "@solidjs/router";
2024-07-14 20:28:44 +08:00
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 hasCustomEmoji(s: string) {
return CUSTOM_EMOJI_REGEX.test(s);
}