Skip to content

Commit

Permalink
chore(internal/godocfx): add URL helpers (#3654)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbpg committed Feb 2, 2021
1 parent d6ee080 commit e88cbc4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 64 deletions.
25 changes: 20 additions & 5 deletions internal/godocfx/parse.go
Expand Up @@ -342,11 +342,11 @@ func (l *linker) linkify(s string) string {
// If s is not exported, it's probably a builtin.
if !token.IsExported(s) {
if doc.IsPredeclared(s) {
return fmt.Sprintf(`%s<a href="https://pkg.go.dev/builtin#%s">%s</a>`, prefix, strings.ToLower(s), s)
return href(toURL("builtin", s), s)
}
return fmt.Sprintf("%s%s", prefix, s)
}
return fmt.Sprintf(`%s<a href="#%s">%s</a>`, prefix, strings.ToLower(s), s)
return fmt.Sprintf("%s%s", prefix, href(toURL("", s), s))
}
// Otherwise, it's in another package.
split := strings.Split(s, ".")
Expand All @@ -362,9 +362,24 @@ func (l *linker) linkify(s string) string {
return fmt.Sprintf("%s%s", prefix, s)
}
name := split[1]
pkgLink := fmt.Sprintf("http://pkg.go.dev/%s", pkgPath)
link := fmt.Sprintf("%s#%s", pkgLink, name)
return fmt.Sprintf("%s<a href=%q>%s</a>.<a href=%q>%s</a>", prefix, pkgLink, pkg, link, name)
return fmt.Sprintf("%s%s.%s", prefix, href(toURL(pkgPath, ""), pkg), href(toURL(pkgPath, name), name))
}

// TODO: link to the right baseURL, with the right module name and version
// pattern.
func toURL(pkg, name string) string {
if pkg == "" {
return fmt.Sprintf("#%s", strings.ToLower(name))
}
baseURL := "https://pkg.go.dev"
if name == "" {
return fmt.Sprintf("%s/%s", baseURL, pkg)
}
return fmt.Sprintf("%s/%s#%s", baseURL, pkg, name)
}

func href(url, text string) string {
return fmt.Sprintf(`<a href="%s">%s</a>`, url, text)
}

// processExamples converts the examples to []example.
Expand Down

0 comments on commit e88cbc4

Please sign in to comment.