From dfd2bbc52f8ee4437ad07e6cf5b6bf0cf755b3bb Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 17 Oct 2019 11:20:02 -0400 Subject: 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 Reviewed-by: Robert Findley Reviewed-by: Julie Qiu --- internal/frontend/overview.go | 19 ++++++++++++------- internal/frontend/overview_test.go | 13 ++++++++++++- internal/source/source.go | 9 +++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) (limited to 'internal') 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("

\"Hugo

\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(`

alt

` + "\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(`

something

` + "\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, -- cgit v1.3-5-g45d5