From 223dfc359321d661bbcaaa9c8b0123de811caf77 Mon Sep 17 00:00:00 2001 From: Digital Date: Sat, 24 Aug 2024 14:35:29 +0200 Subject: [PATCH] refactor: better emoji parsing (#458) * refactor: update getGlobalEmojiUrl to parse Discord emoji syntax and inline emojis in titles/fields * Add animated to regex directly Co-authored-by: Jeroen Claassens * chore: really ignore ignored worsd --------- Co-authored-by: Jeroen Claassens Co-authored-by: Jeroen Claassens --- .../discord-embed-field/DiscordEmbedField.ts | 2 +- .../components/discord-embed/DiscordEmbed.ts | 2 +- packages/core/src/util.ts | 20 ++++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/core/src/components/discord-embed-field/DiscordEmbedField.ts b/packages/core/src/components/discord-embed-field/DiscordEmbedField.ts index 7e22e855f..6387cafb2 100644 --- a/packages/core/src/components/discord-embed-field/DiscordEmbedField.ts +++ b/packages/core/src/components/discord-embed-field/DiscordEmbedField.ts @@ -132,7 +132,7 @@ export class DiscordEmbedField extends LitElement implements LightTheme { return el.map((wordOrHtmlTemplate) => { if (typeof wordOrHtmlTemplate === 'string') { - return html`
${wordOrHtmlTemplate}
`; + return html`${wordOrHtmlTemplate}`; } return wordOrHtmlTemplate; diff --git a/packages/core/src/components/discord-embed/DiscordEmbed.ts b/packages/core/src/components/discord-embed/DiscordEmbed.ts index 4509bca2e..adcd1566b 100644 --- a/packages/core/src/components/discord-embed/DiscordEmbed.ts +++ b/packages/core/src/components/discord-embed/DiscordEmbed.ts @@ -426,7 +426,7 @@ export class DiscordEmbed extends LitElement implements LightTheme { return el.map((wordOrHtmlTemplate) => { if (typeof wordOrHtmlTemplate === 'string') { - return html`
${wordOrHtmlTemplate}
`; + return html`${wordOrHtmlTemplate}`; } return wordOrHtmlTemplate; diff --git a/packages/core/src/util.ts b/packages/core/src/util.ts index 2edbff917..555b45fab 100644 --- a/packages/core/src/util.ts +++ b/packages/core/src/util.ts @@ -38,4 +38,22 @@ export const validateImageExtension = (url: string) => { throw new DiscordComponentsError(`The url of an image for discord-image-attachment should match the regex ${IMAGE_EXTENSION}`); }; -export const getGlobalEmojiUrl = (emojiName: string): Emoji | undefined => getConfig().emojis?.[emojiName]; +const emojiRegex = /(?:<(?a)?:(?\w{2,32}):)?(?\d{17,21})>?/; +export const getGlobalEmojiUrl = (emojiName: string): Emoji | undefined => { + const globalEmoji = getConfig().emojis?.[emojiName]; + if (globalEmoji) return globalEmoji; + + const match = emojiRegex.exec(emojiName); + + if (match?.groups) { + const { name, id, animated } = match.groups; + const extension = animated ? 'gif' : 'png'; + + return { + name, + url: `https://cdn.discordapp.com/emojis/${id}.${extension}` + }; + } + + return undefined; +};