From 0362d49da0d09784f9b974718839f5e69df3bae1 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Fri, 15 Mar 2024 18:26:53 +0100 Subject: [PATCH] [bugfix] Parse links that contain non-ascii characters (#2762) --- internal/text/markdown.go | 6 +++++- internal/text/plain.go | 6 +++++- internal/text/plain_test.go | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/internal/text/markdown.go b/internal/text/markdown.go index 05d0a02f9..50cd6a141 100644 --- a/internal/text/markdown.go +++ b/internal/text/markdown.go @@ -24,6 +24,7 @@ import ( "codeberg.org/gruf/go-byteutil" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/log" + "github.com/superseriousbusiness/gotosocial/internal/regexes" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/renderer/html" @@ -61,7 +62,10 @@ func (f *Formatter) FromMarkdown( false, // emojiOnly = false. result, }, - extension.Linkify, // Turns URLs into links. + // Turns URLs into links. + extension.NewLinkify( + extension.WithLinkifyURLRegexp(regexes.LinkScheme), + ), extension.Strikethrough, ), ) diff --git a/internal/text/plain.go b/internal/text/plain.go index 1935cec8c..362941773 100644 --- a/internal/text/plain.go +++ b/internal/text/plain.go @@ -24,6 +24,7 @@ import ( "codeberg.org/gruf/go-byteutil" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/log" + "github.com/superseriousbusiness/gotosocial/internal/regexes" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/parser" @@ -158,7 +159,10 @@ func (f *Formatter) fromPlain( emojiOnly, result, }, - extension.Linkify, // Turns URLs into links. + // Turns URLs into links. + extension.NewLinkify( + extension.WithLinkifyURLRegexp(regexes.LinkScheme), + ), ), ) diff --git a/internal/text/plain_test.go b/internal/text/plain_test.go index 43cc588c5..48280bb44 100644 --- a/internal/text/plain_test.go +++ b/internal/text/plain_test.go @@ -34,6 +34,8 @@ const ( 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:

" + withUTF8Link = "here's a link with utf-8 characters in it: https://example.org/söme_url" + withUTF8LinkExpected = "

here's a link with utf-8 characters in it: https://example.org/söme_url

" ) type PlainTestSuite struct { @@ -70,6 +72,11 @@ func (suite *PlainTestSuite) TestParseMoreComplex() { suite.Equal(moreComplexExpected, formatted.HTML) } +func (suite *PlainTestSuite) TestWithUTF8Link() { + formatted := suite.FromPlain(withUTF8Link) + suite.Equal(withUTF8LinkExpected, formatted.HTML) +} + func (suite *PlainTestSuite) TestLinkNoMention() { statusText := `here's a link to a post by zork