Skip to content

Commit

Permalink
x/pkgsite: remove protocol from source code links
Browse files Browse the repository at this point in the history
A new template function for stripping scheme from URL applied to repository
URL in the Details block.

Fixes golang/go#40943

Change-Id: Ie767e90c3763e8792544b5df62ce79a5cf00374f
GitHub-Last-Rev: a2b0ca2
GitHub-Pull-Request: #26
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/310229
Trust: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
  • Loading branch information
Stexxe authored and jba committed Apr 19, 2021
1 parent 594a8c6 commit 06dca04
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion content/static/html/helpers/_unit_meta.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div class="UnitMeta-repo">
{{if .Details.RepositoryURL}}
<a href="{{.Details.RepositoryURL}}" title="{{.Details.RepositoryURL}}" target="_blank" rel="noopener">
{{.Details.RepositoryURL}}
{{stripscheme .Details.RepositoryURL}}
</a>
{{else}}
Repository URL not available.
Expand Down
9 changes: 9 additions & 0 deletions internal/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,15 @@ var templateFuncs = template.FuncMap{
"commaseparate": func(s []string) string {
return strings.Join(s, ", ")
},
"stripscheme": stripscheme,
}

func stripscheme(url string) string {
if i := strings.Index(url, "://"); i > 0 {
return url[i+len("://"):]
}

return url
}

// parsePageTemplates parses html templates contained in the given base
Expand Down
16 changes: 16 additions & 0 deletions internal/frontend/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1626,3 +1626,19 @@ func TestEmptyDirectoryBetweenNestedModulesRedirect(t *testing.T) {
})
}
}

func TestStripScheme(t *testing.T) {
cases := map[string]string{
"http://github.com": "github.com",
"https://github.com/path/to/something": "github.com/path/to/something",
"example.com": "example.com",
"chrome-extension://abcd": "abcd",
"nonwellformed.com/path?://query=1": "query=1",
}

for url, expected := range cases {
if actual := stripscheme(url); actual != expected {
t.Errorf("Failed for %q: got %q, want %q", url, actual, expected)
}
}
}

0 comments on commit 06dca04

Please sign in to comment.