Fix confluence links bug

This commit is contained in:
Stéphane Goetz 2016-09-15 00:49:48 +02:00
parent aa4ad02b46
commit a40769b48a
3 changed files with 18 additions and 6 deletions

View File

@ -58,6 +58,14 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
throw new LinkNotFoundException("Could not locate file '$url'"); throw new LinkNotFoundException("Could not locate file '$url'");
} }
protected function isValidUrl($url) {
return !empty($url) && $url[0] != '#';
}
protected function isExternalUrl($url) {
return preg_match('|^(?:[a-z]+:)?//|', $url);
}
/** /**
* @param AbstractInline|Link $inline * @param AbstractInline|Link $inline
* @param ElementRendererInterface $htmlRenderer * @param ElementRendererInterface $htmlRenderer
@ -83,12 +91,12 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
// empty urls and anchors should // empty urls and anchors should
// not go through the url resolver // not go through the url resolver
if (empty($url) || $url[0] == '#') { if (!$this->isValidUrl($url)) {
return $element; return $element;
} }
// Absolute urls, shouldn't either // Absolute urls, shouldn't either
if (preg_match('|^(?:[a-z]+:)?//|', $url)) { if ($this->isExternalUrl($url)) {
$element->setAttribute('class', 'external'); $element->setAttribute('class', 'external');
return $element; return $element;
} }

View File

@ -8,7 +8,7 @@ use League\CommonMark\Inline\Element\Link;
class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
{ {
/** /**
* @param Link $inline * @param AbstractInline|Link $inline
* @param ElementRendererInterface $htmlRenderer * @param ElementRendererInterface $htmlRenderer
* *
* @return HtmlElement * @return HtmlElement
@ -28,13 +28,17 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
// Default handling // Default handling
$element = parent::render($inline, $htmlRenderer); $element = parent::render($inline, $htmlRenderer);
$url = $inline->getUrl(); $url = $inline->getUrl();
if (empty($url) || $url[0] != '!') {
// empty urls, anchors and absolute urls
// should not go through the url resolver
if (!$this->isValidUrl($url) || $this->isExternalUrl($url)) {
return $element; return $element;
} }
//Internal links //Internal links
$file = $this->resolveInternalFile(ltrim($url, '!')); $file = $this->resolveInternalFile($url);
$link_props = [ $link_props = [
'ri:content-title' => trim($this->daux['confluence']['prefix']) . ' ' . $file->getTitle(), 'ri:content-title' => trim($this->daux['confluence']['prefix']) . ' ' . $file->getTitle(),

View File

@ -37,7 +37,7 @@ class LinkRendererTest extends \PHPUnit_Framework_TestCase
{ {
return [ return [
// /Widgets/Page // /Widgets/Page
['<a href="http://google.ch">Link</a>', '[Link](http://google.ch)', 'Widgets/Page.html'], ['<a href="http://google.ch" class="external">Link</a>', '[Link](http://google.ch)', 'Widgets/Page.html'],
['<a href="#features">Link</a>', '[Link](#features)', 'Widgets/Page.html'], ['<a href="#features">Link</a>', '[Link](#features)', 'Widgets/Page.html'],
['<a href="Button.html">Link</a>', '[Link](Button.md)', 'Widgets/Page.html'], ['<a href="Button.html">Link</a>', '[Link](Button.md)', 'Widgets/Page.html'],
['<a href="Button.html">Link</a>', '[Link](./Button.md)', 'Widgets/Page.html'], ['<a href="Button.html">Link</a>', '[Link](./Button.md)', 'Widgets/Page.html'],