From 6dcbd6de401732501c612433f5b71a861da2c6c9 Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 3 Nov 2023 15:35:46 +0200 Subject: [PATCH] make description links clickable #30 --- composer.json | 4 +++- src/Twig/AppExtension.php | 18 ++++++++++++++++++ templates/default/torrent/info.html.twig | 4 ++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index a91504c..0126d61 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.16", "jdenticon/jdenticon": "^1.0", + "league/commonmark": "^2.4", "phpdocumentor/reflection-docblock": "^5.3", "phpstan/phpdoc-parser": "^1.24", "rhilip/bencode": "^2.3", @@ -45,8 +46,9 @@ "symfony/validator": "6.3.*", "symfony/web-link": "6.3.*", "symfony/yaml": "6.3.*", - "twig/extra-bundle": "^2.12|^3.0", + "twig/extra-bundle": "^3.7", "twig/intl-extra": "^3.7", + "twig/markdown-extra": "^3.7", "twig/string-extra": "^3.7", "twig/twig": "^2.12|^3.0", "yggverse/scrapeer": "^0.5.4" diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 6cf5813..6d4e987 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -39,6 +39,13 @@ class AppExtension extends AbstractExtension 'formatAgo' ] ), + new TwigFilter( + 'url_to_markdown', + [ + $this, + 'urlToMarkdown' + ] + ), ]; } @@ -135,6 +142,17 @@ class AppExtension extends AbstractExtension } } + public function urlToMarkdown( + string $text + ) : string + { + return preg_replace( + '~(https?://(?:www\.)?[^\s]+)~i', + '[$1]($1)', + $text + ); + } + private function plural(int $number, array $texts) { $cases = [2, 0, 1, 1, 1, 2]; diff --git a/templates/default/torrent/info.html.twig b/templates/default/torrent/info.html.twig index 3488f23..5aebc08 100644 --- a/templates/default/torrent/info.html.twig +++ b/templates/default/torrent/info.html.twig @@ -226,7 +226,7 @@ {{ 'Source' | trans }} - {{ file.source }} + {{ file.source | url_to_markdown | markdown_to_html }} {% endif %} @@ -246,7 +246,7 @@ {{ 'Comment' | trans }} - {{ file.comment | nl2br }} + {{ file.comment | url_to_markdown | markdown_to_html }} {% endif %}