From d64377f3e35b01df22bccced8882a4a2764aa5a5 Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 6 Dec 2023 23:50:54 +0200 Subject: [PATCH] implement clickable mentions --- composer.json | 5 +++- config/bundles.php | 1 + src/Twig/AppExtension.php | 40 ++++++++++++++++++++++++++ templates/default/room/index.html.twig | 2 +- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c82578e..77db457 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "ext-iconv": "*", "jdenticon/jdenticon": "^1.0", "kevachat/kevacoin": "^1.0", + "league/commonmark": "^2.4", "symfony/console": "7.0.*", "symfony/dotenv": "7.0.*", "symfony/flex": "^2", @@ -16,7 +17,9 @@ "symfony/runtime": "7.0.*", "symfony/translation": "7.0.*", "symfony/twig-bundle": "7.0.*", - "symfony/yaml": "7.0.*" + "symfony/yaml": "7.0.*", + "twig/extra-bundle": "^3.8", + "twig/markdown-extra": "^3.8" }, "config": { "allow-plugins": { diff --git a/config/bundles.php b/config/bundles.php index bdd37ac..4ce0d70 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -3,4 +3,5 @@ return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true], + Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true], ]; diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index f637371..55bf600 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -31,6 +31,13 @@ class AppExtension extends AbstractExtension 'formatAgo' ] ), + new TwigFilter( + 'message_to_markdown', + [ + $this, + 'messageToMarkdown' + ] + ), new TwigFilter( 'url_to_markdown', [ @@ -38,6 +45,13 @@ class AppExtension extends AbstractExtension 'urlToMarkdown' ] ), + new TwigFilter( + 'mention_to_markdown', + [ + $this, + 'mentionToMarkdown' + ] + ), new TwigFilter( 'keva_namespace_value', [ @@ -119,6 +133,21 @@ class AppExtension extends AbstractExtension } } + public function messageToMarkdown( + string $text + ): string + { + $text = $this->urlToMarkdown( + $text + ); + + $text = $this->mentionToMarkdown( + $text + ); + + return $text; + } + public function urlToMarkdown( string $text ): string @@ -130,6 +159,17 @@ class AppExtension extends AbstractExtension ); } + public function mentionToMarkdown( + string $text + ): string + { + return preg_replace( + '~(@[A-z0-9]{64})~i', + '[$1](#$1)', + $text + ); + } + private function plural(int $number, array $texts) { $cases = [2, 0, 1, 1, 1, 2]; diff --git a/templates/default/room/index.html.twig b/templates/default/room/index.html.twig index 46f05f2..606ff47 100644 --- a/templates/default/room/index.html.twig +++ b/templates/default/room/index.html.twig @@ -35,7 +35,7 @@ {% endif %}

- {{ post.message }} + {{ post.message | message_to_markdown | markdown_to_html }}

{% endfor %}