Skip to content

Commit 26c41c4

Browse files
committed
[TASK] Replace generateOutputUrlFromDocumentPath behaviour mostly with behaviour of canonical URL
1 parent 92c27b0 commit 26c41c4

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

packages/guides/src/RenderContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class RenderContext
3232
private array $allDocuments;
3333

3434
private function __construct(
35-
string $outputFolder,
35+
private readonly string $outputFolder,
3636
private readonly string $currentFileName,
3737
private readonly FilesystemInterface $origin,
3838
private readonly FilesystemInterface $destination,
@@ -103,7 +103,7 @@ public function relativeDocUrl(string $linkedDocument, string|null $anchor = nul
103103

104104
return $this->urlGenerator->generateOutputUrlFromDocumentPath(
105105
$this->getDirName(),
106-
$this->destinationPath,
106+
$this->outputFolder,
107107
$linkedDocument,
108108
$this->outputFormat,
109109
$anchor,

packages/guides/src/UrlGenerator.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
namespace phpDocumentor\Guides;
1515

16-
use League\Uri\Uri;
1716
use League\Uri\UriInfo;
1817

1918
use function array_pop;
2019
use function explode;
2120
use function implode;
2221
use function ltrim;
22+
use function rtrim;
2323
use function trim;
2424

2525
final class UrlGenerator implements UrlGeneratorInterface
@@ -93,16 +93,16 @@ public function generateOutputUrlFromDocumentPath(
9393
string $outputFormat,
9494
string|null $anchor = null,
9595
): string {
96-
$fileUrl = $this->createFileUrl($linkedDocument, $outputFormat, $anchor);
97-
if (UriInfo::isAbsolutePath(Uri::createFromString($linkedDocument))) {
98-
return $destinationPath . $fileUrl;
99-
}
96+
$canonicalUrl = $this->canonicalUrl(
97+
$currentDirectory,
98+
$linkedDocument,
99+
);
100100

101-
$baseUrl = ltrim($this->absoluteUrl($destinationPath, $currentDirectory), '/');
101+
$fileUrl = $this->createFileUrl($canonicalUrl, $outputFormat, $anchor);
102+
if ($destinationPath === '') {
103+
return $fileUrl;
104+
}
102105

103-
return $this->canonicalUrl(
104-
$baseUrl,
105-
$fileUrl,
106-
);
106+
return rtrim($destinationPath, '/') . '/' . $fileUrl;
107107
}
108108
}

packages/guides/tests/unit/UrlGeneratorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ public static function documentPathProvider(): array
197197
'linkedDocument' => '../../references/installing',
198198
'result' => 'guide/references/installing.txt',
199199
],
200+
'Empty destination' => [
201+
'currentDirectory' => 'getting-started/something',
202+
'destinationPath' => '',
203+
'linkedDocument' => '../../references/installing',
204+
'result' => 'references/installing.txt',
205+
],
206+
'Destination is empty absolute path' => [
207+
'currentDirectory' => 'getting-started/something',
208+
'destinationPath' => '/',
209+
'linkedDocument' => '../../references/installing',
210+
'result' => '/references/installing.txt',
211+
],
212+
'Destination is absolute' => [
213+
'currentDirectory' => 'getting-started/something',
214+
'destinationPath' => '/guide/',
215+
'linkedDocument' => '../../references/installing',
216+
'result' => '/guide/references/installing.txt',
217+
],
200218
];
201219
}
202220
}

0 commit comments

Comments
 (0)