diff --git a/app/lib/shared/urls.dart b/app/lib/shared/urls.dart index 45eccda9ca..4881d26666 100644 --- a/app/lib/shared/urls.dart +++ b/app/lib/shared/urls.dart @@ -154,9 +154,22 @@ String pkgDocUrl( String? version, bool includeHost = false, String? relativePath, - bool omitTrailingSlash = false, bool isLatest = false, }) { + final relativePathSegments = relativePath == null + ? const [] + : Uri.parse(relativePath) + .pathSegments + .where((e) => e.isNotEmpty) + .toList(); + + var forceEndingSlash = relativePathSegments.isEmpty; + if (relativePathSegments.isNotEmpty && + relativePathSegments.last == 'index.html') { + relativePathSegments.removeLast(); + forceEndingSlash = true; + } + if (isLatest || version == null) { version = 'latest'; } @@ -164,22 +177,11 @@ String pkgDocUrl( 'documentation', package, version, + ...relativePathSegments, + if (forceEndingSlash) '', ]; final baseUri = includeHost ? _siteRootUri : _pathRootUri; - String url = baseUri.resolveUri(Uri(pathSegments: segments)).toString(); - - if (relativePath != null) { - url = p.join(url, relativePath); - } else if (!omitTrailingSlash) { - url = '$url/'; - } - if (url.endsWith('/index.html')) { - url = url.substring(0, url.length - 'index.html'.length); - } - if (omitTrailingSlash && url.endsWith('/')) { - url = url.substring(0, url.length - 1); - } - return url; + return baseUri.resolveUri(Uri(pathSegments: segments)).toString(); } String publisherUrl(String publisherId) => '/publishers/$publisherId'; diff --git a/app/test/shared/urls_test.dart b/app/test/shared/urls_test.dart index 4cf6c16ac9..385542e69c 100644 --- a/app/test/shared/urls_test.dart +++ b/app/test/shared/urls_test.dart @@ -26,8 +26,6 @@ void main() { expect(pkgDocUrl('foo_bar'), '/documentation/foo_bar/latest/'); expect(pkgDocUrl('foo_bar', version: '1.0.0'), '/documentation/foo_bar/1.0.0/'); - expect(pkgDocUrl('foo_bar', version: '1.0.0', omitTrailingSlash: true), - '/documentation/foo_bar/1.0.0'); }); test('with host', () { @@ -35,10 +33,11 @@ void main() { 'https://pub.dev/documentation/foo_bar/latest/'); expect(pkgDocUrl('foo_bar', version: '1.0.0', includeHost: true), 'https://pub.dev/documentation/foo_bar/1.0.0/'); - expect( - pkgDocUrl('foo_bar', - version: '1.0.0', includeHost: true, omitTrailingSlash: true), - 'https://pub.dev/documentation/foo_bar/1.0.0'); + }); + + test('escaped segments', () { + expect(pkgDocUrl('foo', relativePath: 'árvíztűrő.png'), + '/documentation/foo/latest/%C3%A1rv%C3%ADzt%C5%B1r%C5%91.png'); }); });