mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
absolute link checking
This commit is contained in:
parent
f3b752d831
commit
4d1bf3a0ca
@ -63,8 +63,8 @@ func checkLinks(filePath string, fileBytes []byte) ([]byte, error) {
|
|||||||
return in
|
return in
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.Host != "" {
|
if u.Host != "" && u.Host != "github.com" {
|
||||||
// We only care about relative links.
|
// We only care about relative links and links within github.
|
||||||
return in
|
return in
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,6 +78,14 @@ func checkLinks(filePath string, fileBytes []byte) ([]byte, error) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
u.Path = newPath
|
u.Path = newPath
|
||||||
|
if strings.HasPrefix(u.Path, "/") {
|
||||||
|
u.Host = "github.com"
|
||||||
|
u.Scheme = "https"
|
||||||
|
} else {
|
||||||
|
// Remove host and scheme from relative paths
|
||||||
|
u.Host = ""
|
||||||
|
u.Scheme = ""
|
||||||
|
}
|
||||||
// Make the visible text show the absolute path if it's
|
// Make the visible text show the absolute path if it's
|
||||||
// not nested in or beneath the current directory.
|
// not nested in or beneath the current directory.
|
||||||
if strings.HasPrefix(u.Path, "..") {
|
if strings.HasPrefix(u.Path, "..") {
|
||||||
@ -95,7 +103,7 @@ func checkLinks(filePath string, fileBytes []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
// If the current visible text is trying to be a file name, use
|
// If the current visible text is trying to be a file name, use
|
||||||
// the correct file name.
|
// the correct file name.
|
||||||
if (strings.Contains(visibleText, ".md") || strings.Contains(visibleText, "/")) && !strings.ContainsAny(visibleText, ` '"`+"`") {
|
if strings.HasSuffix(visibleText, ".md") && !strings.ContainsAny(visibleText, ` '"`+"`") {
|
||||||
visibleText = suggestedVisibleText
|
visibleText = suggestedVisibleText
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,13 +137,25 @@ func cleanPath(dirPath, linkPath string) string {
|
|||||||
|
|
||||||
func checkPath(filePath, linkPath string) (newPath string, ok bool) {
|
func checkPath(filePath, linkPath string) (newPath string, ok bool) {
|
||||||
dir := path.Dir(filePath)
|
dir := path.Dir(filePath)
|
||||||
if strings.HasPrefix(linkPath, "/") {
|
absFilePrefixes := []string{
|
||||||
if !strings.HasPrefix(linkPath, "/GoogleCloudPlatform") {
|
"/GoogleCloudPlatform/kubernetes/blob/master/",
|
||||||
// Any absolute paths that aren't relative to github.com are wrong.
|
"/GoogleCloudPlatform/kubernetes/tree/master/",
|
||||||
// Try to fix.
|
}
|
||||||
linkPath = linkPath[1:]
|
for _, prefix := range absFilePrefixes {
|
||||||
|
if strings.HasPrefix(linkPath, prefix) {
|
||||||
|
linkPath = strings.TrimPrefix(linkPath, prefix)
|
||||||
|
// Now linkPath is relative to the root of the repo. The below
|
||||||
|
// loop that adds ../ at the beginning of the path should find
|
||||||
|
// the right path.
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(linkPath, "/") {
|
||||||
|
// These links might go to e.g. the github issues page, or a
|
||||||
|
// file at a particular revision, or another github project
|
||||||
|
// entirely.
|
||||||
|
return linkPath, true
|
||||||
|
}
|
||||||
linkPath = cleanPath(dir, linkPath)
|
linkPath = cleanPath(dir, linkPath)
|
||||||
|
|
||||||
// Fast exit if the link is already correct.
|
// Fast exit if the link is already correct.
|
||||||
@ -174,7 +194,7 @@ func checkPath(filePath, linkPath string) (newPath string, ok bool) {
|
|||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
return newPath + "/", true
|
return newPath + "/", true
|
||||||
}
|
}
|
||||||
return newPath, true
|
return cleanPath(dir, newPath), true
|
||||||
}
|
}
|
||||||
newPath = path.Join("..", newPath)
|
newPath = path.Join("..", newPath)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user