Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(escapeX): emojis with underlines #8945

Merged
merged 2 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function escapeItalic(text) {
return `\\*${match}`;
});
i = 0;
return text.replace(/(?<=^|[^_])_([^_]|__|$)/g, (_, match) => {
return text.replace(/(?<=^|[^_])(?<!<a?:.+)_(?!:\d+>)([^_]|__|$)/g, (_, match) => {
if (match === '__') return ++i % 2 ? `\\_${match}` : `${match}\\_`;
return `\\_${match}`;
});
Expand All @@ -211,7 +211,7 @@ function escapeBold(text) {
*/
function escapeUnderline(text) {
let i = 0;
return text.replace(/__(_)?/g, (_, match) => {
return text.replace(/(?<!<a?:.+)__(_)?(?!:\d+>)/g, (_, match) => {
if (match) return ++i % 2 ? `${match}\\_\\_` : `\\_\\_${match}`;
return '\\_\\_';
});
Expand Down
14 changes: 14 additions & 0 deletions packages/discord.js/test/escapeMarkdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ describe('escapeItalic', () => {
test('basic (*)', () => {
expect(Util.escapeItalic('*test*')).toEqual('\\*test\\*');
});

test('emoji', () => {
const testOne = 'This is a test with _emojis_ <:Frost_ed_Wreath:1053399941210443826> and **bold text**.';
expect(Util.escapeItalic(testOne)).toEqual(
'This is a test with \\_emojis\\_ <:Frost_ed_Wreath:1053399941210443826> and **bold text**.',
);
});
});

describe('escapeUnderline', () => {
Expand All @@ -70,6 +77,13 @@ describe('escapeUnderline', () => {
test('basic', () => {
expect(Util.escapeUnderline('__test__')).toEqual('\\_\\_test\\_\\_');
});

test('emoji', () => {
const testTwo = 'This is a test with __emojis__ <:Frost__ed__Wreath:1053399939654352978> and **bold text**.';
expect(Util.escapeUnderline(testTwo)).toBe(
'This is a test with \\_\\_emojis\\_\\_ <:Frost__ed__Wreath:1053399939654352978> and **bold text**.',
);
});
});

describe('escapeStrikethrough', () => {
Expand Down