fix: preserve literal markdown on renderer fallback
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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**")
|
||||
|
||||
Reference in New Issue
Block a user