nextcloud / nextcloud/server

URL_REGEX doesn't match URLs in markdown link syntax

Open Beginner friendly
#55,849 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3. to review 33-feedback bug
Dominant language
PHP
Stars
36.9k
Forks
5.2k
Avg merge
2d 3h
Merged PRs (30d)
713

Description

Issue Description

Component: OCP\IURLGenerator::URL_REGEX / Reference extraction in ReferenceManager

Current Behavior:

The URL_REGEX pattern used by ReferenceManager::extractReferences() fails to match URLs when they're embedded in markdown link syntax:

// lib/public/IURLGenerator.php
public const URL_REGEX_NO_MODIFIERS = '(\s|\n|^)(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|\!:,.;()]*)*)(\s|\n|$)';

The regex requires whitespace/newline before the URL ((\s|\n|^)), which doesn't match markdown syntax where URLs are preceded by ](.

Test Case:

Plain URL (works):
"Check https://github.com/nextcloud/server/issues/55845 for details"
✅ Extracted: https://github.com/nextcloud/server/issues/55845

Markdown link (fails):
"Check [GH #55845](https://github.com/nextcloud/server/issues/55845) for details"
❌ Extracted: (nothing)

Root Cause:

In lib/private/Collaboration/Reference/ReferenceManager.php line 53:

public function extractReferences(string $text): array {
    preg_match_all(IURLGenerator::URL_REGEX, $text, $matches);
    // ...
}

The regex pattern doesn't account for markdown link syntax [label](url) where the URL is preceded by ]( instead of whitespace.

Impact:

Reference providers (GitHub, GitLab, Zammad, custom integrations) don't generate rich previews when users paste markdown-formatted links, forcing users to paste plain URLs which reduces text readability.

Affected Use Cases:

  • [GH #55845](https://github.com/nextcloud/server/issues/55845) - GitHub issues
  • [Ticket #12345](https://support.example.com/ticket/12345) - Support tickets
  • [PROJ-123](https://jira.example.com/browse/PROJ-123) - JIRA issues
  • Any markdown link in Text app, Talk, or Comments

Proposed Solution:

Update URL_REGEX_NO_MODIFIERS to also match URLs preceded by markdown syntax:

public const URL_REGEX_NO_MODIFIERS = '(\s|\n|^|\]\()(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|\!:,.;()]*)*)(\s|\n|$|\))';
//                      ^^^^ added markdown start      ^^^ added closing paren

Alternative Solution:

Strip markdown syntax before URL extraction:

public function extractReferences(string $text): array {
    // Strip markdown link syntax: [label](url) → url
    $text = preg_replace('/\[([^\]]+)\]\(([^)]+)\)/', ' $2 ', $text);
    preg_match_all(IURLGenerator::URL_REGEX, $text, $matches);
    // ...
}

Backward Compatibility:

  • Existing plain URL extraction continues to work
  • New: Markdown links also extracted
  • No breaking changes for reference providers

Related Code:

  • lib/public/IURLGenerator.php - URL_REGEX definition
  • lib/private/Collaboration/Reference/ReferenceManager.php - extractReferences()
  • Frontend: core/src/OCP/comments.js (mentioned in comments as needing sync)

Nextcloud Version:

  • Affects: All versions with Reference Provider system (NC 25+)
  • Tested on: Nextcloud 30

Workaround:

Users must paste plain URLs without markdown formatting to trigger rich previews.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with URL_REGEX_NO_MODIFIERS in lib/public/IURLGenerator.php and extractReferences() in lib/private/Collaboration/Reference/ReferenceManager.php. Check the plain URL and markdown-link examples from the issue, then verify that extraction still handles existing URLs and also returns URLs enclosed by markdown link syntax; review core/src/OCP/comments.js for the mentioned synchronization concern.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.