From 71dec733b0334a5e31f18a8e9d96e1fdd1e1ffca Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Thu, 13 Jun 2024 02:07:41 -0400 Subject: [PATCH] fix: don't prepend if the prefix is already in the URL --- src/utils/links.go | 12 +++++++++++- src/utils/links_test.go | 4 +++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/utils/links.go b/src/utils/links.go index 2044fa5..4ae9bf2 100644 --- a/src/utils/links.go +++ b/src/utils/links.go @@ -56,6 +56,16 @@ func ConvertRelativeAnchorURLsToAbsolute(html, prefix string) string { } return relativeAnchorURLRegex.ReplaceAllStringFunc(html, func(match string) string { - return strings.Replace(match, "href=\"/", "href=\""+prefix, 1) + // Extract the URL from the match + url := strings.TrimPrefix(match, `href="`) + url = strings.TrimSuffix(url, `"`) + + // If the URL already has the desired prefix, return the match as is + if strings.HasPrefix(url, prefix) { + return match + } + + // Otherwise, prepend the prefix + return strings.Replace(match, `href="/`, `href="`+prefix, 1) }) } diff --git a/src/utils/links_test.go b/src/utils/links_test.go index 0c08209..9b950f2 100644 --- a/src/utils/links_test.go +++ b/src/utils/links_test.go @@ -77,10 +77,12 @@ var sampleRelativeAnchorURLsInput = `