From 07b597df16b9412c23ec2387d54564e4d1bcf7ed Mon Sep 17 00:00:00 2001 From: Almeida Date: Sun, 25 Dec 2022 20:04:26 +0000 Subject: [PATCH] fix(escapeX): emojis with underlines (#8945) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/discord.js/src/util/Util.js | 4 ++-- packages/discord.js/test/escapeMarkdown.test.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/discord.js/src/util/Util.js b/packages/discord.js/src/util/Util.js index d1ca63e02..0b608210d 100644 --- a/packages/discord.js/src/util/Util.js +++ b/packages/discord.js/src/util/Util.js @@ -185,7 +185,7 @@ function escapeItalic(text) { return `\\*${match}`; }); i = 0; - return text.replace(/(?<=^|[^_])_([^_]|__|$)/g, (_, match) => { + return text.replace(/(?<=^|[^_])(?)([^_]|__|$)/g, (_, match) => { if (match === '__') return ++i % 2 ? `\\_${match}` : `${match}\\_`; return `\\_${match}`; }); @@ -211,7 +211,7 @@ function escapeBold(text) { */ function escapeUnderline(text) { let i = 0; - return text.replace(/__(_)?/g, (_, match) => { + return text.replace(/(?)/g, (_, match) => { if (match) return ++i % 2 ? `${match}\\_\\_` : `\\_\\_${match}`; return '\\_\\_'; }); diff --git a/packages/discord.js/test/escapeMarkdown.test.js b/packages/discord.js/test/escapeMarkdown.test.js index 246efce19..9cc6f731a 100644 --- a/packages/discord.js/test/escapeMarkdown.test.js +++ b/packages/discord.js/test/escapeMarkdown.test.js @@ -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', () => { @@ -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', () => {