diff --git a/fcts/quote.py b/fcts/quote.py index b67a80bfc..569d9e4d3 100644 --- a/fcts/quote.py +++ b/fcts/quote.py @@ -10,6 +10,7 @@ from libs.arguments import args from libs.bot_classes import Axobot from libs.quote.generator import QuoteGeneration, QuoteStyle +from libs.text_cleanup import remove_markdown class Quote(commands.Cog): @@ -70,7 +71,7 @@ async def quote_message( self, message: discord.Message, channel: discord.abc.Messageable, style: QuoteStyle ) -> Optional[discord.Message]: "Generate a Quote card from a message and post it to the channel" - text = discord.utils.remove_markdown(message.clean_content) + text = remove_markdown(message.clean_content) while '\n\n' in text: text = text.replace('\n\n', '\n') author_name = message.author.display_name diff --git a/libs/text_cleanup.py b/libs/text_cleanup.py new file mode 100644 index 000000000..0d79fd57c --- /dev/null +++ b/libs/text_cleanup.py @@ -0,0 +1,31 @@ +import re + +_MKD_LINE_BEGINNING = r'^([#>]{1,3} ?)(?P\S+)' +_MKD_CODE = r'(`+)(?P[^`]+)(`+)' +_MKD_UNDERSCORE = r'(_+)(?P[^_\n]+)(_+)' +_MKD_ASTERISK = r'(\*+)(?P[^*\n]+)(\*+)' +_MKD_SPOILER = r'(\|\|)(?P[^|\n]+)(\|\|)' +_MKD_TILDE = r'(~~)(?P[^~\n]+)(~~)' +_MKD_FULL_REGEX = re.compile( + '(?:' + + '|'.join([ + _MKD_LINE_BEGINNING, + _MKD_CODE, + _MKD_UNDERSCORE, + _MKD_ASTERISK, + _MKD_SPOILER, + _MKD_TILDE, + ]) + + ')', + flags=re.MULTILINE +) + +def _replacement(match: re.Match) -> str: + """Replace a markdown match with its content""" + for text in match.groupdict().values(): + if text: + return text + return "" + +def remove_markdown(source: str): + return _MKD_FULL_REGEX.sub(_replacement, source)