Skip to content

Commit

Permalink
Fix: #3763 - source citations that are URLs should be links
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Mar 19, 2021
1 parent ecef0b8 commit a3287c6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 43 deletions.
31 changes: 6 additions & 25 deletions app/Elements/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,12 @@
use Fisharebest\Webtrees\Html;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Tree;
use League\CommonMark\Block\Element\Document;
use League\CommonMark\Block\Element\Paragraph;
use League\CommonMark\Block\Renderer\DocumentRenderer;
use League\CommonMark\Block\Renderer\ParagraphRenderer;
use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment;
use League\CommonMark\Inline\Element\Link;
use League\CommonMark\Inline\Element\Text;
use League\CommonMark\Inline\Parser\AutolinkParser;
use League\CommonMark\Inline\Renderer\LinkRenderer;
use League\CommonMark\Inline\Renderer\TextRenderer;

use function array_key_exists;
use function array_map;
use function e;
use function is_numeric;
use function preg_replace;
use function preg_match;
use function strpos;
use function trim;
use function view;
Expand Down Expand Up @@ -271,21 +260,13 @@ public function values(): array
*/
protected function valueAutoLink(string $value): string
{
// Convert URLs into markdown auto-links.
$value = preg_replace(self::REGEX_URL, '<$0>', $value);

// Create a minimal commonmark processor - just add support for autolinks.
$environment = new Environment();
$environment
->addBlockRenderer(Document::class, new DocumentRenderer())
->addBlockRenderer(Paragraph::class, new ParagraphRenderer())
->addInlineRenderer(Text::class, new TextRenderer())
->addInlineRenderer(Link::class, new LinkRenderer())
->addInlineParser(new AutolinkParser());
$canonical = $this->canonical($value);

$converter = new CommonMarkConverter(['html_input' => Environment::HTML_INPUT_ESCAPE], $environment);
if (preg_match(static::REGEX_URL, $canonical)) {
return '<a href="' . e($canonical) . '" rel="no-follow">' . e($canonical) . '</a>';
}

return $converter->convertToHtml($value);
return e($canonical);
}

/**
Expand Down
8 changes: 1 addition & 7 deletions app/Elements/MultimediaFileReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ public function canonical(string $value): string
*/
public function value(string $value, Tree $tree): string
{
$canonical = $this->canonical($value);

if (preg_match(static::REGEX_URL, $canonical)) {
return '<a href="' . e($canonical) . '">' . e($canonical) . '</a>';
}

return parent::value($value, $tree);
return $this->valueAutoLink($value);
}
}
4 changes: 1 addition & 3 deletions app/Elements/SourceCallNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class SourceCallNumber extends AbstractElement
*/
public function value(string $value, Tree $tree): string
{
$canonical = $this->canonical($value);

return $this->valueAutoLink($canonical);
return $this->valueAutoLink($value);
}
}
4 changes: 1 addition & 3 deletions app/Elements/SourcePublicationFacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class SourcePublicationFacts extends AbstractElement
*/
public function value(string $value, Tree $tree): string
{
$canonical = $this->canonical($value);

return $this->valueAutoLink($canonical);
return $this->valueAutoLink($value);
}
}
13 changes: 13 additions & 0 deletions app/Elements/WhereWithinSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,17 @@ public function edit(string $id, string $name, string $value, Tree $tree): strin
{
return '<input data-autocomplete-url="' . e(route(AutoCompleteCitation::class, ['tree' => $tree->name()])) . '" data-autocomplete-extra="SOUR" autocomplete="off" class="form-control" type="text" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" />';
}

/**
* Display the value of this type of element.
*
* @param string $value
* @param Tree $tree
*
* @return string
*/
public function value(string $value, Tree $tree): string
{
return $this->valueAutoLink($value);
}
}
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ parameters:
count: 1
path: app/Date/AbstractCalendarDate.php

-
message: "#^Parameter \\#1 \\$commonMark of method League\\\\CommonMark\\\\Converter\\:\\:convertToHtml\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: app/Elements/AbstractElement.php

-
message: "#^Method Fisharebest\\\\Webtrees\\\\Elements\\\\DateValue\\:\\:escape\\(\\) should return string but returns string\\|null\\.$#"
count: 1
Expand Down

0 comments on commit a3287c6

Please sign in to comment.