Skip to content

Commit

Permalink
🐛 (template): Fix hashtag not transformed into link if at beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 12, 2023
1 parent 935a2c0 commit d9bf0b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
18 changes: 14 additions & 4 deletions internal/template_funcs/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func FormatUrls(s string) string {
var (
hashtagRe = regexp.MustCompile("(^|\n| )#[A-Za-z0-9]+")
hashtagTmpl = template.Must(
template.New("").Parse(`{{ .prefix }}<a href="https://youtube.com/hashtag/{{ .hashtag }}">{{ .text }}</a>`),
template.New("").Parse(`{{ .prefix }}<a href="https://youtube.com/hashtag/{{ .slug }}">{{ .text }}</a>`),
)
)

Expand All @@ -66,10 +66,20 @@ func FormatHashtags(s string) string {

var buf bytes.Buffer
for _, hashtag := range hashtags {
prefix := string(hashtag[0])
slug := hashtag[2:]
text := hashtag[1:]

if prefix == "#" {
prefix = ""
slug = text
text = hashtag
}

if err := hashtagTmpl.Execute(&buf, map[string]string{
"prefix": string(hashtag[0]),
"hashtag": hashtag[2:],
"text": hashtag[1:],
"prefix": prefix,
"slug": slug,
"text": text,
}); err != nil {
continue
}
Expand Down
1 change: 1 addition & 0 deletions internal/template_funcs/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestFormatHashtags(t *testing.T) {
}{
{"no change", args{"test"}, "test"},
{"hashtag", args{" #test"}, ` <a href="https://youtube.com/hashtag/test">#test</a>`},
{"no prefix", args{"#test"}, `<a href="https://youtube.com/hashtag/test">#test</a>`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit d9bf0b8

Please sign in to comment.