Skip to content

Commit df350e6

Browse files
Pluviobytesteipete
andauthored
fix(android): render chat content through Markdown (#88899)
* fix(android): render all chat text as Markdown Co-authored-by: Pluviobyte <[email protected]> * chore(android): sync native i18n inventory --------- Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: Pluviobyte <[email protected]>
1 parent e7384d5 commit df350e6

4 files changed

Lines changed: 49 additions & 32 deletions

File tree

‎apps/.i18n/native-source.json‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4515,7 +4515,7 @@
45154515
},
45164516
{
45174517
"kind": "ui-named-argument",
4518-
"line": 697,
4518+
"line": 699,
45194519
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatMarkdown.kt",
45204520
"source": "Image unavailable",
45214521
"surface": "android",
@@ -4675,111 +4675,111 @@
46754675
},
46764676
{
46774677
"kind": "ui-named-argument",
4678-
"line": 712,
4678+
"line": 704,
46794679
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
46804680
"source": "Tools running",
46814681
"surface": "android",
46824682
"id": "native.android.781f84dfaf7da9d6"
46834683
},
46844684
{
46854685
"kind": "ui-named-argument",
4686-
"line": 717,
4686+
"line": 709,
46874687
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
46884688
"source": "+${toolCalls.size - 4} more",
46894689
"surface": "android",
46904690
"id": "native.android.0fd6b75cadc0ce75"
46914691
},
46924692
{
46934693
"kind": "ui-named-argument",
4694-
"line": 727,
4694+
"line": 719,
46954695
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
46964696
"source": "Thinking",
46974697
"surface": "android",
46984698
"id": "native.android.7e0aea1dca378135"
46994699
},
47004700
{
47014701
"kind": "ui-named-argument",
4702-
"line": 728,
4702+
"line": 720,
47034703
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47044704
"source": "OpenClaw is preparing a response.",
47054705
"surface": "android",
47064706
"id": "native.android.98a0d4d5bba788c4"
47074707
},
47084708
{
47094709
"kind": "ui-named-argument",
4710-
"line": 821,
4710+
"line": 813,
47114711
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47124712
"source": "Stop",
47134713
"surface": "android",
47144714
"id": "native.android.76d574acfac94fee"
47154715
},
47164716
{
47174717
"kind": "ui-named-argument",
4718-
"line": 838,
4718+
"line": 830,
47194719
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47204720
"source": "Gateway offline",
47214721
"surface": "android",
47224722
"id": "native.android.8e3e367df24cae4b"
47234723
},
47244724
{
47254725
"kind": "ui-named-argument",
4726-
"line": 928,
4726+
"line": 920,
47274727
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47284728
"source": "Attach image",
47294729
"surface": "android",
47304730
"id": "native.android.623e434c83e3c020"
47314731
},
47324732
{
47334733
"kind": "ui-named-argument",
4734-
"line": 943,
4734+
"line": 935,
47354735
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47364736
"source": "Message OpenClaw",
47374737
"surface": "android",
47384738
"id": "native.android.f1d69eb66c5dfa39"
47394739
},
47404740
{
47414741
"kind": "ui-named-argument",
4742-
"line": 958,
4742+
"line": 950,
47434743
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47444744
"source": "Voice",
47454745
"surface": "android",
47464746
"id": "native.android.19396391033f4fa8"
47474747
},
47484748
{
47494749
"kind": "ui-named-argument",
4750-
"line": 996,
4750+
"line": 988,
47514751
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47524752
"source": "Remove attachment",
47534753
"surface": "android",
47544754
"id": "native.android.5054269049758d6a"
47554755
},
47564756
{
47574757
"kind": "conditional-branch",
4758-
"line": 1008,
4758+
"line": 1000,
47594759
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47604760
"source": "New chat",
47614761
"surface": "android",
47624762
"id": "native.android.326caa6b92aa9bdf"
47634763
},
47644764
{
47654765
"kind": "conditional-branch",
4766-
"line": 1017,
4766+
"line": 1009,
47674767
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47684768
"source": "Main",
47694769
"surface": "android",
47704770
"id": "native.android.5fa853a957713a56"
47714771
},
47724772
{
47734773
"kind": "ui-named-argument",
4774-
"line": 1073,
4774+
"line": 1065,
47754775
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47764776
"source": "Send",
47774777
"surface": "android",
47784778
"id": "native.android.6e2fcc9d67fc1273"
47794779
},
47804780
{
47814781
"kind": "conditional-branch",
4782-
"line": 1112,
4782+
"line": 1104,
47834783
"path": "apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatScreen.kt",
47844784
"source": "$contextLabel · ${contextMeterThinkingLabel(thinkingLevel)}",
47854785
"surface": "android",

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fun ChatMarkdown(
104104
text: String,
105105
textColor: Color,
106106
) {
107-
val document = remember(text) { markdownParser.parse(text) as Document }
107+
val document = remember(text) { parseChatMarkdown(text) }
108108
val inlineStyles =
109109
InlineStyles(inlineCodeBg = mobileCodeBg, inlineCodeColor = mobileCodeText, linkColor = mobileAccent, baseCallout = mobileCallout)
110110

@@ -588,7 +588,7 @@ internal fun buildChatInlineMarkdown(
588588
text: String,
589589
linkColor: Color = Color.Blue,
590590
): AnnotatedString {
591-
val document = markdownParser.parse(text) as Document
591+
val document = parseChatMarkdown(text)
592592
val paragraph = document.firstChild as? Paragraph ?: return AnnotatedString("")
593593
return buildInlineMarkdown(
594594
paragraph.firstChild,
@@ -601,6 +601,8 @@ internal fun buildChatInlineMarkdown(
601601
)
602602
}
603603

604+
internal fun parseChatMarkdown(text: String): Document = markdownParser.parse(text) as Document
605+
604606
private fun buildPlainText(start: Node?): String {
605607
val sb = StringBuilder()
606608
var node = start

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,7 @@ private fun ChatText(
694694
text: String,
695695
textColor: Color,
696696
) {
697-
if (text.hasMarkdownSyntax()) {
698-
ChatMarkdown(text = text, textColor = textColor)
699-
} else {
700-
Text(
701-
text = text,
702-
style = ClawTheme.type.body,
703-
color = textColor,
704-
)
705-
}
697+
ChatMarkdown(text = text, textColor = textColor)
706698
}
707699

708700
@Composable
@@ -1121,9 +1113,3 @@ internal fun contextMeterThinkingLabel(value: String): String =
11211113
}
11221114

11231115
private fun formatChatTimestamp(timestampMs: Long): String = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()).format(Date(timestampMs))
1124-
1125-
/** Quick markdown detector used to avoid routing plain chat text through the markdown renderer. */
1126-
private fun String.hasMarkdownSyntax(): Boolean =
1127-
any { it == '#' || it == '*' || it == '`' || it == '[' || it == '|' } ||
1128-
contains("\n- ") ||
1129-
contains("\n1. ")

‎apps/android/app/src/test/java/ai/openclaw/app/ui/chat/ChatMarkdownTest.kt‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package ai.openclaw.app.ui.chat
22

33
import androidx.compose.ui.text.LinkAnnotation
4+
import androidx.compose.ui.text.font.FontStyle
5+
import org.commonmark.node.BlockQuote
6+
import org.commonmark.node.BulletList
7+
import org.commonmark.node.Emphasis
8+
import org.commonmark.node.Paragraph
49
import org.junit.Assert.assertEquals
510
import org.junit.Assert.assertNull
611
import org.junit.Assert.assertTrue
@@ -57,6 +62,30 @@ class ChatMarkdownTest {
5762
assertTrue(annotated.getLinkAnnotations(0, annotated.length).isEmpty())
5863
}
5964

65+
@Test
66+
fun leadingListsAndQuotesParseAsBlockMarkdown() {
67+
assertTrue(parseChatMarkdown("- first\n- second").firstChild is BulletList)
68+
assertTrue(parseChatMarkdown("> quoted").firstChild is BlockQuote)
69+
}
70+
71+
@Test
72+
fun underscoreEmphasisRendersAsItalicText() {
73+
val document = parseChatMarkdown("_important_")
74+
val paragraph = document.firstChild as Paragraph
75+
76+
assertTrue(paragraph.firstChild is Emphasis)
77+
val annotated = buildChatInlineMarkdown("_important_")
78+
assertEquals("important", annotated.text)
79+
val emphasis =
80+
annotated.spanStyles
81+
.single()
82+
.item
83+
assertEquals(
84+
FontStyle.Italic,
85+
emphasis.fontStyle,
86+
)
87+
}
88+
6089
@Test
6190
fun parseDataImageDestinationAcceptsBoundedPayloads() {
6291
val parsed = parseDataImageDestination("data:image/png;base64,QUJD")

0 commit comments

Comments
 (0)