Skip to content

Commit

Permalink
Fix super and sub script not being properly rendered (#1410)
Browse files Browse the repository at this point in the history
* Better attempt

* Fix it
  • Loading branch information
MV-GH authored Mar 1, 2024
1 parent bf84fa8 commit ac39d87
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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")) })
Expand Down
Original file line number Diff line number Diff line change
@@ -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>")
}
}
}
Original file line number Diff line number Diff line change
@@ -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("", ""),
)
}

0 comments on commit ac39d87

Please sign in to comment.