Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backlinks): Accept <link> syntax as valid in parsed markdown #1012

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Service/PageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,8 @@ public function getPageLink(string $collectiveName, PageInfo $pageInfo, bool $wi
* @return bool
*/
public function matchBacklinks(PageInfo $pageInfo, string $content): bool {
$prefix = '/\[[^\]]+\]\(';
$suffix = '\)/';
$prefix = '/(\[[^\]]+\]\(|\<)';
$suffix = '[\)\>]/';

$protocol = 'https?:\/\/';
$trustedDomainArray = array_map(static function (string $domain) {
Expand Down
32 changes: 24 additions & 8 deletions tests/Unit/Service/PageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function testMatchBacklinks(): void {
$urlPathBase = '/apps/collectives/mycollective';
$urlPath = $urlPathBase . '/page1/pageX/subpage2';

// Relative link with fileId
// Relative link with fileId in [text](link) syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link](' . $urlPath . '?fileId=123).'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link](/index.php' . $urlPath . '?fileId=123).'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link to wrong path but correct fileId](' . $urlPathBase . '/subpage2?fileId=123).'));
Expand All @@ -272,18 +272,34 @@ public function testMatchBacklinks(): void {
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with [a broken link(' . $urlPath . '?fileId=123).'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with [a broken link] (' . $urlPath . '?fileId=123).'));

// Relative link without fileId
// Relative link with fileId in <link> syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with a link: <' . $urlPath . '?fileId=123>'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with a link: </index.php' . $urlPath . '?fileId=123>'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with a link to wrong path but correct fileId: <' . $urlPathBase . '/subpage2?fileId=123>'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with a relative link: <pageX/subpage2?fileId=123>.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with a link to wrong fileId <' . $urlPath . '?fileId=345>.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with a broken link: <' . $urlPath . '?fileId=123'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with a broken link: <' . $urlPath . '?fileId=123]>'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with a broken link: ' . $urlPath . '?fileId=123>'));

// Relative link without fileId in [text](link) syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link](' . $urlPath . ') in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with [a link to wrong path](' . $urlPathBase . '/page1/subpage2) in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with [a link to wrong webroot](/index.php/instance2' . $urlPath . ') in it.'));

// Absolute link with fileId
// Relative link without fileId in <link> syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with a link: <' . $urlPath . '> in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with a broken link: <[' . $urlPath . '> in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with a link to wrong path: <' . $urlPathBase . '/page1/subpage2> in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with a link to wrong webroot: </index.php/instance2' . $urlPath . '> in it.'));

// Absolute link with fileId in [text](link) syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link](http://nextcloud.local' . $urlPath . '?fileId=123) in it.'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link](https://nextcloud.local' . $urlPath . '?fileId=123) in it.'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link](https://nextcloud.local/index.php' . $urlPath . '?fileId=123) in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with [a link to wrong host (with fileId)](https://example.org/' . $urlPath . 'fileId=123) in it.'));

// Absolute link without fileId
// Absolute link without fileId in [text](link) syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link](http://nextcloud.local' . $urlPath . ') in it.'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link](https://nextcloud.local' . $urlPath . ') in it.'));
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link with many slashes](https://nextcloud.local/////' . str_replace('/', '//', $urlPath) . ') in it.'));
Expand All @@ -294,15 +310,15 @@ public function testMatchBacklinks(): void {

\OC::$WEBROOT = 'mycloud';

// Relative link with fileId with webroot
// Relative link with fileId with webroot in [text](link) syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link with webroot](' . \OC::$WEBROOT . $urlPath . '?fileId=123).'));
// Relative link without fileId with webroot
// Relative link without fileId with webroot in [text](link) syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link with webroot](' . \OC::$WEBROOT . $urlPath . ') in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with [a link with missing webroot](' . $urlPath . ') in it.'));
// Absolute link with fileId with webroot
// Absolute link with fileId with webroot in [text](link) syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link with webroot](http://nextcloud.local' . \OC::$WEBROOT . $urlPath . '?fileId=123) in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with [a link with missing webroot](http://nextcloud.local' . $urlPath . '?fileId=123) in it.'));
// Absolute link without fileId with webroot
// Absolute link without fileId with webroot in [text](link) syntax
self::assertTrue($this->service->matchBacklinks($pageInfo, 'content with [a link with webroot](http://nextcloud.local' . \OC::$WEBROOT . $urlPath . ') in it.'));
self::assertFalse($this->service->matchBacklinks($pageInfo, 'content with [a link with missing webroot](http://nextcloud.local' . $urlPath . ') in it.'));
}
Expand Down
Loading