diff options
-rw-r--r-- | core/src/main/kotlin/Kotlin/ContentBuilder.kt | 18 | ||||
-rw-r--r-- | core/src/test/kotlin/format/MarkdownFormatTest.kt | 8 | ||||
-rw-r--r-- | core/testdata/format/externalReferenceLink.kt | 10 | ||||
-rw-r--r-- | core/testdata/format/externalReferenceLink.md | 10 | ||||
-rw-r--r-- | core/testdata/format/referenceLink.kt | 16 | ||||
-rw-r--r-- | core/testdata/format/referenceLink.md | 15 |
6 files changed, 72 insertions, 5 deletions
diff --git a/core/src/main/kotlin/Kotlin/ContentBuilder.kt b/core/src/main/kotlin/Kotlin/ContentBuilder.kt index a244a48e..b7b7a044 100644 --- a/core/src/main/kotlin/Kotlin/ContentBuilder.kt +++ b/core/src/main/kotlin/Kotlin/ContentBuilder.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka import org.intellij.markdown.MarkdownElementTypes import org.intellij.markdown.MarkdownTokenTypes import org.intellij.markdown.html.entities.EntityConverter +import org.intellij.markdown.parser.LinkMap import java.util.* fun buildContent(tree: MarkdownNode, linkResolver: (String) -> ContentBlock, inline: Boolean = false): MutableContent { @@ -21,6 +22,8 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri val nodeStack = ArrayDeque<ContentBlock>() nodeStack.push(target) + val linkMap = LinkMap.buildLinkMap(tree.node, tree.text) + tree.visit {node, processChildren -> val parent = nodeStack.peek() @@ -74,14 +77,15 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri } MarkdownElementTypes.SHORT_REFERENCE_LINK, MarkdownElementTypes.FULL_REFERENCE_LINK -> { - val labelText = node.child(MarkdownElementTypes.LINK_LABEL)?.getLabelText() - if (labelText != null) { - val link = linkResolver(labelText) + val labelElement = node.child(MarkdownElementTypes.LINK_LABEL) + if (labelElement != null) { + val linkInfo = linkMap.getLinkInfo(labelElement.text) + val labelText = labelElement.getLabelText() + val link = linkInfo?.let { linkResolver(it.destination.toString()) } ?: linkResolver(labelText) val linkText = node.child(MarkdownElementTypes.LINK_TEXT) if (linkText != null) { renderLinkTextTo(linkText, link, linkResolver) - } - else { + } else { link.append(ContentText(labelText)) } parent.append(link) @@ -148,6 +152,10 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri MarkdownTokenTypes.CODE_FENCE_CONTENT -> { parent.append(ContentText(node.text)) } + + MarkdownElementTypes.LINK_DEFINITION -> { + } + else -> { processChildren() } diff --git a/core/src/test/kotlin/format/MarkdownFormatTest.kt b/core/src/test/kotlin/format/MarkdownFormatTest.kt index e3f4bae7..48b06d6e 100644 --- a/core/src/test/kotlin/format/MarkdownFormatTest.kt +++ b/core/src/test/kotlin/format/MarkdownFormatTest.kt @@ -364,6 +364,14 @@ class MarkdownFormatTest { verifyMarkdownNode("nestedLists") } + @Test fun referenceLink() { + verifyMarkdownNode("referenceLink") + } + + @Test fun externalReferenceLink() { + verifyMarkdownNode("externalReferenceLink") + } + @Test fun newlineInTableCell() { verifyMarkdownPackage("newlineInTableCell") } diff --git a/core/testdata/format/externalReferenceLink.kt b/core/testdata/format/externalReferenceLink.kt new file mode 100644 index 00000000..4ca0ee21 --- /dev/null +++ b/core/testdata/format/externalReferenceLink.kt @@ -0,0 +1,10 @@ +/** + * It is link to [example site][example.com] + * + * Sure, it is [example.com] + * + * [example.com]: http://example.com + */ +fun a() { + +}
\ No newline at end of file diff --git a/core/testdata/format/externalReferenceLink.md b/core/testdata/format/externalReferenceLink.md new file mode 100644 index 00000000..29d64afe --- /dev/null +++ b/core/testdata/format/externalReferenceLink.md @@ -0,0 +1,10 @@ +[test](test/index) / [a](test/a) + +# a + +`fun a(): Unit` + +It is link to [example site](http://example.com) + +Sure, it is [example.com](http://example.com) + diff --git a/core/testdata/format/referenceLink.kt b/core/testdata/format/referenceLink.kt new file mode 100644 index 00000000..c6550f04 --- /dev/null +++ b/core/testdata/format/referenceLink.kt @@ -0,0 +1,16 @@ +package example + +/** + * It is link to [example other func][example] + * + * Sure, it is [example] + * + * [example]: example.someOtherFunc + */ +fun a() { + +} + +fun someOtherFunc() { + +}
\ No newline at end of file diff --git a/core/testdata/format/referenceLink.md b/core/testdata/format/referenceLink.md new file mode 100644 index 00000000..b3d89e0d --- /dev/null +++ b/core/testdata/format/referenceLink.md @@ -0,0 +1,15 @@ +[test](test/index) / [example](test/example/index) / [a](test/example/a) + +# a + +`fun a(): Unit` + +It is link to [example other func](test/example/some-other-func) + +Sure, it is [example](test/example/some-other-func) + +[test](test/index) / [example](test/example/index) / [someOtherFunc](test/example/some-other-func) + +# someOtherFunc + +`fun someOtherFunc(): Unit`
\ No newline at end of file |