From 3712d1ccfba7120efcbd023e25f91f0a00c23373 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 3 Jul 2024 14:41:33 -0400 Subject: [PATCH] cmd/golangorg: fix package link URLs Doc comment text was linking to / instead of /pkg, which was creating dead links. Fix that. All other doc links were using /pkg/cmd/ instead of /cmd/. And we were serving everything in cmd in both places. Fix the links, and redirect /pkg/cmd/ to /cmd/. Change-Id: Ib5c205b3faf53aff0f0bb6d475fa942a6718e616 Reviewed-on: https://go-review.googlesource.com/c/website/+/596436 Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov --- cmd/golangorg/testdata/godev.txt | 11 +++++++++++ internal/pkgdoc/doc.go | 5 +++++ internal/pkgdoc/funcs.go | 20 ++++++++++++++++++-- internal/texthtml/ast.go | 9 +++++++-- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/cmd/golangorg/testdata/godev.txt b/cmd/golangorg/testdata/godev.txt index 9d0ce4d5..8e7cffa2 100644 --- a/cmd/golangorg/testdata/godev.txt +++ b/cmd/golangorg/testdata/godev.txt @@ -123,3 +123,14 @@ body contains This content is part of the Go Wiki. GET https://go.dev/wiki/Comments body contains Go Wiki: Comments body contains This content is part of the Go Wiki. + +GET https://go.dev/pkg/cmd/go/?m=old +redirect == /cmd/go/?m=old + +GET https://go.dev/pkg/slices/?m=old +body !contains href="/cmp +body contains href="/pkg/cmp/?m=old#Compare + +GET https://go.dev/cmd/link/internal/ld/?m=old +body !contains href="/pkg/cmd +body contains href="/cmd/link/internal/loader/?m=old#Loader diff --git a/internal/pkgdoc/doc.go b/internal/pkgdoc/doc.go index ef1712a3..e95cde77 100644 --- a/internal/pkgdoc/doc.go +++ b/internal/pkgdoc/doc.go @@ -520,10 +520,15 @@ func (p *Page) ModeQuery() string { } func maybeRedirect(w http.ResponseWriter, r *http.Request) (redirected bool) { + r.URL.Host = "" + r.URL.Scheme = "" canonical := path.Clean(r.URL.Path) if !strings.HasSuffix(canonical, "/") { canonical += "/" } + if strings.HasPrefix(canonical, "/pkg/cmd/") { + canonical = canonical[len("/pkg"):] + } if r.URL.Path != canonical { url := *r.URL url.Path = canonical diff --git a/internal/pkgdoc/funcs.go b/internal/pkgdoc/funcs.go index 66972281..f9b8dfee 100644 --- a/internal/pkgdoc/funcs.go +++ b/internal/pkgdoc/funcs.go @@ -10,6 +10,7 @@ import ( "fmt" "go/ast" "go/doc" + "go/doc/comment" "go/format" "go/printer" "go/token" @@ -154,8 +155,23 @@ func firstIdent(x []byte) string { } // Comment formats the given documentation comment as HTML. -func (p *Page) Comment(comment string) template.HTML { - return template.HTML(p.PDoc.HTML(comment)) +func (p *Page) Comment(text string) template.HTML { + pr := p.PDoc.Printer() + pr.DocLinkURL = func(link *comment.DocLink) string { + url := link.DefaultURL("/pkg/") + if strings.HasPrefix(url, "/pkg/cmd/") { + url = url[len("/pkg"):] + } + if p.OldDocs { + if base, frag, ok := strings.Cut(url, "#"); ok { + url = base + "?m=old#" + frag + } else { + url += "?m=old" + } + } + return url + } + return template.HTML(pr.HTML(p.PDoc.Parser().Parse(text))) } // sanitize sanitizes the argument src by replacing newlines with diff --git a/internal/texthtml/ast.go b/internal/texthtml/ast.go index c7e05606..1c755415 100644 --- a/internal/texthtml/ast.go +++ b/internal/texthtml/ast.go @@ -11,6 +11,7 @@ import ( "go/doc" "go/token" "strconv" + "strings" "unicode" "unicode/utf8" ) @@ -24,13 +25,17 @@ type goLink struct { } func (l *goLink) tags() (start, end string) { + prefix := "/pkg/" + if strings.HasPrefix(l.path, "cmd/") { + prefix = "/" + } switch { case l.path != "" && l.name == "": // package path - return ``, `` + return ``, `` case l.path != "" && l.name != "": // qualified identifier - return ``, `` + return ``, `` case l.path == "" && l.name != "": // local identifier if l.isVal {