Render assistant text as markdown in the chat transcript
Add a dependency-free CommonMark-subset parser/renderer (ui/chat/Markdown.kt): paragraphs, headings, fenced code, block quotes, bullet/ordered lists, tables, thematic rules and inline code/bold/italic/links/autolinks. The renderer turns blocks into themed, selectable Compose content: fenced code reuses the shared CodeBlock (existing whole-block copy action), tables are a bordered grid with a bold header, links use LinkAnnotation.Url, and inline code/headings use the shared type roles. ChatScreen.MessagePart now calls MarkdownText(part.text) for PART_TEXT instead of a single plain Text; the assistant message stays one SelectionContainer, so all of it remains selectable and long content stays inside the bubble. Geometry reuses the chat tokens (CHAT_ITEM_GAP promoted private -> internal). Add MarkdownTest with 10 unit tests covering fences/language, unterminated fences, soft-wrap joining, headings/rules, lists, tables, inline spans, bare-URL autolinks and snake_case non-emphasis. Document the change in android/README.md.
This commit is contained in:
@@ -2359,3 +2359,64 @@ Evidence (macOS, JDK 17, Gradle 9.7.1 `--offline`,
|
||||
|
||||
Still not verified: the live brokered server (separate lane), streaming against
|
||||
real OpenCode, and attachments — unchanged from the pass above.
|
||||
|
||||
## Conversation markdown rendering (16 Oct 2026)
|
||||
|
||||
The assistant's `text` part was rendered as one plain `Text`; it is now rendered
|
||||
as markdown, matching the reference experience (research report Part 1 §1.2).
|
||||
`android/` only; no new dependency (still just the declared
|
||||
`material-icons-extended`), no Pipelines server/Go/installer/Docker file touched,
|
||||
no run started from the app, and `emulator-5556` only (lane A's `emulator-5554`
|
||||
was never addressed).
|
||||
|
||||
- **Parser.** `ui/chat/Markdown.kt` adds a small, dependency-free CommonMark
|
||||
subset. It is pure Kotlin (no Compose, no transport) so it is unit-testable:
|
||||
paragraphs (soft-wrapped lines join with a space), `#`..`######` headings,
|
||||
fenced code (``` ``` / `~~~`, with the info string as the language), block
|
||||
quotes, bullet and ordered lists, `|` tables (with a `---` delimiter row),
|
||||
thematic rules, and inline code / bold / italic / `[label](url)` /
|
||||
bare-`http(s)://` autolinks. Underscore emphasis needs word boundaries so
|
||||
`snake_case` is never italicised. Anything outside the subset renders as plain
|
||||
text, never dropped.
|
||||
- **Renderer.** The same file turns the blocks into themed, selectable Compose
|
||||
content: fenced code reuses the shared `CodeBlock`, so the whole-block copy
|
||||
action is the existing one (label = the fence language, `code` when absent);
|
||||
tables are a bordered grid with a bold header and equal-weight wrapping cells;
|
||||
links use `LinkAnnotation.Url`, so they are tap-openable and styled from the
|
||||
scheme; inline code and headings use the shared type roles
|
||||
(`pipelinesText()`), not ad-hoc sizes. `ChatScreen.MessagePart` now calls
|
||||
`MarkdownText(part.text)` for `PART_TEXT`; the assistant message stays one
|
||||
`SelectionContainer`, so all of it remains selectable and the bounded code
|
||||
block/table keep long content inside the bubble. Geometry reuses the chat
|
||||
tokens (`CHAT_ITEM_GAP`, promoted from `private` to `internal`); only the
|
||||
quote-bar width and table border, which had no token, are new named constants.
|
||||
|
||||
Evidence (macOS, JDK 17, Gradle 9.7.1 `--offline`,
|
||||
`ANDROID_HOME=/Users/alan/Library/Android/sdk`):
|
||||
|
||||
- `gradle :app:assembleDebug :app:testDebugUnitTest --offline` → `BUILD
|
||||
SUCCESSFUL`; the JUnit XML totals **243 tests, 0 failures, 0 errors, 0
|
||||
skipped** (new `MarkdownTest`: fences keep their language, an unterminated
|
||||
fence still yields code, soft-wrapped paragraphs join, headings/rules parse by
|
||||
level, bullet/ordered lists, table header/rows, inline code/bold/italic/link,
|
||||
bare-URL autolink trims trailing punctuation, `snake_case` is not emphasis,
|
||||
empty source → no blocks).
|
||||
- The debug APK was installed on `emulator-5556` only and driven against a
|
||||
stdlib `/connect` WebSocket double serving a fixed history; capture was
|
||||
validated with `android/scripts/capture_png.sh -s emulator-5556`:
|
||||
`chat_markdown_conversation.png` shows the bottom-anchored transcript with the
|
||||
user bubble, the secondary reasoning line, body markdown with inline code and
|
||||
an underlined link, a `kotlin` fenced code block with its copy action, and the
|
||||
bordered `Surface | Role` table. `adb -s emulator-5556 logcat -d` held 0
|
||||
`FATAL EXCEPTION` and 0 `E AndroidRuntime` during the sweep.
|
||||
- **APK publish.** `app-debug.apk` (18,076,412 bytes, sha256
|
||||
`bb290a7f42a3b28e613098dfffea438c34a5332081cffc6a2fbc0d275c207f68`) copied to
|
||||
`~/.local/share/pipelines/data/artifacts/124/1/pipelines-debug-wip-0718f5b.apk`;
|
||||
`HEAD` is `0718f5b` and a streamed `curl | shasum -a 256` of
|
||||
`http://10.0.0.249:8080/artifacts/124/1/pipelines-debug-wip-0718f5b.apk`
|
||||
matched.
|
||||
|
||||
Not verified: the live brokered server (separate lane), streaming against real
|
||||
OpenCode, and attachments — unchanged from the passes above. The markdown subset
|
||||
is intentionally smaller than full CommonMark (no HTML, no reference links, no
|
||||
nested lists, no strikethrough).
|
||||
|
||||
@@ -93,7 +93,7 @@ import net.awan.pipelines.ui.theme.pipelinesText
|
||||
* governs touch targets.
|
||||
*/
|
||||
private val CHAT_GUTTER = 16.dp
|
||||
private val CHAT_ITEM_GAP = 8.dp
|
||||
internal val CHAT_ITEM_GAP = 8.dp
|
||||
private val CHAT_COMPOSER_GAP = 12.dp
|
||||
private val CHAT_STATUS_DOT = 8.dp
|
||||
|
||||
@@ -519,7 +519,7 @@ private fun MessageItem(message: ChatMessage) {
|
||||
private fun MessagePart(part: ChatPart) {
|
||||
when (part.type) {
|
||||
ChatProtocol.PART_TEXT -> if (part.text.isNotBlank()) {
|
||||
Text(part.text, style = MaterialTheme.typography.bodyMedium)
|
||||
MarkdownText(part.text)
|
||||
}
|
||||
ChatProtocol.PART_REASONING -> ReasoningBlock(part.text)
|
||||
ChatProtocol.PART_TOOL -> ToolCallCard(part)
|
||||
|
||||
@@ -0,0 +1,462 @@
|
||||
package net.awan.pipelines.ui.chat
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.IntrinsicSize
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.text.LinkAnnotation
|
||||
import androidx.compose.ui.text.SpanStyle
|
||||
import androidx.compose.ui.text.TextLinkStyles
|
||||
import androidx.compose.ui.text.buildAnnotatedString
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontStyle
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextDecoration
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.text.withLink
|
||||
import androidx.compose.ui.text.withStyle
|
||||
import androidx.compose.ui.unit.dp
|
||||
import net.awan.pipelines.ui.components.CodeBlock
|
||||
import net.awan.pipelines.ui.theme.pipelinesText
|
||||
|
||||
/**
|
||||
* A small, dependency-free CommonMark subset for assistant prose. The brokered
|
||||
* protocol carries assistant text as markdown and the reference experience renders
|
||||
* it (research §1.2): body paragraphs, `#`..`######` headings, fenced code (the
|
||||
* shared [CodeBlock], so the copy action comes for free), block quotes, bullet and
|
||||
* ordered lists, `|` tables, thematic rules, and inline code / bold / italic /
|
||||
* links. Anything outside the subset renders as plain text, never dropped.
|
||||
*
|
||||
* The parser is pure Kotlin (no Compose, no transport) so it is unit-testable; the
|
||||
* composables below turn the same blocks into themed, selectable content.
|
||||
*/
|
||||
|
||||
// ---- Parsed model ------------------------------------------------------------
|
||||
|
||||
internal sealed interface MdBlock
|
||||
|
||||
internal data class MdHeading(val level: Int, val text: String) : MdBlock
|
||||
internal data class MdParagraph(val text: String) : MdBlock
|
||||
internal data class MdCodeBlock(val language: String, val code: String) : MdBlock
|
||||
internal data class MdQuote(val lines: List<String>) : MdBlock
|
||||
internal data class MdListBlock(val ordered: Boolean, val items: List<String>) : MdBlock
|
||||
internal data class MdTableBlock(val header: List<String>, val rows: List<List<String>>) : MdBlock
|
||||
internal data object MdRule : MdBlock
|
||||
|
||||
internal sealed interface MdSpan
|
||||
|
||||
internal data class MdPlain(val text: String) : MdSpan
|
||||
internal data class MdCodeSpan(val text: String) : MdSpan
|
||||
internal data class MdStrong(val spans: List<MdSpan>) : MdSpan
|
||||
internal data class MdEmphasis(val spans: List<MdSpan>) : MdSpan
|
||||
internal data class MdLinkSpan(val spans: List<MdSpan>, val url: String) : MdSpan
|
||||
|
||||
// ---- Block parser ------------------------------------------------------------
|
||||
|
||||
/** Parses [source] into the block list the renderer walks. Never throws. */
|
||||
internal fun parseMarkdown(source: String): List<MdBlock> {
|
||||
val lines = source.replace("\r\n", "\n").replace('\r', '\n').split('\n')
|
||||
val blocks = mutableListOf<MdBlock>()
|
||||
var i = 0
|
||||
while (i < lines.size) {
|
||||
val line = lines[i]
|
||||
val trimmed = line.trim()
|
||||
when {
|
||||
trimmed.isEmpty() -> i++
|
||||
|
||||
fenceRun(trimmed) != null -> {
|
||||
val fence = fenceRun(trimmed)!!
|
||||
val language = trimmed.removePrefix(fence).trim().substringBefore(' ')
|
||||
val code = mutableListOf<String>()
|
||||
i++
|
||||
while (i < lines.size && fenceRun(lines[i].trim()) == null) {
|
||||
code.add(lines[i])
|
||||
i++
|
||||
}
|
||||
if (i < lines.size) i++ // consume the closing fence
|
||||
blocks.add(MdCodeBlock(language, code.joinToString("\n")))
|
||||
}
|
||||
|
||||
headingLevel(trimmed) > 0 -> {
|
||||
val level = headingLevel(trimmed)
|
||||
blocks.add(MdHeading(level, trimmed.drop(level).trim().trimEnd('#')))
|
||||
i++
|
||||
}
|
||||
|
||||
isRule(trimmed) -> {
|
||||
blocks.add(MdRule)
|
||||
i++
|
||||
}
|
||||
|
||||
trimmed.startsWith(">") -> {
|
||||
val quote = mutableListOf<String>()
|
||||
while (i < lines.size && lines[i].trim().startsWith(">")) {
|
||||
quote.add(lines[i].trim().removePrefix(">").removePrefix(" ").trimEnd())
|
||||
i++
|
||||
}
|
||||
blocks.add(MdQuote(quote))
|
||||
}
|
||||
|
||||
isTableStart(lines, i) -> {
|
||||
val header = splitRow(lines[i])
|
||||
i += 2 // header + delimiter row
|
||||
val rows = mutableListOf<List<String>>()
|
||||
while (i < lines.size && lines[i].contains('|') && lines[i].isNotBlank()) {
|
||||
rows.add(fitRow(splitRow(lines[i]), header.size))
|
||||
i++
|
||||
}
|
||||
blocks.add(MdTableBlock(header, rows))
|
||||
}
|
||||
|
||||
listMarker(line) != null -> {
|
||||
val ordered = listMarker(line)!!.first
|
||||
val items = mutableListOf<String>()
|
||||
while (i < lines.size && listMarker(lines[i])?.first == ordered) {
|
||||
items.add(listMarker(lines[i])!!.second)
|
||||
i++
|
||||
}
|
||||
blocks.add(MdListBlock(ordered, items))
|
||||
}
|
||||
|
||||
else -> {
|
||||
val paragraph = mutableListOf(line.trim())
|
||||
i++
|
||||
while (i < lines.size && lines[i].isNotBlank() && !startsBlock(lines, i)) {
|
||||
paragraph.add(lines[i].trim())
|
||||
i++
|
||||
}
|
||||
blocks.add(MdParagraph(paragraph.joinToString(" ")))
|
||||
}
|
||||
}
|
||||
}
|
||||
return blocks
|
||||
}
|
||||
|
||||
/** The fence run (` ``` ` / `~~~`) a line opens or closes with, or null. */
|
||||
private fun fenceRun(trimmed: String): String? {
|
||||
val first = trimmed.firstOrNull() ?: return null
|
||||
if (first != '`' && first != '~') return null
|
||||
val run = trimmed.takeWhile { it == first }
|
||||
return if (run.length >= 3) run else null
|
||||
}
|
||||
|
||||
private fun headingLevel(trimmed: String): Int {
|
||||
val hashes = trimmed.takeWhile { it == '#' }.length
|
||||
if (hashes == 0 || hashes > 6) return 0
|
||||
return if (trimmed.length == hashes || trimmed[hashes] == ' ') hashes else 0
|
||||
}
|
||||
|
||||
private fun isRule(trimmed: String): Boolean =
|
||||
trimmed.length >= 3 && trimmed.all { it == trimmed[0] } && trimmed[0] in "-*_"
|
||||
|
||||
/** True when [lines] at [index] is a table header followed by a `---` delimiter row. */
|
||||
private fun isTableStart(lines: List<String>, index: Int): Boolean {
|
||||
if (index + 1 >= lines.size || !lines[index].contains('|')) return false
|
||||
val delimiter = splitRow(lines[index + 1])
|
||||
if (delimiter.isEmpty() || splitRow(lines[index]).size < 2) return false
|
||||
return delimiter.all { cell -> cell.isNotEmpty() && cell.all { it == '-' || it == ':' } && cell.contains('-') }
|
||||
}
|
||||
|
||||
/** Splits a table row on unescaped `|`, dropping the leading/trailing empty cell. */
|
||||
private fun splitRow(line: String): List<String> =
|
||||
line.trim().removePrefix("|").removeSuffix("|").split('|').map { it.replace("\\|", "|").trim() }
|
||||
|
||||
private fun fitRow(row: List<String>, size: Int): List<String> =
|
||||
List(size) { row.getOrElse(it) { "" } }
|
||||
|
||||
/** The ordered flag and item text of a list line, or null for a non-item line. */
|
||||
private fun listMarker(line: String): Pair<Boolean, String>? {
|
||||
val trimmed = line.trimStart()
|
||||
val orderedMatch = ORDERED_ITEM.matchEntire(trimmed)
|
||||
if (orderedMatch != null) return true to orderedMatch.groupValues[2]
|
||||
val bulletMatch = BULLET_ITEM.matchEntire(trimmed)
|
||||
if (bulletMatch != null) return false to bulletMatch.groupValues[2]
|
||||
return null
|
||||
}
|
||||
|
||||
/** True when [lines] at [index] begins a block that must interrupt a paragraph. */
|
||||
private fun startsBlock(lines: List<String>, index: Int): Boolean {
|
||||
val trimmed = lines[index].trim()
|
||||
return fenceRun(trimmed) != null ||
|
||||
headingLevel(trimmed) > 0 ||
|
||||
isRule(trimmed) ||
|
||||
trimmed.startsWith(">") ||
|
||||
listMarker(lines[index]) != null ||
|
||||
isTableStart(lines, index)
|
||||
}
|
||||
|
||||
// ---- Inline parser -----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Parses inline markup in [text] into a flat span tree. Handles `` `code` ``,
|
||||
* `**bold**` / `__bold__`, `*italic*` / `_italic_`, `[label](url)` and bare
|
||||
* `http(s)://` autolinks; everything else stays plain text. Underscore emphasis
|
||||
* requires word boundaries so `snake_case` is never italicised.
|
||||
*/
|
||||
internal fun parseInline(text: String): List<MdSpan> {
|
||||
val spans = mutableListOf<MdSpan>()
|
||||
val buffer = StringBuilder()
|
||||
var i = 0
|
||||
|
||||
fun flush() {
|
||||
if (buffer.isNotEmpty()) {
|
||||
spans.add(MdPlain(buffer.toString()))
|
||||
buffer.clear()
|
||||
}
|
||||
}
|
||||
|
||||
while (i < text.length) {
|
||||
val c = text[i]
|
||||
when {
|
||||
c == '`' -> {
|
||||
val end = text.indexOf('`', i + 1)
|
||||
if (end > i) {
|
||||
flush()
|
||||
spans.add(MdCodeSpan(text.substring(i + 1, end)))
|
||||
i = end + 1
|
||||
} else {
|
||||
buffer.append(c); i++
|
||||
}
|
||||
}
|
||||
|
||||
c == '*' || c == '_' -> {
|
||||
val doubled = i + 1 < text.length && text[i + 1] == c
|
||||
val marker = if (doubled) "$c$c" else c.toString()
|
||||
val boundOk = c != '_' || i == 0 || !text[i - 1].isLetterOrDigit()
|
||||
val end = if (boundOk) text.indexOf(marker, i + marker.length) else -1
|
||||
val closes = end > i + marker.length &&
|
||||
(c != '_' || end + 1 >= text.length || !text[end + 1].isLetterOrDigit())
|
||||
if (closes) {
|
||||
flush()
|
||||
val inner = parseInline(text.substring(i + marker.length, end))
|
||||
spans.add(if (doubled) MdStrong(inner) else MdEmphasis(inner))
|
||||
i = end + marker.length
|
||||
} else {
|
||||
buffer.append(c); i++
|
||||
}
|
||||
}
|
||||
|
||||
c == '[' -> {
|
||||
val close = text.indexOf(']', i + 1)
|
||||
if (close > i && close + 1 < text.length && text[close + 1] == '(') {
|
||||
val urlEnd = text.indexOf(')', close + 2)
|
||||
val url = if (urlEnd > close) text.substring(close + 2, urlEnd).trim() else ""
|
||||
if (url.isNotEmpty()) {
|
||||
flush()
|
||||
spans.add(MdLinkSpan(parseInline(text.substring(i + 1, close)), url))
|
||||
i = urlEnd + 1
|
||||
} else {
|
||||
buffer.append(c); i++
|
||||
}
|
||||
} else {
|
||||
buffer.append(c); i++
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
val url = autolinkAt(text, i)
|
||||
if (url != null) {
|
||||
flush()
|
||||
spans.add(MdLinkSpan(listOf(MdPlain(url)), url))
|
||||
i += url.length
|
||||
} else {
|
||||
buffer.append(c); i++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
flush()
|
||||
return spans
|
||||
}
|
||||
|
||||
/** A bare `http(s)://` URL starting exactly at [index], trailing punctuation trimmed. */
|
||||
private fun autolinkAt(text: String, index: Int): String? {
|
||||
if (!text.startsWith("http://", index) && !text.startsWith("https://", index)) return null
|
||||
var end = index
|
||||
while (end < text.length && !text[end].isWhitespace() && text[end] !in "<>\"'") end++
|
||||
var url = text.substring(index, end)
|
||||
while (url.isNotEmpty() && url.last() in ".,;:!?)]}") url = url.dropLast(1)
|
||||
return url.takeIf { it.length > "https://".length }
|
||||
}
|
||||
|
||||
private val ORDERED_ITEM = Regex("""^(\d{1,9})[.)]\s+(.*)$""")
|
||||
private val BULLET_ITEM = Regex("""^([-*+])\s+(.*)$""")
|
||||
|
||||
// ---- Renderers ---------------------------------------------------------------
|
||||
|
||||
private val MD_QUOTE_BAR = 3.dp
|
||||
private val MD_TABLE_BORDER = 1.dp
|
||||
|
||||
/**
|
||||
* Renders [text] as markdown at the app's shared body style. The caller owns the
|
||||
* selection container (a chat message is one selectable region), so this only
|
||||
* lays out the blocks.
|
||||
*/
|
||||
@Composable
|
||||
internal fun MarkdownText(text: String, modifier: Modifier = Modifier) {
|
||||
val blocks = remember(text) { parseMarkdown(text) }
|
||||
Column(modifier, verticalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP)) {
|
||||
blocks.forEach { MarkdownBlock(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun MarkdownBlock(block: MdBlock) {
|
||||
val texts = pipelinesText()
|
||||
when (block) {
|
||||
is MdHeading -> Text(
|
||||
text = inline(block.text),
|
||||
style = when (block.level) {
|
||||
1 -> MaterialTheme.typography.titleLarge
|
||||
2 -> MaterialTheme.typography.titleMedium
|
||||
else -> texts.cardTitle
|
||||
},
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
|
||||
is MdParagraph -> Text(text = inline(block.text), style = texts.body)
|
||||
|
||||
is MdCodeBlock -> CodeBlock(label = block.language.ifBlank { "code" }, text = block.code)
|
||||
|
||||
MdRule -> HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant)
|
||||
|
||||
is MdQuote -> Row(Modifier.height(IntrinsicSize.Min)) {
|
||||
Box(
|
||||
Modifier
|
||||
.width(MD_QUOTE_BAR)
|
||||
.fillMaxHeight()
|
||||
.background(MaterialTheme.colorScheme.outlineVariant),
|
||||
)
|
||||
Column(
|
||||
modifier = Modifier.padding(start = CHAT_ITEM_GAP),
|
||||
verticalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP / 2),
|
||||
) {
|
||||
block.lines.forEach { line ->
|
||||
if (line.isNotBlank()) {
|
||||
Text(
|
||||
text = inline(line),
|
||||
style = texts.body,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is MdListBlock -> Column(verticalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP / 2)) {
|
||||
block.items.forEachIndexed { index, item ->
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP)) {
|
||||
Text(
|
||||
text = if (block.ordered) "${index + 1}." else "•",
|
||||
style = texts.body,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Text(text = inline(item), style = texts.body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is MdTableBlock -> MarkdownTable(block)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun MarkdownTable(table: MdTableBlock) {
|
||||
val border = MaterialTheme.colorScheme.outlineVariant
|
||||
val shape = RoundedCornerShape(CHAT_ITEM_GAP)
|
||||
Column(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.clip(shape)
|
||||
.border(MD_TABLE_BORDER, border, shape),
|
||||
) {
|
||||
TableRow(table.header, header = true, border = border)
|
||||
table.rows.forEach { row ->
|
||||
HorizontalDivider(color = border)
|
||||
TableRow(row, header = false, border = border)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TableRow(cells: List<String>, header: Boolean, border: Color) {
|
||||
val texts = pipelinesText()
|
||||
Row(Modifier.fillMaxWidth().height(IntrinsicSize.Min)) {
|
||||
cells.forEachIndexed { index, cell ->
|
||||
if (index > 0) {
|
||||
Box(Modifier.width(MD_TABLE_BORDER).fillMaxHeight().background(border))
|
||||
}
|
||||
Text(
|
||||
text = inline(cell),
|
||||
style = if (header) texts.body.copy(fontWeight = FontWeight.Bold) else texts.body,
|
||||
modifier = Modifier.weight(1f).padding(CHAT_ITEM_GAP),
|
||||
maxLines = if (header) 1 else Int.MAX_VALUE,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Inline text as one [AnnotatedString]; links are tappable and code is monospace. */
|
||||
@Composable
|
||||
private fun inline(text: String): AnnotatedString {
|
||||
val linkColor = MaterialTheme.colorScheme.primary
|
||||
val codeBackground = MaterialTheme.colorScheme.surfaceContainerHigh
|
||||
val codeColor = MaterialTheme.colorScheme.onSurface
|
||||
return remember(text, linkColor, codeBackground, codeColor) {
|
||||
buildInline(parseInline(text), linkColor, codeBackground, codeColor)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildInline(
|
||||
spans: List<MdSpan>,
|
||||
linkColor: Color,
|
||||
codeBackground: Color,
|
||||
codeColor: Color,
|
||||
): AnnotatedString = buildAnnotatedString { appendSpans(spans, linkColor, codeBackground, codeColor) }
|
||||
|
||||
private fun androidx.compose.ui.text.AnnotatedString.Builder.appendSpans(
|
||||
spans: List<MdSpan>,
|
||||
linkColor: Color,
|
||||
codeBackground: Color,
|
||||
codeColor: Color,
|
||||
) {
|
||||
for (span in spans) {
|
||||
when (span) {
|
||||
is MdPlain -> append(span.text)
|
||||
is MdCodeSpan -> withStyle(
|
||||
SpanStyle(fontFamily = FontFamily.Monospace, background = codeBackground, color = codeColor),
|
||||
) { append(span.text) }
|
||||
is MdStrong -> withStyle(SpanStyle(fontWeight = FontWeight.Bold)) {
|
||||
appendSpans(span.spans, linkColor, codeBackground, codeColor)
|
||||
}
|
||||
is MdEmphasis -> withStyle(SpanStyle(fontStyle = FontStyle.Italic)) {
|
||||
appendSpans(span.spans, linkColor, codeBackground, codeColor)
|
||||
}
|
||||
is MdLinkSpan -> withLink(
|
||||
LinkAnnotation.Url(
|
||||
span.url,
|
||||
TextLinkStyles(style = SpanStyle(color = linkColor, textDecoration = TextDecoration.Underline)),
|
||||
),
|
||||
) { appendSpans(span.spans, linkColor, codeBackground, codeColor) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package net.awan.pipelines.ui.chat
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class MarkdownTest {
|
||||
|
||||
@Test
|
||||
fun fencedCodeKeepsTheLanguageAndBody() {
|
||||
val blocks = parseMarkdown(
|
||||
"""
|
||||
Intro text.
|
||||
|
||||
```kotlin
|
||||
val gap = 8.dp
|
||||
println(gap)
|
||||
```
|
||||
""".trimIndent(),
|
||||
)
|
||||
assertEquals(MdParagraph("Intro text."), blocks[0])
|
||||
val code = blocks[1] as MdCodeBlock
|
||||
assertEquals("kotlin", code.language)
|
||||
assertEquals("val gap = 8.dp\nprintln(gap)", code.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun anUnterminatedFenceStillYieldsItsCode() {
|
||||
val blocks = parseMarkdown("```\nline one\nline two")
|
||||
val code = blocks.single() as MdCodeBlock
|
||||
assertEquals("", code.language)
|
||||
assertEquals("line one\nline two", code.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun softWrappedParagraphLinesJoinWithASpace() {
|
||||
assertEquals(listOf(MdParagraph("one two three")), parseMarkdown("one\ntwo\nthree"))
|
||||
assertEquals(listOf(MdParagraph("one"), MdParagraph("two")), parseMarkdown("one\n\ntwo"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun headingsAndThematicRulesParseByLevel() {
|
||||
val blocks = parseMarkdown("# Title\n\n## Section\n\n---")
|
||||
assertEquals(MdHeading(1, "Title"), blocks[0])
|
||||
assertEquals(MdHeading(2, "Section"), blocks[1])
|
||||
assertEquals(MdRule, blocks[2])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bulletAndOrderedListsKeepTheirOrder() {
|
||||
val bullets = parseMarkdown("- one\n- two").single() as MdListBlock
|
||||
assertEquals(false, bullets.ordered)
|
||||
assertEquals(listOf("one", "two"), bullets.items)
|
||||
|
||||
val ordered = parseMarkdown("1. first\n2. second").single() as MdListBlock
|
||||
assertEquals(true, ordered.ordered)
|
||||
assertEquals(listOf("first", "second"), ordered.items)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aPipeTableParsesHeaderAndRows() {
|
||||
val blocks = parseMarkdown("| Name | Value |\n| --- | ---: |\n| a | 1 |\n| b | 2 |")
|
||||
val table = blocks.single() as MdTableBlock
|
||||
assertEquals(listOf("Name", "Value"), table.header)
|
||||
assertEquals(listOf(listOf("a", "1"), listOf("b", "2")), table.rows)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun inlineCodeBoldItalicAndLinksBecomeSpans() {
|
||||
val spans = parseInline("Use `CodeBlock` and **bold** and *italic* and [docs](https://example.com/x).")
|
||||
assertTrue(spans.any { it is MdCodeSpan && it.text == "CodeBlock" })
|
||||
assertTrue(spans.any { it is MdStrong })
|
||||
assertTrue(spans.any { it is MdEmphasis })
|
||||
val link = spans.filterIsInstance<MdLinkSpan>().single()
|
||||
assertEquals("https://example.com/x", link.url)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bareUrlsAutolinkAndTrimTrailingPunctuation() {
|
||||
val spans = parseInline("See https://example.com/a, then stop.")
|
||||
val link = spans.filterIsInstance<MdLinkSpan>().single()
|
||||
assertEquals("https://example.com/a", link.url)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun underscoresInsideAWordAreNotEmphasis() {
|
||||
assertEquals(listOf(MdPlain("snake_case_name")), parseInline("snake_case_name"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun anEmptySourceParsesToNoBlocks() {
|
||||
assertEquals(emptyList<MdBlock>(), parseMarkdown(""))
|
||||
assertEquals(emptyList<MdBlock>(), parseMarkdown("\n\n"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user