Skip to content

Commit

Permalink
x/pkgsite: Trim HTTP or HTTPS Scheme from Source Code links
Browse files Browse the repository at this point in the history
Trimming the URL by creating a new template function which trims the
scheme of the URL

Fixes golang/go#40943
  • Loading branch information
Rahul Wadhwani committed Aug 27, 2020
1 parent 7427310 commit d642347
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions content/static/html/pages/overview.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
<h2>Source Code</h2>
<p class="Overview-sourceCodeLink">
{{if .RepositoryURL}}
Repository: <a href="{{.RepositoryURL}}" target="_blank" rel="noopener">{{.RepositoryURL}}</a><br/>
Repository: <a href="{{.RepositoryURL}}" target="_blank" rel="noopener">{{removeURLScheme .RepositoryURL}}</a><br/>
{{else}}
Source code link not available.
{{end}}
{{if .PackageSourceURL}}
Package: <a href="{{.PackageSourceURL}}" target="_blank" rel="noopener">{{.PackageSourceURL}}</a>
Package: <a href="{{.PackageSourceURL}}" target="_blank" rel="noopener">{{removeURLScheme .PackageSourceURL}}</a>
{{end}}
</p>
</div>
Expand Down
16 changes: 16 additions & 0 deletions internal/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d642347

Please sign in to comment.