From 704c33bdef4bbc66e1a9a9e1c3fcc6f489558423 Mon Sep 17 00:00:00 2001 From: Hafiz Date: Wed, 17 May 2023 12:17:30 -0400 Subject: [PATCH] Preprocess image paths if needed 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") --- .../common/habitica/helpers/MarkdownParser.kt | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/com/habitrpg/common/habitica/helpers/MarkdownParser.kt b/common/src/main/java/com/habitrpg/common/habitica/helpers/MarkdownParser.kt index e6f7a38d0..4d21e7246 100644 --- a/common/src/main/java/com/habitrpg/common/habitica/helpers/MarkdownParser.kt +++ b/common/src/main/java/com/habitrpg/common/habitica/helpers/MarkdownParser.kt @@ -34,6 +34,8 @@ object MarkdownParser { private val cache = sortedMapOf() 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 {