Files
openide/platform/markdown-utils/test/com/intellij/markdown/utils/CodeFenceSyntaxHighlighterGeneratingProviderTest.kt
Ivan Semenov 87236b59ad [md] preserve code formatting when converting a code fence block to HTML
GitOrigin-RevId: 7ff6415275668175f479bd729217404a854e82d2
2024-04-19 14:32:52 +00:00

121 lines
3.2 KiB
Kotlin

// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.markdown.utils
import com.intellij.markdown.utils.lang.HtmlSyntaxHighlighter
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.text.HtmlChunk
import org.intellij.markdown.IElementType
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.flavours.gfm.GFMFlavourDescriptor
import org.intellij.markdown.html.GeneratingProvider
import org.intellij.markdown.parser.LinkMap
import org.junit.Test
import java.net.URI
import kotlin.test.assertEquals
class CodeFenceSyntaxHighlighterGeneratingProviderTest {
private val emptyFlavourDescriptor = object : GFMFlavourDescriptor() {
override fun createHtmlGeneratingProviders(linkMap: LinkMap, baseURI: URI?): Map<IElementType, GeneratingProvider> {
val providers = super.createHtmlGeneratingProviders(linkMap, baseURI).toMutableMap()
providers[MarkdownElementTypes.CODE_FENCE] = CodeFenceSyntaxHighlighterGeneratingProvider(
object : HtmlSyntaxHighlighter {
override fun color(language: String?, rawContent: @NlsSafe String): HtmlChunk {
return if (language == "empty")
HtmlChunk.text(rawContent)
else
HtmlChunk.text(SYNTAX_HIGHLIGHTER_RESULT)
}
}
)
return providers
}
}
private val converter = MarkdownToHtmlConverter(emptyFlavourDescriptor)
@Test
fun `java syntax highlighter call check`() {
val markdownText = """
```java
public class A {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
""".trimIndent()
// language=HTML
val htmlText = """
<body>
<pre>
<code class="language-java">
${SYNTAX_HIGHLIGHTER_RESULT}
</code>
</pre>
</body>
""".trimIndent()
markdownText shouldBe htmlText
}
@Test
fun `kotlin syntax highlighter call check`() {
val markdownText = """
```kotlin
class A {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println("Multi to multi suggestion")
}
}
}
""".trimIndent()
// language=HTML
val htmlText = """
<body>
<pre>
<code class="language-kotlin">
${SYNTAX_HIGHLIGHTER_RESULT}
</code>
</pre>
</body>
""".trimIndent()
markdownText shouldBe htmlText
}
@Test
fun `syntax highlighter call check without language`() {
val markdownText = """
```empty
class A
""".trimIndent()
// language=HTML
val htmlText = """
<body>
<pre>
<code class="language-empty">
class A
</code>
</pre>
</body>
""".trimIndent()
markdownText shouldBe htmlText
}
private infix fun String.shouldBe(htmlText: String) {
assertEquals(
htmlText.lines().joinToString("") { it.trim() },
converter.convertMarkdownToHtml(this)
)
}
private companion object {
private const val SYNTAX_HIGHLIGHTER_RESULT = "Empty line"
}
}