fix: preserve literal markdown on renderer fallback

This commit is contained in:
2026-03-16 21:15:44 -04:00
parent 72e23fded8
commit a0255c2487
4 changed files with 30 additions and 4 deletions

View File

@@ -55,7 +55,6 @@ dependencies {
implementation(libs.androidx.swiperefreshlayout)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.commonmark)
implementation(libs.commonmark.ext.gfm.tables)
testImplementation(libs.junit)
testImplementation(libs.kotlinx.coroutines.test)

View File

@@ -20,7 +20,7 @@ class MarkdownRenderer(
val html = parseToHtml(markdown)
htmlToSpanned(html)
} catch (_: Exception) {
htmlToSpanned(markdown)
PlainTextSpanned(markdown)
}
}
@@ -51,3 +51,31 @@ class MarkdownRenderer(
}
}
}
private class PlainTextSpanned(
private val text: String,
) : Spanned {
override val length: Int
get() = text.length
override fun get(index: Int): Char = text[index]
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
return text.subSequence(startIndex, endIndex)
}
override fun toString(): String = text
override fun getSpanStart(tag: Any): Int = -1
override fun getSpanEnd(tag: Any): Int = -1
override fun getSpanFlags(tag: Any): Int = 0
override fun nextSpanTransition(start: Int, limit: Int, kind: Class<*>?): Int = limit
override fun <T : Any> getSpans(start: Int, end: Int, kind: Class<T>): Array<T> {
@Suppress("UNCHECKED_CAST")
return java.lang.reflect.Array.newInstance(kind, 0) as Array<T>
}
}

View File

@@ -34,7 +34,7 @@ class MarkdownRendererTest {
fun render_whenRenderingFails_returnsPlainTextFallback() {
val renderer = MarkdownRenderer(
parseToHtml = { throw IllegalStateException("boom") },
htmlToSpanned = { html -> TestSpanned(html) },
htmlToSpanned = { throw AssertionError("htmlToSpanned should not run on fallback") },
)
val result = renderer.render("**keep this literal**")

View File

@@ -37,7 +37,6 @@ androidx-swiperefreshlayout = { group = "androidx.swiperefreshlayout", name = "s
androidx-recyclerview = { group = "androidx.recyclerview", name = "recyclerview", version.ref = "recyclerview" }
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
commonmark = { group = "org.commonmark", name = "commonmark", version.ref = "commonmark" }
commonmark-ext-gfm-tables = { group = "org.commonmark", name = "commonmark-ext-gfm-tables", version.ref = "commonmark" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }