From 3d7ce16f2fa3a871b4cc877103f0e270b6b4eacd Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Sat, 30 Jan 2021 08:48:27 -0600 Subject: [PATCH] Fix Linkify to allow host with beginning single-letter domain label Fixes #190 --- extension/_test/linkify.txt | 8 ++++++++ extension/linkify.go | 24 ++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/extension/_test/linkify.txt b/extension/_test/linkify.txt index d619a8b..134f52b 100644 --- a/extension/_test/linkify.txt +++ b/extension/_test/linkify.txt @@ -169,3 +169,11 @@ http://server.intranet.acme.com:1313 //- - - - - - - - -//

http://server.intranet.acme.com:1313

//= = = = = = = = = = = = = = = = = = = = = = = =// + + +17 +//- - - - - - - - -// +https://g.page/foo +//- - - - - - - - -// +

https://g.page/foo

+//= = = = = = = = = = = = = = = = = = = = = = = =// diff --git a/extension/linkify.go b/extension/linkify.go index 31203a6..42a87c8 100644 --- a/extension/linkify.go +++ b/extension/linkify.go @@ -11,9 +11,9 @@ import ( "github.com/yuin/goldmark/util" ) -var wwwURLRegxp = regexp.MustCompile(`^www\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]+(?:[/#?][-a-zA-Z0-9@:%_\+.~#!?&/=\(\);,'">\^{}\[\]` + "`" + `]*)?`) +var wwwURLRegxp = regexp.MustCompile(`^www\.[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]+(?:[/#?][-a-zA-Z0-9@:%_\+.~#!?&/=\(\);,'">\^{}\[\]` + "`" + `]*)?`) -var urlRegexp = regexp.MustCompile(`^(?:http|https|ftp)://[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]+(?::\d+)?(?:[/#?][-a-zA-Z0-9@:%_+.~#$!?&/=\(\);,'">\^{}\[\]` + "`" + `]*)?`) +var urlRegexp = regexp.MustCompile(`^(?:http|https|ftp)://[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]+(?::\d+)?(?:[/#?][-a-zA-Z0-9@:%_+.~#$!?&/=\(\);,'">\^{}\[\]` + "`" + `]*)?`) // An LinkifyConfig struct is a data structure that holds configuration of the // Linkify extension. @@ -24,10 +24,12 @@ type LinkifyConfig struct { EmailRegexp *regexp.Regexp } -const optLinkifyAllowedProtocols parser.OptionName = "LinkifyAllowedProtocols" -const optLinkifyURLRegexp parser.OptionName = "LinkifyURLRegexp" -const optLinkifyWWWRegexp parser.OptionName = "LinkifyWWWRegexp" -const optLinkifyEmailRegexp parser.OptionName = "LinkifyEmailRegexp" +const ( + optLinkifyAllowedProtocols parser.OptionName = "LinkifyAllowedProtocols" + optLinkifyURLRegexp parser.OptionName = "LinkifyURLRegexp" + optLinkifyWWWRegexp parser.OptionName = "LinkifyWWWRegexp" + optLinkifyEmailRegexp parser.OptionName = "LinkifyEmailRegexp" +) // SetOption implements SetOptioner. func (c *LinkifyConfig) SetOption(name parser.OptionName, value interface{}) { @@ -156,10 +158,12 @@ func (s *linkifyParser) Trigger() []byte { return []byte{' ', '*', '_', '~', '('} } -var protoHTTP = []byte("http:") -var protoHTTPS = []byte("https:") -var protoFTP = []byte("ftp:") -var domainWWW = []byte("www.") +var ( + protoHTTP = []byte("http:") + protoHTTPS = []byte("https:") + protoFTP = []byte("ftp:") + domainWWW = []byte("www.") +) func (s *linkifyParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node { if pc.IsInLinkLabel() {