From d642347990e1d7f6aa212bd8780f7181177d198f Mon Sep 17 00:00:00 2001 From: Rahul Wadhwani Date: Fri, 28 Aug 2020 00:03:47 +0100 Subject: [PATCH] x/pkgsite: Trim HTTP or HTTPS Scheme from Source Code links Trimming the URL by creating a new template function which trims the scheme of the URL Fixes golang/go#40943 --- content/static/html/pages/overview.tmpl | 4 ++-- internal/frontend/server.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/content/static/html/pages/overview.tmpl b/content/static/html/pages/overview.tmpl index fe6f3fe43..d8afd19f8 100644 --- a/content/static/html/pages/overview.tmpl +++ b/content/static/html/pages/overview.tmpl @@ -19,12 +19,12 @@

Source Code

diff --git a/internal/frontend/server.go b/internal/frontend/server.go index 451ff57da..88558544f 100644 --- a/internal/frontend/server.go +++ b/internal/frontend/server.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "net/http" + "net/url" "strings" "sync" "time" @@ -437,6 +438,20 @@ func executeTemplate(ctx context.Context, templateName string, tmpl *template.Te return buf.Bytes(), nil } +// removeURLScheme removes schemes from the URL +func removeURLScheme(urlString string) (string, error) { + baseURL, err := url.Parse(urlString) + if err != nil { + return baseURL.String(), fmt.Errorf("Error parsing URL: %q", baseURL.String()) + } + scheme := baseURL.Scheme + if len(scheme) > 0 { + urlWithoutScheme := strings.Replace(baseURL.String(), scheme, "", 1) + return strings.Replace(urlWithoutScheme, "://", "", 1), nil + } + return baseURL.String(), nil +} + // parsePageTemplates parses html templates contained in the given base // directory in order to generate a map of Name->*template.Template. // @@ -477,6 +492,7 @@ func parsePageTemplates(base template.TrustedSource) (map[string]*template.Templ "commaseparate": func(s []string) string { return strings.Join(s, ", ") }, + "removeURLScheme": removeURLScheme, }).ParseFilesFromTrustedSources(join(base, tsc("base.tmpl"))) if err != nil { return nil, fmt.Errorf("ParseFiles: %v", err)