Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve gh bio #32

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions function/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func GithubUserLink(id string, bio bool) (link string) {

// return the original text if there are Markdown style link exist
if hasLink(id) {
if bio {
return GithubUserLink(GetIDFromGHLink(id), bio)
}
return
}

Expand All @@ -151,6 +154,12 @@ func GitHubEmojiLink(user string) (output string) {
return
}

// GetIDFromGHLink return the GitHub ID from a link
func GetIDFromGHLink(link string) string {
reg, _ := regexp.Compile("\\[.*\\]\\(.*/|\\)")
return reg.ReplaceAllString(link, "")
}

// hasLink determines if there are Markdown style links
func hasLink(text string) (ok bool) {
reg, _ := regexp.Compile(".*\\[.*\\]\\(.*\\)")
Expand Down
29 changes: 29 additions & 0 deletions function/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func TestGithubUserLink(t *testing.T) {
bio: false,
},
want: "[name](link)",
}, {
name: "has Markdown style link, want bio",
args: args{
id: "[Rick](https://github.com/linuxsuren)",
bio: true,
},
want: `[Rick](https://github.com/LinuxSuRen) (程序员,业余开源布道者)`,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -255,3 +262,25 @@ func TestGitHubEmojiLink(t *testing.T) {
})
}
}

func Test_getIDFromGHLink(t *testing.T) {
type args struct {
link string
}
tests := []struct {
name string
args args
want string
}{{
name: "normal",
args: args{
link: "[Rick](https://github.com/LinuxSuRen)",
},
want: "LinuxSuRen",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetIDFromGHLink(tt.args.link), "GetIDFromGHLink(%v)", tt.args.link)
})
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func getFuncMap(readmeTpl string) template.FuncMap {
"twitterLink": function.TwitterLink,
"youTubeLink": function.YouTubeLink,
"gstatic": function.GStatic,
"ghID": function.GetIDFromGHLink,
}
}

Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ fullpath`,
hasError: false,
expectOutput: `gh
ghEmoji
ghID
ghs
gstatic
link
Expand Down