diff options
Diffstat (limited to 'brokenlinks/worker.go')
| -rw-r--r-- | brokenlinks/worker.go | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/brokenlinks/worker.go b/brokenlinks/worker.go index 3e089fc..7683730 100644 --- a/brokenlinks/worker.go +++ b/brokenlinks/worker.go @@ -12,6 +12,7 @@ import ( "net/http" "net/url" "os" + "path" "slices" "strings" "time" @@ -396,7 +397,7 @@ func (wrk *worker) processLink(parentUrl *url.URL, val string, kind int) ( newUrl = wrk.baseUrl.JoinPath(newUrl.Path) } else { // val is relative to parent URL. - newUrl = parentUrl.JoinPath(`/`, newUrl.Path) + newUrl = genURLRelative(parentUrl, newUrl.Path) } } linkq.Url = strings.TrimSuffix(newUrl.String(), `/`) @@ -405,3 +406,16 @@ func (wrk *worker) processLink(parentUrl *url.URL, val string, kind int) ( } return linkq } + +// genURLRelative generate new URL from parent URL and relative path +// `relPath`. +func genURLRelative(parentUrl *url.URL, relPath string) (newUrl *url.URL) { + var parentPath = parentUrl.Path + var ext = strings.ToLower(path.Ext(parentPath)) + if ext == `.html` || ext == `.htm` { + parentPath = path.Dir(parentPath) + } + newUrl, _ = url.Parse(parentUrl.String()) + newUrl.Path = path.Join(parentPath, relPath) + return newUrl +} |
