diff --git a/modules/markup/html.go b/modules/markup/html.go index 9040d43e8ca..8ce87407488 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -66,7 +66,7 @@ var ( // matches http/https links. used for autlinking those. partly modified from // the original present in autolink.js - linkRegex = regexp.MustCompile(`(?:(?:http|https):\/\/(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+:=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?`) + linkRegex = regexp.MustCompile(`(?:(?:http|https):\/\/(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+(?:\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+:=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?`) ) // regexp for full links to issues/pulls diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index bf7606e1da4..f17d00cd67e 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -67,6 +67,68 @@ func TestMisc_IsSameDomain(t *testing.T) { assert.False(t, IsSameDomain("favicon.ico")) } +func TestRender_links(t *testing.T) { + setting.AppURL = AppURL + setting.AppSubURL = AppSubURL + + test := func(input, expected string) { + buffer := RenderString("a.md", input, setting.AppSubURL, nil) + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer))) + } + // Text that should be turned into URL + + test( + "https://www.example.com", + `

https://www.example.com

`) + test( + "http://www.example.com", + `

http://www.example.com

`) + test( + "https://example.com", + `

https://example.com

`) + test( + "http://example.com", + `

http://example.com

`) + test( + "http://foo.com/blah_blah", + `

http://foo.com/blah_blah

`) + test( + "http://foo.com/blah_blah/", + `

http://foo.com/blah_blah/

`) + test( + "http://www.example.com/wpstyle/?p=364", + `

http://www.example.com/wpstyle/?p=364

`) + test( + "https://www.example.com/foo/?bar=baz&inga=42&quux", + `

https://www.example.com/foo/?bar=baz&inga=42&quux

`) + test( + "http://142.42.1.1/", + `

http://142.42.1.1/

`) + + // Test that should *not* be turned into URL + test( + "www.example.com", + `

www.example.com

`) + test( + "example.com", + `

example.com

`) + test( + "test.example.com", + `

test.example.com

`) + test( + "http://", + `

http://

`) + test( + "https://", + `

https://

`) + test( + "://", + `

://

`) + test( + "www", + `

www

`) +} + func TestRender_ShortLinks(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL