From 88a54350dfbc9fa339bc6e4be7e765823958d650 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 30 Jun 2026 21:59:18 +0600 Subject: [PATCH] Rename RichText source to rawText and move it into members Replace the RichText.source extension property (a when over every subtype) with a rawText member declared on RichText and implemented directly by each inheritor: RichTextPlain.rawText = text, the wrapping entities delegate to text.rawText, and custom emoji / mathematical expression / anchor fall back to their own representation. Update the RichBlockPreformatted and media markdown builders and the formatting tests accordingly. Co-Authored-By: Claude Opus 4.8 --- tgbotapi.core/api/tgbotapi.core.api | 29 ++++++++++++++- .../types/rich/RichBlockFormatting.kt | 2 +- .../inmo/tgbotapi/types/rich/RichBlocks.kt | 2 +- .../dev/inmo/tgbotapi/types/rich/RichText.kt | 9 +++++ .../tgbotapi/types/rich/RichTextEntities.kt | 27 +++++++++++++- .../tgbotapi/types/rich/RichTextFormatting.kt | 36 ------------------- .../types/rich/RichTextFormattingTest.kt | 10 +++--- 7 files changed, 70 insertions(+), 45 deletions(-) diff --git a/tgbotapi.core/api/tgbotapi.core.api b/tgbotapi.core/api/tgbotapi.core.api index 2994b3843a..4a0a2d0415 100644 --- a/tgbotapi.core/api/tgbotapi.core.api +++ b/tgbotapi.core/api/tgbotapi.core.api @@ -35388,6 +35388,7 @@ public abstract interface class dev/inmo/tgbotapi/types/rich/RichText { public static final field Companion Ldev/inmo/tgbotapi/types/rich/RichText$Companion; public abstract fun getHtml ()Ljava/lang/String; public abstract fun getMarkdown ()Ljava/lang/String; + public abstract fun getRawText ()Ljava/lang/String; } public final class dev/inmo/tgbotapi/types/rich/RichText$Companion { @@ -35405,6 +35406,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextAnchor : dev/inmo/tgbota public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; public final fun getName ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public fun getType ()Ljava/lang/String; public fun hashCode ()I public fun toString ()Ljava/lang/String; @@ -35439,6 +35441,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextAnchorLink : dev/inmo/tg public final fun getAnchorName ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35474,6 +35477,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextBankCardNumber : dev/inm public final fun getBankCardNumber ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35507,6 +35511,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextBold : dev/inmo/tgbotapi public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35542,6 +35547,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextBotCommand : dev/inmo/tg public final fun getBotCommand ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35632,6 +35638,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextCashtag : dev/inmo/tgbot public final fun getCashtag ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35665,6 +35672,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextCode : dev/inmo/tgbotapi public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35701,6 +35709,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextCustomEmoji : dev/inmo/t public final fun getCustomEmojiId-dDnjveI ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public fun getType ()Ljava/lang/String; public fun hashCode ()I public fun toString ()Ljava/lang/String; @@ -35736,6 +35745,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextDateTime : dev/inmo/tgbo public final fun getDateTimeFormat ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public final fun getUnixTime ()J @@ -35782,6 +35792,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextEmailAddress : dev/inmo/ public final fun getEmailAddress ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35822,7 +35833,6 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextEntitySerializer : kotli public final class dev/inmo/tgbotapi/types/rich/RichTextFormattingKt { public static final fun escapeRichMarkdown (Ljava/lang/String;)Ljava/lang/String; - public static final fun getSource (Ldev/inmo/tgbotapi/types/rich/RichText;)Ljava/lang/String; } public final class dev/inmo/tgbotapi/types/rich/RichTextGroup : dev/inmo/tgbotapi/types/rich/RichText { @@ -35835,6 +35845,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextGroup : dev/inmo/tgbotap public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; public final fun getParts ()Ljava/util/List; + public fun getRawText ()Ljava/lang/String; public fun hashCode ()I public fun toString ()Ljava/lang/String; } @@ -35868,6 +35879,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextHashtag : dev/inmo/tgbot public final fun getHashtag ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35931,6 +35943,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextItalic : dev/inmo/tgbota public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35964,6 +35977,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextMarked : dev/inmo/tgbota public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -35998,6 +36012,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextMathematicalExpression : public final fun getExpression ()Ljava/lang/String; public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public fun getType ()Ljava/lang/String; public fun hashCode ()I public fun toString ()Ljava/lang/String; @@ -36031,6 +36046,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextMention : dev/inmo/tgbot public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public final fun getUsername ()Ljava/lang/String; @@ -36067,6 +36083,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextPhoneNumber : dev/inmo/t public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; public final fun getPhoneNumber ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -36099,6 +36116,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextPlain : dev/inmo/tgbotap public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ljava/lang/String; public fun hashCode ()I public fun toString ()Ljava/lang/String; @@ -36133,6 +36151,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextReference : dev/inmo/tgb public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; public final fun getName ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -36167,6 +36186,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextReferenceLink : dev/inmo public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getReferenceName ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; @@ -36210,6 +36230,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextSpoiler : dev/inmo/tgbot public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -36243,6 +36264,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextStrikethrough : dev/inmo public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -36276,6 +36298,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextSubscript : dev/inmo/tgb public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -36309,6 +36332,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextSuperscript : dev/inmo/t public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -36343,6 +36367,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextTextMention : dev/inmo/t public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public final fun getUser ()Ldev/inmo/tgbotapi/types/chat/User; @@ -36377,6 +36402,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextUnderline : dev/inmo/tgb public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public fun hashCode ()I @@ -36411,6 +36437,7 @@ public final class dev/inmo/tgbotapi/types/rich/RichTextUrl : dev/inmo/tgbotapi/ public fun equals (Ljava/lang/Object;)Z public fun getHtml ()Ljava/lang/String; public fun getMarkdown ()Ljava/lang/String; + public fun getRawText ()Ljava/lang/String; public final fun getText ()Ldev/inmo/tgbotapi/types/rich/RichText; public fun getType ()Ljava/lang/String; public final fun getUrl ()Ljava/lang/String; diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichBlockFormatting.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichBlockFormatting.kt index bfd64293ea..13d6601104 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichBlockFormatting.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichBlockFormatting.kt @@ -45,7 +45,7 @@ internal fun richMediaContainerHtml(tag: String, blocks: List, captio } internal fun richMediaMarkdown(source: String, caption: RichBlockCaption?): String = - caption?.let { "![](" + source + " \"" + it.text.source + "\")" } ?: "![]($source)" + caption?.let { "![](" + source + " \"" + it.text.rawText + "\")" } ?: "![]($source)" internal fun richMediaHtml(tag: String, source: String, spoiler: Boolean, selfClosing: Boolean, caption: RichBlockCaption?): String { val spoilerAttribute = if (spoiler) " tg-spoiler" else "" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichBlocks.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichBlocks.kt index f74b61f22b..844b48d558 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichBlocks.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichBlocks.kt @@ -109,7 +109,7 @@ data class RichBlockPreformatted( companion object { const val TYPE = "pre" - fun markdown(text: RichText, language: String?): String = "```" + (language ?: "") + "\n" + text.source + "\n```" + fun markdown(text: RichText, language: String?): String = "```" + (language ?: "") + "\n" + text.rawText + "\n```" fun html(text: RichText, language: String?): String = language?.let { "
${text.html}
" } ?: "
${text.html}
" } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichText.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichText.kt index 00dec95955..800d8dba4e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichText.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichText.kt @@ -20,6 +20,13 @@ import kotlinx.serialization.json.* @Serializable(RichTextSerializer::class) @ClassCastsIncluded sealed interface RichText { + /** + * Plain (unformatted) text of this [RichText]. For [RichTextEntity]s without an inner [RichText] it falls back to + * the most meaningful textual representation: alternative text for custom emojis, the expression for mathematical + * expressions and an empty string for anchors. + */ + val rawText: String + /** * [Rich Markdown style](https://core.telegram.org/bots/api#rich-markdown-style) representation of this [RichText]. */ @@ -38,6 +45,7 @@ sealed interface RichText { data class RichTextPlain( val text: String ) : RichText { + override val rawText: String = text override val markdown: String = markdown(text) override val html: String = html(text) @@ -54,6 +62,7 @@ data class RichTextPlain( data class RichTextGroup( val parts: List ) : RichText { + override val rawText: String = parts.joinToString(separator = "") { it.rawText } override val markdown: String = markdown(parts) override val html: String = html(parts) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichTextEntities.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichTextEntities.kt index d607b020fb..a708074dad 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichTextEntities.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichTextEntities.kt @@ -41,6 +41,7 @@ data class RichTextBold( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -65,6 +66,7 @@ data class RichTextItalic( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -89,6 +91,7 @@ data class RichTextUnderline( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -113,6 +116,7 @@ data class RichTextStrikethrough( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -137,6 +141,7 @@ data class RichTextSpoiler( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -161,6 +166,7 @@ data class RichTextSubscript( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -185,6 +191,7 @@ data class RichTextSuperscript( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -209,6 +216,7 @@ data class RichTextMarked( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -233,12 +241,13 @@ data class RichTextCode( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) companion object { const val TYPE = "code" - fun markdown(text: RichText): String = "`${text.source}`" + fun markdown(text: RichText): String = "`${text.rawText}`" fun html(text: RichText): String = "${text.html}" } } @@ -261,6 +270,7 @@ data class RichTextDateTime( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text, unixTime, dateTimeFormat) override val html: String = html(text, unixTime, dateTimeFormat) @@ -289,6 +299,7 @@ data class RichTextTextMention( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text, user) override val html: String = html(text, user) @@ -317,6 +328,7 @@ data class RichTextCustomEmoji( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = alternativeText override val markdown: String = markdown(customEmojiId, alternativeText) override val html: String = html(customEmojiId, alternativeText) @@ -343,6 +355,7 @@ data class RichTextMathematicalExpression( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = expression override val markdown: String = markdown(expression) override val html: String = html(expression) @@ -369,6 +382,7 @@ data class RichTextUrl( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text, url) override val html: String = html(text, url) @@ -395,6 +409,7 @@ data class RichTextEmailAddress( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text, emailAddress) override val html: String = html(text, emailAddress) @@ -421,6 +436,7 @@ data class RichTextPhoneNumber( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text, phoneNumber) override val html: String = html(text, phoneNumber) @@ -447,6 +463,7 @@ data class RichTextBankCardNumber( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -473,6 +490,7 @@ data class RichTextMention( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -499,6 +517,7 @@ data class RichTextHashtag( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -525,6 +544,7 @@ data class RichTextCashtag( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -551,6 +571,7 @@ data class RichTextBotCommand( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text) override val html: String = html(text) @@ -575,6 +596,7 @@ data class RichTextAnchor( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = "" override val markdown: String = markdown(name) override val html: String = html(name) @@ -601,6 +623,7 @@ data class RichTextAnchorLink( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text, anchorName) override val html: String = html(text, anchorName) @@ -627,6 +650,7 @@ data class RichTextReference( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text, name) override val html: String = html(text, name) @@ -653,6 +677,7 @@ data class RichTextReferenceLink( @SerialName(typeField) override val type: String = TYPE + override val rawText: String = text.rawText override val markdown: String = markdown(text, referenceName) override val html: String = html(text, referenceName) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichTextFormatting.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichTextFormatting.kt index 3c031ecf50..add7ec3345 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichTextFormatting.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/rich/RichTextFormatting.kt @@ -21,39 +21,3 @@ fun String.escapeRichMarkdown(): String = buildString { append(character) } } - -/** - * Plain (unformatted) text of this [RichText]. For [RichTextEntity]s without an inner [RichText] it falls back to the - * most meaningful textual representation: alternative text for custom emojis, the expression for mathematical - * expressions and an empty string for anchors. - */ -val RichText.source: String - get() = when (this) { - is RichTextPlain -> text - is RichTextGroup -> parts.joinToString(separator = "") { it.source } - is RichTextCustomEmoji -> alternativeText - is RichTextMathematicalExpression -> expression - is RichTextAnchor -> "" - is RichTextBold -> text.source - is RichTextItalic -> text.source - is RichTextUnderline -> text.source - is RichTextStrikethrough -> text.source - is RichTextSpoiler -> text.source - is RichTextSubscript -> text.source - is RichTextSuperscript -> text.source - is RichTextMarked -> text.source - is RichTextCode -> text.source - is RichTextDateTime -> text.source - is RichTextTextMention -> text.source - is RichTextUrl -> text.source - is RichTextEmailAddress -> text.source - is RichTextPhoneNumber -> text.source - is RichTextBankCardNumber -> text.source - is RichTextMention -> text.source - is RichTextHashtag -> text.source - is RichTextCashtag -> text.source - is RichTextBotCommand -> text.source - is RichTextAnchorLink -> text.source - is RichTextReference -> text.source - is RichTextReferenceLink -> text.source - } diff --git a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/rich/RichTextFormattingTest.kt b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/rich/RichTextFormattingTest.kt index ff5a77f6d1..18c8d288c7 100644 --- a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/rich/RichTextFormattingTest.kt +++ b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/rich/RichTextFormattingTest.kt @@ -209,13 +209,13 @@ class RichTextFormattingTest { } @Test - fun sourceExtractsPlainText() { - assertEquals("x", RichTextBold(RichTextPlain("x")).source) - assertEquals("alt", RichTextCustomEmoji(CustomEmojiId("1"), "alt").source) - assertEquals("e", RichTextMathematicalExpression("e").source) + fun rawTextExtractsPlainText() { + assertEquals("x", RichTextBold(RichTextPlain("x")).rawText) + assertEquals("alt", RichTextCustomEmoji(CustomEmojiId("1"), "alt").rawText) + assertEquals("e", RichTextMathematicalExpression("e").rawText) assertEquals( "a b", - RichTextGroup(listOf(RichTextPlain("a "), RichTextBold(RichTextPlain("b")))).source + RichTextGroup(listOf(RichTextPlain("a "), RichTextBold(RichTextPlain("b")))).rawText ) } }