diff --git a/src/markup/engine/__tests__/remarkup/link-alternate.txt b/src/markup/engine/__tests__/remarkup/link-alternate.txt new file mode 100644 index 0000000..87072a6 --- /dev/null +++ b/src/markup/engine/__tests__/remarkup/link-alternate.txt @@ -0,0 +1,12 @@ +[Example](http://www.example.com/) + +x[0][1](**ptr); + +~~~~~~~~~~ +

Example

+ +

x[0][1](**ptr);

+~~~~~~~~~~ +Example + +x[0][1](**ptr); diff --git a/src/markup/engine/__tests__/remarkup/link-square.txt b/src/markup/engine/__tests__/remarkup/link-square.txt index 2855005..2f255a4 100644 --- a/src/markup/engine/__tests__/remarkup/link-square.txt +++ b/src/markup/engine/__tests__/remarkup/link-square.txt @@ -1,18 +1,18 @@ [[http://www.example.com/]] [[http://www.example.com/ | example.com]] [[/]] ~~~~~~~~~~

http://www.example.com/

example.com

/

~~~~~~~~~~ -[[ http://www.example.com/ ]] +http://www.example.com/ -[[ http://www.example.com/ | example.com ]] +example.com -[[ http://www.example.com/ ]] +http://www.example.com/ diff --git a/src/markup/engine/__tests__/remarkup/simple-table-with-link.txt b/src/markup/engine/__tests__/remarkup/simple-table-with-link.txt index da243d7..7211137 100644 --- a/src/markup/engine/__tests__/remarkup/simple-table-with-link.txt +++ b/src/markup/engine/__tests__/remarkup/simple-table-with-link.txt @@ -1,7 +1,7 @@ | [[ http://example.com | name ]] | ~~~~~~~~~~
name
~~~~~~~~~~ -| [[ http://example.com | name ]] | +| name | diff --git a/src/markup/engine/remarkup/markuprule/PhutilRemarkupRuleDocumentLink.php b/src/markup/engine/remarkup/markuprule/PhutilRemarkupRuleDocumentLink.php index 27387a4..dc59781 100644 --- a/src/markup/engine/remarkup/markuprule/PhutilRemarkupRuleDocumentLink.php +++ b/src/markup/engine/remarkup/markuprule/PhutilRemarkupRuleDocumentLink.php @@ -1,70 +1,104 @@ getEngine()->isTextMode()) { $text = $link; if (strncmp($link, '/', 1) == 0) { $text = rtrim($this->getEngine()->getConfig('uri.prefix'), '/').$text; } - if ($link != $name) { - $text .= ' | '.$name; + if ($link == $name) { + return $text; } - return '[[ '.$text.' ]]'; + return $name.' <'.$text.'>'; } if ($this->getEngine()->getState('toc')) { return $name; } else { return phutil_tag( 'a', array( 'href' => $link, 'target' => '_blank', ), $name); } } + public function markupAlternateLink($matches) { + $uri = trim($matches[2]); + + // NOTE: We apply some special rules to avoid false positives here. The + // major concern is that we do not want to convert `x[0][1](y)` in a + // discussion about C source code into a link. To this end, we: + // + // - Don't match at word boundaries; + // - require the URI to contain a "/" character; and + // - reject URIs which being with a quote character. + + if ($uri[0] == '"' || $uri[0] == "'" || $uri[0] == '`') { + return $matches[0]; + } + + if (strpos($uri, '/') === false) { + return $matches[0]; + } + + return $this->markupDocumentLink( + array( + $matches[0], + $matches[2], + $matches[1], + )); + } + public function markupDocumentLink($matches) { $uri = trim($matches[1]); $name = trim(idx($matches, 2, $uri)); // If whatever is being linked to begins with "/" or has "://", treat it // as a URI instead of a wiki page. $is_uri = preg_match('@(^/)|(://)@', $uri); if ($is_uri && strncmp('/', $uri, 1)) { $protocols = $this->getEngine()->getConfig( 'uri.allowed-protocols', array()); $protocol = id(new PhutilURI($uri))->getProtocol(); if (!idx($protocols, $protocol)) { // Don't treat this as a URI if it's not an allowed protocol. $is_uri = false; } } if (!$is_uri) { return $matches[0]; } return $this->getEngine()->storeText($this->renderHyperlink($uri, $name)); } }