From 636a4d9ad0e7600098727fa93c665cd187611151 Mon Sep 17 00:00:00 2001 From: Vyr Cossont Date: Thu, 30 Jan 2025 15:15:54 -0800 Subject: [PATCH] Disallow just numbers + marks + underscore as hashtag --- internal/text/plain_test.go | 34 ++++++++++++++++++++-------------- internal/text/util.go | 6 ++---- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/internal/text/plain_test.go b/internal/text/plain_test.go index 50b13cc4d..ffa64ce44 100644 --- a/internal/text/plain_test.go +++ b/internal/text/plain_test.go @@ -118,20 +118,20 @@ func (suite *PlainTestSuite) TestDeriveHashtagsOK() { ` tags := suite.FromPlain(statusText).Tags - suite.Len(tags, 13) - suite.Equal("testing123", tags[0].Name) - suite.Equal("also", tags[1].Name) - suite.Equal("thisshouldwork", tags[2].Name) - suite.Equal("dupe", tags[3].Name) - suite.Equal("ThisShouldAlsoWork", tags[4].Name) - suite.Equal("this_should_not_be_split", tags[5].Name) - suite.Equal("111111", tags[6].Name) - suite.Equal("alimentación", tags[7].Name) - suite.Equal("saúde", tags[8].Name) - suite.Equal("lävistää", tags[9].Name) - suite.Equal("ö", tags[10].Name) - suite.Equal("네", tags[11].Name) - suite.Equal("ThisOneIsThirteyCharactersLong", tags[12].Name) + if suite.Len(tags, 12) { + suite.Equal("testing123", tags[0].Name) + suite.Equal("also", tags[1].Name) + suite.Equal("thisshouldwork", tags[2].Name) + suite.Equal("dupe", tags[3].Name) + suite.Equal("ThisShouldAlsoWork", tags[4].Name) + suite.Equal("this_should_not_be_split", tags[5].Name) + suite.Equal("alimentación", tags[6].Name) + suite.Equal("saúde", tags[7].Name) + suite.Equal("lävistää", tags[8].Name) + suite.Equal("ö", tags[9].Name) + suite.Equal("네", tags[10].Name) + suite.Equal("ThisOneIsThirteyCharactersLong", tags[11].Name) + } statusText = `#올빼미 hej` tags = suite.FromPlain(statusText).Tags @@ -177,6 +177,12 @@ func (suite *PlainTestSuite) TestZalgoHashtag() { } } +func (suite *PlainTestSuite) TestNumbersAreNotHashtags() { + statusText := `yo who else thinks #19_98 is #1?` + f := suite.FromPlain(statusText) + suite.Len(f.Tags, 0) +} + func TestPlainTestSuite(t *testing.T) { suite.Run(t, new(PlainTestSuite)) } diff --git a/internal/text/util.go b/internal/text/util.go index 6178766a6..47b2416dd 100644 --- a/internal/text/util.go +++ b/internal/text/util.go @@ -20,15 +20,13 @@ package text import "unicode" func isPermittedInHashtag(r rune) bool { - return unicode.IsLetter(r) || - unicode.IsNumber(r) || - isPermittedIfNotEntireHashtag(r) + return unicode.IsLetter(r) || isPermittedIfNotEntireHashtag(r) } // isPermittedIfNotEntireHashtag is true for characters that may be in a hashtag // but are not allowed to be the only characters making up the hashtag. func isPermittedIfNotEntireHashtag(r rune) bool { - return unicode.IsMark(r) || r == '_' + return unicode.IsNumber(r) || unicode.IsMark(r) || r == '_' } // isHashtagBoundary returns true if rune r