Skip to content

Commit

Permalink
Merge pull request #101 from CptPie/moreLinkImprovements#91
Browse files Browse the repository at this point in the history
Cleaning up mobile link fragments from inserted links #91
  • Loading branch information
zorchenhimer authored Feb 2, 2021
2 parents 61a46cd + 99a33fc commit 2457302
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions common/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ var re_validLink = *regexp.MustCompile(`[a-zA-Z0-9:._\+]{1,256}\.[a-zA-Z0-9()]{1

func (l *Link) validateLink() error {
url := l.Url
url = cleanupLink(url)
if re_validLink.MatchString(url) {
url = stripRefFromLink(url)

if len(url) <= 8 {
// lets be stupid when the link is too short
Expand All @@ -68,11 +68,19 @@ func (l *Link) validateLink() error {
return fmt.Errorf("Invalid link: %v", l.Url)
}

func stripRefFromLink(link string) string {
var replacements = map[string]string{
"m.imdb.com": "imdb.com",
}

func cleanupLink(link string) string {
idx := strings.Index(link, "/?")
if idx != -1 {
return link[:idx]
link = link[:idx]
}
for from, to := range replacements {
link = strings.Replace(link, from, to, 1)
}

return link
}

Expand Down

0 comments on commit 2457302

Please sign in to comment.