-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks.go
26 lines (24 loc) · 860 Bytes
/
links.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
package main
import (
"regexp"
)
func AddLinks(original string, repoLink string) string {
// \[`[^\[\]]+?`\][^(]
// Matches [`...`] but not [`...`](, but annoyingly because of lack
// of lookahead it matches one extra proceeding character.
reLinkAdd := regexp.MustCompile(`\[` + "`" + `[^\[\]]+?` + "`" + `\][^(]`)
reWord := regexp.MustCompile(`\w+`)
return reLinkAdd.ReplaceAllStringFunc(original, func(s string) string {
splitString := reWord.FindAllString(s, -1)
addition := ""
if len(splitString) == 1 {
addition =
"(https://pkg.go.dev/" + repoLink + "#" + string(splitString[0]) + ")" + string(s[len(s)-1])
} else if len(splitString) >= 2 {
addition =
"(https://pkg.go.dev/" + repoLink + "#" + string(splitString[0]) + "." +
string(splitString[1]) + ")" + string(s[len(s)-1])
}
return s[:len(s)-1] + addition
})
}