Merge pull request #1978 from Hafizzle/Fiz/fix-image-processing

Preprocess image paths if needed
This commit is contained in:
Phillip Thelen 2023-05-18 13:44:07 +02:00 committed by GitHub
commit 1ab2b95e31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,6 +34,8 @@ object MarkdownParser {
private val cache = sortedMapOf<Int, Spanned>()
internal var markwon: Markwon? = null
fun setup(context: Context) {
markwon = Markwon.builder(context)
.usePlugin(StrikethroughPlugin.create())
@ -91,15 +93,18 @@ object MarkdownParser {
if (input == null) {
return SpannableString("")
}
val hashCode = input.hashCode()
val preProcessedInput = preprocessImageMarkdown(input)
val hashCode = preProcessedInput.hashCode()
try {
if (cache.containsKey(hashCode)) {
return cache[hashCode] ?: SpannableString(input)
return cache[hashCode] ?: SpannableString(preProcessedInput)
}
} catch (_: NullPointerException) {
// Sometimes happens
}
val text = EmojiParser.parseEmojis(input) ?: input
val text = EmojiParser.parseEmojis(preProcessedInput) ?: preProcessedInput
// Adding this space here bc for some reason some markdown is not rendered correctly when the whole string is supposed to be formatted
val result = markwon?.toMarkdown("$text ") ?: SpannableString(text)
@ -113,6 +118,22 @@ object MarkdownParser {
}
return result
}
fun preprocessImageMarkdown(markdown: String): String {
// Used to handle an image tag with a URL that ends with .jpg or .png (Else the image may be shown as broken, a link, or not at all)
// Example: (..ample_image_name.png"Zombie hatching potion") -> (..ample_image_name.png "Zombie hatching potion")
val regex = Regex("""!\[.*?]\(.*?".*?"\)""")
return markdown.replace(regex) { matchResult ->
val match = matchResult.value
if (match.contains(".jpg\"")) {
match.replace(".jpg\"", ".jpg \"")
} else if (match.contains(".png\"")) {
match.replace(".png\"", ".png \"")
} else {
match
}
}
}
fun parseMarkdownAsync(input: String?, onSuccess: (Spanned) -> Unit) {
CoroutineScope(Dispatchers.IO).launch {