mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
Render Agent Markdown and open web links in an external browser (#2546)
* Render Agent Markdown with clickable links in an external browser * Preserve Markdown selection and expose table text to copy and accessibility
This commit is contained in:
1 parent
b5f6122d49
commit
60098d4a1b
5 files changed
+327
-100
No files matched your search
@@ -382,6 +382,11 @@ dependencies {
|
||||
implementation 'androidx.activity:activity-compose:1.8.2'
|
||||
implementation 'androidx.compose.material:material-icons-extended'
|
||||
implementation 'io.coil-kt:coil-compose:2.6.0'
|
||||
implementation 'io.noties.markwon:core:4.6.2'
|
||||
implementation 'io.noties.markwon:ext-strikethrough:4.6.2'
|
||||
implementation 'io.noties.markwon:ext-tables:4.6.2'
|
||||
implementation 'io.noties.markwon:ext-tasklist:4.6.2'
|
||||
implementation 'com.atlassian.commonmark:commonmark-ext-autolink:0.13.0'
|
||||
implementation 'com.caverock:androidsvg-aar:1.4'
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
|
||||
implementation 'com.flyfishxu:kadb-android:1.2.1'
|
||||
|
||||
@@ -42,14 +42,10 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.res.pluralStringResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.text.SpanStyle
|
||||
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.TextOverflow
|
||||
import androidx.compose.ui.text.withStyle
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import coil.compose.AsyncImage
|
||||
@@ -329,81 +325,3 @@ internal fun AttachmentChip(path: String, onRemove: (() -> Unit)?) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The Markdown agents write: headings, lists, fenced code, `code` and **bold**. */
|
||||
@Composable
|
||||
internal fun AgentMarkdown(text: String, modifier: Modifier) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
Column(modifier, verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
for (block in markdownBlocks(text)) when (block) {
|
||||
is MdBlock.Code -> Text(block.text, fontSize = 13.sp, fontFamily = FontFamily.Monospace, color = colors.onSurface,
|
||||
modifier = Modifier.fillMaxWidth().clip(Shape12).background(pal.surfaceVariant).padding(12.dp))
|
||||
is MdBlock.Line -> Text(
|
||||
inlineMarkdown(block.text, pal.primary2, pal.surfaceVariant),
|
||||
fontSize = if (block.heading) 17.sp else 15.sp,
|
||||
fontWeight = if (block.heading) FontWeight.Bold else null,
|
||||
lineHeight = 22.sp, color = colors.onSurface,
|
||||
modifier = if (block.bullet) Modifier.padding(start = 12.dp) else Modifier,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed interface MdBlock {
|
||||
data class Line(val text: String, val heading: Boolean = false, val bullet: Boolean = false) : MdBlock
|
||||
data class Code(val text: String) : MdBlock
|
||||
}
|
||||
|
||||
private val HEADING = Regex("^#{1,6}\\s+")
|
||||
private val BULLET = Regex("^\\s*[-*+]\\s+")
|
||||
|
||||
internal fun markdownBlocks(text: String): List<MdBlock> {
|
||||
val out = mutableListOf<MdBlock>()
|
||||
val code = StringBuilder()
|
||||
var inCode = false
|
||||
for (raw in text.lines()) {
|
||||
if (raw.trimStart().startsWith("```")) {
|
||||
if (inCode) {
|
||||
out += MdBlock.Code(code.toString().trimEnd('\n'))
|
||||
code.clear()
|
||||
}
|
||||
inCode = !inCode
|
||||
continue
|
||||
}
|
||||
if (inCode) {
|
||||
code.append(raw).append('\n')
|
||||
continue
|
||||
}
|
||||
val line = raw.trimEnd()
|
||||
if (line.isBlank()) continue
|
||||
val heading = HEADING.find(line)
|
||||
val bullet = BULLET.find(line)
|
||||
out += when {
|
||||
heading != null -> MdBlock.Line(line.substring(heading.range.last + 1), heading = true)
|
||||
bullet != null -> MdBlock.Line("• " + line.substring(bullet.range.last + 1), bullet = true)
|
||||
else -> MdBlock.Line(line)
|
||||
}
|
||||
}
|
||||
if (inCode && code.isNotEmpty()) out += MdBlock.Code(code.toString().trimEnd('\n'))
|
||||
return out
|
||||
}
|
||||
|
||||
internal fun inlineMarkdown(text: String, codeColor: Color, codeBackground: Color): AnnotatedString = buildAnnotatedString {
|
||||
var i = 0
|
||||
while (i < text.length) {
|
||||
val boldEnd = if (text.startsWith("**", i)) text.indexOf("**", i + 2) else -1
|
||||
val tickEnd = if (text[i] == '`') text.indexOf('`', i + 1) else -1
|
||||
when {
|
||||
boldEnd > i + 2 -> {
|
||||
withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { append(text, i + 2, boldEnd) }
|
||||
i = boldEnd + 2
|
||||
}
|
||||
tickEnd > i + 1 -> {
|
||||
withStyle(SpanStyle(fontFamily = FontFamily.Monospace, color = codeColor, background = codeBackground)) { append(text, i + 1, tickEnd) }
|
||||
i = tickEnd + 1
|
||||
}
|
||||
else -> append(text[i++])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.text.Selection
|
||||
import android.text.Spannable
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.Spanned
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.viewinterop.AndroidView
|
||||
import com.droiddeck.launcher.R
|
||||
import io.noties.markwon.AbstractMarkwonPlugin
|
||||
import io.noties.markwon.Markwon
|
||||
import io.noties.markwon.MarkwonConfiguration
|
||||
import io.noties.markwon.MarkwonVisitor
|
||||
import io.noties.markwon.core.MarkwonTheme
|
||||
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
|
||||
import io.noties.markwon.ext.tables.TableAwareMovementMethod
|
||||
import io.noties.markwon.ext.tables.TablePlugin
|
||||
import io.noties.markwon.ext.tables.TableRowSpan
|
||||
import io.noties.markwon.ext.tasklist.TaskListPlugin
|
||||
import org.commonmark.ext.autolink.AutolinkExtension
|
||||
import org.commonmark.parser.Parser
|
||||
import org.commonmark.ext.gfm.tables.TableCell
|
||||
import org.commonmark.ext.gfm.tables.TableHead
|
||||
import org.commonmark.ext.gfm.tables.TableRow
|
||||
import org.commonmark.node.AbstractVisitor
|
||||
import org.commonmark.node.CustomNode
|
||||
import org.commonmark.node.Node
|
||||
|
||||
@Composable
|
||||
internal fun AgentMarkdown(text: String, modifier: Modifier) {
|
||||
val context = LocalContext.current
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
val density = LocalDensity.current
|
||||
val textSize = with(density) { 15.sp.toPx() }
|
||||
val lineSpacing = with(density) { 3.sp.toPx() }
|
||||
val renderer = remember(context, pal.primary2, pal.surfaceVariant, density) {
|
||||
agentMarkdownRenderer(context, pal.primary2.toArgb(), pal.surfaceVariant.toArgb())
|
||||
}
|
||||
val rendered = remember(renderer, text) { renderer.toMarkdown(text) }
|
||||
AndroidView(
|
||||
modifier = modifier,
|
||||
factory = { TextView(it).apply {
|
||||
setTextIsSelectable(true)
|
||||
movementMethod = TableAwareMovementMethod.create()
|
||||
includeFontPadding = false
|
||||
} },
|
||||
update = { view ->
|
||||
view.setTextColor(colors.onSurface.toArgb())
|
||||
view.setLinkTextColor(pal.primary2.toArgb())
|
||||
view.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, textSize)
|
||||
view.setLineSpacing(lineSpacing, 1f)
|
||||
setAgentMarkdown(view, renderer, rendered)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
internal fun agentMarkdownRenderer(context: Context, linkColor: Int, codeBackground: Int): Markwon =
|
||||
Markwon.builder(context)
|
||||
.usePlugin(StrikethroughPlugin.create())
|
||||
.usePlugin(TablePlugin.create(context))
|
||||
.usePlugin(TaskListPlugin.create(context))
|
||||
.usePlugin(object : AbstractMarkwonPlugin() {
|
||||
override fun configureParser(builder: Parser.Builder) {
|
||||
builder.extensions(listOf(AutolinkExtension.create()))
|
||||
}
|
||||
|
||||
override fun afterRender(node: Node, visitor: MarkwonVisitor) {
|
||||
val rows = mutableListOf<String>()
|
||||
node.accept(object : AbstractVisitor() {
|
||||
override fun visit(customNode: CustomNode) {
|
||||
if (customNode is TableHead || customNode is TableRow) {
|
||||
val cells = generateSequence(customNode.firstChild) { it.next }.filterIsInstance<TableCell>().toList()
|
||||
if (cells.isNotEmpty()) rows += cells.joinToString(" | ") { markdownCellText(it) }
|
||||
else visitChildren(customNode)
|
||||
} else visitChildren(customNode)
|
||||
}
|
||||
})
|
||||
if (rows.isEmpty()) return
|
||||
// Markwon keeps table cells outside the text buffer; expose them for copy and accessibility.
|
||||
val builder = visitor.builder()
|
||||
val text = SpannableStringBuilder(builder.removeFromEnd(0))
|
||||
val spans = text.getSpans(0, text.length, TableRowSpan::class.java).sortedBy { text.getSpanStart(it) }
|
||||
for ((row, span) in spans.withIndex().reversed()) {
|
||||
val start = text.getSpanStart(span)
|
||||
val end = text.getSpanEnd(span)
|
||||
val content = rows[row].ifEmpty { "\u00a0" }
|
||||
text.removeSpan(span)
|
||||
text.replace(start, end, content)
|
||||
text.setSpan(span, start, start + content.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
builder.append(text)
|
||||
}
|
||||
|
||||
override fun configureTheme(builder: MarkwonTheme.Builder) {
|
||||
builder.linkColor(linkColor).codeBackgroundColor(codeBackground)
|
||||
}
|
||||
|
||||
override fun configureConfiguration(builder: MarkwonConfiguration.Builder) {
|
||||
builder.linkResolver { view, link ->
|
||||
val intent = agentBrowserIntent(link) ?: return@linkResolver
|
||||
try {
|
||||
view.context.startActivity(intent)
|
||||
} catch (_: android.content.ActivityNotFoundException) {
|
||||
Toast.makeText(view.context, R.string.agent_browser_unavailable, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.build()
|
||||
|
||||
private fun markdownCellText(node: Node): String = buildString {
|
||||
node.accept(object : AbstractVisitor() {
|
||||
override fun visit(text: org.commonmark.node.Text) { append(text.literal) }
|
||||
override fun visit(code: org.commonmark.node.Code) { append(code.literal) }
|
||||
override fun visit(lineBreak: org.commonmark.node.SoftLineBreak) { append(' ') }
|
||||
override fun visit(lineBreak: org.commonmark.node.HardLineBreak) { append('\n') }
|
||||
override fun visit(html: org.commonmark.node.HtmlInline) { append(html.literal) }
|
||||
})
|
||||
}
|
||||
|
||||
internal fun setAgentMarkdown(view: TextView, renderer: Markwon, rendered: Spanned) {
|
||||
if (view.tag === rendered) return
|
||||
val start = view.selectionStart
|
||||
val end = view.selectionEnd
|
||||
val sameText = view.text.toString() == rendered.toString()
|
||||
renderer.setParsedMarkdown(view, rendered)
|
||||
view.tag = rendered
|
||||
if (sameText && start in 0..rendered.length && end in 0..rendered.length) {
|
||||
(view.text as? Spannable)?.let { Selection.setSelection(it, start, end) }
|
||||
}
|
||||
}
|
||||
|
||||
internal fun agentBrowserIntent(link: String): Intent? {
|
||||
val uri = Uri.parse(link)
|
||||
if (uri.scheme?.lowercase() !in listOf("http", "https") || uri.host.isNullOrBlank()) return null
|
||||
return Intent(Intent.ACTION_VIEW, uri).apply {
|
||||
addCategory(Intent.CATEGORY_BROWSABLE)
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
selector = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_BROWSER)
|
||||
}
|
||||
}
|
||||
@@ -3998,4 +3998,5 @@
|
||||
|
||||
<string name="theme_white_red">Scarlet Snow</string>
|
||||
<string name="theme_white_red_detail">White surfaces with red accents.</string>
|
||||
<string name="agent_browser_unavailable">No web browser is available to open this link.</string>
|
||||
</resources>
|
||||
@@ -1,27 +1,176 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import org.junit.Assert.assertEquals
|
||||
import android.content.Intent
|
||||
import android.content.ClipboardManager
|
||||
import android.text.Selection
|
||||
import android.text.Spannable
|
||||
import io.noties.markwon.ext.tables.TableRowSpan
|
||||
import android.graphics.Color
|
||||
import android.text.Spanned
|
||||
import android.text.style.ClickableSpan
|
||||
import android.widget.TextView
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.ConscryptMode
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
@ConscryptMode(ConscryptMode.Mode.OFF)
|
||||
class AgentMarkdownTest {
|
||||
@Test fun blocksSplitHeadingsBulletsAndCode() {
|
||||
val blocks = markdownBlocks("## Done\n\n- one\n* two\n```kotlin\nval a = 1\n\nval b = 2\n```\nplain")
|
||||
assertEquals(
|
||||
listOf(
|
||||
MdBlock.Line("Done", heading = true),
|
||||
MdBlock.Line("• one", bullet = true),
|
||||
MdBlock.Line("• two", bullet = true),
|
||||
MdBlock.Code("val a = 1\n\nval b = 2"),
|
||||
MdBlock.Line("plain"),
|
||||
),
|
||||
blocks,
|
||||
)
|
||||
private val context get() = RuntimeEnvironment.getApplication()
|
||||
private val renderer get() = agentMarkdownRenderer(context, Color.BLUE, Color.DKGRAY)
|
||||
private fun Spanned.links() = getSpans(0, length, ClickableSpan::class.java).toList()
|
||||
|
||||
@Test fun namedPullRequestLinksKeepTheirLabelsAndOpenAnExternalBrowser() {
|
||||
val text = renderer.toMarkdown("Created [**PR #2540**](https://github.com/Droid-Deck/DroidDeck/pull/2540).")
|
||||
assertEquals("Created PR #2540.", text.toString())
|
||||
val link = text.links().single()
|
||||
assertEquals("PR #2540", text.subSequence(text.getSpanStart(link), text.getSpanEnd(link)).toString())
|
||||
link.onClick(TextView(context))
|
||||
val intent = shadowOf(context).nextStartedActivity
|
||||
assertEquals(Intent.ACTION_VIEW, intent.action)
|
||||
assertEquals("https://github.com/Droid-Deck/DroidDeck/pull/2540", intent.dataString)
|
||||
val browser = requireNotNull(intent.selector)
|
||||
assertEquals(Intent.ACTION_MAIN, browser.action)
|
||||
assertTrue(browser.hasCategory(Intent.CATEGORY_APP_BROWSER))
|
||||
assertTrue(intent.flags and Intent.FLAG_ACTIVITY_NEW_TASK != 0)
|
||||
}
|
||||
|
||||
@Test fun inlineMarkersAreDroppedAndUnclosedOnesKept() {
|
||||
val text = inlineMarkdown("a **b** `c` d ** e ` f", Color.Red, Color.Black)
|
||||
assertEquals("a b c d ** e ` f", text.text)
|
||||
assertEquals(2, text.spanStyles.size)
|
||||
@Test fun bareUrlsAndAutolinksAreClickableWithoutTrailingPunctuation() {
|
||||
val text = renderer.toMarkdown("Visit https://github.com/Droid-Deck/DroidDeck/pull/2540. Also <https://example.org/help>.")
|
||||
assertEquals(2, text.links().size)
|
||||
val labels = text.links().map { text.subSequence(text.getSpanStart(it), text.getSpanEnd(it)).toString() }
|
||||
assertTrue(labels.contains("https://github.com/Droid-Deck/DroidDeck/pull/2540"))
|
||||
assertTrue(labels.contains("https://example.org/help"))
|
||||
}
|
||||
|
||||
@Test fun referenceLinksAndEscapedParenthesesAreParsed() {
|
||||
val text = renderer.toMarkdown("[Docs][guide] and [details](https://example.org/a\\(b\\)).\n\n[guide]: https://example.org/docs")
|
||||
assertEquals("Docs and details.", text.toString().trimEnd())
|
||||
assertEquals(2, text.links().size)
|
||||
}
|
||||
|
||||
@Test fun codeStaysLiteralAndDoesNotBecomeClickable() {
|
||||
val text = renderer.toMarkdown("`[PR](https://example.org)`\n\n~~~kotlin\nval url = \"https://example.org\"\n~~~")
|
||||
assertTrue(text.toString().contains("[PR](https://example.org)"))
|
||||
assertTrue(text.toString().contains("val url = \"https://example.org\""))
|
||||
assertTrue(text.links().isEmpty())
|
||||
}
|
||||
|
||||
@Test fun commonMarkdownBlocksAndNestedFormattingRenderWithoutMarkers() {
|
||||
val text = renderer.toMarkdown("# Heading\n\n> A **bold and *italic*** quote\n\n1. First\n - Nested\n2. Second\n\n~~Removed~~\n\n- [x] Done\n- [ ] Pending")
|
||||
val result = text.toString()
|
||||
for (word in listOf("Heading", "A bold and italic quote", "First", "Nested", "Second", "Removed", "Done", "Pending")) {
|
||||
assertTrue(result, result.contains(word))
|
||||
}
|
||||
for (marker in listOf("# Heading", "**", "~~", "[x]", "[ ]")) assertFalse(result, result.contains(marker))
|
||||
}
|
||||
|
||||
@Test fun tablesAreParsedWithLinksInsideCells() {
|
||||
val root = renderer.parse("| Result | Link |\n| --- | --- |\n| Ready | [PR](https://github.com/owner/repo/pull/1) |")
|
||||
assertEquals("TableBlock", root.firstChild.javaClass.simpleName)
|
||||
var links = 0
|
||||
root.accept(object : org.commonmark.node.AbstractVisitor() {
|
||||
override fun visit(link: org.commonmark.node.Link) {
|
||||
links++
|
||||
assertEquals("https://github.com/owner/repo/pull/1", link.destination)
|
||||
}
|
||||
})
|
||||
assertEquals(1, links)
|
||||
renderer.render(root)
|
||||
}
|
||||
|
||||
@Test fun colorChangesPreserveSelectionAndRefreshLinkSpans() {
|
||||
val source = "Read [the PR](https://github.com/owner/repo/pull/1)"
|
||||
val view = TextView(context).apply { setTextIsSelectable(true) }
|
||||
val original = renderer
|
||||
setAgentMarkdown(view, original, original.toMarkdown(source))
|
||||
Selection.setSelection(view.text as Spannable, 5, 11)
|
||||
val updated = agentMarkdownRenderer(context, Color.RED, Color.LTGRAY)
|
||||
val rendered = updated.toMarkdown(source)
|
||||
setAgentMarkdown(view, updated, rendered)
|
||||
assertEquals(5, view.selectionStart)
|
||||
assertEquals(11, view.selectionEnd)
|
||||
assertSame(rendered, view.tag)
|
||||
val paint = android.text.TextPaint()
|
||||
(view.text as Spanned).links().single().updateDrawState(paint)
|
||||
assertEquals(Color.RED, paint.color)
|
||||
}
|
||||
|
||||
@Test fun reversedSelectionsAndCursorsSurviveColorChangesButNotContentChanges() {
|
||||
val view = TextView(context).apply { setTextIsSelectable(true) }
|
||||
val original = renderer
|
||||
val updated = agentMarkdownRenderer(context, Color.RED, Color.LTGRAY)
|
||||
setAgentMarkdown(view, original, original.toMarkdown("A long reply"))
|
||||
for ((start, end) in listOf(8 to 2, 4 to 4)) {
|
||||
Selection.setSelection(view.text as Spannable, start, end)
|
||||
setAgentMarkdown(view, updated, updated.toMarkdown("A long reply"))
|
||||
assertEquals(start, view.selectionStart)
|
||||
assertEquals(end, view.selectionEnd)
|
||||
}
|
||||
setAgentMarkdown(view, updated, updated.toMarkdown("New"))
|
||||
assertFalse(view.selectionStart == 4 && view.selectionEnd == 4)
|
||||
}
|
||||
|
||||
@Test fun tableCellsRemainInNativeCopyAndAccessibilityTextOnAndroid28() {
|
||||
val source = "Before\n\n| Result | Link |\n| --- | --- |\n| **Ready** | [PR](https://github.com/owner/repo/pull/1) |\n| Pending | `code` |\n\nAfter"
|
||||
val markwon = renderer
|
||||
val rendered = markwon.toMarkdown(source)
|
||||
val view = TextView(context).apply { setTextIsSelectable(true) }
|
||||
setAgentMarkdown(view, markwon, rendered)
|
||||
val expected = "Result | Link\nReady | PR\nPending | code"
|
||||
assertTrue(rendered.toString(), rendered.toString().contains(expected))
|
||||
assertTrue(view.createAccessibilityNodeInfo().text.toString().contains(expected))
|
||||
Selection.setSelection(view.text as Spannable, 0, view.text.length)
|
||||
assertTrue(view.onTextContextMenuItem(android.R.id.copy))
|
||||
val copied = context.getSystemService(ClipboardManager::class.java).primaryClip!!.getItemAt(0).text.toString()
|
||||
assertEquals(rendered.toString(), copied)
|
||||
val rows = rendered.getSpans(0, rendered.length, TableRowSpan::class.java)
|
||||
assertEquals(3, rows.size)
|
||||
assertTrue(rows.all { rendered.getSpanEnd(it) > rendered.getSpanStart(it) })
|
||||
}
|
||||
|
||||
@Test fun tableLinksStillOpenTheBrowserAfterExposingCellText() {
|
||||
val markwon = renderer
|
||||
val rendered = markwon.toMarkdown("| Result | Link |\n| --- | --- |\n| Ready | [PR](https://github.com/owner/repo/pull/1) |")
|
||||
val view = TextView(context).apply { setTextIsSelectable(true) }
|
||||
setAgentMarkdown(view, markwon, rendered)
|
||||
view.measure(android.view.View.MeasureSpec.makeMeasureSpec(900, android.view.View.MeasureSpec.EXACTLY),
|
||||
android.view.View.MeasureSpec.makeMeasureSpec(2000, android.view.View.MeasureSpec.AT_MOST))
|
||||
view.layout(0, 0, 900, view.measuredHeight)
|
||||
val canvas = android.graphics.Canvas(android.graphics.Bitmap.createBitmap(900, 2000, android.graphics.Bitmap.Config.ARGB_8888))
|
||||
view.draw(canvas)
|
||||
val row = rendered.getSpans(0, rendered.length, TableRowSpan::class.java).maxBy { rendered.getSpanStart(it) }
|
||||
val cell = requireNotNull(row.findLayoutForHorizontalOffset(row.cellWidth() + 1))
|
||||
(cell.text as Spanned).links().single().onClick(view)
|
||||
assertEquals("https://github.com/owner/repo/pull/1", shadowOf(context).nextStartedActivity.dataString)
|
||||
}
|
||||
|
||||
@Test fun multipleTablesAndEmptyCellsKeepTheirOwnTextAndRowSpans() {
|
||||
val rendered = renderer.toMarkdown("| One | Two |\n| --- | --- |\n| | X |\n\nBetween\n\n| Three | Four |\n| --- | --- |\n| Y | |")
|
||||
val text = rendered.toString()
|
||||
assertTrue(text, text.contains("One | Two\n | X"))
|
||||
assertTrue(text, text.contains("Three | Four\nY | "))
|
||||
assertTrue(text, text.contains("Between"))
|
||||
assertEquals(4, rendered.getSpans(0, rendered.length, TableRowSpan::class.java).size)
|
||||
}
|
||||
|
||||
@Test fun unfinishedStreamingMarkdownDoesNotLoseTextOrCrash() {
|
||||
val source = "Read [the PR](https://github.com/owner/repo/pull/1)\n\n```kotlin\nval x = 1"
|
||||
for (end in 1..source.length) renderer.toMarkdown(source.take(end))
|
||||
assertTrue(renderer.toMarkdown(source).toString().contains("val x = 1"))
|
||||
}
|
||||
|
||||
@Test fun browserIntentsOnlyAcceptAbsoluteWebLinks() {
|
||||
for (link in listOf("javascript:alert(1)", "file:///etc/passwd", "intent://open", "content://private", "/local/file", "https:")) {
|
||||
assertNull(link, agentBrowserIntent(link))
|
||||
}
|
||||
assertNotNull(agentBrowserIntent("http://example.org"))
|
||||
assertNotNull(agentBrowserIntent("https://github.com/owner/repo/pull/1?diff=split#discussion"))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user