Page MenuHomePhabricator

D16006.diff
No OneTemporary

D16006.diff

diff --git a/src/markup/engine/__tests__/PhutilRemarkupEngineTestCase.php b/src/markup/engine/__tests__/PhutilRemarkupEngineTestCase.php
--- a/src/markup/engine/__tests__/PhutilRemarkupEngineTestCase.php
+++ b/src/markup/engine/__tests__/PhutilRemarkupEngineTestCase.php
@@ -38,6 +38,13 @@
case 'toc.txt':
$engine->setConfig('header.generate-toc', true);
break;
+ case 'link-same-window.txt':
+ $engine->setConfig('uri.same-window', true);
+ break;
+ case 'link-square.txt':
+ $engine->setConfig('uri.base', 'http://www.example.com/');
+ $engine->setConfig('uri.here', 'http://www.example.com/page/');
+ break;
}
$actual_output = (string)$engine->markupText($input_remarkup);
@@ -66,7 +73,6 @@
private function buildNewTestEngine() {
$engine = new PhutilRemarkupEngine();
- $engine->setConfig('uri.prefix', 'http://www.example.com/');
$engine->setConfig(
'uri.allowed-protocols',
diff --git a/src/markup/engine/__tests__/remarkup/link-same-window.txt b/src/markup/engine/__tests__/remarkup/link-same-window.txt
new file mode 100644
--- /dev/null
+++ b/src/markup/engine/__tests__/remarkup/link-same-window.txt
@@ -0,0 +1,11 @@
+[[http://www.example.com/]]
+
+http://www.example.com/
+~~~~~~~~~~
+<p><a href="http://www.example.com/" class="remarkup-link" rel="noreferrer">http://www.example.com/</a></p>
+
+<p><a href="http://www.example.com/" class="remarkup-link" rel="noreferrer">http://www.example.com/</a></p>
+~~~~~~~~~~
+http://www.example.com/
+
+http://www.example.com/
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
@@ -12,11 +12,11 @@
<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">example.com</a></p>
-<p><a href="/" class="remarkup-link" target="_blank">/</a></p>
+<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a></p>
-<p><a href="#anchor" class="remarkup-link">#anchor</a></p>
+<p><a href="http://www.example.com/page/#anchor" class="remarkup-link" rel="noreferrer">http://www.example.com/page/#anchor</a></p>
-<p><a href="#anchor" class="remarkup-link">Anchors</a></p>
+<p><a href="http://www.example.com/page/#anchor" class="remarkup-link" rel="noreferrer">Anchors</a></p>
~~~~~~~~~~
http://www.example.com/
@@ -24,6 +24,6 @@
http://www.example.com/
-http://www.example.com/#anchor
+http://www.example.com/page/#anchor
-Anchors <http://www.example.com/#anchor>
+Anchors <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
@@ -23,55 +23,60 @@
}
protected function renderHyperlink($link, $name) {
- if ($this->getEngine()->isTextMode()) {
- $text = $link;
- if (strncmp($link, '/', 1) == 0 || strncmp($link, '#', 1) == 0) {
- $base = $this->getEngine()->getConfig('uri.prefix');
- if (strncmp($link, '/', 1) == 0) {
- $base = rtrim($base, '/');
- }
- $text = $base.$text;
- }
+ $engine = $this->getEngine();
+
+ $is_anchor = false;
+ if (strncmp($link, '/', 1) == 0) {
+ $base = $engine->getConfig('uri.base');
+ $base = rtrim($base, '/');
+ $link = $base.$link;
+ } else if (strncmp($link, '#', 1) == 0) {
+ $here = $engine->getConfig('uri.here');
+ $link = $here.$link;
+
+ $is_anchor = true;
+ }
+ if ($engine->isTextMode()) {
// If present, strip off "mailto:" or "tel:".
- $text = preg_replace('/^(?:mailto|tel):/', '', $text);
+ $link = preg_replace('/^(?:mailto|tel):/', '', $link);
- if ($link == $name) {
- return $text;
+ if (!strlen($name)) {
+ return $link;
}
- return $name.' <'.$text.'>';
- } else if ($this->getEngine()->isHTMLMailMode()) {
- if (strncmp($link, '/', 1) == 0 || strncmp($link, '#', 1) == 0) {
- $base = $this->getEngine()->getConfig('uri.base');
- $text = $link;
- if (strncmp($link, '/', 1) == 0) {
- $base = rtrim($base, '/');
- }
- $link = $base.$text;
- }
- }
- // By default, we open links in a new window or tab. For anchors on the same
- // page, just jump normally.
- $target = '_blank';
- if (strncmp($link, '#', 1) == 0) {
- $target = null;
+ return $name.' <'.$link.'>';
}
- $name = preg_replace('/^(?:mailto|tel):/', '', $name);
+ if (!strlen($name)) {
+ $name = $link;
+ $name = preg_replace('/^(?:mailto|tel):/', '', $name);
+ }
- if ($this->getEngine()->getState('toc')) {
+ if ($engine->getState('toc')) {
return $name;
+ }
+
+ $same_window = $engine->getConfig('uri.same-window', false);
+ if ($same_window) {
+ $target = null;
} else {
- return phutil_tag(
- 'a',
- array(
- 'href' => $link,
- 'class' => 'remarkup-link',
- 'target' => $target,
- ),
- $name);
+ $target = '_blank';
}
+
+ // For anchors on the same page, always stay here.
+ if ($is_anchor) {
+ $target = null;
+ }
+
+ return phutil_tag(
+ 'a',
+ array(
+ 'href' => $link,
+ 'class' => 'remarkup-link',
+ 'target' => $target,
+ ),
+ $name);
}
public function markupAlternateLink(array $matches) {
@@ -105,7 +110,7 @@
public function markupDocumentLink(array $matches) {
$uri = trim($matches[1]);
- $name = trim(idx($matches, 2, $uri));
+ $name = trim(idx($matches, 2));
// If whatever is being linked to begins with "/" or "#", or has "://",
// or is "mailto:" or "tel:", treat it as a URI instead of a wiki page.
diff --git a/src/markup/engine/remarkup/markuprule/PhutilRemarkupHyperlinkRule.php b/src/markup/engine/remarkup/markuprule/PhutilRemarkupHyperlinkRule.php
--- a/src/markup/engine/remarkup/markuprule/PhutilRemarkupHyperlinkRule.php
+++ b/src/markup/engine/remarkup/markuprule/PhutilRemarkupHyperlinkRule.php
@@ -51,22 +51,31 @@
}
protected function renderHyperlink($link) {
- if ($this->getEngine()->isTextMode()) {
+ $engine = $this->getEngine();
+
+ if ($engine->isTextMode()) {
return $link;
}
- if ($this->getEngine()->getState('toc')) {
+ if ($engine->getState('toc')) {
return $link;
+ }
+
+ $same_window = $engine->getConfig('uri.same-window', false);
+ if ($same_window) {
+ $target = null;
} else {
- return phutil_tag(
- 'a',
- array(
- 'href' => $link,
- 'class' => 'remarkup-link',
- 'target' => '_blank',
- ),
- $link);
+ $target = '_blank';
}
+
+ return phutil_tag(
+ 'a',
+ array(
+ 'href' => $link,
+ 'class' => 'remarkup-link',
+ 'target' => $target,
+ ),
+ $link);
}
protected function markupHyperlinkUngreedy($matches) {

File Metadata

Mime Type
text/plain
Expires
Thu, Dec 26, 3:57 PM (2 h, 43 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6929604
Default Alt Text
D16006.diff (7 KB)

Event Timeline