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

Update math support to trim surrounding backticks #157

Merged
Show file tree
Hide file tree
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
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 @@ -154,11 +154,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
Loading