internal/frontend: trim URL scheme in search query
This change removes the url scheme in the beginning of a search query to
allow the use of full repository urls to directly redirect to the
documentation page.
Example Behavior: https://photos.app.goo.gl/THDyCV2juVLasaVP6
Updates golang/go#41095
Change-Id: I951aa634529aa554edac12487d333106e2d52fe9
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/254371
Trust: Miguel Acero <acero@google.com>
Run-TryBot: Miguel Acero <acero@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/internal/frontend/search.go b/internal/frontend/search.go
index b890c2a..4759d67 100644
--- a/internal/frontend/search.go
+++ b/internal/frontend/search.go
@@ -185,6 +185,10 @@
// element (such as fmt, errors, etc.) will not redirect, to allow users to
// search by those terms.
func searchRequestRedirectPath(ctx context.Context, ds internal.DataSource, query string) string {
+ urlSchemeIdx := strings.Index(query, "://")
+ if urlSchemeIdx > -1 {
+ query = query[urlSchemeIdx+3:]
+ }
requestedPath := path.Clean(query)
if !strings.Contains(requestedPath, "/") {
return ""
diff --git a/internal/frontend/search_test.go b/internal/frontend/search_test.go
index b754210..b5f49a3 100644
--- a/internal/frontend/search_test.go
+++ b/internal/frontend/search_test.go
@@ -231,6 +231,7 @@
{"stdlib directory does redirect", "cmd/go/internal", "/cmd/go/internal"},
{"std does not redirect", "std", ""},
{"non-existent path does not redirect", "github.com/non-existent", ""},
+ {"trim URL scheme from query", "https://golang.org/x/tools", "/mod/golang.org/x/tools"},
} {
t.Run(tc.name, func(t *testing.T) {
if got := searchRequestRedirectPath(ctx, testDB, tc.query); got != tc.want {