diff --git a/src/__tests__/regex.spec.ts b/src/__tests__/regex.spec.ts index 0aa208d..6b6cf60 100644 --- a/src/__tests__/regex.spec.ts +++ b/src/__tests__/regex.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { isASocialNetworkUrl, removeEmoji } from '../helpers/regex.helper'; +import { isASocialNetworkUrl, removeEmoji, removeMarkdown } from '../helpers/regex.helper'; describe('Helpers: Regex', () => { describe('Rule: isASocialNetworkUrl should regex correctly an url', () => { @@ -22,4 +22,11 @@ describe('Helpers: Regex', () => { expect(result).toBe(' Hello, World!'); }); }); + describe('Rule: removeMarkdown should remove all markdown from a string', () => { + it('removeMarkdown() should remove all markdown from a string', () => { + const text = 'Hello, **World!** This is _me_ I **give** you `reactions` and ~more~ **quoi**'; + const result = removeMarkdown(text); + expect(result).toBe('Hello, World! This is me I give you reactions and more quoi'); + }); + }); }); diff --git a/src/helpers/regex.helper.ts b/src/helpers/regex.helper.ts index d0e07c4..2044aa0 100644 --- a/src/helpers/regex.helper.ts +++ b/src/helpers/regex.helper.ts @@ -2,6 +2,7 @@ const socialNetworksUrlRegex = new RegExp( '^(https?://)?(www.)?(facebook.com|fb.me|twitter.com|vxtwitter.com|instagram.com|linkedin.com|youtube.com|youtu.be|pinterest.com|snapchat.com|tiktok.com)/[a-zA-Z0-9.-/?=&#_]+$', ); const punctuationRegex = /[.,!?]/g; +const markdownRegex = /(\*\*|__|\*|_|`|~)(.*?)\1/g; const emojiRegex = /|\p{Extended_Pictographic}/gu; export const isASocialNetworkUrl = (url: string): boolean => { @@ -10,3 +11,4 @@ export const isASocialNetworkUrl = (url: string): boolean => { export const removePunctuation = (text: string) => text.replaceAll(punctuationRegex, ''); export const removeEmoji = (text: string) => text.replaceAll(emojiRegex, ''); +export const removeMarkdown = (text: string) => text.replaceAll(markdownRegex, '$2'); diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index 9c9e849..7a66433 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -7,12 +7,13 @@ import { } from 'discord.js'; import { cache } from '../../core/cache'; -import { removeEmoji, removePunctuation } from '../../helpers/regex.helper'; +import { removeEmoji, removeMarkdown, removePunctuation } from '../../helpers/regex.helper'; const ONE_MINUTE = 1 * 60 * 1000; const quoiDetectorRegex = /\bquoi\s*$/i; -const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(text))); +const endWithQuoi = (text: string) => + quoiDetectorRegex.test(removeEmoji(removePunctuation(removeMarkdown(text)))); const reactWith = async (message: Message, reactions: string[]) => { for (const reaction of reactions) {