Show broken links in red, external link with an arrow. fixes #395

This commit is contained in:
Stéphane Goetz 2016-08-16 23:25:06 +02:00
parent 2644a7881c
commit 8ac4137007
11 changed files with 53 additions and 16 deletions

View File

@ -88,6 +88,16 @@ class Config extends ArrayObject
return array_key_exists('languages', $this) && !empty($this['languages']);
}
public function isLive()
{
return $this['mode'] == Daux::LIVE_MODE;
}
public function isStatic()
{
return $this['mode'] == Daux::STATIC_MODE;
}
public function shouldInheritIndex()
{
// As the global configuration is always present, we

View File

@ -6,7 +6,7 @@ use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Inline\Element\Link;
use Todaymade\Daux\Config;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception;
use Todaymade\Daux\Exception\LinkNotFoundException;
use Todaymade\Daux\Tree\Entry;
class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
@ -24,7 +24,7 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
/**
* @param string $url
* @return Entry
* @throws Exception
* @throws LinkNotFoundException
*/
protected function resolveInternalFile($url)
{
@ -55,14 +55,14 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
return $file;
}
throw new Exception("Could not locate file '$url'");
throw new LinkNotFoundException("Could not locate file '$url'");
}
/**
* @param AbstractInline|Link $inline
* @param ElementRendererInterface $htmlRenderer
* @return HtmlElement
* @throws Exception
* @throws LinkNotFoundException
*/
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
{
@ -81,15 +81,28 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
$url = $inline->getUrl();
// Absolute urls, empty urls and anchors
// should not go through the url resolver
if (empty($url) || $url[0] == '#' || preg_match('|^(?:[a-z]+:)?//|', $url)) {
// empty urls and anchors should
// not go through the url resolver
if (empty($url) || $url[0] == '#') {
return $element;
}
$file = $this->resolveInternalFile($url);
// Absolute urls, shouldn't either
if (preg_match('|^(?:[a-z]+:)?//|', $url)) {
$element->setAttribute('class', 'external');
return $element;
}
try {
$file = $this->resolveInternalFile($url);
$url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
} catch(LinkNotFoundException $e) {
if ($this->daux->isStatic()) {
throw $e;
}
$element->setAttribute('class', 'broken');
}
$element->setAttribute('href', $url);

View File

@ -0,0 +1,5 @@
<?php namespace Todaymade\Daux\Exception;
class LinkNotFoundException extends \Todaymade\Daux\Exception
{
}

View File

@ -110,7 +110,7 @@ class Builder
}
$uri = static::removeSortingInformations($name);
if ($config['mode'] === Daux::STATIC_MODE) {
if ($config->isStatic()) {
$uri .= '.html';
}
@ -184,7 +184,7 @@ class Builder
if (!$raw) {
$title = static::getName($path);
$uri = DauxHelper::slug($title);
if ($parent->getConfig()['mode'] === Daux::STATIC_MODE) {
if ($parent->getConfig()->isStatic()) {
$uri .= '.html';
}
}

View File

@ -106,6 +106,7 @@ blockquote {
table {
width: 100%;
padding: 0;
border-collapse: collapse;
tr {
border-top: 1px solid #eee;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,14 @@ Base tags
a {
text-decoration: none;
color: @light;
&.external::after {
content: " " url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAVklEQVR4Xn3PgQkAMQhDUXfqTu7kTtkpd5RA8AInfArtQ2iRXFWT2QedAfttj2FsPIOE1eCOlEuoWWjgzYaB/IkeGOrxXhqB+uA9Bfcm0lAZuh+YIeAD+cAqSz4kCMUAAAAASUVORK5CYII=);
}
&.broken {
color: red;
}
}
p {

File diff suppressed because one or more lines are too long