Skip to content

Commit 07d2043

Browse files
neeravmakwanasteipete
authored andcommitted
fix(android): make chat links tappable
1 parent 03a7b19 commit 07d2043

2 files changed

Lines changed: 108 additions & 14 deletions

File tree

‎apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatMarkdown.kt‎

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ import androidx.compose.ui.Modifier
3030
import androidx.compose.ui.graphics.Color
3131
import androidx.compose.ui.layout.ContentScale
3232
import androidx.compose.ui.text.AnnotatedString
33+
import androidx.compose.ui.text.LinkAnnotation
3334
import androidx.compose.ui.text.SpanStyle
35+
import androidx.compose.ui.text.TextLinkStyles
3436
import androidx.compose.ui.text.TextStyle
3537
import androidx.compose.ui.text.buildAnnotatedString
3638
import androidx.compose.ui.text.font.FontFamily
3739
import androidx.compose.ui.text.font.FontStyle
3840
import androidx.compose.ui.text.font.FontWeight
3941
import androidx.compose.ui.text.style.TextDecoration
42+
import androidx.compose.ui.text.withLink
4043
import androidx.compose.ui.text.withStyle
4144
import androidx.compose.ui.unit.dp
4245
import androidx.compose.ui.unit.sp
@@ -499,19 +502,12 @@ private fun AnnotatedString.Builder.appendInlineNode(
499502
}
500503
}
501504
is Link -> {
502-
withStyle(
503-
SpanStyle(
504-
color = linkColor,
505-
textDecoration = TextDecoration.Underline,
506-
),
507-
) {
508-
appendInlineNode(
509-
current.firstChild,
510-
inlineCodeBg = inlineCodeBg,
511-
inlineCodeColor = inlineCodeColor,
512-
linkColor = linkColor,
513-
)
514-
}
505+
appendLinkNode(
506+
link = current,
507+
inlineCodeBg = inlineCodeBg,
508+
inlineCodeColor = inlineCodeColor,
509+
linkColor = linkColor,
510+
)
515511
}
516512
is MarkdownImage -> {
517513
val alt = buildPlainText(current.firstChild)
@@ -527,13 +523,69 @@ private fun AnnotatedString.Builder.appendInlineNode(
527523
}
528524
}
529525
else -> {
530-
appendInlineNode(current.firstChild, inlineCodeBg = inlineCodeBg, inlineCodeColor = inlineCodeColor, linkColor = linkColor)
526+
appendInlineNode(
527+
current.firstChild,
528+
inlineCodeBg = inlineCodeBg,
529+
inlineCodeColor = inlineCodeColor,
530+
linkColor = linkColor,
531+
)
531532
}
532533
}
533534
current = current.next
534535
}
535536
}
536537

538+
private fun AnnotatedString.Builder.appendLinkNode(
539+
link: Link,
540+
inlineCodeBg: Color,
541+
inlineCodeColor: Color,
542+
linkColor: Color,
543+
) {
544+
val destination = link.destination?.trim().orEmpty()
545+
val linkStyle =
546+
SpanStyle(
547+
color = linkColor,
548+
textDecoration = TextDecoration.Underline,
549+
)
550+
if (destination.isEmpty()) {
551+
withStyle(linkStyle) {
552+
appendInlineNode(
553+
link.firstChild,
554+
inlineCodeBg = inlineCodeBg,
555+
inlineCodeColor = inlineCodeColor,
556+
linkColor = linkColor,
557+
)
558+
}
559+
return
560+
}
561+
562+
withLink(LinkAnnotation.Url(url = destination, styles = TextLinkStyles(style = linkStyle))) {
563+
appendInlineNode(
564+
link.firstChild,
565+
inlineCodeBg = inlineCodeBg,
566+
inlineCodeColor = inlineCodeColor,
567+
linkColor = linkColor,
568+
)
569+
}
570+
}
571+
572+
internal fun buildChatInlineMarkdown(
573+
text: String,
574+
linkColor: Color = Color.Blue,
575+
): AnnotatedString {
576+
val document = markdownParser.parse(text) as Document
577+
val paragraph = document.firstChild as? Paragraph ?: return AnnotatedString("")
578+
return buildInlineMarkdown(
579+
paragraph.firstChild,
580+
InlineStyles(
581+
inlineCodeBg = Color.Transparent,
582+
inlineCodeColor = Color.Unspecified,
583+
linkColor = linkColor,
584+
baseCallout = TextStyle.Default,
585+
),
586+
)
587+
}
588+
537589
private fun buildPlainText(start: Node?): String {
538590
val sb = StringBuilder()
539591
var node = start
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ai.openclaw.app.ui.chat
2+
3+
import androidx.compose.ui.text.LinkAnnotation
4+
import org.junit.Assert.assertEquals
5+
import org.junit.Assert.assertTrue
6+
import org.junit.Test
7+
8+
class ChatMarkdownTest {
9+
@Test
10+
fun bareUrlsCarryClickableUrlAnnotations() {
11+
val url = "https://www.amazon.it/GAZEBO-CANOPY-ACCIAIO-BIANCO-IMPERMEABILE/dp/B01G5R9FCK"
12+
13+
val annotated = buildChatInlineMarkdown("Open $url")
14+
15+
assertEquals("Open $url", annotated.text)
16+
val links = annotated.getLinkAnnotations(0, annotated.length)
17+
assertEquals(1, links.size)
18+
assertEquals(5, links.single().start)
19+
assertEquals(5 + url.length, links.single().end)
20+
assertEquals(url, (links.single().item as LinkAnnotation.Url).url)
21+
}
22+
23+
@Test
24+
fun markdownLinksUseLabelTextAndDestinationUrl() {
25+
val annotated = buildChatInlineMarkdown("Open [docs](https://docs.openclaw.ai/help/testing) now")
26+
27+
assertEquals("Open docs now", annotated.text)
28+
val links = annotated.getLinkAnnotations(0, annotated.length)
29+
assertEquals(1, links.size)
30+
assertEquals(5, links.single().start)
31+
assertEquals(9, links.single().end)
32+
assertEquals("https://docs.openclaw.ai/help/testing", (links.single().item as LinkAnnotation.Url).url)
33+
}
34+
35+
@Test
36+
fun plainTextDoesNotAddLinkAnnotations() {
37+
val annotated = buildChatInlineMarkdown("No link here")
38+
39+
assertEquals("No link here", annotated.text)
40+
assertTrue(annotated.getLinkAnnotations(0, annotated.length).isEmpty())
41+
}
42+
}

0 commit comments

Comments
 (0)