diff options
-rw-r--r-- | core/build.gradle.kts | 2 | ||||
-rw-r--r-- | plugins/base/src/main/kotlin/parsers/MarkdownParser.kt | 18 | ||||
-rw-r--r-- | plugins/base/src/test/kotlin/markdown/LinkTest.kt | 133 |
3 files changed, 144 insertions, 9 deletions
diff --git a/core/build.gradle.kts b/core/build.gradle.kts index b1c1dbb9..e64224de 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } dependencies { - api("org.jetbrains:markdown:0.1.45") + api("org.jetbrains:markdown:0.2.0") implementation(kotlin("reflect")) implementation("org.jsoup:jsoup:1.12.1") implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.1") diff --git a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt index a27734cc..9a4aa39f 100644 --- a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt +++ b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka.base.parsers import com.intellij.psi.PsiElement +import org.intellij.markdown.MarkdownElementType import org.intellij.markdown.MarkdownElementTypes import org.intellij.markdown.MarkdownTokenTypes import org.intellij.markdown.ast.ASTNode @@ -370,7 +371,19 @@ open class MarkdownParser( this.filterNot { it.type == GFMTokenTypes.TABLE_SEPARATOR } private fun List<ASTNode>.evaluateChildren(): List<DocTag> = - this.removeUselessTokens().mergeLeafASTNodes().map { visitNode(it) } + this.removeUselessTokens().swapImagesThatShouldBeLinks().mergeLeafASTNodes().map { visitNode(it) } + + private fun List<ASTNode>.swapImagesThatShouldBeLinks(): List<ASTNode> = + flatMap { node -> + if (node.type == MarkdownElementTypes.IMAGE + && node.children.firstOrNull()?.let { it is LeafASTNode && it.type.name == "!" } == true + && node.children.lastOrNull()?.type == MarkdownElementTypes.SHORT_REFERENCE_LINK + ) { + node.children + } else { + listOf(node) + } + } private fun List<ASTNode>.removeUselessTokens(): List<ASTNode> = this.filterIndexed { index, node -> @@ -452,7 +465,8 @@ open class MarkdownParser( if (link is DocumentationLink) link.dri else null } - val allTags = listOf(kDocTag) + if(kDocTag.canHaveParent()) getAllKDocTags(findParent(kDocTag)) else emptyList() + val allTags = + listOf(kDocTag) + if (kDocTag.canHaveParent()) getAllKDocTags(findParent(kDocTag)) else emptyList() DocumentationNode( allTags.map { when (it.knownTag) { diff --git a/plugins/base/src/test/kotlin/markdown/LinkTest.kt b/plugins/base/src/test/kotlin/markdown/LinkTest.kt index 2bcc0bf0..186fe9ee 100644 --- a/plugins/base/src/test/kotlin/markdown/LinkTest.kt +++ b/plugins/base/src/test/kotlin/markdown/LinkTest.kt @@ -6,7 +6,8 @@ import org.jetbrains.dokka.pages.ClasslikePageNode import org.jetbrains.dokka.pages.ContentDRILink import org.jetbrains.dokka.pages.MemberPageNode import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest -import org.jetbrains.dokka.model.doc.DocumentationLink +import org.jetbrains.dokka.links.* +import org.jetbrains.dokka.model.doc.* import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Test @@ -39,7 +40,8 @@ class LinkTest : BaseAbstractTest() { .content .dfs { node -> node is ContentDRILink && - node.address.toString() == "parser//test/#java.lang.ClassLoader/PointingToDeclaration/"} + node.address.toString() == "parser//test/#java.lang.ClassLoader/PointingToDeclaration/" + } ) } } @@ -75,7 +77,7 @@ class LinkTest : BaseAbstractTest() { val destinationDri = (root.documentable as WithGenerics).generics.first().dri.toString() assertEquals(destinationDri, "/Outer///PointingToGenericParameters(0)/") - assertNotNull(foo.content.dfs { it is ContentDRILink && it.address.toString() == destinationDri } ) + assertNotNull(foo.content.dfs { it is ContentDRILink && it.address.toString() == destinationDri }) } } } @@ -91,7 +93,7 @@ class LinkTest : BaseAbstractTest() { } testInline( - """ + """ |/src/main/kotlin/Test.kt |package example | @@ -101,14 +103,133 @@ class LinkTest : BaseAbstractTest() { |fun stop(hammerTime: String, waitAMinute: String) {} | """.trimMargin(), - configuration + configuration ) { documentablesMergingStage = { module -> val parameter = module.dfs { it.name == "waitAMinute" } - val link = module.dfs { it.name == "stop" }!!.documentation.values.single().dfs { it is DocumentationLink } as DocumentationLink + val link = module.dfs { it.name == "stop" }!!.documentation.values.single() + .dfs { it is DocumentationLink } as DocumentationLink assertEquals(parameter!!.dri, link.dri) } } } + + @Test + fun `link with exclamation mark`() { + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/") + } + } + } + + testInline( + """ + |/src/main/kotlin/Test.kt + |package example + | + |/** + |* Link to ![waitAMinute] + |*/ + |fun stop(hammerTime: String, waitAMinute: String) {} + | + """.trimMargin(), + configuration + ) { + documentablesMergingStage = { module -> + val functionDocs = module.packages.flatMap { it.functions }.first().documentation.values.first() + val expected = Description( + root = CustomDocTag( + children = listOf( + P( + children = listOf( + Text("Link to !"), + DocumentationLink( + dri = DRI( + packageName = "example", + callable = Callable( + "stop", + receiver = null, + params = listOf( + TypeConstructor("kotlin.String", emptyList()), + TypeConstructor("kotlin.String", emptyList()) + ) + ), + target = PointingToCallableParameters(1) + ), + children = listOf( + Text("waitAMinute") + ), + params = mapOf("href" to "[waitAMinute]") + ) + ) + ) + ), + name = "MARKDOWN_FILE" + ) + ) + + assertEquals(expected, functionDocs.children.first()) + } + } + } + + @Test + fun `link to property with exclamation mark`() { + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/") + } + } + } + + testInline( + """ + |/src/main/kotlin/Testing.kt + |package example + | + |/** + |* Link to ![Testing.property] + |*/ + |class Testing { + | var property = "" + |} + | + """.trimMargin(), + configuration + ) { + documentablesMergingStage = { module -> + val functionDocs = module.packages.flatMap { it.classlikes }.first().documentation.values.first() + val expected = Description( + root = CustomDocTag( + children = listOf( + P( + children = listOf( + Text("Link to !"), + DocumentationLink( + dri = DRI( + packageName = "example", + classNames = "Testing", + callable = Callable("property", null, emptyList()), + target = PointingToDeclaration + ), + children = listOf( + Text("Testing.property") + ), + params = mapOf("href" to "[Testing.property]") + ) + ) + ) + ), + name = "MARKDOWN_FILE" + ) + ) + + assertEquals(expected, functionDocs.children.first()) + } + } + } } |