Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix super and sub script not being properly rendered #1410

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("", ""),
)
}