From dc1596409002d93e665ec98c7d116e7dc4ca9450 Mon Sep 17 00:00:00 2001 From: Ekaterina Berezhko Date: Tue, 23 Apr 2024 16:18:03 +0300 Subject: [PATCH] Update math support to trim surrounding backticks --- .../markdown/flavours/gfm/GFMFlavourDescriptor.kt | 2 +- .../markdown/flavours/gfm/GFMGeneratingProviders.kt | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/GFMFlavourDescriptor.kt b/src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/GFMFlavourDescriptor.kt index 45657af..2100d55 100644 --- a/src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/GFMFlavourDescriptor.kt +++ b/src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/GFMFlavourDescriptor.kt @@ -43,8 +43,8 @@ open class GFMFlavourDescriptor( override val sequentialParserManager = object : SequentialParserManager() { override fun getParserSequence(): List { return listOf(AutolinkParser(listOf(MarkdownTokenTypes.AUTOLINK, GFMTokenTypes.GFM_AUTOLINK)), - MathParser(), BacktickParser(), + MathParser(), ImageParser(), InlineLinkParser(), ReferenceLinkParser(), diff --git a/src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/GFMGeneratingProviders.kt b/src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/GFMGeneratingProviders.kt index d389b4e..10b5636 100644 --- a/src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/GFMGeneratingProviders.kt +++ b/src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/GFMGeneratingProviders.kt @@ -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 {