mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
Link hashtag bug (#121)
* link + hashtag bug * remove printlns * tidy up some duplicated code
This commit is contained in:
@@ -21,6 +21,9 @@ package text
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||
)
|
||||
|
||||
// preformat contains some common logic for making a string ready for formatting, which should be used for all user-input text.
|
||||
@@ -35,7 +38,7 @@ func preformat(in string) string {
|
||||
func postformat(in string) string {
|
||||
// do some postformatting of the text
|
||||
// 1. sanitize html to remove any dodgy scripts or other disallowed elements
|
||||
s := SanitizeHTML(in)
|
||||
s := SanitizeOutgoing(in)
|
||||
// 2. wrap the whole thing in a paragraph
|
||||
s = fmt.Sprintf(`<p>%s</p>`, s)
|
||||
// 3. remove any cheeky newlines
|
||||
@@ -44,3 +47,29 @@ func postformat(in string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
return s
|
||||
}
|
||||
|
||||
func (f *formatter) ReplaceTags(in string, tags []*gtsmodel.Tag) string {
|
||||
return util.HashtagFinderRegex.ReplaceAllStringFunc(in, func(match string) string {
|
||||
for _, tag := range tags {
|
||||
if strings.TrimSpace(match) == fmt.Sprintf("#%s", tag.Name) {
|
||||
tagContent := fmt.Sprintf(`<a href="%s" class="mention hashtag" rel="tag">#<span>%s</span></a>`, tag.URL, tag.Name)
|
||||
if strings.HasPrefix(match, " ") {
|
||||
tagContent = " " + tagContent
|
||||
}
|
||||
return tagContent
|
||||
}
|
||||
}
|
||||
return in
|
||||
})
|
||||
}
|
||||
|
||||
func (f *formatter) ReplaceMentions(in string, mentions []*gtsmodel.Mention) string {
|
||||
for _, menchie := range mentions {
|
||||
targetAccount := >smodel.Account{}
|
||||
if err := f.db.GetByID(menchie.TargetAccountID, targetAccount); err == nil {
|
||||
mentionContent := fmt.Sprintf(`<span class="h-card"><a href="%s" class="u-url mention">@<span>%s</span></a></span>`, targetAccount.URL, targetAccount.Username)
|
||||
in = strings.ReplaceAll(in, menchie.NameString, mentionContent)
|
||||
}
|
||||
}
|
||||
return in
|
||||
}
|
||||
|
Reference in New Issue
Block a user