[bugfix] Parse links that contain non-ascii characters (#2762)

This commit is contained in:
tobi 2024-03-15 18:26:53 +01:00 committed by GitHub
parent e6e696ae22
commit 0362d49da0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 2 deletions

View file

@ -24,6 +24,7 @@ import (
"codeberg.org/gruf/go-byteutil" "codeberg.org/gruf/go-byteutil"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/regexes"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html" "github.com/yuin/goldmark/renderer/html"
@ -61,7 +62,10 @@ func (f *Formatter) FromMarkdown(
false, // emojiOnly = false. false, // emojiOnly = false.
result, result,
}, },
extension.Linkify, // Turns URLs into links. // Turns URLs into links.
extension.NewLinkify(
extension.WithLinkifyURLRegexp(regexes.LinkScheme),
),
extension.Strikethrough, extension.Strikethrough,
), ),
) )

View file

@ -24,6 +24,7 @@ import (
"codeberg.org/gruf/go-byteutil" "codeberg.org/gruf/go-byteutil"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/regexes"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/parser"
@ -158,7 +159,10 @@ func (f *Formatter) fromPlain(
emojiOnly, emojiOnly,
result, result,
}, },
extension.Linkify, // Turns URLs into links. // Turns URLs into links.
extension.NewLinkify(
extension.WithLinkifyURLRegexp(regexes.LinkScheme),
),
), ),
) )

View file

@ -34,6 +34,8 @@ const (
withHTMLExpected = "<p>&lt;div>blah this should just be html escaped blah&lt;/div></p>" withHTMLExpected = "<p>&lt;div>blah this should just be html escaped blah&lt;/div></p>"
moreComplex = "Another test @foss_satan@fossbros-anonymous.io\n\n#Hashtag\n\nText\n\n:rainbow:" moreComplex = "Another test @foss_satan@fossbros-anonymous.io\n\n#Hashtag\n\nText\n\n:rainbow:"
moreComplexExpected = "<p>Another test <span class=\"h-card\"><a href=\"http://fossbros-anonymous.io/@foss_satan\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>foss_satan</span></a></span><br><br><a href=\"http://localhost:8080/tags/hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a><br><br>Text<br><br>:rainbow:</p>" moreComplexExpected = "<p>Another test <span class=\"h-card\"><a href=\"http://fossbros-anonymous.io/@foss_satan\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>foss_satan</span></a></span><br><br><a href=\"http://localhost:8080/tags/hashtag\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>Hashtag</span></a><br><br>Text<br><br>:rainbow:</p>"
withUTF8Link = "here's a link with utf-8 characters in it: https://example.org/söme_url"
withUTF8LinkExpected = "<p>here's a link with utf-8 characters in it: <a href=\"https://example.org/s%C3%B6me_url\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">https://example.org/söme_url</a></p>"
) )
type PlainTestSuite struct { type PlainTestSuite struct {
@ -70,6 +72,11 @@ func (suite *PlainTestSuite) TestParseMoreComplex() {
suite.Equal(moreComplexExpected, formatted.HTML) suite.Equal(moreComplexExpected, formatted.HTML)
} }
func (suite *PlainTestSuite) TestWithUTF8Link() {
formatted := suite.FromPlain(withUTF8Link)
suite.Equal(withUTF8LinkExpected, formatted.HTML)
}
func (suite *PlainTestSuite) TestLinkNoMention() { func (suite *PlainTestSuite) TestLinkNoMention() {
statusText := `here's a link to a post by zork statusText := `here's a link to a post by zork