diff --git a/src/markup/engine/__tests__/remarkup/link-square.txt b/src/markup/engine/__tests__/remarkup/link-square.txt --- a/src/markup/engine/__tests__/remarkup/link-square.txt +++ b/src/markup/engine/__tests__/remarkup/link-square.txt @@ -2,7 +2,7 @@ [[http://www.example.com/ | example.com]] -[[/]] +[[/x/]] [[#anchor]] @@ -12,7 +12,7 @@

example.com

-

http://www.example.com/

+

http://www.example.com/x/

http://www.example.com/page/#anchor

@@ -22,7 +22,7 @@ example.com -http://www.example.com/ +http://www.example.com/x/ http://www.example.com/page/#anchor diff --git a/src/markup/engine/remarkup/markuprule/PhutilRemarkupDocumentLinkRule.php b/src/markup/engine/remarkup/markuprule/PhutilRemarkupDocumentLinkRule.php --- a/src/markup/engine/remarkup/markuprule/PhutilRemarkupDocumentLinkRule.php +++ b/src/markup/engine/remarkup/markuprule/PhutilRemarkupDocumentLinkRule.php @@ -15,7 +15,26 @@ // Handle markdown-style links: [name](href) $text = preg_replace_callback( - '@\B\\[([^\\]]+)\\]\\(([^\\)]+)\\)\B@U', + '@'. + '\B'. + '\\[([^\\]]+)\\]'. + '\\('. + '(\s*'. + // See T12343. This is making some kind of effort to implement + // parenthesis balancing rules. It won't get nested parentheses + // right, but should do OK for Wikipedia pages, which seem to be + // the most important use case. + + // Match zero or more non-parenthesis, non-space characters. + '[^\s()]*'. + // Match zero or more sequences of "(...)", where two balanced + // parentheses enclose zero or more normal characters. If we + // match some, optionally match more stuff at the end. + '(?:(?:\\([^ ()]*\\))+[^\s()]*)?'. + '\s*)'. + '\\)'. + '\B'. + '@U', array($this, 'markupAlternateLink'), $text); @@ -94,6 +113,10 @@ return $matches[0]; } + if (!strlen($uri[0])) { + return $matches[0]; + } + if (strpos($uri, '/') === false && strpos($uri, '@') === false && strncmp($uri, 'tel:', 4)) { @@ -134,6 +157,13 @@ } } + // As a special case, skip "[[ / ]]" so that Phriction picks it up as a + // link to the Phriction root. It is more useful to be able to use this + // syntax to link to the root document than the home page of the install. + if ($uri == '/') { + $is_uri = false; + } + if (!$is_uri) { return $matches[0]; }