Skip to content

Commit c5274e6

Browse files
authored
improve gh bio (#32)
* Support to show bio even the text is a link * Add a new function to turn github link to id Co-authored-by: rick <[email protected]>
1 parent bca2044 commit c5274e6

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

function/github.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func GithubUserLink(id string, bio bool) (link string) {
125125

126126
// return the original text if there are Markdown style link exist
127127
if hasLink(id) {
128+
if bio {
129+
return GithubUserLink(GetIDFromGHLink(id), bio)
130+
}
128131
return
129132
}
130133

@@ -151,6 +154,12 @@ func GitHubEmojiLink(user string) (output string) {
151154
return
152155
}
153156

157+
// GetIDFromGHLink return the GitHub ID from a link
158+
func GetIDFromGHLink(link string) string {
159+
reg, _ := regexp.Compile("\\[.*\\]\\(.*/|\\)")
160+
return reg.ReplaceAllString(link, "")
161+
}
162+
154163
// hasLink determines if there are Markdown style links
155164
func hasLink(text string) (ok bool) {
156165
reg, _ := regexp.Compile(".*\\[.*\\]\\(.*\\)")

function/github_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ func TestGithubUserLink(t *testing.T) {
8484
bio: false,
8585
},
8686
want: "[name](link)",
87+
}, {
88+
name: "has Markdown style link, want bio",
89+
args: args{
90+
id: "[Rick](https://github.com/linuxsuren)",
91+
bio: true,
92+
},
93+
want: `[Rick](https://github.com/LinuxSuRen) (程序员,业余开源布道者)`,
8794
}}
8895
for _, tt := range tests {
8996
t.Run(tt.name, func(t *testing.T) {
@@ -255,3 +262,25 @@ func TestGitHubEmojiLink(t *testing.T) {
255262
})
256263
}
257264
}
265+
266+
func Test_getIDFromGHLink(t *testing.T) {
267+
type args struct {
268+
link string
269+
}
270+
tests := []struct {
271+
name string
272+
args args
273+
want string
274+
}{{
275+
name: "normal",
276+
args: args{
277+
link: "[Rick](https://github.com/LinuxSuRen)",
278+
},
279+
want: "LinuxSuRen",
280+
}}
281+
for _, tt := range tests {
282+
t.Run(tt.name, func(t *testing.T) {
283+
assert.Equalf(t, tt.want, GetIDFromGHLink(tt.args.link), "GetIDFromGHLink(%v)", tt.args.link)
284+
})
285+
}
286+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ func getFuncMap(readmeTpl string) template.FuncMap {
233233
"twitterLink": function.TwitterLink,
234234
"youTubeLink": function.YouTubeLink,
235235
"gstatic": function.GStatic,
236+
"ghID": function.GetIDFromGHLink,
236237
}
237238
}
238239

main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ fullpath`,
292292
hasError: false,
293293
expectOutput: `gh
294294
ghEmoji
295+
ghID
295296
ghs
296297
gstatic
297298
link

0 commit comments

Comments
 (0)