1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 00:03:48 +00:00

Added "createMarkdownText" and extensions for "CaptionedMediaContent" and "TextContent"

This commit is contained in:
InsanusMokrassar 2019-01-17 09:42:23 +08:00
parent 8acba29a61
commit b4ef91cc77
2 changed files with 40 additions and 0 deletions

View File

@ -17,3 +17,5 @@
* Now `ForwardedMessage` contains nullable `from` * Now `ForwardedMessage` contains nullable `from`
## 0.8.4 ## 0.8.4
* Added `createMarkdownText` and extensions for `CaptionedMediaContent` and `TextContent`

View File

@ -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
)