Open T8RIN opened 4 months ago
i solved by doing this, but looks ugly
fun String.replaceCodeMarkdown(): String =
replace("```", "`")
.replaceFractions()
.replaceSqrt()
.replaceMathBBToSymbol()
.let {
mathPairs.fold(it) { acc, (pattern, char) ->
acc.replace(pattern, mathString(char))
}
}
private fun mathString(string: String) = string
private fun String.replaceFractions(): String {
val fraction = Regex("\\\\frac\\{([^}]*)\\}\\{([^}]*)\\}")
return fraction.replace(this) { matchResult ->
val numerator = matchResult.groupValues[1]
val denominator = matchResult.groupValues[2]
mathString("$numerator/$denominator")
}
}
private fun String.replaceMathBBToSymbol(): String {
val replacementMap = mapOf(
"A" to "πΈ", "B" to "πΉ", "C" to "β", "D" to "π»", "E" to "πΌ",
"F" to "π½", "G" to "πΎ", "H" to "β", "I" to "π", "J" to "π",
"K" to "π", "L" to "π", "M" to "π", "N" to "β", "O" to "π",
"P" to "β", "Q" to "β", "R" to "β", "S" to "π", "T" to "π",
"U" to "π", "V" to "π", "W" to "π", "X" to "π", "Y" to "π",
"Z" to "β€"
)
val regex = Regex("\\\\mathbb\\{([A-Za-z])\\}")
return regex.replace(this) { matchResult ->
val letter = matchResult.groupValues[1].uppercase()[0].toString()
replacementMap[letter] ?: letter
}
}
private fun String.replaceSqrt(): String {
val sqrt = Regex("\\\\sqrt(?:\\[([^]]+)])?\\{([^}]*)\\}")
return sqrt.replace(this) { matchResult ->
val n = matchResult.groupValues[1].takeIf { it.isNotEmpty() }?.toIntOrNull()
val x = matchResult.groupValues[2]
mathString(n?.let { "(${n})β$x" } ?: "β$x")
}
}
private val mathPairs by lazy {
listOf(
"\\(" to "(",
"\\)" to ")",
"\\[" to "[",
"\\[" to "]",
"\\quad" to " ",
"\\neg" to "Β¬",
"\\pm" to "Β±",
"\\mp" to "β",
"\\div" to "Γ·",
"\\%" to "%",
"\\oplus" to "β",
"\\otimes" to "β",
"\\odot" to "β",
"\\setminus" to "β",
"\\int!!!int!!!int" to "β",
"\\int!!!int" to "β¬",
"\\int" to "β«",
"\\cdot" to "Β·",
"\\to" to "β",
"\\infty" to "β",
"\\Rightarrow" to "β",
"\\Leftrightarrow" to "β",
"\\forall" to "β",
"\\partial" to "β",
"\\exists" to "β",
"\\emptyset" to "β
",
"\\nabla" to "β",
"\\in" to "β",
"\\not\\in" to "β",
"\\notin" to "β",
"\\prod" to "β",
"\\sum" to "β",
"\\surd" to "β",
"\\wedge" to "β§",
"\\land" to "β§",
"\\vee" to "β¨",
"\\lor" to "β¨",
"\\lnot" to "!",
"\\cap" to "β©",
"\\cup" to "βͺ",
"\\int" to "β«",
"\\approx" to "β",
"\\neq" to "β ",
"\\equiv" to "β‘",
"\\leq" to "β€",
"\\geq" to "β₯",
"\\subseteq" to "β",
"\\subsetneq" to "β",
"\\supseteq" to "β",
"\\supsetneq" to "β",
"\\subset" to "β",
"\\supset" to "β",
"^\\circ" to "Β°",
"\\times" to "Γ",
"\\lfloor" to "β",
"\\rfloor" to "β",
"\\lceil" to "β",
"\\rceil" to "β",
"\\nexists" to "β
",
"\\Delta" to "Ξ",
"\\sphericalangle" to "β’",
"\\measuredangle" to "β‘",
"\\angle" to "β ",
"\\cong" to "β
",
"\\widehat" to "β‘",
"\\parallel" to "β₯",
"\\perp" to "β₯",
"\\triangle" to "β³",
"\\sim" to "βΌ",
"\\overline" to "",
"\\overrightarrow" to "",
"\\aleph_0" to "β΅",
"\\alpha" to "Ξ±",
"\\kappa" to "ΞΊ",
"\\psi" to "Ο",
"\\digamma" to "z",
"\\Delta" to "β",
"\\Theta" to "Ξ",
"\\beta" to "Ξ²",
"\\lambda" to "Ξ»",
"\\rho" to "Ο",
"\\varepsilon" to "Ξ΅",
"\\Gamma" to "Ξ",
"\\Upsilon" to "Ξ₯",
"\\chi" to "Ο",
"\\mu" to "Β΅",
"\\sigma" to "Ο",
"\\varkappa" to "ΞΊ",
"\\Lambda" to "Ξ",
"\\Xi" to "Ξ",
"\\delta" to "Ξ΄",
"\\nu" to "Ξ½",
"\\tau" to "Ο",
"\\varphi" to "Ο",
"\\Omega" to "β¦",
"\\epsilon" to "Ξ΅",
"\\theta" to "ΞΈ",
"\\varpi" to "Ο",
"\\Phi" to "Ξ¦",
"\\aleph" to "β΅",
"\\eta" to "Ξ·",
"\\omega" to "Ο",
"\\upsilon" to "Ο
",
"\\varrho" to "Ο",
"\\Pi" to "Ξ ",
"\\gamma" to "Ξ³",
"\\phi" to "Ο",
"\\xi" to "ΞΎ",
"\\varsigma" to "Ο",
"\\Psi" to "Ξ¨",
"\\iota" to "ΞΉ",
"\\pi" to "Ο",
"\\zeta" to "ΞΆ",
"\\vartheta" to "Ο",
"\\Sigma" to "Ξ£"
)
}
Hi, Thanks for creating this issue, I think that's an important feature to have, I will try to add it ASAP.
Thanks !
Is it possible to parse math expressions in markdown, like
\(a \times b\)