https://kotlinlang.org logo
#compose-web
Title
# compose-web
a

Ayfri

08/01/2022, 12:48 PM
Hi, is there a simple way to insert an element into a Text() ? Like
${Br()}
into a raw string ? For now it just prints the result of the method which is
kotlin.Unit
h

hfhbd

08/01/2022, 1:19 PM
What do you mean? What do you want to have?
a

Ayfri

08/01/2022, 1:21 PM
Copy code
Text("my text blah blah ${InsertBRTag} then blah blah")
So I don't have to do this
Copy code
Text("my text blah blah ")
Br()
Text(" then blah blah")
For links it's even more verbose :)
b

Big Chungus

08/01/2022, 1:21 PM
println("${'$'}{Br()}")
Note the dollar escape
Oh, nvm. I see what you're trying to do. CfW does not support rich html strings
i.e. not possible without external libs
h

hfhbd

08/01/2022, 1:23 PM
So you could just overload the Text and parse it and split it by yourself
Or, dont know if it is a little bit overkill but I would write my own dsl 😄
a

Ayfri

08/01/2022, 1:23 PM
Ah, I'll try then
h

hfhbd

08/01/2022, 1:26 PM
eg:
Copy code
@Composable
fun Text(text: String) {
    val components = text.split("<br>")
    for ((index, component) in components.withIndex()) {
        org.jetbrains.compose.web.dom.Text(component)
        if (index != component.lastIndex) {
            Br()
        }
    }
}
a

Ayfri

08/01/2022, 1:28 PM
Maybe do you know any Markdown library for Compose ?
h

hfhbd

08/01/2022, 1:31 PM
I don't know of any, but I didn't search :)
a

Ayfri

08/01/2022, 1:45 PM
It seems that there are none for CfW
h

hfhbd

08/01/2022, 1:50 PM
Sounds like a new project 😄
a

Ayfri

08/01/2022, 1:54 PM
For me I only need searching for links and br :)
h

hfhbd

08/01/2022, 2:23 PM
Hm, never mind 😄
Copy code
val regex = """<([^>]*)>""".toRegex()
fun String.getTokens(): Sequence<Pair<String, MatchResult?>> = sequence {
    var last = 0
    for (match in regex.findAll(this@getTokens)) {
        yield(this@getTokens.substring(last until match.range.first) to null)
        last = match.range.last + 1
        yield(this@getTokens.substring(match.range) to match)
    }
    if (last != this@getTokens.length) {
        yield(this@getTokens.substring(last) to null)
    }
}
parsing the href tag is a little bit ugly
Okay, this works for href and br only, but is ugly... Using a real bnf grammar would be better than hacking with regex...
Copy code
package app.softwork.markdowncompose

import androidx.compose.runtime.*
import org.jetbrains.compose.web.attributes.*
import org.jetbrains.compose.web.dom.*

@Composable
public fun renderMarkdown(markdown: String) {
    val iterator = markdown.getTokens().iterator()
    while (iterator.hasNext()) {
        val (rawText, htmlTag) = iterator.next()
        val htmlValue = htmlTag?.groups?.get(1)?.value
        val markdownLink = htmlTag?.groups?.get(2)?.value
        when {
            markdownLink != null -> renderLink(markdownLink)
            htmlValue == null -> Text(rawText)
            htmlValue == "br" -> Br()
            htmlValue.startsWith("a") -> renderA(htmlValue, iterator)
        }
    }
}

private const val markdownLink = """!\[([^\]]*)\]\(([^\)*])\)"""
private val markdownLinkRegex = markdownLink.toRegex()
private val regex = """<([^>]*)>|($markdownLink)""".toRegex()

private fun String.getTokens(): Sequence<Pair<String, MatchResult?>> = sequence {
    var last = 0
    for (match in regex.findAll(this@getTokens)) {
        yield(substring(last until match.range.first) to null)
        last = match.range.last + 1
        yield(substring(match.range) to match)
    }
    if (last != length) {
        yield(substring(last) to null)
    }
}

@Composable
private fun renderA(htmlValue: String, iterator: Iterator<Pair<String, MatchResult?>>) {
    val attrs = htmlValue.split(" ")
    val content = iterator.next()
    A(
        attrs = {
            for (attr in attrs) {
                when {
                    attr.startsWith("href") -> href(attr.removePrefix("href=\"").dropLast(1))
                }
            }
        }) {
        Text(content.first)
    }
    iterator.next()
}

@Composable
private fun renderLink(markdown: String) {
    val link = markdownLinkRegex.matchEntire(markdown)!!.groups
    val content = link[1]!!.value
    val href = link[2]!!.value
    A(href = href) {
        Text(content)
    }
}
Why do you want to do this in the browser? For static converter it would be better to convert it locally instead dynamically in the browser
a

Ayfri

08/01/2022, 3:55 PM
I mean I want it any way :)
h

hfhbd

08/01/2022, 4:02 PM
If you want to convert it only statically, I would do it locally and use/write a proper library. If you expect to update your view dynamically, eg based on user input, the latter would be better with highlighting etc. 😄 I would use only this hack, if you get the markdown from an api limited only to br and links
m

Michael Paus

08/01/2022, 7:13 PM
a

Ayfri

08/01/2022, 7:29 PM
Sadly your links are only for Compose Desktop and Android
34 Views