Skip to content

Commit

Permalink
Update math support to trim surrounding backticks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekaterina Berezhko authored and Ekaterina Berezhko committed May 7, 2024
1 parent 0642c8a commit 7d4c8f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ open class GFMFlavourDescriptor(
override val sequentialParserManager = object : SequentialParserManager() {
override fun getParserSequence(): List<SequentialParser> {
return listOf(AutolinkParser(listOf(MarkdownTokenTypes.AUTOLINK, GFMTokenTypes.GFM_AUTOLINK)),
MathParser(),
BacktickParser(),
MathParser(),
ImageParser(),
InlineLinkParser(),
ReferenceLinkParser(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,19 @@ open class TableAwareCodeSpanGeneratingProvider : GeneratingProvider {
internal class MathGeneratingProvider(private val inline: Boolean = false): GeneratingProvider {
override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
val nodes = node.children.subList(1, node.children.size - 1)
val output = nodes.joinToString(separator = "") { HtmlGenerator.leafText(text, it, false) }.trim()
val output = nodes.joinToString(separator = "") { HtmlGenerator.leafText(text, it, false) }.trimSurroundingBackticksAndWhitespaces()
visitor.consumeTagOpen(node, "span", "class=\"math\"", "inline = \"$inline\"")
visitor.consumeHtml(output)
visitor.consumeTagClose("span")
}

private fun String.trimSurroundingBackticksAndWhitespaces(): String {
var i = 0
while (length - i > 2 && this[i] == '`' && this[length - 1 - i] == '`') {
i++
}
return substring(i, length - i).trim()
}
}

internal class TablesGeneratingProvider : GeneratingProvider {
Expand Down

0 comments on commit 7d4c8f6

Please sign in to comment.