fix: preserve literal markdown on renderer fallback
This commit is contained in:
@@ -55,7 +55,6 @@ dependencies {
|
|||||||
implementation(libs.androidx.swiperefreshlayout)
|
implementation(libs.androidx.swiperefreshlayout)
|
||||||
implementation(libs.kotlinx.coroutines.android)
|
implementation(libs.kotlinx.coroutines.android)
|
||||||
implementation(libs.commonmark)
|
implementation(libs.commonmark)
|
||||||
implementation(libs.commonmark.ext.gfm.tables)
|
|
||||||
|
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
testImplementation(libs.kotlinx.coroutines.test)
|
testImplementation(libs.kotlinx.coroutines.test)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class MarkdownRenderer(
|
|||||||
val html = parseToHtml(markdown)
|
val html = parseToHtml(markdown)
|
||||||
htmlToSpanned(html)
|
htmlToSpanned(html)
|
||||||
} catch (_: Exception) {
|
} 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() {
|
fun render_whenRenderingFails_returnsPlainTextFallback() {
|
||||||
val renderer = MarkdownRenderer(
|
val renderer = MarkdownRenderer(
|
||||||
parseToHtml = { throw IllegalStateException("boom") },
|
parseToHtml = { throw IllegalStateException("boom") },
|
||||||
htmlToSpanned = { html -> TestSpanned(html) },
|
htmlToSpanned = { throw AssertionError("htmlToSpanned should not run on fallback") },
|
||||||
)
|
)
|
||||||
|
|
||||||
val result = renderer.render("**keep this literal**")
|
val result = renderer.render("**keep this literal**")
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ androidx-swiperefreshlayout = { group = "androidx.swiperefreshlayout", name = "s
|
|||||||
androidx-recyclerview = { group = "androidx.recyclerview", name = "recyclerview", version.ref = "recyclerview" }
|
androidx-recyclerview = { group = "androidx.recyclerview", name = "recyclerview", version.ref = "recyclerview" }
|
||||||
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
|
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
|
||||||
commonmark = { group = "org.commonmark", name = "commonmark", version.ref = "commonmark" }
|
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]
|
[plugins]
|
||||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||||
|
|||||||
Reference in New Issue
Block a user