From 1efc1c94df8753a4821ad12e7c14137c68c69891 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Mon, 23 Mar 2020 10:05:15 +0100 Subject: fix links rewrting in code blocks --- app/class/Modelrender.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'app/class') diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php index 2338eb4..c3103b6 100644 --- a/app/class/Modelrender.php +++ b/app/class/Modelrender.php @@ -347,7 +347,9 @@ class Modelrender extends Modelpage $text = $this->desctitle($text, $this->page->description(), $this->page->title()); - $text = str_replace('href="http', ' class="external" target="_blank" href="http', $text); + $text = str_replace('href="http', "class=\"external\" $this->externallinkblank href=\"http", $text); + + $text = $this->shortenurl($text); $text = $this->autourl($text); @@ -365,10 +367,25 @@ class Modelrender extends Modelpage return $text; } + /** + * Shorten the urls of links whose content equals the href. + * + * @param string $text the page text as html + */ + public function shortenurl(string $text): string + { + $text = preg_replace('#\2#', "$3", $text); + return $text; + } + public function autourl($text) { - $text = preg_replace('#( |\R|>)(https?:\/\/((\S+)\.([^< ]+)))#', '$1externallinkblank .'>$3', $text); + $text = preg_replace( + '#( |\R|(>)|(<))(https?:\/\/(\S+\.[^< ]+))(((?(3)>|))(?(2)externallinkblank>$4$6", + $text + ); return $text; } -- cgit v1.2.3