diff --git a/core/src/OCP/comments.js b/core/src/OCP/comments.js index 34699a477d173..c852edf19d2bb 100644 --- a/core/src/OCP/comments.js +++ b/core/src/OCP/comments.js @@ -16,7 +16,7 @@ import $ from 'jquery' * * This is a copy of the backend regex in IURLGenerator, make sure to adjust both when changing */ -const urlRegex = /(\s|^)(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig +const urlRegex = /(\s|^|\]\()(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;\[\()]*)*)(\s|$|\))/ig /** * @param {any} content - diff --git a/lib/private/Collaboration/Reference/ReferenceManager.php b/lib/private/Collaboration/Reference/ReferenceManager.php index 9287b66b2a230..caed31a1c0df9 100644 --- a/lib/private/Collaboration/Reference/ReferenceManager.php +++ b/lib/private/Collaboration/Reference/ReferenceManager.php @@ -51,10 +51,20 @@ public function __construct( */ public function extractReferences(string $text): array { preg_match_all(IURLGenerator::URL_REGEX, $text, $matches); - $references = $matches[0] ?? []; - return array_map(function ($reference) { - return trim($reference); - }, $references); + // Use capture groups 2 (protocol) and 3 (domain/path) to extract clean URLs + // This excludes the prefix group 1 (\s|\n|^|\]\() and suffix group 4 (\s|\n|$|\)) + $references = []; + if (!empty($matches[1]) && !empty($matches[2]) && !empty($matches[3])) { + for ($i = 0; $i < count($matches[2]); $i++) { + $url = $matches[2][$i] . $matches[3][$i]; + // If the URL was in markdown syntax [](url), remove the trailing ) + if ($matches[1][$i] === '](') { + $url = rtrim($url, ')'); + } + $references[] = $url; + } + } + return $references; } /** diff --git a/lib/public/IURLGenerator.php b/lib/public/IURLGenerator.php index a336d18b00fa9..1779238cf3364 100644 --- a/lib/public/IURLGenerator.php +++ b/lib/public/IURLGenerator.php @@ -30,8 +30,9 @@ interface IURLGenerator { * * @since 25.0.0 * @since 29.0.0 changed to match localhost and hostnames with ports + * @since 33.0.0 changed to match URLs in markdown link syntax and square brackets in query parameters */ - public const URL_REGEX_NO_MODIFIERS = '(\s|\n|^)(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|\n|$)'; + public const URL_REGEX_NO_MODIFIERS = '(\s|\n|^|\]\()(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;\[\()]*)*)(\s|\n|$|\))'; /** * Returns the URL for a route