mikepenz / multiplatform-markdown-renderer

Markdown renderer for Kotlin Multiplatform Projects (Android, iOS, Desktop), using Compose.
https://mikepenz.github.io/multiplatform-markdown-renderer/
Apache License 2.0
380 stars 25 forks source link

Create and handle key-value tag #196

Closed gs-ts closed 2 weeks ago

gs-ts commented 2 weeks ago

About this issue

Let's say we have defined a tag: MY_TAG = "myCustomTag"

and then I want to style and parse/handle the following (examples): [Important Note 1](?myCustomTag=note1) [Important Note 2](?myCustomTag=note2)

To style the above I think I can do something like this:

markdownAnnotator { content, child ->
    if (child.type == INLINE_LINK && content.contains(MY_TAG)) {
        pushStyle(
            SpanStyle(
                color = Color.Gray,
                textDecoration = TextDecoration.Underline,
                fontWeight = FontWeight.Bold
            )
        )
        true
    } else false
}

Now what I cannot achieve and I am not sure if the following are doable:

  1. handle/intercept the click to not open the browser but to execute another action (e.g. open a screen)
  2. read the value of the tag (e.g. "note1", "note2)

Details

Checklist

mikepenz commented 2 weeks ago

Good day @gs-ts

If I am understanding correctly, you attempt to implement custom links within the markdown which allow you to intercept and modify their behavior?

Overall url navigation is done by the LocalUriHandler so you should be able to provide your own custom implementation and intercept the behavior.

gs-ts commented 2 weeks ago

thank you for your quick response @mikepenz

Actually, I was wrong and I thought the library would handle the custom tags as links and open the browser, but this is not true.

So my question now is it possible to create a handler for my custom tag? An onClick when there is something like this: [Important Note 1](?myCustomTag=note1)

mikepenz commented 2 weeks ago

As noted in my previous comment, the LocalUriHandler is used to open links. So you can provide your own handler and handle links respectively.

val scrollState = rememberScrollState()
CompositionLocalProvider(LocalUriHandler provides object : UriHandler{
    override fun openUri(uri: String) {
        println("Handle this...: $uri")
    }
}) {
    Markdown(
        """
        [Important Note 1](?myCustomTag=note1)
        """.trimIndent(),
        modifier = Modifier.fillMaxSize().verticalScroll(scrollState).padding(16.dp)
    )
}
gs-ts commented 2 weeks ago

apologies for the confusion. You are right. It works as you mentioned.

I had a silly mistake in my previous implementation and the openUri didn't work for me