aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2019-10-17 11:20:02 -0400
committerJulie Qiu <julie@golang.org>2020-03-27 16:46:45 -0400
commitdfd2bbc52f8ee4437ad07e6cf5b6bf0cf755b3bb (patch)
tree5b0077e5b057743207faf625dcec9222cb797067 /internal
parent0f82af2fee2a4b85be021ab11fad83ba509084ad (diff)
downloadgo-x-pkgsite-dfd2bbc52f8ee4437ad07e6cf5b6bf0cf755b3bb.tar.xz
internal/frontend: translate relative non-image links in readmes
If a Readme has a relative link that is not an image, convert it to an absolute link to the file using source info. Fixes b/140250096. Change-Id: I9ca629963ca87d7605c0217e49671b33c7dd0f86 Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/576296 CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Julie Qiu <julieqiu@google.com>
Diffstat (limited to 'internal')
-rw-r--r--internal/frontend/overview.go19
-rw-r--r--internal/frontend/overview_test.go13
-rw-r--r--internal/source/source.go9
3 files changed, 33 insertions, 8 deletions
diff --git a/internal/frontend/overview.go b/internal/frontend/overview.go
index 2a256090..45fc11e5 100644
--- a/internal/frontend/overview.go
+++ b/internal/frontend/overview.go
@@ -77,7 +77,7 @@ func readmeHTML(vi *internal.VersionInfo) template.HTML {
b := &bytes.Buffer{}
rootNode := parser.Parse(vi.ReadmeContents)
rootNode.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
- if node.Type == blackfriday.Image {
+ if node.Type == blackfriday.Image || node.Type == blackfriday.Link {
translateRelativeLink(node, vi)
}
return renderer.RenderNode(b, node, entering)
@@ -93,14 +93,19 @@ func readmeHTML(vi *internal.VersionInfo) template.HTML {
// full repository content, in order for the image to render, we need to
// convert the relative path to an absolute URL to a hosted image.
func translateRelativeLink(node *blackfriday.Node, vi *internal.VersionInfo) {
- imageURL, err := url.Parse(string(node.LinkData.Destination))
- if err != nil || imageURL.IsAbs() {
+ destURL, err := url.Parse(string(node.LinkData.Destination))
+ if err != nil || destURL.IsAbs() {
return
}
// Paths are relative to the README location.
- imagePath := path.Join(path.Dir(vi.ReadmeFilePath), path.Clean(imageURL.Path))
- rawURL := vi.SourceInfo.RawURL(imagePath)
- if rawURL != "" {
- node.LinkData.Destination = []byte(rawURL)
+ destPath := path.Join(path.Dir(vi.ReadmeFilePath), path.Clean(destURL.Path))
+ var newURL string
+ if node.Type == blackfriday.Image {
+ newURL = vi.SourceInfo.RawURL(destPath)
+ } else {
+ newURL = vi.SourceInfo.FileURL(destPath)
+ }
+ if newURL != "" {
+ node.LinkData.Destination = []byte(newURL)
}
}
diff --git a/internal/frontend/overview_test.go b/internal/frontend/overview_test.go
index a4ee8aff..3a84c921 100644
--- a/internal/frontend/overview_test.go
+++ b/internal/frontend/overview_test.go
@@ -133,7 +133,7 @@ func TestReadmeHTML(t *testing.T) {
want: template.HTML("<p><img src=\"https://raw.githubusercontent.com/gohugoio/hugo/v0.56.3/doc/logo.png\" alt=\"Hugo logo\"/></p>\n"),
},
{
- name: "URLs relative to README directory",
+ name: "image URLs relative to README directory",
vi: &internal.VersionInfo{
ReadmeFilePath: "dir/sub/README.md",
ReadmeContents: []byte("![alt](img/thing.png)"),
@@ -143,6 +143,17 @@ func TestReadmeHTML(t *testing.T) {
},
want: template.HTML(`<p><img src="https://raw.githubusercontent.com/some/repo/v1.2.3/dir/sub/img/thing.png" alt="alt"/></p>` + "\n"),
},
+ {
+ name: "non-image links relative to README directory",
+ vi: &internal.VersionInfo{
+ ReadmeFilePath: "dir/sub/README.md",
+ ReadmeContents: []byte("[something](doc/thing.md)"),
+ Version: "v1.2.3",
+ VersionType: version.TypeRelease,
+ SourceInfo: source.NewGitHubInfo("https://github.com/some/repo", "", "v1.2.3"),
+ },
+ want: template.HTML(`<p><a href="https://github.com/some/repo/blob/v1.2.3/dir/sub/doc/thing.md" rel="nofollow">something</a></p>` + "\n"),
+ },
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
diff --git a/internal/source/source.go b/internal/source/source.go
index fa4dc551..289ffb6d 100644
--- a/internal/source/source.go
+++ b/internal/source/source.go
@@ -63,6 +63,9 @@ func (i *Info) ModuleURL() string {
// DirectoryURL returns a URL for a directory relative to the module's home directory.
func (i *Info) DirectoryURL(dir string) string {
+ if i == nil {
+ return ""
+ }
return strings.TrimSuffix(expand(i.templates.Directory, map[string]string{
"repo": i.repoURL,
"commit": i.commit,
@@ -72,6 +75,9 @@ func (i *Info) DirectoryURL(dir string) string {
// FileURL returns a URL for a file whose pathname is relative to the module's home directory.
func (i *Info) FileURL(pathname string) string {
+ if i == nil {
+ return ""
+ }
return expand(i.templates.File, map[string]string{
"repo": i.repoURL,
"commit": i.commit,
@@ -81,6 +87,9 @@ func (i *Info) FileURL(pathname string) string {
// LineURL returns a URL referring to a line in a file relative to the module's home directory.
func (i *Info) LineURL(pathname string, line int) string {
+ if i == nil {
+ return ""
+ }
return expand(i.templates.Line, map[string]string{
"repo": i.repoURL,
"commit": i.commit,