feat: ConvertRelativeAnchorURLsToAbsolute

This commit is contained in:
httpjamesm 2024-06-13 01:56:28 -04:00
parent bcc932bd22
commit f2ccff9f1a
No known key found for this signature in database
2 changed files with 59 additions and 2 deletions

View file

@ -43,3 +43,19 @@ func ReplaceStackOverflowLinks(html string) string {
return strings.Replace(match, hrefMatch[1], newUrl, 1) return strings.Replace(match, hrefMatch[1], newUrl, 1)
}) })
} }
var relativeAnchorURLRegex = regexp.MustCompile(`href="(/[^"]+)"`)
func ConvertRelativeAnchorURLsToAbsolute(html, prefix string) string {
if prefix == "" {
return html
}
if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
return relativeAnchorURLRegex.ReplaceAllStringFunc(html, func(match string) string {
return strings.Replace(match, "href=\"/", "href=\""+prefix, 1)
})
}

View file

@ -2,9 +2,11 @@ package utils
import ( import (
"fmt" "fmt"
"github.com/stretchr/testify/assert" "log"
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
var sampleInput = `<div class="d-flex fd-column fw-nowrap"> var sampleInput = `<div class="d-flex fd-column fw-nowrap">
@ -47,3 +49,42 @@ func TestReplaceStackOverflowLinks(t *testing.T) {
assert.False(t, strings.Contains(replacedLinks, "stackoverflow.com")) assert.False(t, strings.Contains(replacedLinks, "stackoverflow.com"))
assert.False(t, strings.Contains(replacedLinks, "stackexchange.com")) assert.False(t, strings.Contains(replacedLinks, "stackexchange.com"))
} }
var sampleRelativeAnchorURLsInput = `<aside class="s-notice s-notice__info post-notice js-post-notice mb16" role="status">
<div class="d-flex fd-column fw-nowrap">
<div class="d-flex fw-nowrap">
<div class="flex--item wmn0 fl1 lh-lg">
<div class="flex--item fl1 lh-lg">
<div>
<b>This question already has an answer here</b>:
</div>
</div>
</div>
</div>
<div class="flex--item mb0 mt4">
<a href="/questions/5771/is-it-possible-to-restrict-gnu-gplv3-to-non-commercial-use-only" dir="ltr">Is it possible to restrict GNU GPLv3 to non-commercial use only?</a>
<span class="question-originals-answer-count">
(1 answer)
</span>
</div>
<div class="flex--item mb0 mt8">Closed <span title="2018-09-30 22:47:30Z" class="relativetime">5 years ago</span>.</div>
</div>
</aside>
<div class="answer-author-parent">
<div class="answer-author">
Answered Sep 27, 2018 at 21:21 by
<a href="https://notopensource.stackexchange.com/users/1212/amon" target="_blank" rel="noopener noreferrer">amon</a>
</div>
</div>
`
func TestConvertRelativeAnchorURLsToAbsolute(t *testing.T) {
prefix := "https://opensource.stackexchange.com"
fixedHTML := ConvertRelativeAnchorURLsToAbsolute(sampleRelativeAnchorURLsInput, prefix)
log.Println(fixedHTML)
assert.True(t, strings.Contains(fixedHTML, prefix))
assert.True(t, strings.Contains(fixedHTML, "https://notopensource.stackexchange.com"))
}