mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-21 15:53:47 +00:00
Added "createMarkdownText" and extensions for "CaptionedMediaContent" and "TextContent"
This commit is contained in:
parent
8acba29a61
commit
b4ef91cc77
@ -17,3 +17,5 @@
|
||||
* Now `ForwardedMessage` contains nullable `from`
|
||||
|
||||
## 0.8.4
|
||||
|
||||
* Added `createMarkdownText` and extensions for `CaptionedMediaContent` and `TextContent`
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.CaptionedMediaContent
|
||||
|
||||
fun createMarkdownText(
|
||||
text: String,
|
||||
messageEntities: List<MessageEntity>
|
||||
): String {
|
||||
val builder = StringBuilder()
|
||||
var offset = 0
|
||||
for (entity in messageEntities) {
|
||||
builder.append(
|
||||
text.substring(offset until entity.offset)
|
||||
)
|
||||
builder.append(
|
||||
entity.asMarkdownSource
|
||||
)
|
||||
offset += entity.length
|
||||
}
|
||||
builder.append(
|
||||
text.substring(offset)
|
||||
)
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
fun CaptionedMediaContent.toMarkdownCaption(): String? = caption ?.let {
|
||||
createMarkdownText(
|
||||
it,
|
||||
captionEntities
|
||||
)
|
||||
}
|
||||
|
||||
fun TextContent.toMarkdownText(): String = createMarkdownText(
|
||||
text,
|
||||
entities
|
||||
)
|
Loading…
Reference in New Issue
Block a user