-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
36 lines (30 loc) · 1016 Bytes
/
link.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"fmt"
"log"
"net/url"
"strings"
)
// LinkMatches returns true when given
func LinkMatches(mainURL *url.URL, replacement Replacement) bool {
if mainURL.Host != replacement.HostSource {
return false
} else if !strings.HasPrefix(mainURL.RequestURI(), replacement.Path) {
return false
}
return true
}
// RewriteLink Replaces the current replacement by the target one, detects the prefix path and converts any further URL parameters into a hash path
func RewriteLink(mainURL *url.URL, replacement Replacement) string {
if !LinkMatches(mainURL, replacement) {
log.Fatalln("RewriteLink state is undefined if the link doesn't match the given replacement")
}
newHash := strings.Replace(mainURL.RequestURI(), replacement.Path, "", 1)
if newHash == "" {
return fmt.Sprintf("https://%s%s", replacement.HostTarget, replacement.Path)
}
if newHash[0] != '/' {
newHash = "/" + newHash
}
return fmt.Sprintf("https://%s%s#%s", replacement.HostTarget, replacement.Path, newHash)
}