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, "$1") + .replace(SUBSCRIPT_RGX, "$1") + } + } +} 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^", "2"), + listOf("~2~", "2"), + listOf("~2~ ~2~", "2 2"), + listOf("^2^ ^2^", "2 2"), + listOf("^^", "^^"), + listOf("^\n^", "^\n^"), + listOf("~2~~2~", "22"), + listOf("~2~\n~2~", "2\n2"), + listOf("~2~\n~2~", "2\n2"), + listOf("~ blah blah", "~ blah blah"), + listOf("", ""), + ) +}