-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix super and sub script not being properly rendered (#1410)
* Better attempt * Fix it
- Loading branch information
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/jerboa/util/markwon/ScriptRewriteSupportPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>") | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/test/java/com/jerboa/util/markwon/ScriptRewriteSupportPluginTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("", ""), | ||
) | ||
} |