From 15e21eeba89d2cc5305c636f6a0ec4c232dec852 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Mon, 2 Dec 2024 20:38:24 +0330 Subject: [PATCH] Add getHTML method --- .../Message/Entities/TextWithEntities.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/EventHandler/Message/Entities/TextWithEntities.php b/src/EventHandler/Message/Entities/TextWithEntities.php index a5f6634f2..90ea7398b 100644 --- a/src/EventHandler/Message/Entities/TextWithEntities.php +++ b/src/EventHandler/Message/Entities/TextWithEntities.php @@ -17,6 +17,7 @@ namespace danog\MadelineProto\EventHandler\Message\Entities; use danog\MadelineProto\ParseMode; +use danog\MadelineProto\StrTools; use JsonSerializable; final class TextWithEntities implements JsonSerializable @@ -42,4 +43,25 @@ final class TextWithEntities implements JsonSerializable } return $res; } + + protected readonly string $html; + protected readonly string $htmlTelegram; + + /** + * Get an HTML version of the message. + * + * @psalm-suppress InaccessibleProperty + * + * @param bool $allowTelegramTags Whether to allow telegram-specific tags like tg-spoiler, tg-emoji, mention links and so on... + */ + public function getHTML(bool $allowTelegramTags = false): string + { + if (!$this->entities) { + return StrTools::htmlEscape($this->text); + } + if ($allowTelegramTags) { + return $this->htmlTelegram ??= StrTools::entitiesToHtml($this->text, MessageEntity::fromRawEntities($this->entities), $allowTelegramTags); + } + return $this->html ??= StrTools::entitiesToHtml($this->text, MessageEntity::fromRawEntities($this->entities), $allowTelegramTags); + } }