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

Math expressions #276

Open
T8RIN opened this issue May 13, 2024 · 3 comments
Open

Math expressions #276

T8RIN opened this issue May 13, 2024 · 3 comments

Comments

@T8RIN
Copy link
Contributor

T8RIN commented May 13, 2024

Is it possible to parse math expressions in markdown, like \(a \times b\)

@T8RIN
Copy link
Contributor Author

T8RIN commented May 13, 2024

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 "Σ"
    )
}

@MohamedRejeb
Copy link
Owner

Hi,
Thanks for creating this issue,
I think that's an important feature to have, I will try to add it ASAP.

@T8RIN
Copy link
Contributor Author

T8RIN commented May 19, 2024

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants