@@ -122,11 +122,9 @@ enum PackageController {
122122 . init( archive: $0, isCurrent: $0. name == route. archive)
123123 }
124124
125- let canonicalUrl = Self . canonicalDocumentationUrl ( from : " \( req. url) " ,
125+ let canonicalUrl = Self . canonicalDocumentationUrl ( fromUrlPath : " \( req. url) " ,
126126 owner: documentationMetadata. owner,
127- repository: documentationMetadata. repository,
128- docVersion: route. docVersion,
129- toTarget: documentationMetadata. canonicalTarget)
127+ repository: documentationMetadata. repository)
130128
131129 // Try and parse the page and add our header, but fall back to the unprocessed page if it fails.
132130 guard let body = awsResponse. body,
@@ -457,21 +455,19 @@ extension PackageController {
457455}
458456
459457extension PackageController {
460- static func canonicalDocumentationUrl( from url : String ,
458+ static func canonicalDocumentationUrl( fromUrlPath urlPath : String ,
461459 owner: String ? ,
462- repository: String ? ,
463- docVersion: DocVersion ,
464- toTarget target: DocumentationTarget ? ) -> String ? {
465- guard let owner, let repository, let target else { return nil }
466-
467- // It's important to use `docVersion.reference` here to make sure we match with true reference urls and not ~
468- let urlPrefix = " / \( owner) / \( repository) / \( docVersion. reference. pathEncoded) / "
469- if case let . internal( canonicalReference, _) = target,
470- url. lowercased ( ) . hasPrefix ( urlPrefix. lowercased ( ) ) {
471- return " / \( owner) / \( repository) / \( canonicalReference) / \( url. dropFirst ( urlPrefix. count) ) "
472- } else {
473- return nil
474- }
460+ repository: String ? ) -> String ? {
461+ guard let owner, let repository else { return nil }
462+
463+ var urlComponents = urlPath. components ( separatedBy: " / " )
464+
465+ guard urlComponents. prefix ( 3 ) == [ " " , owner, repository] , urlComponents. count > 4
466+ else { return nil }
467+
468+ // Replace the reference with the "current" tilde character regardless of the incoming reference.
469+ urlComponents [ 3 ] = " ~ "
470+ return Current . siteURL ( ) + urlComponents. joined ( by: " / " )
475471 }
476472}
477473
0 commit comments