Skip to content

Commit

Permalink
don't allow underscores and asterisks as end chars
Browse files Browse the repository at this point in the history
This way, we match URLs within markdown emphasis like *this* or _this_
without the trailing character.

It's highly unlikely that URLs would genuinely want to end in either of
those characters. Asterisks are reserved, so they shouldn't be in the
path to begin with. And underscores are most common in the middle of the
path, and are already common to style plaintext at the start or end of
URLs.

Still allow them in the middle of URLs, because there's no reason not
to.

Fixes #29.
  • Loading branch information
mvdan committed Sep 22, 2019
1 parent 2427ea9 commit 06587e4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions xurls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const (
iriChar = letter + mark + number
currency = `\p{Sc}`
otherSymb = `\p{So}`
endChar = iriChar + `/\-+_&~*%=#` + currency + otherSymb
endChar = iriChar + `/\-+&~%=#` + currency + otherSymb
otherPunc = `\p{Po}`
midChar = endChar + otherPunc
midChar = endChar + "_*" + otherPunc
wellParen = `\([` + midChar + `]*(\([` + midChar + `]*\)[` + midChar + `]*)*\)`
wellBrack = `\[[` + midChar + `]*(\[[` + midChar + `]*\][` + midChar + `]*)*\]`
wellBrace = `\{[` + midChar + `]*(\{[` + midChar + `]*\}[` + midChar + `]*)*\}`
Expand Down
7 changes: 5 additions & 2 deletions xurls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,26 @@ var constantTestCases = []testCase{
{`.http://foo.com/bar.more`, `http://foo.com/bar.more`},
{`,http://foo.com/bar,`, `http://foo.com/bar`},
{`,http://foo.com/bar,more`, `http://foo.com/bar,more`},
{`*http://foo.com/bar*`, `http://foo.com/bar`},
{`*http://foo.com/bar*more`, `http://foo.com/bar*more`},
{`_http://foo.com/bar_`, `http://foo.com/bar`},
{`_http://foo.com/bar_more`, `http://foo.com/bar_more`},
{`(http://foo.com/bar)`, `http://foo.com/bar`},
{`(http://foo.com/bar)more`, `http://foo.com/bar`},
{`[http://foo.com/bar]`, `http://foo.com/bar`},
{`[http://foo.com/bar]more`, `http://foo.com/bar`},
{`'http://foo.com/bar'`, `http://foo.com/bar`},
{`'http://foo.com/bar'more`, `http://foo.com/bar'more`},
{`"http://foo.com/bar"`, `http://foo.com/bar`},
{`"http://foo.com/bar"more`, `http://foo.com/bar"more`},
{`http://a.b/a0/-+_&~*%=#@.,:;'?![]()a`, true},
{`http://a.b/a0/$€¥`, true},
{`http://✪foo.bar/pa✪th©more`, true},
{`http://foo.bar/path/`, true},
{`http://foo.bar/path-`, true},
{`http://foo.bar/path+`, true},
{`http://foo.bar/path_`, true},
{`http://foo.bar/path&`, true},
{`http://foo.bar/path~`, true},
{`http://foo.bar/path*`, true},
{`http://foo.bar/path%`, true},
{`http://foo.bar/path=`, true},
{`http://foo.bar/path#`, true},
Expand Down

0 comments on commit 06587e4

Please sign in to comment.