rubenlagus / Telegraph

Easy to use library to interact with Telegra.ph
https://telegram.me/JavaBotsApi
MIT License
32 stars 10 forks source link

Markdown #6

Open RoxyHana opened 5 years ago

RoxyHana commented 5 years ago

How do you use formatting for the content?

 val contentNode = NodeText("I want some formatting here like bold text, links...")
 val content = ArrayList<Node>()
  content.add(contentNode)

Thanks!

RoxyHana commented 5 years ago

I use Kotlin and here's my code:

I created a function so I don't have to repeat the same lines all the time

fun generateNodeElement(tag: String, attrs: String, children: String):NodeElement{
            return when (tag){
                "a" -> NodeElement("a",hashMapOf("href" to attrs), listOf(NodeText(children)))
                "img" -> NodeElement("img",hashMapOf("src" to attrs), emptyList())
                else ->  NodeElement(tag, emptyMap(), listOf(NodeText(children)))
                }
            }
            fun lineJump(): NodeElement{
                return NodeElement("br", emptyMap(), emptyList())
            }
            fun separator(): NodeElement{
                return NodeElement("hr", emptyMap(), emptyList())
            }

And I add the content like:

 val content = ArrayList<Node>()
        content.apply {
            add(Aux.generateNodeElement("h3","","Some text")) // has children but not attrs
            add(Aux.generateNodeElement("img","http://your_url","")) // has attrs but not children
            ... // So you don't have to worry about what kind of tag it is to create attrs or children.
        }

But I still struggle about the images. An img creates a figure with a figcaption but I have no idea how to put text in the caption.