teloxide_ng/utils/render/helper.rs
1//! A helpful trait for rendering text/caption and entities back to HTML or
2//! Markdown.
3
4use teloxide_core_ng::types::Message;
5
6use super::Renderer;
7
8/// Generates HTML and Markdown representations of text and captions in a
9/// Telegram message.
10pub trait RenderMessageTextHelper {
11 /// Returns the HTML representation of the message text, if the message
12 /// contains text. This method will parse the text and any entities
13 /// (such as bold, italic, links, etc.) and return the HTML-formatted
14 /// string.
15 #[must_use]
16 fn html_text(&self) -> Option<String>;
17
18 /// Returns the Markdown representation of the message text, if the message
19 /// contains text. This method will parse the text and any entities
20 /// (such as bold, italic, links, etc.) and return the
21 /// Markdown-formatted string.
22 #[must_use]
23 fn markdown_text(&self) -> Option<String>;
24
25 /// Returns the HTML representation of the message caption, if the message
26 /// contains caption. This method will parse the caption and any
27 /// entities (such as bold, italic, links, etc.) and return the
28 /// HTML-formatted string.
29 #[must_use]
30 fn html_caption(&self) -> Option<String>;
31
32 /// Returns the Markdown representation of the message caption, if the
33 /// message contains caption. This method will parse the caption and any
34 /// entities (such as bold, italic, links, etc.) and return the
35 /// Markdown-formatted string.
36 #[must_use]
37 fn markdown_caption(&self) -> Option<String>;
38}
39
40impl RenderMessageTextHelper for Message {
41 fn html_text(&self) -> Option<String> {
42 self.text()
43 .zip(self.entities())
44 .map(|(text, entities)| Renderer::new(text, entities).as_html())
45 }
46
47 fn markdown_text(&self) -> Option<String> {
48 self.text()
49 .zip(self.entities())
50 .map(|(text, entities)| Renderer::new(text, entities).as_markdown())
51 }
52
53 fn html_caption(&self) -> Option<String> {
54 self.caption()
55 .zip(self.caption_entities())
56 .map(|(text, entities)| Renderer::new(text, entities).as_html())
57 }
58
59 fn markdown_caption(&self) -> Option<String> {
60 self.caption()
61 .zip(self.caption_entities())
62 .map(|(text, entities)| Renderer::new(text, entities).as_markdown())
63 }
64}