Skip to content

Commit

Permalink
Trim four spaces inside indented code block (#2661)
Browse files Browse the repository at this point in the history
* Trim four spaces inside indented code block

* Fix test
  • Loading branch information
vmishenev committed Sep 26, 2022
1 parent a0250a5 commit a816e91
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
3 changes: 2 additions & 1 deletion plugins/base/src/main/kotlin/parsers/MarkdownParser.kt
Expand Up @@ -10,6 +10,7 @@ import org.intellij.markdown.ast.impl.ListItemCompositeNode
import org.intellij.markdown.flavours.gfm.GFMElementTypes
import org.intellij.markdown.flavours.gfm.GFMFlavourDescriptor
import org.intellij.markdown.flavours.gfm.GFMTokenTypes
import org.intellij.markdown.html.HtmlGenerator
import org.jetbrains.dokka.base.parsers.factories.DocTagsFromIElementFactory
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.PointingToDeclaration
Expand Down Expand Up @@ -343,7 +344,7 @@ open class MarkdownParser(
DocTagsFromIElementFactory.getInstance(node.type, children = node.children.mergeLeafASTNodes().flatMap {
DocTagsFromIElementFactory.getInstance(
MarkdownTokenTypes.TEXT,
body = text.substring(it.startOffset, it.endOffset)
body = HtmlGenerator.trimIndents(text.substring(it.startOffset, it.endOffset), 4).toString()
)
})

Expand Down
10 changes: 5 additions & 5 deletions plugins/base/src/test/kotlin/markdown/ParserTest.kt
Expand Up @@ -1340,11 +1340,11 @@ class ParserTest : KDocTest() {
CodeBlock(
listOf(
Text(
" val x = 1\n" +
" val y = 2\n" +
" if (x == 1) {\n" +
" println(y)\n" +
" }"
"val x = 1\n" +
"val y = 2\n" +
"if (x == 1) {\n" +
" println(y)\n" +
"}"
)
)
)
Expand Down
37 changes: 37 additions & 0 deletions plugins/base/src/test/kotlin/model/CommentTest.kt
@@ -1,6 +1,8 @@
package model

import org.jetbrains.dokka.model.DClass
import org.jetbrains.dokka.model.DProperty
import org.jetbrains.dokka.model.doc.CodeBlock
import org.jetbrains.dokka.model.doc.CustomTagWrapper
import org.jetbrains.dokka.model.doc.Text
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -267,4 +269,39 @@ class CommentTest : AbstractModelTest("/src/main/kotlin/comment/Test.kt", "comme
}
}
}

@Test
fun `should remove spaces inside indented code block`() {
inlineModelTest(
"""
|/**
| * Welcome:
| *
| * ```kotlin
| * fun main() {
| * println("Hello World!")
| * }
| * ```
| *
| * fun thisIsACodeBlock() {
| * val butWhy = "per markdown spec, because four-spaces prefix"
| * }
| */
|class Foo
"""
) {
with((this / "comment" / "Foo").cast<DClass>()) {
docs()[0].children[2] equals CodeBlock(
listOf(
Text(
"fun thisIsACodeBlock() {\n" +
" val butWhy = \"per markdown spec, because four-spaces prefix\"\n" +
"}"
)
)
)
}
}
}

}
Expand Up @@ -5,7 +5,6 @@ import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.doc.*
import org.junit.Assert
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Disabled
Expand Down Expand Up @@ -148,15 +147,15 @@ class DefaultDescriptorToDocumentableTranslatorTest : BaseAbstractTest() {
CodeBlock(
children = listOf(
Text(
""" val soapAttrs = attrs("soap-env" to "http://www.w3.org/2001/12/soap-envelope",
"soap-env:encodingStyle" to "http://www.w3.org/2001/12/soap-encoding")
val soapXml = node("soap-env:Envelope", soapAttrs,
node("soap-env:Body", attrs("xmlns:m" to "http://example"),
node("m:GetExample",
node("m:GetExampleName", "BasePair")
)
"""val soapAttrs = attrs("soap-env" to "http://www.w3.org/2001/12/soap-envelope",
"soap-env:encodingStyle" to "http://www.w3.org/2001/12/soap-encoding")
val soapXml = node("soap-env:Envelope", soapAttrs,
node("soap-env:Body", attrs("xmlns:m" to "http://example"),
node("m:GetExample",
node("m:GetExampleName", "BasePair")
)
)"""
)
)"""
)
)
)
Expand Down

0 comments on commit a816e91

Please sign in to comment.