Skip to content

Commit 3b9a768

Browse files
committed
fix(comments): Correctly treat end of message as end of code block/inline
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 11959a1 commit 3b9a768

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

lib/private/Comments/Comment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ public function getMentions(bool $supportMarkdown = true): array {
198198
$message = $this->getMessage();
199199
if ($supportMarkdown) {
200200
// Strip fenced code blocks and inline code so mentions inside them are ignored
201-
$message = preg_replace('/^```.*?^```|^~~~.*?^~~~/sm', '', $message);
202-
$message = preg_replace('/`[^`\n]*`/', '', $message);
201+
$message = preg_replace('/^```.*?(^```|\z)|^~~~.*?(^~~~|\z)/sm', '', $message);
202+
$message = preg_replace('/`[^`\n]*(`|\z)/', '', $message);
203203
}
204204

205205
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"(guest|email)\/[a-f0-9]+\"|\"(?:federated_)?(?:group|team|user){1}\/[a-z0-9_\-@\.\' \/:]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $message, $mentions);

tests/lib/Comments/CommentTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ public static function mentionsProvider(): array {
240240
null,
241241
false,
242242
],
243+
[
244+
'Mention @alice but not `@bob at end of line',
245+
[['type' => 'user', 'id' => 'alice']],
246+
],
247+
[
248+
'Mention @alice but not `Hello @bob at end of line',
249+
[['type' => 'user', 'id' => 'alice']],
250+
],
251+
[
252+
"Mention @alice but not in unclosed fenced code block\n```\n@bob\n@charlie",
253+
[['type' => 'user', 'id' => 'alice']],
254+
],
255+
[
256+
"Mention @alice but not in unclosed tilde code block\n~~~\n@bob",
257+
[['type' => 'user', 'id' => 'alice']],
258+
],
243259
];
244260
}
245261

0 commit comments

Comments
 (0)