Repository: LemmyNet/jerboa (language: kotlin)

Issue:
Sub and super script in markdown doesn't work
### Jerboa Version

0.0.55

### Android Version + Phone

n/a

### Describe The Bug

The sub and sup markdown actions don't work, it puts the symbols but the markdown renders them as normal.

### To Reproduce

press the sub/sup button, preview the changes

### In the case of a crash or when relevant include the logs

_No response_


Discussion hints:
Alright not sure why Jerboa uses ~ and ^ for those but markwon never supported that syntax

But they do support script through HTML

![image](https://github.com/dessalines/jerboa/assets/67873169/a88e91e7-645f-42cb-9df0-f2ae53487f71)


So this will be a simple fix

Pull request: Fix super and sub script not being properly rendered
![image](https://github.com/dessalines/jerboa/assets/67873169/a428351d-fb9b-411f-943c-f8def38badbb)


Fix #1341 


Test command: ./gradlew :app:testDebugUnitTest --tests "com.jerboa.util.markwon.ScriptRewriteSupportPluginTest"

Fix patch:
diff --git a/app/src/main/java/com/jerboa/ui/components/common/MarkdownHelper.kt b/app/src/main/java/com/jerboa/ui/components/common/MarkdownHelper.kt
index dd431924a..fb0f4d1f5 100644
--- a/app/src/main/java/com/jerboa/ui/components/common/MarkdownHelper.kt
+++ b/app/src/main/java/com/jerboa/ui/components/common/MarkdownHelper.kt
@@ -32,6 +32,7 @@ import com.jerboa.util.markwon.BetterLinkMovementMethod
 import com.jerboa.util.markwon.ForceHttpsPlugin
 import com.jerboa.util.markwon.MarkwonLemmyLinkPlugin
 import com.jerboa.util.markwon.MarkwonSpoilerPlugin
+import com.jerboa.util.markwon.ScriptRewriteSupportPlugin
 import io.noties.markwon.AbstractMarkwonPlugin
 import io.noties.markwon.Markwon
 import io.noties.markwon.MarkwonConfiguration
@@ -92,6 +93,7 @@ object MarkdownHelper {
                 .usePlugin(ForceHttpsPlugin())
                 // email urls interfere with lemmy links
                 .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
+                .usePlugin(ScriptRewriteSupportPlugin())
                 .usePlugin(MarkwonLemmyLinkPlugin())
                 .usePlugin(MarkwonSpoilerPlugin(true))
                 .usePlugin(StrikethroughPlugin.create())
@@ -127,6 +129,7 @@ object MarkdownHelper {
                 // email urls interfere with lemmy links
                 .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
                 .usePlugin(MarkwonLemmyLinkPlugin())
+                .usePlugin(ScriptRewriteSupportPlugin())
                 .usePlugin(StrikethroughPlugin.create())
                 .usePlugin(TablePlugin.create(context))
                 .usePlugin(HtmlPlugin.create { plugin -> plugin.addHandler(TagHandlerNoOp.create("img")) })
diff --git a/app/src/main/java/com/jerboa/util/markwon/ScriptRewriteSupportPlugin.kt b/app/src/main/java/com/jerboa/util/markwon/ScriptRewriteSupportPlugin.kt
new file mode 100644
index 000000000..9d61dc02d
--- /dev/null
+++ b/app/src/main/java/com/jerboa/util/markwon/ScriptRewriteSupportPlugin.kt
@@ -0,0 +1,26 @@
+package com.jerboa.util.markwon
+
+import io.noties.markwon.AbstractMarkwonPlugin
+
+class ScriptRewriteSupportPlugin : AbstractMarkwonPlugin() {
+    override fun processMarkdown(markdown: String): String {
+        return super.processMarkdown(
+            if (markdown.contains("^") || markdown.contains("~")) {
+                rewriteLemmyScriptToMarkwonScript(markdown)
+            } else { // Fast path: if there are no markdown characters, we don't need to do anything
+                markdown
+            },
+        )
+    }
+
+    companion object {
+        val SUPERSCRIPT_RGX = Regex("""\^([^\n^]+)\^""")
+        val SUBSCRIPT_RGX = Regex("""~([^\n~]+)~""")
+
+        fun rewriteLemmyScriptToMarkwonScript(text: String): String {
+            return text
+                .replace(SUPERSCRIPT_RGX, "<sup>$1</sup>")
+                .replace(SUBSCRIPT_RGX, "<sub>$1</sub>")
+        }
+    }
+}


Test patch:
diff --git a/app/src/test/java/com/jerboa/util/markwon/ScriptRewriteSupportPluginTest.kt b/app/src/test/java/com/jerboa/util/markwon/ScriptRewriteSupportPluginTest.kt
new file mode 100644
index 000000000..1be365921
--- /dev/null
+++ b/app/src/test/java/com/jerboa/util/markwon/ScriptRewriteSupportPluginTest.kt
@@ -0,0 +1,37 @@
+package com.jerboa.util.markwon
+
+import junitparams.JUnitParamsRunner
+import junitparams.Parameters
+import org.junit.Assert
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@RunWith(JUnitParamsRunner::class)
+class ScriptRewriteSupportPluginTest {
+    @Test
+    @Parameters(
+        method = "successCases",
+    )
+    fun `should rewrite lemmy script to markwon script`(
+        input: String,
+        expected: String,
+    ) {
+        val result = ScriptRewriteSupportPlugin.rewriteLemmyScriptToMarkwonScript(input)
+        Assert.assertEquals(expected, result)
+    }
+
+    fun successCases() =
+        listOf(
+            listOf("^2^", "<sup>2</sup>"),
+            listOf("~2~", "<sub>2</sub>"),
+            listOf("~2~ ~2~", "<sub>2</sub> <sub>2</sub>"),
+            listOf("^2^ ^2^", "<sup>2</sup> <sup>2</sup>"),
+            listOf("^^", "^^"),
+            listOf("^\n^", "^\n^"),
+            listOf("~2~~2~", "<sub>2</sub><sub>2</sub>"),
+            listOf("~2~\n~2~", "<sub>2</sub>\n<sub>2</sub>"),
+            listOf("~2~\n~2~", "<sub>2</sub>\n<sub>2</sub>"),
+            listOf("~ blah blah", "~ blah blah"),
+            listOf("", ""),
+        )
+}