Adds up one dir relative linking fix

This commit is contained in:
sloum 2020-05-26 15:10:12 -07:00
parent f793bdd806
commit cba3682c27
1 changed files with 21 additions and 14 deletions

View File

@ -417,25 +417,33 @@ func parseGemini(b, rootUrl, currentUrl string) (string, []string) {
return strings.Join(splitContent[:outputIndex], "\n"), links return strings.Join(splitContent[:outputIndex], "\n"), links
} }
// handleRelativeUrl provides link completion
// takes:
// - u : the relative link as written on the page
// - root : the root url for the site
// - current : the current url a user is on
func handleRelativeUrl(u, root, current string) string { func handleRelativeUrl(u, root, current string) string {
if len(u) < 1 { if len(u) < 1 {
return u return u
} }
currentIsDir := (current[len(current)-1] == '/') currentIsDir := strings.HasSuffix(current, "/")
if u[0] == '/' { if u[0] == '/' {
// Handle relative off of root
return fmt.Sprintf("%s%s", root, u) return fmt.Sprintf("%s%s", root, u)
} else if strings.HasPrefix(u, "../") { } else if strings.HasPrefix(u, "../") {
// Handle up one dir
currentDir := strings.LastIndex(current, "/") currentDir := strings.LastIndex(current, "/")
if currentIsDir { upOne := strings.LastIndex(current[:currentDir], "/")
upOne := strings.LastIndex(current[:currentDir], "/") dirRoot := current[:upOne]
dirRoot := current[:upOne] if upOne < len(root) {
return dirRoot + u[2:] return fmt.Sprintf("%s%s", root, u[2:])
} }
return current[:currentDir] + u[2:] return dirRoot + u[2:]
} }
if strings.HasPrefix(u, "./") { if strings.HasPrefix(u, "./") {
// Handle explicit same dir
if len(u) == 2 { if len(u) == 2 {
return current return current
} }
@ -443,16 +451,15 @@ func handleRelativeUrl(u, root, current string) string {
} }
if currentIsDir { if currentIsDir {
indPrevDir := strings.LastIndex(current[:len(current)-1], "/") // Handle non-prefixed relative link when current URL is a directory
if indPrevDir < 9 { ind := strings.LastIndex(current, "/")
return current + u current = current[:ind+1]
} return current + u
return current[:indPrevDir+1] + u
} }
ind := strings.LastIndex(current, "/") // Handle non-prefixed relative link when current URL is a file
current = current[:ind+1] indPrevDir := strings.LastIndex(current, "/")
return current + u return current[:indPrevDir+1] + u
} }
func hashCert(cert []byte) string { func hashCert(cert []byte) string {