From e80b64b122950f2a623ce86ea68abdb0061bed27 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Thu, 18 May 2017 16:01:52 +0300 Subject: Support reference-style markdown links in KDoc #KT-17924 fixed --- core/src/main/kotlin/Kotlin/ContentBuilder.kt | 18 +++++++++++++----- core/src/test/kotlin/format/MarkdownFormatTest.kt | 8 ++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'core/src') 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() 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") } -- cgit