1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-13 13:25:26 +00:00
tgbotapi/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionSourcer.kt

40 lines
1.1 KiB
Kotlin
Raw Normal View History

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
2019-01-23 04:15:57 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
fun createMarkdownText(
text: String,
messageEntities: List<MessageEntity>
): String {
val builder = StringBuilder()
var offset = 0
for (entity in messageEntities) {
builder.append(
2019-01-23 04:15:57 +00:00
text.substring(offset until entity.offset).toMarkdown()
)
builder.append(
entity.asMarkdownSource
)
offset += entity.length
}
builder.append(
2019-01-23 04:15:57 +00:00
text.substring(offset).toMarkdown()
)
return builder.toString()
}
fun CaptionedMediaContent.toMarkdownCaption(): String? = caption ?.let {
createMarkdownText(
it,
captionEntities
)
}
fun TextContent.toMarkdownText(): String = createMarkdownText(
text,
entities
)