Repository: LemmyNet/jerboa (language: kotlin)

Issue:
Markdown rending subtext 
### Jerboa Version

0.0.71

### Android Version + Phone

N/a

### Describe The Bug

![image](https://github.com/user-attachments/assets/aeaf4604-ecf5-4d67-b5b9-d1064359c7e7)

![image](https://github.com/user-attachments/assets/0b185986-5cb5-4299-bdea-2435197ea0ef)

This should not be formatted as subtext. But it is following the current spec in Jerboa. Which is any text between a tilde that is uninterrupted by a linebreak becomes subscript.

There doesn't seem to be a commonmark spec to follow?

What does Lemmy UI do?

### To Reproduce

- this text

```
Been driving an EV for 6 years now. Tesla model X (range ~ 260kms) first, now a model Y LR (range ~ 480kms)
```


### In the case of a crash or when relevant include the logs

_No response_


Discussion hints:
Part of that `ScriptRewriteSupportPlugin`. We need to look at how other markwon projects are adding super / subscript support.

[Here's a sample](https://github.com/noties/Markwon/blob/master/app-sample/src/main/java/io/noties/markwon/app/samples/parser/RedditSuperscriptSample.kt)
We just need a better definition. I don't if lemmy follows an exact spec for sup/subscript. Noties follows the \<sub> syntax which is the most popular way.
Here's everything we try to support: https://join-lemmy.org/docs/users/02-media.html?highlight=markdown#text

I think we're following reddits style of super / subscript.
I can make the subscript definition more strict to prevent the above example for being picked up. By not allowing whitespace.

The definition becomes:

Any text between a tilde that is not interrupted by a linebreak where the starting or ending text can't be a whitespace character.

According to this https://www.markdownguide.org/tools/reddit/

reddit does not support subscript. Lots of markdown implementations use \~text\~ instead for strikethrough like Github.





Pull request: Make sup/subscript more strict
Fixes #1628 

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

Fix patch:
diff --git a/app/src/main/java/com/jerboa/util/markwon/ScriptRewriteSupportPlugin.kt b/app/src/main/java/com/jerboa/util/markwon/ScriptRewriteSupportPlugin.kt
index 49a00dab4..9862a46a6 100644
--- a/app/src/main/java/com/jerboa/util/markwon/ScriptRewriteSupportPlugin.kt
+++ b/app/src/main/java/com/jerboa/util/markwon/ScriptRewriteSupportPlugin.kt
@@ -13,8 +13,20 @@ class ScriptRewriteSupportPlugin : AbstractMarkwonPlugin() {
         )
 
     companion object {
-        val SUPERSCRIPT_RGX = Regex("""\^([^\n^]+)\^""")
-        val SUBSCRIPT_RGX = Regex("""(?<!~)~([^\n~]+)~""")
+        /*
+         * Superscript has the definition:
+         * Any text between a '^' that is not interrupted by a linebreak where the starting
+         * or ending text can't be a whitespace character.
+         */
+        val SUPERSCRIPT_RGX = Regex("""\^(?!\s)([^\n^]+)(?<!\s)\^""")
+
+        /*
+         * Subscript has the definition:
+         * Any text between a tilde that is not interrupted by a linebreak where the starting
+         * or ending text can't be a whitespace character. And where the starting the tilde is not prefixed
+         * by another tilde. (To prevent matching with strikethrough)
+         */
+        val SUBSCRIPT_RGX = Regex("""(?<!~)~(?!\s)([^\n~]+)(?<!\s)~""")
 
         fun rewriteLemmyScriptToMarkwonScript(text: String): String =
             text


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
index 0164bacf4..eebd3639a 100644
--- a/app/src/test/java/com/jerboa/util/markwon/ScriptRewriteSupportPluginTest.kt
+++ b/app/src/test/java/com/jerboa/util/markwon/ScriptRewriteSupportPluginTest.kt
@@ -39,5 +39,22 @@ class ScriptRewriteSupportPluginTest {
             listOf("~~text~~", "~~text~~"),
             // Intended to fail, else it will increase the complexity of the regex by a huge margin
             listOf("~~text~", "~~text~"),
+            listOf("~text~~", "<sub>text</sub>~"),
+            listOf(
+                "Tesla model X (range ~ 260kms) first, now a model Y LR (range ~ 480kms)",
+                "Tesla model X (range ~ 260kms) first, now a model Y LR (range ~ 480kms)",
+            ),
+            listOf("~ 5 ~ 6 ~", "~ 5 ~ 6 ~"),
+            listOf("^ ^", "^ ^"),
+            listOf("^", "^"),
+            listOf("~", "~"),
+            listOf("~~", "~~"),
+            listOf("~~~", "~~~"),
+            listOf("^ 99 ^", "^ 99 ^"),
+            listOf("^ 99^", "^ 99^"),
+            listOf("^99 ^", "^99 ^"),
+            listOf("~ 99 ~", "~ 99 ~"),
+            listOf("~ 99~", "~ 99~"),
+            listOf("~99 ~", "~99 ~"),
         )
 }