Skip to content

Commit 237a6b6

Browse files
authored
fix: emoji parse for πŸ–οΈ (#1587)
* fix: emoji parse for πŸ–οΈ Signed-off-by: Adam Setch <[email protected]> * fix: emoji parse for πŸ–οΈ Signed-off-by: Adam Setch <[email protected]> * fix: emoji parse for πŸ–οΈ Signed-off-by: Adam Setch <[email protected]> * fix: emoji parse for πŸ–οΈ Signed-off-by: Adam Setch <[email protected]> --------- Signed-off-by: Adam Setch <[email protected]>
1 parent 1607852 commit 237a6b6

File tree

5 files changed

+52
-51
lines changed

5 files changed

+52
-51
lines changed

β€Žconfig/webpack.config.renderer.base.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { merge } from 'webpack-merge';
77
import baseConfig from './webpack.config.common';
88
import webpackPaths from './webpack.paths';
99

10-
import { EMOJI_CODE_POINTS } from '../src/renderer/utils/emojis';
10+
import { ALL_EMOJI_SVG_FILENAMES } from '../src/renderer/utils/emojis';
1111

1212
const configuration: webpack.Configuration = {
1313
devtool: 'inline-source-map',
@@ -76,8 +76,8 @@ const configuration: webpack.Configuration = {
7676
to: 'images/twemoji',
7777
// Only copy the SVGs for the emojis we use
7878
filter: (resourcePath) => {
79-
return EMOJI_CODE_POINTS.some((svg) =>
80-
resourcePath.endsWith(`/${svg}.svg`),
79+
return ALL_EMOJI_SVG_FILENAMES.some((filename) =>
80+
resourcePath.endsWith(`/${filename}`),
8181
);
8282
},
8383
},

β€Žsrc/renderer/components/EmojiText.tsx

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
import path from 'node:path';
2-
import twemoji from '@discordapp/twemoji';
31
import { type FC, useEffect, useRef } from 'react';
2+
import { convertTextToEmojiImgHtml } from '../utils/emojis';
43

54
export interface IEmojiText {
65
text: string;
76
}
87

9-
type TwemojiOptions = {
10-
base: string;
11-
size: string;
12-
ext: string;
13-
};
14-
158
export const EmojiText: FC<IEmojiText> = ({ text }) => {
169
const ref = useRef(null);
1710

1811
useEffect(() => {
1912
if (ref.current) {
20-
ref.current.innerHTML = twemoji.parse(text, {
21-
folder: 'svg',
22-
ext: '.svg',
23-
callback: (
24-
icon: string,
25-
_options: TwemojiOptions,
26-
_variant: string,
27-
) => {
28-
return path.join('images', 'twemoji', `${icon}.svg`);
29-
},
30-
});
13+
ref.current.innerHTML = convertTextToEmojiImgHtml(text);
3114
}
3215
}, [text]);
3316

β€Žsrc/renderer/utils/__snapshots__/emojis.test.ts.snap

+21-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žsrc/renderer/utils/emojis.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { EMOJI_CODE_POINTS } from './emojis';
1+
import { ALL_EMOJI_SVG_FILENAMES } from './emojis';
22

33
describe('renderer/utils/emojis.ts', () => {
4-
it('emoji code points', () => {
5-
expect(EMOJI_CODE_POINTS).toHaveLength(20);
6-
expect(EMOJI_CODE_POINTS).toMatchSnapshot();
4+
it('emoji svg filenames', () => {
5+
expect(ALL_EMOJI_SVG_FILENAMES).toHaveLength(20);
6+
expect(ALL_EMOJI_SVG_FILENAMES).toMatchSnapshot();
77
});
88
});

β€Žsrc/renderer/utils/emojis.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import twemoji from '@discordapp/twemoji';
1+
import path from 'node:path';
2+
import twemoji, { type TwemojiOptions } from '@discordapp/twemoji';
23
import { Constants } from './constants';
34
import { Errors } from './errors';
45

6+
const EMOJI_FORMAT = 'svg';
7+
58
const ALL_EMOJIS = [
69
...Constants.ALL_READ_EMOJIS,
710
...Errors.BAD_CREDENTIALS.emojis,
@@ -11,6 +14,21 @@ const ALL_EMOJIS = [
1114
...Errors.UNKNOWN.emojis,
1215
];
1316

14-
export const EMOJI_CODE_POINTS = ALL_EMOJIS.map((emoji) =>
15-
twemoji.convert.toCodePoint(emoji),
16-
);
17+
export const ALL_EMOJI_SVG_FILENAMES = ALL_EMOJIS.map((emoji) => {
18+
const imgHtml = convertTextToEmojiImgHtml(emoji);
19+
return extractSvgFilename(imgHtml);
20+
});
21+
22+
export function convertTextToEmojiImgHtml(text: string): string {
23+
return twemoji.parse(text, {
24+
folder: EMOJI_FORMAT,
25+
callback: (icon: string, _options: TwemojiOptions) => {
26+
return path.join('images', 'twemoji', `${icon}.${EMOJI_FORMAT}`);
27+
},
28+
});
29+
}
30+
31+
function extractSvgFilename(imgHtml: string): string {
32+
const srcMatch = imgHtml.match(/src="(.*)"/);
33+
return path.basename(srcMatch[1]);
34+
}

0 commit comments

Comments
Β (0)