From 9f1361767976b4ffa0802da6f97f61736d799099 Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Fri, 11 Oct 2024 15:19:31 +0200 Subject: [PATCH] [TASK] Allow linkToDocs to fallback to 'main' for unknown versions (#194) * [TASK] Allow linkToDocs to fallback to 'main' for unknown versions Follow-up of #188 * [BUGFIX] linkToDocs drive-by fix of wrong URL hash replacement * [TASK] Declare `MAIN_IDENTIFIER` private --------- Co-authored-by: Andreas Kienast --- legacy_hook/src/DocumentationLinker.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/legacy_hook/src/DocumentationLinker.php b/legacy_hook/src/DocumentationLinker.php index 3b368e81..0c1c2b22 100644 --- a/legacy_hook/src/DocumentationLinker.php +++ b/legacy_hook/src/DocumentationLinker.php @@ -102,6 +102,8 @@ */ final readonly class DocumentationLinker { + private const MAIN_IDENTIFIER = 'main'; + private FilesystemAdapter $cache; private int $cacheTime; @@ -133,10 +135,16 @@ public function redirectToLink(): Response $matches) ) { [, $repository, $index] = $matches; - $version = str_replace('@', '', $matches[3] ?? '') ?: 'main'; + $version = str_replace('@', '', $matches[3] ?? '') ?: self::MAIN_IDENTIFIER; $entrypoint = $this->resolveEntryPoint($repository, $version); $objectsContents = $this->getObjectsFile($entrypoint); + if ($objectsContents === '' && $version !== self::MAIN_IDENTIFIER) { + // soft-fail to resolve a maybe not-yet released version to main. + $entrypoint = $this->resolveEntryPoint($repository, self::MAIN_IDENTIFIER); + $objectsContents = $this->getObjectsFile($entrypoint); + } + if ($objectsContents === '') { return new ResponseDescriber(404, [], 'Invalid shortcode, no objects.inv.json found.'); } @@ -194,7 +202,7 @@ private function parseInventoryForIndex(string $index, array $json): string // std:confval + pagelink.html#some-entry // to: // pagelink.html#confval-some-entry - $link = str_replace('#', '#-' . $docNodeTypeParts[1], $indexMetaData[2]); + $link = str_replace('#', '#' . $docNodeTypeParts[1] . '-', $indexMetaData[2]); } } }