Skip to content

Commit

Permalink
refactor: better emoji parsing (#458)
Browse files Browse the repository at this point in the history
* refactor: update getGlobalEmojiUrl to parse Discord emoji syntax and inline emojis in titles/fields

* Add animated to regex directly

Co-authored-by: Jeroen Claassens <jeroen.claassens@live.nl>

* chore: really ignore ignored worsd

---------

Co-authored-by: Jeroen Claassens <jeroen.claassens@live.nl>
Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
3 people authored Aug 24, 2024
1 parent 62b1294 commit 223dfc3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class DiscordEmbedField extends LitElement implements LightTheme {

return el.map((wordOrHtmlTemplate) => {
if (typeof wordOrHtmlTemplate === 'string') {
return html`<div>${wordOrHtmlTemplate}</div>`;
return html`<span>${wordOrHtmlTemplate}</span>`;
}

return wordOrHtmlTemplate;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/discord-embed/DiscordEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class DiscordEmbed extends LitElement implements LightTheme {

return el.map((wordOrHtmlTemplate) => {
if (typeof wordOrHtmlTemplate === 'string') {
return html`<div>${wordOrHtmlTemplate}</div>`;
return html`<span>${wordOrHtmlTemplate}</span>`;
}

return wordOrHtmlTemplate;
Expand Down
20 changes: 19 additions & 1 deletion packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /(?:<(?<animated>a)?:(?<name>\w{2,32}):)?(?<id>\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;
};

0 comments on commit 223dfc3

Please # to comment.