Merge branch 'master' of https://github.com/eteeselink/daux.io into development

* 'master' of https://github.com/eteeselink/daux.io:
  Add support for anchors on internal links

# Conflicts:
#	libs/ContentTypes/Markdown/LinkRenderer.php
This commit is contained in:
Stéphane Goetz 2016-09-21 21:49:55 +02:00
commit f8806b18b7
2 changed files with 17 additions and 3 deletions

View File

@ -58,11 +58,13 @@ 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) { protected function isValidUrl($url)
{
return !empty($url) && $url[0] != '#'; return !empty($url) && $url[0] != '#';
} }
protected function isExternalUrl($url) { protected function isExternalUrl($url)
{
return preg_match('|^(?:[a-z]+:)?//|', $url); return preg_match('|^(?:[a-z]+:)?//|', $url);
} }
@ -101,10 +103,15 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
return $element; return $element;
} }
// if there's a hash component in the url, ensure we
// don't put that part through the resolver.
$urlAndHash = explode("#", $url);
$url = $urlAndHash[0];
try { try {
$file = $this->resolveInternalFile($url); $file = $this->resolveInternalFile($url);
$url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl()); $url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
} catch(LinkNotFoundException $e) { } catch (LinkNotFoundException $e) {
if ($this->daux->isStatic()) { if ($this->daux->isStatic()) {
throw $e; throw $e;
} }
@ -112,6 +119,10 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
$element->setAttribute('class', 'broken'); $element->setAttribute('class', 'broken');
} }
if (isset($urlAndHash[1])) {
$url .= "#" . $urlAndHash[1];
}
$element->setAttribute('href', $url); $element->setAttribute('href', $url);
return $element; return $element;

View File

@ -45,6 +45,9 @@ class LinkRendererTest extends \PHPUnit_Framework_TestCase
['<a href="Button.html">Link</a>', '[Link](./Button)', 'Widgets/Page.html'], ['<a href="Button.html">Link</a>', '[Link](./Button)', 'Widgets/Page.html'],
['<a href="Button.html">Link</a>', '[Link](!Widgets/Button)', 'Widgets/Page.html'], ['<a href="Button.html">Link</a>', '[Link](!Widgets/Button)', 'Widgets/Page.html'],
['<a href="Button.html#Test">Link</a>', '[Link](./Button#Test)', 'Widgets/Page.html'],
['<a href="Button.html#Test">Link</a>', '[Link](!Widgets/Button#Test)', 'Widgets/Page.html'],
// /Content/Page // /Content/Page
['<a href="../Widgets/Button.html">Link</a>', '[Link](../Widgets/Button.md)', 'Content/Page.html'], ['<a href="../Widgets/Button.html">Link</a>', '[Link](../Widgets/Button.md)', 'Content/Page.html'],
['<a href="../Widgets/Button.html">Link</a>', '[Link](!Widgets/Button)', 'Content/Page.html'], ['<a href="../Widgets/Button.html">Link</a>', '[Link](!Widgets/Button)', 'Content/Page.html'],