diff --git a/lib/private/Comments/Comment.php b/lib/private/Comments/Comment.php index 551b9d29e29d0..7d2d861e8b4ce 100644 --- a/lib/private/Comments/Comment.php +++ b/lib/private/Comments/Comment.php @@ -198,7 +198,7 @@ public function getMentions(bool $supportMarkdown = true): array { $message = $this->getMessage(); if ($supportMarkdown) { // Strip fenced code blocks and inline code so mentions inside them are ignored - $message = preg_replace('/^```.*?^```|^~~~.*?^~~~/sm', '', $message); + $message = preg_replace('/^```.*?(^```|\z)|^~~~.*?(^~~~|\z)/sm', '', $message); $message = preg_replace('/`[^`\n]*`/', '', $message); } diff --git a/tests/lib/Comments/CommentTest.php b/tests/lib/Comments/CommentTest.php index 67d84d4ddbe9b..80bc5d7818fce 100644 --- a/tests/lib/Comments/CommentTest.php +++ b/tests/lib/Comments/CommentTest.php @@ -240,6 +240,18 @@ public static function mentionsProvider(): array { null, false, ], + [ + 'Mention @alice and `also @bob as end of text only applies to code blocks', + [['type' => 'user', 'id' => 'alice'], ['type' => 'user', 'id' => 'bob']], + ], + [ + "Mention @alice but not in unclosed fenced code block\n```\n@bob\n@charlie", + [['type' => 'user', 'id' => 'alice']], + ], + [ + "Mention @alice but not in unclosed tilde code block\n~~~\n@bob", + [['type' => 'user', 'id' => 'alice']], + ], ]; }