diff --git a/telers/src/utils/text/builder.rs b/telers/src/utils/text/builder.rs index 1ca15ba..633a467 100644 --- a/telers/src/utils/text/builder.rs +++ b/telers/src/utils/text/builder.rs @@ -240,6 +240,31 @@ where .expect("Failed to add spoiler. Report this issue to the developers") } + /// # Warning + /// If the given text length is greater than [`u16::MAX`], then the text will be truncated. + #[must_use] + pub fn blockquote(self, text: impl AsRef) -> Self { + let text = text.as_ref(); + let entity = MessageEntity::new_blockquote(self.text.len() as u16, text.len() as u16); + + self.text(text) + .entity(&entity) + .expect("Failed to add blockquote. Report this issue to the developers") + } + + /// # Warning + /// If the given text length is greater than [`u16::MAX`], then the text will be truncated. + #[must_use] + pub fn expandable_blockquote(self, text: impl AsRef) -> Self { + let text = text.as_ref(); + let entity = + MessageEntity::new_expandable_blockquote(self.text.len() as u16, text.len() as u16); + + self.text(text) + .entity(&entity) + .expect("Failed to add expandable blockquote. Report this issue to the developers") + } + /// Add code as monowidth string. /// # Arguments /// * `code` - Code that will be added as monowidth string. @@ -432,7 +457,11 @@ mod tests { .text(" ") .text_mention("text_mention", User::default()) .text(" ") - .custom_emoji("custom_emoji", "emoji_id"); + .custom_emoji("custom_emoji", "emoji_id") + .text(" ") + .blockquote("blockquote") + .text(" ") + .expandable_blockquote("expandable_blockquote"); assert_eq!( builder.get_text(), @@ -444,7 +473,9 @@ mod tests {
pre_language
\ text_link \ text_mention \ - custom_emoji\ + custom_emoji \ +
blockquote
\ +
expandable_blockquote
\ " ); } diff --git a/telers/src/utils/text/formatter.rs b/telers/src/utils/text/formatter.rs index f2eb004..5470fe9 100644 --- a/telers/src/utils/text/formatter.rs +++ b/telers/src/utils/text/formatter.rs @@ -56,6 +56,11 @@ pub trait Formatter { where T: AsRef; + #[must_use] + fn expandable_blockquote(&self, text: T) -> String + where + T: AsRef; + #[must_use] fn text_link(&self, text: T, url: U) -> String where @@ -223,6 +228,13 @@ mod tests { todo!() } + fn expandable_blockquote(&self, _text: T) -> String + where + T: AsRef, + { + todo!() + } + fn text_link(&self, _text: T, _url: U) -> String where T: AsRef, diff --git a/telers/src/utils/text/html_formatter.rs b/telers/src/utils/text/html_formatter.rs index 074a840..1a341e2 100644 --- a/telers/src/utils/text/html_formatter.rs +++ b/telers/src/utils/text/html_formatter.rs @@ -128,6 +128,16 @@ impl TextFormatter for Formatter { format!("
{text}
", text = text.as_ref()) } + fn expandable_blockquote(&self, text: T) -> String + where + T: AsRef, + { + format!( + "
{text}
", + text = text.as_ref() + ) + } + fn text_link(&self, text: T, url: U) -> String where T: AsRef, @@ -235,7 +245,7 @@ impl TextFormatter for Formatter { MessageEntityKind::Strikethrough => self.strikethrough(editable_text), MessageEntityKind::Spoiler => self.spoiler(editable_text), MessageEntityKind::Blockquote => self.blockquote(editable_text), - MessageEntityKind::ExpandableBlockquote => self.blockquote(editable_text), + MessageEntityKind::ExpandableBlockquote => self.expandable_blockquote(editable_text), MessageEntityKind::Code => self.code(editable_text), MessageEntityKind::Pre(PreMessageEntity { language }) => match language { Some(language) => self.pre_language(editable_text, language), @@ -282,6 +292,10 @@ pub fn blockquote(text: impl AsRef) -> String { FORMATTER.blockquote(text) } +pub fn expandable_blockquote(text: impl AsRef) -> String { + FORMATTER.expandable_blockquote(text) +} + pub fn text_link(text: impl AsRef, url: impl AsRef) -> String { FORMATTER.text_link(text, url) } @@ -353,6 +367,15 @@ mod tests { ); } + #[test] + fn test_expandable_blockquote() { + let formatter = Formatter::default(); + assert_eq!( + formatter.expandable_blockquote("text"), + "
text
" + ); + } + #[test] fn test_text_link() { let formatter = Formatter::default(); diff --git a/telers/src/utils/text/markdown_formatter.rs b/telers/src/utils/text/markdown_formatter.rs index ba8fca9..c6325ca 100644 --- a/telers/src/utils/text/markdown_formatter.rs +++ b/telers/src/utils/text/markdown_formatter.rs @@ -85,6 +85,16 @@ impl TextFormatter for Formatter { .join("\n") } + fn expandable_blockquote(&self, text: T) -> String + where + T: AsRef, + { + let mut text = self.blockquote(text); + text.push_str("||"); + + text + } + fn text_link(&self, text: T, url: U) -> String where T: AsRef, @@ -180,7 +190,7 @@ impl TextFormatter for Formatter { MessageEntityKind::Strikethrough => self.strikethrough(editable_text), MessageEntityKind::Spoiler => self.spoiler(editable_text), MessageEntityKind::Blockquote => self.blockquote(editable_text), - MessageEntityKind::ExpandableBlockquote => self.blockquote(editable_text), + MessageEntityKind::ExpandableBlockquote => self.expandable_blockquote(editable_text), MessageEntityKind::Code => self.code(editable_text), MessageEntityKind::Pre(PreMessageEntity { language }) => match language { Some(language) => self.pre_language(editable_text, language), @@ -227,6 +237,10 @@ pub fn blockquote(text: impl AsRef) -> String { FORMATTER.blockquote(text) } +pub fn expandable_blockquote(text: impl AsRef) -> String { + FORMATTER.expandable_blockquote(text) +} + pub fn text_link(text: impl AsRef, url: &str) -> String { FORMATTER.text_link(text, url) } @@ -296,6 +310,16 @@ mod tests { assert_eq!(formatter.blockquote("text\ntext"), ">text\n>text"); } + #[test] + fn expandable_blockquote() { + let formatter = Formatter::default(); + assert_eq!(formatter.expandable_blockquote("text"), ">text||"); + assert_eq!( + formatter.expandable_blockquote("text\ntext"), + ">text\n>text||" + ); + } + #[test] fn test_text_link() { let formatter = Formatter::default();