From 49beb17a8fbdbf3517c103a477a5459a3bba404d Mon Sep 17 00:00:00 2001
From: Autumn! <86073772+autumnull@users.noreply.github.com>
Date: Fri, 3 Feb 2023 10:58:58 +0000
Subject: [chore] Text formatting overhaul (#1406)
* Implement goldmark debug print for hashtags and mentions
* Minify HTML in FromPlain
* Convert plaintext status parser to goldmark
* Move mention/tag/emoji finding logic into formatter
* Combine mention and hashtag boundary characters
* Normalize unicode when rendering hashtags
---
internal/text/plain_test.go | 125 ++++++++++++++++++++++++++++++++++----------
1 file changed, 97 insertions(+), 28 deletions(-)
(limited to 'internal/text/plain_test.go')
diff --git a/internal/text/plain_test.go b/internal/text/plain_test.go
index 6b850cb45..3693ada9a 100644
--- a/internal/text/plain_test.go
+++ b/internal/text/plain_test.go
@@ -19,22 +19,21 @@
package text_test
import (
- "context"
"testing"
+ "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
- "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
const (
- simple = "this is a plain and simple status"
- simpleExpected = "
this is a plain and simple status
"
- withTag = "here's a simple status that uses hashtag #welcome!"
- withTagExpected = "here's a simple status that uses hashtag #welcome!
"
- withHTML = "blah this should just be html escaped blah
"
- withHTMLExpected = "<div>blah this should just be html escaped blah</div>
"
- moreComplex = "Another test @foss_satan@fossbros-anonymous.io\n\n#Hashtag\n\nText"
- moreComplexFull = "Another test @foss_satan
#Hashtag
Text
"
+ simple = "this is a plain and simple status"
+ simpleExpected = "this is a plain and simple status
"
+ withTag = "here's a simple status that uses hashtag #welcome!"
+ withTagExpected = "here's a simple status that uses hashtag #welcome!
"
+ withHTML = "blah this should just be html escaped blah
"
+ withHTMLExpected = "<div>blah this should just be html escaped blah</div>
"
+ moreComplex = "Another test @foss_satan@fossbros-anonymous.io\n\n#Hashtag\n\nText\n\n:rainbow:"
+ moreComplexExpected = "Another test @foss_satan
#Hashtag
Text
:rainbow:
"
)
type PlainTestSuite struct {
@@ -42,35 +41,105 @@ type PlainTestSuite struct {
}
func (suite *PlainTestSuite) TestParseSimple() {
- f := suite.formatter.FromPlain(context.Background(), simple, nil, nil)
- suite.Equal(simpleExpected, f)
+ formatted := suite.FromPlain(simple)
+ suite.Equal(simpleExpected, formatted.HTML)
}
func (suite *PlainTestSuite) TestParseWithTag() {
- foundTags := []*gtsmodel.Tag{
- suite.testTags["welcome"],
- }
-
- f := suite.formatter.FromPlain(context.Background(), withTag, nil, foundTags)
- suite.Equal(withTagExpected, f)
+ formatted := suite.FromPlain(withTag)
+ suite.Equal(withTagExpected, formatted.HTML)
}
func (suite *PlainTestSuite) TestParseWithHTML() {
- f := suite.formatter.FromPlain(context.Background(), withHTML, nil, nil)
- suite.Equal(withHTMLExpected, f)
+ formatted := suite.FromPlain(withHTML)
+ suite.Equal(withHTMLExpected, formatted.HTML)
}
func (suite *PlainTestSuite) TestParseMoreComplex() {
- foundTags := []*gtsmodel.Tag{
- suite.testTags["Hashtag"],
- }
+ formatted := suite.FromPlain(moreComplex)
+ suite.Equal(moreComplexExpected, formatted.HTML)
+}
+
+func (suite *PlainTestSuite) TestLinkNoMention() {
+ statusText := `here's a link to a post by zork
+
+https://example.com/@the_mighty_zork/statuses/01FGVP55XMF2K6316MQRX6PFG1
+
+that link shouldn't come out formatted as a mention!`
+
+ menchies := suite.FromPlain(statusText).Mentions
+ suite.Empty(menchies)
+}
+
+func (suite *PlainTestSuite) TestDeriveMentionsEmpty() {
+ statusText := ``
+ menchies := suite.FromPlain(statusText).Mentions
+ assert.Len(suite.T(), menchies, 0)
+}
+
+func (suite *PlainTestSuite) TestDeriveHashtagsOK() {
+ statusText := `weeeeeeee #testing123 #also testing
+
+# testing this one shouldn't work
+
+ #thisshouldwork #dupe #dupe!! #dupe
+
+ here's a link with a fragment: https://example.org/whatever#ahhh
+ here's another link with a fragment: https://example.org/whatever/#ahhh
- foundMentions := []*gtsmodel.Mention{
- suite.testMentions["zork_mention_foss_satan"],
- }
+(#ThisShouldAlsoWork) #this_should_be_split
+
+#111111 thisalsoshouldn'twork#### ##
+
+#alimentación, #saúde, #lävistää, #ö, #네
+#ThisOneIsThirtyOneCharactersLon... ...ng
+#ThisOneIsThirteyCharactersLong
+`
+
+ tags := suite.FromPlain(statusText).Tags
+ assert.Len(suite.T(), tags, 13)
+ assert.Equal(suite.T(), "testing123", tags[0].Name)
+ assert.Equal(suite.T(), "also", tags[1].Name)
+ assert.Equal(suite.T(), "thisshouldwork", tags[2].Name)
+ assert.Equal(suite.T(), "dupe", tags[3].Name)
+ assert.Equal(suite.T(), "ThisShouldAlsoWork", tags[4].Name)
+ assert.Equal(suite.T(), "this", tags[5].Name)
+ assert.Equal(suite.T(), "111111", tags[6].Name)
+ assert.Equal(suite.T(), "alimentación", tags[7].Name)
+ assert.Equal(suite.T(), "saúde", tags[8].Name)
+ assert.Equal(suite.T(), "lävistää", tags[9].Name)
+ assert.Equal(suite.T(), "ö", tags[10].Name)
+ assert.Equal(suite.T(), "네", tags[11].Name)
+ assert.Equal(suite.T(), "ThisOneIsThirteyCharactersLong", tags[12].Name)
+
+ statusText = `#올빼미 hej`
+ tags = suite.FromPlain(statusText).Tags
+ assert.Equal(suite.T(), "올빼미", tags[0].Name)
+}
+
+func (suite *PlainTestSuite) TestDeriveMultiple() {
+ statusText := `Another test @foss_satan@fossbros-anonymous.io
+
+ #Hashtag
+
+ Text`
+
+ f := suite.FromPlain(statusText)
+
+ assert.Len(suite.T(), f.Mentions, 1)
+ assert.Equal(suite.T(), "@foss_satan@fossbros-anonymous.io", f.Mentions[0].NameString)
+
+ assert.Len(suite.T(), f.Tags, 1)
+ assert.Equal(suite.T(), "Hashtag", f.Tags[0].Name)
+
+ assert.Len(suite.T(), f.Emojis, 0)
+}
- f := suite.formatter.FromPlain(context.Background(), moreComplex, foundMentions, foundTags)
- suite.Equal(moreComplexFull, f)
+func (suite *PlainTestSuite) TestZalgoHashtag() {
+ statusText := `yo who else loves #praying to #z̸͉̅a̸͚͋l̵͈̊g̸̫͌ỏ̷̪?`
+ f := suite.FromPlain(statusText)
+ assert.Len(suite.T(), f.Tags, 1)
+ assert.Equal(suite.T(), "praying", f.Tags[0].Name)
}
func TestPlainTestSuite(t *testing.T) {
--
cgit v1.2.3