mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
Currently, links inside blog articles are absolute links to golang.org. But when a godoc server is run locally, the blog package should serve local links pointing to the local godoc server. It is not possible to simply change the links inside the blog templates to relative urls because the blog articles are independant pages on their own. And moreover, they are served from blog.golang.org. Rather, the blog package consumes and serves blog articles. So, a flag was added in the Config struct to denote whether to convert the links or not. This flag is then set from the call site in godoc package where the blog server is initialized from. This was required because "golang.org/x/tools/blog" is a package which can be used by other code to serve blog pages and not just godoc. This preserves existing functionality for all working code which imports "golang.org/x/tools/blog" and changes the functionality only when a godoc server is run locally. And while here, replace relevant bytes.Buffer occurences with strings.Builder. Fixes golang/go#22681 Change-Id: I7dbf9c5f2f93fd0b7e17915238de1c084fcd1431 Reviewed-on: https://go-review.googlesource.com/105835 Reviewed-by: Andrew Bonventre <andybons@golang.org> Run-TryBot: Andrew Bonventre <andybons@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
45 lines
1.9 KiB
Go
45 lines
1.9 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package blog
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLinkRewrite(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
output string
|
|
}{
|
|
{
|
|
`For instance, the <a href="http://golang.org/pkg/bytes/" target="_blank">bytes package</a> from the standard library exports the <code>Buffer</code> type.`,
|
|
`For instance, the <a href="/pkg/bytes/" target="_blank">bytes package</a> from the standard library exports the <code>Buffer</code> type.`},
|
|
{
|
|
`(The <a href="http://golang.org/cmd/gofmt/" target="_blank">gofmt command</a> has a <code>-r</code> flag that provides a syntax-aware search and replace, making large-scale refactoring easier.)`,
|
|
`(The <a href="/cmd/gofmt/" target="_blank">gofmt command</a> has a <code>-r</code> flag that provides a syntax-aware search and replace, making large-scale refactoring easier.)`,
|
|
},
|
|
{
|
|
`<a href="//golang.org/LICENSE">BSD license</a>.<br> <a href="//golang.org/doc/tos.html">Terms of Service</a> `,
|
|
`<a href="//golang.org/LICENSE">BSD license</a>.<br> <a href="//golang.org/doc/tos.html">Terms of Service</a> `,
|
|
},
|
|
{
|
|
`For instance, the <code>websocket</code> package from the <code>go.net</code> sub-repository has an import path of <code>"golang.org/x/net/websocket"</code>.`,
|
|
`For instance, the <code>websocket</code> package from the <code>go.net</code> sub-repository has an import path of <code>"golang.org/x/net/websocket"</code>.`,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
var buf strings.Builder
|
|
_, err := golangOrgAbsLinkReplacer.WriteString(&buf, test.input)
|
|
if err != nil {
|
|
t.Errorf("unexpected error during replacing links. Got: %#v, Want: nil.\n", err)
|
|
continue
|
|
}
|
|
if got, want := buf.String(), test.output; got != want {
|
|
t.Errorf("WriteString(%q) = %q. Expected: %q", test.input, got, want)
|
|
}
|
|
}
|
|
}
|