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 <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Russ Cox 2024-07-03 14:41:33 -04:00
Родитель c9e6fa92a3
Коммит 3712d1ccfb
4 изменённых файлов: 41 добавлений и 4 удалений

11
cmd/golangorg/testdata/godev.txt поставляемый
Просмотреть файл

@ -123,3 +123,14 @@ body contains <i>This content is part of the <a href="/wiki/">Go Wiki</a>.</i>
GET https://go.dev/wiki/Comments
body contains Go Wiki: Comments
body contains <i>This content is part of the <a href="/wiki/">Go Wiki</a>.</i>
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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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 `<a href="/pkg/` + l.path + `/` + l.docSuffix() + `">`, `</a>`
return `<a href="` + prefix + l.path + `/` + l.docSuffix() + `">`, `</a>`
case l.path != "" && l.name != "":
// qualified identifier
return `<a href="/pkg/` + l.path + `/` + l.docSuffix() + `#` + l.name + `">`, `</a>`
return `<a href="` + prefix + l.path + `/` + l.docSuffix() + `#` + l.name + `">`, `</a>`
case l.path == "" && l.name != "":
// local identifier
if l.isVal {