diff options
author | Vincent Peugnet <33429034+vincent-peugnet@users.noreply.github.com> | 2020-03-23 12:53:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-23 12:53:48 +0100 |
commit | fa92ec6b40676677595047b71b3c2fafb1bf89ea (patch) | |
tree | e8f7d474a7c2d990596b72e6b8a0c3819d0c6529 /app/class/Modelrender.php | |
parent | adb3d38893acb75b838fc6ccce5edb4321d08bf9 (diff) | |
parent | 1efc1c94df8753a4821ad12e7c14137c68c69891 (diff) | |
download | wcms-fa92ec6b40676677595047b71b3c2fafb1bf89ea.tar.gz wcms-fa92ec6b40676677595047b71b3c2fafb1bf89ea.zip |
Merge pull request #73 from n-peugnet/fix-html-links-rewrite
fix links rewrting in code blocks
Diffstat (limited to 'app/class/Modelrender.php')
-rw-r--r-- | app/class/Modelrender.php | 21 |
1 files changed, 19 insertions, 2 deletions
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('#<a(.*href="(https?:\/\/(.+))".*)>\2</a>#', "<a$1>$3</a>", $text); + return $text; + } + public function autourl($text) { - $text = preg_replace('#( |\R|>)(https?:\/\/((\S+)\.([^< ]+)))#', '$1<a href="$2" class="external" '. $this->externallinkblank .'>$3</a>', $text); + $text = preg_replace( + '#( |\R|(>)|(<))(https?:\/\/(\S+\.[^< ]+))(((?(3)>|))(?(2)</[^a]|))#', + "$1<a href=\"$4\" class=\"external\" $this->externallinkblank>$4</a>$6", + $text + ); return $text; } |