Link hashtag bug (#121)

* link + hashtag bug

* remove printlns

* tidy up some duplicated code
This commit is contained in:
Tobi Smethurst
2021-07-29 13:18:22 +02:00
committed by GitHub
parent ea8ad8b346
commit a940a520d3
15 changed files with 349 additions and 97 deletions

View File

@ -29,7 +29,6 @@ import (
//
// It will look for fully-qualified account names in the form "@user@example.org".
// or the form "@username" for local users.
// The case of the returned mentions will be lowered, for consistency.
func DeriveMentionsFromStatus(status string) []string {
mentionedAccounts := []string{}
for _, m := range mentionFinderRegex.FindAllStringSubmatch(status, -1) {
@ -44,16 +43,15 @@ func DeriveMentionsFromStatus(status string) []string {
// tags will be lowered, for consistency.
func DeriveHashtagsFromStatus(status string) []string {
tags := []string{}
for _, m := range hashtagFinderRegex.FindAllStringSubmatch(status, -1) {
tags = append(tags, m[1])
for _, m := range HashtagFinderRegex.FindAllStringSubmatch(status, -1) {
tags = append(tags, strings.TrimPrefix(m[1], "#"))
}
return unique(tags)
return uniqueLower(tags)
}
// DeriveEmojisFromStatus takes a plaintext (ie., not html-formatted) status,
// and applies a regex to it to return a deduplicated list of emojis
// used in that status, without the surround ::. The case of the returned
// emojis will be lowered, for consistency.
// used in that status, without the surround ::.
func DeriveEmojisFromStatus(status string) []string {
emojis := []string{}
for _, m := range emojiFinderRegex.FindAllStringSubmatch(status, -1) {
@ -94,3 +92,17 @@ func unique(s []string) []string {
}
return list
}
// uniqueLower returns a deduplicated version of a given string slice, with all entries converted to lowercase
func uniqueLower(s []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range s {
eLower := strings.ToLower(entry)
if _, value := keys[eLower]; !value {
keys[eLower] = true
list = append(list, eLower)
}
}
return list
}