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

Make sup/subscript more strict #1632

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
@@ -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
Original file line number Diff line number Diff line change
@@ -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 ~"),
)
}